From 9c3bb15f95e7fea50bfd1395e04f56efd420251f Mon Sep 17 00:00:00 2001 From: Patrick Schmitz Date: Wed, 12 Sep 2012 16:42:16 -0700 Subject: [PATCH] CSPACE-5523 - Fix various problems with relations and refNames. --- .../common/vocabulary/AuthorityResource.java | 4 +- .../nuxeo/AuthorityDocumentModelHandler.java | 1 + .../services/common/api/RefName.java | 66 ++++++++++++++++--- .../services/common/api/RefNameUtils.java | 14 ++-- .../services/common/ResourceBase.java | 23 +++++-- .../client/java/DocumentModelHandler.java | 2 +- .../nuxeo/RelationDocumentModelHandler.java | 14 +++- 7 files changed, 103 insertions(+), 21 deletions(-) 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 814d7be5e..152e74626 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 @@ -257,7 +257,9 @@ public abstract class AuthorityResource protected String buildAuthorityRefNameBase( ServiceContext ctx, String shortIdentifier) { RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(), - ctx.getServiceName(), shortIdentifier, null); + ctx.getServiceName(), + null, // Only use shortId form!!! + 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 48b8b06f7..81260435a 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 @@ -131,6 +131,7 @@ public abstract class AuthorityDocumentModelHandler String displayName = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.DISPLAY_NAME); RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(), ctx.getServiceName(), + null, // Only use shortId form!!! shortIdentifier, displayName); refname = authority; 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 b0a72c4fe..dd03e1b31 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 @@ -60,7 +60,8 @@ public class RefName { public String tenantName = ""; public String resource = ""; - public String shortIdentifier = ""; + public String csid = null; + public String shortIdentifier = null; public String displayName = ""; public static Authority parse(String urn) { @@ -83,6 +84,10 @@ public class RefName { return this.shortIdentifier; } + public String getCSID() { + return this.csid; + } + public boolean equals(Object other) { if (other == null) { return false; @@ -91,28 +96,71 @@ public class RefName { Authority ao = (Authority) other; return (this.tenantName.equals(ao.tenantName) && this.resource.equals(ao.resource) - && this.shortIdentifier.equals(ao.shortIdentifier)); + && ((this.shortIdentifier != null && + this.shortIdentifier.equals(ao.shortIdentifier)) + || (this.csid != null && this.csid.equals(ao.csid)))); } else { return false; } } public String getRelativeUri() { - return "/" + resource + "/" + URN_NAME_PREFIX + "(" + shortIdentifier + ")"; + StringBuilder sb = new StringBuilder(); + sb.append("/"); + sb.append(resource); + sb.append("/"); + if(csid!=null) { + sb.append(csid); + } else if(shortIdentifier!= null) { + sb.append(URN_NAME_PREFIX); + sb.append("("); + sb.append(shortIdentifier); + sb.append(")"); + } else { + throw new NullPointerException("Authority has neither CSID nor shortID!"); + } + return sb.toString(); } public String toString() { String displaySuffix = (displayName != null && (!displayName.isEmpty())) ? '\'' + displayName + '\'' : ""; - return URN_PREFIX + tenantName + ':' + resource + ":" + "name" + "(" + shortIdentifier + ")" + displaySuffix; + //return URN_PREFIX + tenantName + ':' + resource + ":" + "name" + "(" + shortIdentifier + ")" + displaySuffix; + StringBuilder sb = new StringBuilder(); + sb.append(URN_PREFIX); + sb.append(tenantName); + sb.append(RefNameUtils.SEPARATOR); + sb.append(resource); + sb.append(RefNameUtils.SEPARATOR); + if(csid!=null) { + sb.append(RefNameUtils.ID_SPECIFIER); + sb.append("("); + sb.append(csid); + sb.append(")"); + } else if(shortIdentifier!= null) { + sb.append(RefNameUtils.NAME_SPECIFIER); + sb.append("("); + sb.append(shortIdentifier); + sb.append(")"); + } else { + throw new NullPointerException("Authority has neither CSID nor shortID!"); + } + sb.append(displaySuffix); + return sb.toString(); } - public static Authority buildAuthority(String tenantName, String serviceName, String authorityShortIdentifier, String authorityDisplayName) { + public static Authority buildAuthority( + String tenantName, + String serviceName, + String csid, + String authorityShortIdentifier, + String authorityDisplayName) { Authority authority = new Authority(); authority.tenantName = tenantName; authority.resource = serviceName; if (Tools.notEmpty(authority.resource)) { authority.resource = authority.resource.toLowerCase(); } + authority.csid = csid; authority.shortIdentifier = authorityShortIdentifier; authority.displayName = authorityDisplayName; return authority; @@ -185,7 +233,7 @@ public class RefName { public static AuthorityItem buildAuthorityItem(String tenantName, String serviceName, String authorityShortID, String itemShortID, String itemDisplayName) { - Authority authority = Authority.buildAuthority(tenantName, serviceName, authorityShortID, ""); + Authority authority = Authority.buildAuthority(tenantName, serviceName, null, authorityShortID, ""); return buildAuthorityItem(authority, itemShortID, itemDisplayName); } @@ -259,8 +307,10 @@ public class RefName { if (authorityInfo.name != null && !authorityInfo.name.trim().isEmpty()) { authority.shortIdentifier = authorityInfo.name; - } else { - authority.shortIdentifier = authorityInfo.csid; + } + if (authorityInfo.csid != null + && !authorityInfo.csid.trim().isEmpty()) { + authority.csid = authorityInfo.csid; } if (includeDisplayName) { authority.displayName = authorityInfo.displayName; diff --git a/services/common-api/src/main/java/org/collectionspace/services/common/api/RefNameUtils.java b/services/common-api/src/main/java/org/collectionspace/services/common/api/RefNameUtils.java index 17f6f36da..c15961751 100644 --- a/services/common-api/src/main/java/org/collectionspace/services/common/api/RefNameUtils.java +++ b/services/common-api/src/main/java/org/collectionspace/services/common/api/RefNameUtils.java @@ -45,6 +45,9 @@ public class RefNameUtils { public static final int URN_PREFIX_LEN = 11; public static final String URN_NAME_PREFIX = "urn:cspace:name("; public static final int URN_NAME_PREFIX_LEN = 16; + public static final String NAME_SPECIFIER = "name"; + public static final String ID_SPECIFIER = "id"; + // FIXME Should not be hard-coded private static final String ITEMS_REGEX = "item|person|organization"; // In a list of tokens, these are indices for each part @@ -83,14 +86,15 @@ public class RefNameUtils { if(idTokens.length ctx = + resource.createServiceContext(authority.resource); + // HACK - this really must be moved to the doc handler, not here. No Nuxeo specific stuff here! + DocumentModel docModel = NuxeoUtils.getDocFromCsid(ctx, repoSession, authority.csid); + return docModel; + } + return null; } // THis is ugly, but prevents us parsing the refName twice. Once we make refName a little more diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java index 94bfe6977..c2570da50 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java @@ -323,7 +323,7 @@ public abstract class DocumentModelHandler String csid = docWrapper.getWrappedObject().getName(); String refnameDisplayName = getRefnameDisplayName(docWrapper); RefName.RefNameInterface refname = RefName.Authority.buildAuthority(tenantName, serviceName, - csid, refnameDisplayName); + csid, null, refnameDisplayName); return refname; } diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java index d2732224a..dc5f1ad3e 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java @@ -408,7 +408,7 @@ public class RelationDocumentModelHandler } } if(docModel==null) { - throw new DocumentNotFoundException("Relation.getSubjectOrObjectDocModel could not find doc with CSID: " + throw new DocumentNotFoundException("RelationDMH.getSubjectOrObjectDocModel could not find doc with CSID: " +csid+" and/or refName: "+refName ); } return docModel; @@ -436,6 +436,7 @@ public class RelationDocumentModelHandler properties.put((fSubject?RelationJAXBSchema.SUBJECT_URI:RelationJAXBSchema.OBJECT_URI), uri); + /* String common_schema = getCommonSchemaNameForDocType(doctype); if(common_schema!=null) { @@ -444,6 +445,15 @@ public class RelationDocumentModelHandler properties.put((fSubject?RelationJAXBSchema.SUBJECT_REFNAME:RelationJAXBSchema.OBJECT_REFNAME), refname); } + */ + String refname = (String) + subjectOrObjectDocModel.getProperty( + CollectionSpaceClient.COLLECTIONSPACE_CORE_SCHEMA, + CollectionSpaceClient.COLLECTIONSPACE_CORE_REFNAME); + properties.put((fSubject? + RelationJAXBSchema.SUBJECT_REFNAME + :RelationJAXBSchema.OBJECT_REFNAME), + refname); } catch (ClientException ce) { throw new RuntimeException( "populateSubjectOrObjectValues: Problem fetching field " + ce.getLocalizedMessage()); @@ -459,6 +469,7 @@ public class RelationDocumentModelHandler } } + /* private String getCommonSchemaNameForDocType(String docType) { String common_schema = null; if(docType!=null) { @@ -479,5 +490,6 @@ public class RelationDocumentModelHandler } return common_schema; } + */ } -- 2.47.3