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
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
*/
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;
}
}
+ /**
+ * 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();
String tenantId, String serviceName) {
return tenantId + "." + serviceName.toLowerCase();
}
+
+ private RepositoryClient getRepositoryClient(String clientName) {
+ return RepositoryClientFactory.getInstance().getClient(clientName);
+ }
}
import org.collectionspace.services.common.context.ServiceContext;
-
/**
* RepositoryClient is a generic Document Repository client
*
* @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;
}
/**
* 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 {
String domainPath = "/" + tenantDomain;
if(!domain.getPathAsString().equalsIgnoreCase(domainPath)){
continue;
- }
+}
if(logger.isDebugEnabled()){
logger.debug("domain=" + domain.toString());
}
*/
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;
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;
}
}
+ @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
}
}
+ @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