]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-375 workspaces are "created" if not found. RepositoryClient.getWorkspaceId...
authorSanjay Dalal <sanjay.dalal@berkeley.edu>
Thu, 29 Oct 2009 06:01:22 +0000 (06:01 +0000)
committerSanjay Dalal <sanjay.dalal@berkeley.edu>
Thu, 29 Oct 2009 06:01:22 +0000 (06:01 +0000)
test: created workspaces after reinitializing service layer (drop databases, tables, etc.) tested with existing workspaces too. used mvn test at service level.

M    common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java
M    common/src/main/java/org/collectionspace/services/common/ServiceMain.java
M    common/src/main/java/org/collectionspace/services/common/config/TenantBindingConfigReader.java
M    common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java
M    common/src/main/java/org/collectionspace/services/nuxeo/client/java/NuxeoConnector.java
M    common/src/main/java/org/collectionspace/services/nuxeo/client/rest/RepositoryRESTClient.java

services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java
services/common/src/main/java/org/collectionspace/services/common/config/TenantBindingConfigReader.java
services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/NuxeoConnector.java
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java
services/common/src/main/java/org/collectionspace/services/nuxeo/client/rest/RepositoryRESTClient.java

index 2d8e39eb296c123ead7062c12c01f19f18dcda57..e079530a693a4014b9c22fde8b4155371af811d9 100644 (file)
@@ -97,16 +97,6 @@ public class ServiceMain {
         getTenantBindingConfigReader().retrieveAllWorkspaceIds();
     }
 
-    /**
-     * retrieveWorkspaceIds for given tenant domain. 
-     * @param tenantDomain
-     * @return Hashtable<String, String> key=workspace name, value=workspace id
-     * @throws Exception
-     */
-    public Hashtable<String, String> retrieveWorkspaceIds(String tenantDomain) throws Exception {
-        return nuxeoConnector.retrieveWorkspaceIds(tenantDomain);
-    }
-
     /**
      * @return the nuxeoConnector
      */
index d6186a87366c482f5087913621a7388a4a98137f..cd4e052b1825a418a6c4082bbb6d67f77303cde2 100644 (file)
@@ -25,11 +25,11 @@ package org.collectionspace.services.common.config;
 
 import java.io.File;
 import java.util.Hashtable;
-import java.util.List;
 import org.collectionspace.services.common.ClientType;
-import org.collectionspace.services.common.RepositoryWorkspaceType;
-import org.collectionspace.services.common.ServiceConfig;
+import org.collectionspace.services.common.RepositoryClientConfigType;
 import org.collectionspace.services.common.ServiceMain;
+import org.collectionspace.services.common.repository.RepositoryClient;
+import org.collectionspace.services.common.repository.RepositoryClientFactory;
 import org.collectionspace.services.common.service.ServiceBindingType;
 import org.collectionspace.services.common.tenant.TenantBindingType;
 import org.collectionspace.services.common.tenant.TenantBindingConfig;
@@ -110,25 +110,43 @@ public class TenantBindingConfigReader
         }
     }
 
+    /**
+     * retrieveWorkspaceIds retrieves workspace ids for services used by
+     * the given tenant
+     * @param tenantBinding
+     * @throws Exception
+     */
     public void retrieveWorkspaceIds(TenantBindingType tenantBinding) throws Exception {
-        String tenantDomain = tenantBinding.getRepositoryDomain();
         Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
         ServiceMain svcMain = ServiceMain.getInstance();
+        RepositoryClientConfigType rclientConfig = svcMain.getServicesConfigReader().getConfiguration().getRepositoryClient();
         ClientType clientType = svcMain.getClientType();
-        if(clientType.equals(ClientType.JAVA)){
-            workspaceIds = svcMain.retrieveWorkspaceIds(tenantDomain);
+        if(clientType.equals(ClientType.JAVA) &&
+                rclientConfig.getName().equalsIgnoreCase("nuxeo-java")){
+            //FIXME only one repository client is recognized
+            workspaceIds = svcMain.getNuxeoConnector().retrieveWorkspaceIds(
+                    tenantBinding.getRepositoryDomain());
         }
+        //verify if workspace exists for each service in the tenant binding
         for(ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()){
             String serviceName = serviceBinding.getName();
+            RepositoryClient repositoryClient = getRepositoryClient(
+                    serviceBinding.getRepositoryClient());
             String workspaceId = null;
             //workspace name is service name by convention
             String workspace = serviceBinding.getName().toLowerCase();
             if(clientType.equals(ClientType.JAVA)){
                 workspaceId = workspaceIds.get(workspace);
                 if(workspaceId == null){
-                    logger.warn("failed to retrieve workspace id for " + workspace);
-                    //FIXME: should we throw an exception here?
-                    continue;
+                    logger.warn("failed to retrieve workspace id for " + workspace +
+                            " trying to create a new workspace...");
+                    workspaceId = repositoryClient.createWorkspace(
+                            tenantBinding.getRepositoryDomain(),
+                            serviceBinding.getName());
+                    if(workspaceId == null){
+                        logger.warn("failed to create workspace for " + workspace);
+                        continue;
+                    }
                 }
             }else{
                 workspaceId = serviceBinding.getRepositoryWorkspaceId();
@@ -190,4 +208,8 @@ public class TenantBindingConfigReader
             String tenantId, String serviceName) {
         return tenantId + "." + serviceName.toLowerCase();
     }
+
+    private RepositoryClient getRepositoryClient(String clientName) {
+        return RepositoryClientFactory.getInstance().getClient(clientName);
+    }
 }
index d2bfa6bdb0086f9558a1283c205b65e545f43ad2..f186d27438bb9175bcc595938d1daefbbdc78141 100644 (file)
@@ -25,7 +25,6 @@ package org.collectionspace.services.common.repository;
 
 import org.collectionspace.services.common.context.ServiceContext;
 
-
 /**
  * RepositoryClient is a generic Document Repository client
  *
@@ -86,4 +85,22 @@ public interface RepositoryClient {
      * @throws DocumentException
      */
     void update(ServiceContext ctx, String id, DocumentHandler handler) throws BadRequestException, DocumentNotFoundException, DocumentException;
+
+    /**
+     * createWorkspace creates a workspace in default repository under given domain
+     * @param tenantDomain domain representing tenant
+     * @param workspaceName name of the workspace
+     * @return id of newly created workspace
+     * @throws java.lang.Exception
+     */
+    public String createWorkspace(String tenantDomain, String workspaceName) throws Exception;
+
+        /**
+     * getWorkspaceId gets an id of given workspace in default repository under given domain
+     * @param tenantDomain domain representing tenant
+     * @param workspaceName name of the workspace
+     * @return id of the workspace
+     * @throws java.lang.Exception
+     */
+    public String getWorkspaceId(String tenantDomain, String workspaceName) throws Exception;
 }
index 604d64768f1e6b56c1f762a08176ebe142faf325..0de8fedf1bc8d1c6b69ec1216481f3ec423ccc1b 100644 (file)
@@ -209,7 +209,7 @@ public class NuxeoConnector {
     /**
      * retrieveWorkspaceIds retrieves all workspace ids from default repository
      * @param tenantDomain domain representing tenant
-     * @return 
+     * @return
      * @throws java.lang.Exception
      */
     public Hashtable<String, String> retrieveWorkspaceIds(String tenantDomain) throws Exception {
@@ -225,7 +225,7 @@ public class NuxeoConnector {
                 String domainPath = "/" + tenantDomain;
                 if(!domain.getPathAsString().equalsIgnoreCase(domainPath)){
                     continue;
-                }
+}
                 if(logger.isDebugEnabled()){
                     logger.debug("domain=" + domain.toString());
                 }
index 152e946d4cb253ba8afb2fbcf4d9f4f1d77c65ef..097fb241e06765d60758a72fdc14e06c525f7a6f 100644 (file)
@@ -17,6 +17,7 @@
  */
 package org.collectionspace.services.nuxeo.client.java;
 
+import java.util.Hashtable;
 import org.collectionspace.services.common.repository.RepositoryClient;
 import org.collectionspace.services.common.context.ServiceContext;
 import org.collectionspace.services.common.repository.BadRequestException;
@@ -189,8 +190,8 @@ public class RepositoryJavaClient implements RepositoryClient {
         String nuxeoWspaceId = ctx.getRepositoryWorkspaceId();
         if(nuxeoWspaceId == null){
             throw new DocumentNotFoundException(
-                    "Unable to find workspace for service " + 
-                    ctx.getServiceName() + 
+                    "Unable to find workspace for service " +
+                    ctx.getServiceName() +
                     " check if the workspace exists in the Nuxeo repository");
         }
         RepositoryInstance repoSession = null;
@@ -323,6 +324,68 @@ public class RepositoryJavaClient implements RepositoryClient {
         }
     }
 
+    @Override
+    public String createWorkspace(String tenantDomain, String workspaceName) throws Exception {
+        RepositoryInstance repoSession = null;
+        String workspaceId = null;
+        try{
+            repoSession= getRepositorySession();
+            DocumentRef docRef = new PathRef(
+                    "/" + tenantDomain +
+                    "/" + "workspaces");
+            DocumentModel parent = repoSession.getDocument(docRef);
+            DocumentModel doc = repoSession.createDocumentModel(parent.getPathAsString(),
+                    workspaceName, "Workspace");
+            doc.setPropertyValue("dc:title", workspaceName);
+            doc.setPropertyValue("dc:description", "A CollectionSpace workspace for " +
+                    workspaceName);
+            doc = repoSession.createDocument(doc);
+            workspaceId = doc.getId();
+            repoSession.save();
+            if(logger.isDebugEnabled()){
+                logger.debug("created workspace name=" + workspaceName +
+                        " id=" + workspaceId);
+            }
+        }catch(Exception e){
+            if(logger.isDebugEnabled()){
+                logger.debug("createWorkspace caught exception ", e);
+            }
+            throw e;
+        }finally{
+            if(repoSession != null){
+                releaseRepositorySession(repoSession);
+            }
+        }
+        return workspaceId;
+    }
+
+    @Override
+    public String getWorkspaceId(String tenantDomain, String workspaceName) throws Exception {
+        String workspaceId = null;
+        RepositoryInstance repoSession = null;
+        try{
+            repoSession = getRepositorySession();
+            DocumentRef docRef = new PathRef(
+                    "/" + tenantDomain +
+                    "/" + "workspaces" +
+                    "/" + workspaceName);
+            DocumentModel workspace = repoSession.getDocument(docRef);
+            workspaceId = workspace.getId();
+        }catch(DocumentException de){
+            throw de;
+        }catch(Exception e){
+            if(logger.isDebugEnabled()){
+                logger.debug("Caught exception ", e);
+            }
+            throw new DocumentException(e);
+        }finally{
+            if(repoSession != null){
+                releaseRepositorySession(repoSession);
+            }
+        }
+        return workspaceId;
+    }
+
     private RepositoryInstance getRepositorySession() throws Exception {
         // FIXME: is it possible to reuse repository session?
         // Authentication failures happen while trying to reuse the session
index 4ebe43e60126bc890472e3b1242cbbdc5701e87f..f0c44db0028f9b404ff9f7e64dc14223bfe596aa 100644 (file)
@@ -307,6 +307,16 @@ public class RepositoryRESTClient implements RepositoryClient {
         }
     }
 
+    @Override
+    public String createWorkspace(String tenantDomain, String workspaceName) throws Exception {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public String getWorkspaceId(String tenantDomain, String workspaceName) throws Exception {
+        throw new UnsupportedOperationException();
+    }
+
     /**
      * buildRequest build HTTP request given parameters
      * @param method