From d91d1167d4ee4bfce122c63a769d89182010ad8a Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Fri, 31 Aug 2012 17:35:54 -0700 Subject: [PATCH] CSPACE-5492: Added code that places a refname in the collection space core schema of every new resource/record. --- .../vocabulary/AuthorityItemJAXBSchema.java | 2 +- .../common/vocabulary/AuthorityResource.java | 2 +- .../nuxeo/AuthorityDocumentModelHandler.java | 45 ++++++++++--- .../AuthorityItemDocumentModelHandler.java | 63 +++++++++++++++---- .../services/common/api/RefName.java | 28 ++++++--- .../tenants/tenant-bindings-proto.xml | 13 ++++ .../document/AbstractDocumentHandlerImpl.java | 9 +++ .../storage/jpa/JpaDocumentHandler.java | 14 +++++ .../nuxeo/client/java/DocHandlerBase.java | 12 +--- .../client/java/DocumentModelHandler.java | 26 ++++++-- .../java/RemoteDocumentModelHandlerImpl.java | 45 ++++++++++++- .../config/src/main/resources/service.xsd | 1 + 12 files changed, 217 insertions(+), 43 deletions(-) diff --git a/services/authority/jaxb/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityItemJAXBSchema.java b/services/authority/jaxb/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityItemJAXBSchema.java index b2f9caa9b..e48c29eac 100644 --- a/services/authority/jaxb/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityItemJAXBSchema.java +++ b/services/authority/jaxb/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityItemJAXBSchema.java @@ -28,7 +28,7 @@ package org.collectionspace.services.common.vocabulary; */ public interface AuthorityItemJAXBSchema { - final static String IN_AUTHORITY = "inAuthority"; + final static String IN_AUTHORITY = "inAuthority"; // REM - Is this a CSID? Or a refname? Either? final static String REF_NAME = "refName"; final static String ORDER = "order"; final static String SHORT_IDENTIFIER = "shortIdentifier"; diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java index 0d22a5bfd..1852ce0f4 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java @@ -254,7 +254,7 @@ public abstract class AuthorityResource protected String buildAuthorityRefNameBase( ServiceContext ctx, String shortIdentifier) { - RefName.Authority authority = RefName.buildAuthority(ctx.getTenantName(), + RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(), ctx.getServiceName(), shortIdentifier, null); return authority.toString(); } diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java index 28953ba35..d611aa0a1 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java @@ -28,6 +28,7 @@ import java.util.Map; import org.collectionspace.services.client.PoxPayloadIn; import org.collectionspace.services.client.PoxPayloadOut; import org.collectionspace.services.common.api.RefName; +import org.collectionspace.services.common.api.RefName.Authority; import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.common.context.MultipartServiceContext; import org.collectionspace.services.common.context.ServiceContext; @@ -40,6 +41,8 @@ import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl; import org.nuxeo.ecm.core.api.ClientException; import org.nuxeo.ecm.core.api.DocumentModel; import org.nuxeo.ecm.core.api.repository.RepositoryInstance; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * AuthorityDocumentModelHandler @@ -50,6 +53,7 @@ import org.nuxeo.ecm.core.api.repository.RepositoryInstance; public abstract class AuthorityDocumentModelHandler extends DocHandlerBase { + private final Logger logger = LoggerFactory.getLogger(AuthorityDocumentModelHandler.class); private String authorityCommonSchemaName; public AuthorityDocumentModelHandler(String authorityCommonSchemaName) { @@ -112,17 +116,42 @@ public abstract class AuthorityDocumentModelHandler */ protected void updateRefnameForAuthority(DocumentWrapper wrapDoc, String schemaName) throws Exception { DocumentModel docModel = wrapDoc.getWrappedObject(); - String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER); - String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME); - MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext(); - RefName.Authority authority = RefName.buildAuthority(ctx.getTenantName(), - ctx.getServiceName(), - shortIdentifier, - displayName); + RefName.Authority authority = (Authority) getRefName(getServiceContext(), docModel); String refName = authority.toString(); docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName); } - + + @Override + public RefName.RefNameInterface getRefName(ServiceContext ctx, + DocumentModel docModel) { + RefName.RefNameInterface refname = null; + + try { + String shortIdentifier = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER); + String displayName = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.DISPLAY_NAME); + RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(), + ctx.getServiceName(), + shortIdentifier, + displayName); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + + return refname; + } + + @Override + protected String getRefnameDisplayName(DocumentWrapper docWrapper) { + String result = null; + + DocumentModel docModel = docWrapper.getWrappedObject(); + ServiceContext ctx = this.getServiceContext(); + RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel); + result = refname.getDisplayName(); + + return result; + } + public String getShortIdentifier(String authCSID, String schemaName) throws Exception { String shortIdentifier = null; RepositoryInstance repoSession = null; diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java index 2202b1603..7f3db3f3e 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java @@ -37,6 +37,7 @@ import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.DocumentException; import org.collectionspace.services.common.document.DocumentFilter; import org.collectionspace.services.common.document.DocumentWrapper; +import org.collectionspace.services.common.document.DocumentWrapperImpl; import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema; import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema; import org.collectionspace.services.common.vocabulary.RefNameServiceUtils; @@ -95,6 +96,54 @@ public abstract class AuthorityItemDocumentModelHandler this.authorityItemCommonSchemaName = authorityItemCommonSchemaName; } + @Override + protected String getRefnameDisplayName(DocumentWrapper docWrapper) { + String result = null; + + DocumentModel docModel = docWrapper.getWrappedObject(); + ServiceContext ctx = this.getServiceContext(); + RefName.AuthorityItem refname = (RefName.AuthorityItem)getRefName(ctx, docModel); + result = refname.getDisplayName(); + + return result; + } + + /* + * Before calling this method, be sure that the 'this.handleCreate()' was called and was successful. + * (non-Javadoc) + * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#getRefName(org.collectionspace.services.common.context.ServiceContext, org.nuxeo.ecm.core.api.DocumentModel) + */ + @Override + public RefName.RefNameInterface getRefName(ServiceContext ctx, + DocumentModel docModel) { + RefName.RefNameInterface refname = null; + + try { + String shortIdentifier = (String) docModel.getProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.SHORT_IDENTIFIER); + if (Tools.isEmpty(shortIdentifier)) { + throw new Exception("The shortIdentifier for this authority term was empty or not set."); + } + + String displayName = getPrimaryDisplayName(docModel, authorityItemCommonSchemaName, + getItemTermInfoGroupXPathBase(), AuthorityItemJAXBSchema.TERM_DISPLAY_NAME); + if (Tools.isEmpty(displayName)) { + throw new Exception("The displayName for this authority term was empty or not set."); + } + + String authorityRefBaseName = getAuthorityRefNameBase(); + if (Tools.isEmpty(authorityRefBaseName)) { + throw new Exception("Could not create the refName for this authority term, because the refName for its authority parent was empty."); + } + + RefName.Authority parentsRefName = RefName.Authority.parse(authorityRefBaseName); + refname = RefName.buildAuthorityItem(parentsRefName, shortIdentifier, displayName); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + + return refname; + } + public void setInAuthority(String inAuthority) { this.inAuthority = inAuthority; } @@ -365,17 +414,9 @@ public abstract class AuthorityItemDocumentModelHandler String schemaName, String authorityRefBaseName) throws Exception { DocumentModel docModel = wrapDoc.getWrappedObject(); - String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityItemJAXBSchema.SHORT_IDENTIFIER); - String displayName = getPrimaryDisplayName(docModel, authorityItemCommonSchemaName, - getItemTermInfoGroupXPathBase(), AuthorityItemJAXBSchema.TERM_DISPLAY_NAME); - - if (Tools.isEmpty(authorityRefBaseName)) { - throw new Exception("Could not create the refName for this authority term, because the refName for its authority parent was empty."); - } - - RefName.Authority authority = RefName.Authority.parse(authorityRefBaseName); - String refName = RefName.buildAuthorityItem(authority, shortIdentifier, displayName).toString(); - docModel.setProperty(schemaName, AuthorityItemJAXBSchema.REF_NAME, refName); + RefName.RefNameInterface refname = getRefName(this.getServiceContext(), docModel); + String refNameStr = refname.toString(); + docModel.setProperty(schemaName, AuthorityItemJAXBSchema.REF_NAME, refNameStr); } /** diff --git a/services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java b/services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java index 053d9bb4c..b0a72c4fe 100644 --- a/services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java +++ b/services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java @@ -51,8 +51,12 @@ public class RefName { public static final String URN_PREFIX = "urn:cspace:"; public static final String URN_NAME_PREFIX = "urn:cspace:name"; public static final String REFNAME = "refName"; + + public static interface RefNameInterface { + public String toString(); + } - public static class Authority { + public static class Authority implements RefNameInterface { public String tenantName = ""; public String resource = ""; @@ -71,6 +75,10 @@ public class RefName { return authority; } + public String getDisplayName() { + return this.getDisplayName(); + } + public String getShortIdentifier() { return this.shortIdentifier; } @@ -111,7 +119,7 @@ public class RefName { } } - public static class AuthorityItem { + public static class AuthorityItem implements RefNameInterface { public Authority inAuthority; public String shortIdentifier = ""; @@ -129,6 +137,10 @@ public class RefName { return authorityItem; } + public String getDisplayName() { + return this.displayName; + } + public String getParentShortIdentifier() { return this.inAuthority.shortIdentifier; } @@ -171,10 +183,10 @@ public class RefName { } } - public static AuthorityItem buildAuthorityItem(String tenantName, String serviceName, String authorityShortIdentifier, - String itemShortIdentifier, String itemDisplayName) { - Authority authority = Authority.buildAuthority(tenantName, serviceName, authorityShortIdentifier, ""); - return buildAuthorityItem(authority, itemShortIdentifier, itemDisplayName); + public static AuthorityItem buildAuthorityItem(String tenantName, String serviceName, String authorityShortID, + String itemShortID, String itemDisplayName) { + Authority authority = Authority.buildAuthority(tenantName, serviceName, authorityShortID, ""); + return buildAuthorityItem(authority, itemShortID, itemDisplayName); } public static AuthorityItem buildAuthorityItem(String authorityRefName, String itemShortID, String itemDisplayName) { @@ -183,10 +195,10 @@ public class RefName { return item; } - public static AuthorityItem buildAuthorityItem(Authority authority, String itemShortIdentifier, String itemDisplayName) { + public static AuthorityItem buildAuthorityItem(Authority authority, String itemShortID, String itemDisplayName) { AuthorityItem item = new AuthorityItem(); item.inAuthority = authority; - item.shortIdentifier = itemShortIdentifier; + item.shortIdentifier = itemShortID; item.displayName = itemDisplayName; return item; } diff --git a/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml b/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml index e377c5d95..d3f151e0f 100644 --- a/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml +++ b/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml @@ -87,6 +87,13 @@ org.collectionspace.services.collectionobject.nuxeo.CollectionObjectDocumentModelHandler + false + + + objectNumber + objectNumber + + objectNumber @@ -1543,6 +1550,12 @@ true + + + order + order + + diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java b/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java index 0b9edbaef..e130ac95c 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java @@ -28,6 +28,8 @@ import java.util.List; import java.util.Map; import java.util.StringTokenizer; + +import org.collectionspace.services.common.api.RefName; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.query.QueryContext; @@ -66,6 +68,13 @@ public abstract class AbstractDocumentHandlerImpl // Empty constructor } + abstract protected String getRefnameDisplayName(DocumentWrapper docWrapper); + + /* + * Should return a reference name for the wrapper object + */ + abstract public RefName.RefNameInterface getRefName(DocumentWrapper docWrapper, String tenantName, String serviceName); + /* (non-Javadoc) * @see org.collectionspace.services.common.document.DocumentHandler#getServiceContext() */ diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaDocumentHandler.java b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaDocumentHandler.java index cc495124d..49e4f9155 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaDocumentHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaDocumentHandler.java @@ -2,6 +2,7 @@ package org.collectionspace.services.common.storage.jpa; import java.util.List; +import org.collectionspace.services.common.api.RefName; import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl; import org.collectionspace.services.common.document.DocumentFilter; import org.collectionspace.services.common.document.DocumentWrapper; @@ -13,6 +14,19 @@ import org.nuxeo.ecm.core.api.DocumentModel; public abstract class JpaDocumentHandler extends AbstractDocumentHandlerImpl{ + @Override + protected String getRefnameDisplayName(DocumentWrapper wrapDoc) { + return ""; // Empty string since we don't yet support this feature in JPA documents + } + + @Override + public RefName.RefNameInterface getRefName(DocumentWrapper docWrapper, String tenantName, String serviceName) { + // + // Not implemented + // + return null; + } + /** * Extract paging info. * diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java index b6a315468..abf6e796d 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java @@ -157,7 +157,7 @@ public abstract class DocHandlerBase extends RemoteDocumentModelHandlerImpl extends RemoteDocumentModelHandlerImpl queryParams = getServiceContext().getQueryParams(); - markRtSbj = queryParams.getFirst(IQueryManager.MARK_RELATED_TO_CSID_AS_SUBJECT); + markRtSbj = queryParams.getFirst(IQueryManager.MARK_RELATED_TO_CSID_AS_SUBJECT); // REM - We need to document what this query param is for -i.e., what's its purpose? if(markRtSbj!=null && markRtSbj.isEmpty()) markRtSbj = null; @@ -244,13 +244,7 @@ public abstract class DocHandlerBase extends RemoteDocumentModelHandlerImpl abstract public AuthorityRefList getAuthorityRefs(String csid, List authRefsInfo) throws PropertyException; + /* + * Subclasses should override this method if they need to customize their refname generation + */ + public RefName.RefNameInterface getRefName(ServiceContext ctx, + DocumentModel docModel) { + return getRefName(new DocumentWrapperImpl(docModel), ctx.getTenantName(), ctx.getServiceName()); + } + + @Override + public RefName.RefNameInterface getRefName(DocumentWrapper docWrapper, + String tenantName, String serviceName) { + DocumentModel docModel = docWrapper.getWrappedObject(); + String csid = docModel.getName(); + String refnameDisplayName = this.getRefnameDisplayName(docWrapper); + RefName.RefNameInterface refname = RefName.Authority.buildAuthority(tenantName, serviceName, + csid, refnameDisplayName); + return refname; + } + private void handleCoreValues(DocumentWrapper docWrapper, Action action) throws ClientException { DocumentModel documentModel = docWrapper.getWrappedObject(); String now = GregorianCalendarDateTimeUtils.timestampUTC(); ServiceContext ctx = getServiceContext(); String userId = ctx.getUserId(); - if(action==Action.CREATE) { + if (action == Action.CREATE) { // // Add the tenant ID value to the new entity // @@ -321,9 +341,7 @@ public abstract class DocumentModelHandler // // Add the resource's refname // - String csid = documentModel.getName(); - String refname = RefName.Authority.buildAuthority(ctx.getTenantName(), ctx.getServiceName(), - csid, "" /*authorityDisplayName*/).toString(); + String refname = getRefName(ctx, documentModel).toString(); documentModel.setProperty(CollectionSpaceClient.COLLECTIONSPACE_CORE_SCHEMA, CollectionSpaceClient.COLLECTIONSPACE_CORE_REFNAME, refname); // diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java index 6f6a1e3c6..ba58abf30 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java @@ -135,6 +135,35 @@ public abstract class RemoteDocumentModelHandlerImpl + sb.getName()); } + @Override + protected String getRefnameDisplayName(DocumentWrapper docWrapper) { + return getRefnameDisplayName(docWrapper.getWrappedObject()); + } + + private String getRefnameDisplayName(DocumentModel docModel) { + String result = null; + + DocHandlerParams.Params params = null; + try { + params = getDocHandlerParams(); + ListResultField field = params.getRefnameDisplayNameField(); + + String schema = field.getSchema(); + if (schema == null || schema.trim().isEmpty()) { + schema = getServiceContext().getCommonPartLabel(); + } + + result = getStringValue(docModel, schema, field); + } catch (Exception e) { + // TODO Auto-generated catch block + if (logger.isWarnEnabled()) { + logger.warn(String.format("Call failed to getRefnameDisplayName() for class %s", this.getClass().getName())); + } + } + + return result; + } + @Override public boolean supportsHierarchy() { boolean result = false; @@ -806,8 +835,22 @@ public abstract class RemoteDocumentModelHandlerImpl return result; } - + protected String getStringValue(DocumentModel docModel, + String schema, ListResultField field) { + String result = null; + + Object value = getListResultValue(docModel, schema, field); + if (value != null && value instanceof String) { + String strValue = (String) value; + if (strValue.trim().isEmpty() == false) { + result = strValue; + } + } + + return result; + } + protected void removeFromList(List list, RelationsCommonList.RelationListItem item) { list.remove(item); } diff --git a/services/config/src/main/resources/service.xsd b/services/config/src/main/resources/service.xsd index 45cc84096..09895ae19 100644 --- a/services/config/src/main/resources/service.xsd +++ b/services/config/src/main/resources/service.xsd @@ -239,6 +239,7 @@ + -- 2.47.3