From c44b7b3eeaa1ecac5bcee05d6ea39d462a8cb640 Mon Sep 17 00:00:00 2001 From: remillet Date: Sat, 25 Feb 2017 22:14:55 -0800 Subject: [PATCH] DRYD-90: Fixed problem with /servicegroupd/common/item/${csid} when csid pointed to an authority term --- .../services/common/api/RefName.java | 13 ++-- .../services/common/api/RefNameUtils.java | 14 ++-- .../services/common/NuxeoBasedResource.java | 4 +- .../client/java/DocumentModelHandler.java | 2 +- .../java/NuxeoDocumentModelHandler.java | 2 +- .../java/RemoteDocumentModelHandlerImpl.java | 4 +- .../client/java/RepositoryClientImpl.java | 6 +- .../services/nuxeo/util/NuxeoUtils.java | 50 +++++++++---- .../nuxeo/RelationDocumentModelHandler.java | 2 +- .../nuxeo/RelationValidatorHandler.java | 30 +++++--- services/servicegroup/service/pom.xml | 5 ++ .../servicegroup/ServiceGroupResource.java | 6 +- .../ServiceGroupDocumentModelHandler.java | 71 +++++++++++++++---- 13 files changed, 142 insertions(+), 67 deletions(-) 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 54b34ac21..ee87e0bca 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 @@ -173,15 +173,12 @@ public class RefName { public String shortIdentifier = ""; public String displayName = ""; - public static AuthorityItem parse(String urn) { + public static AuthorityItem parse(String urn) throws IllegalArgumentException { AuthorityItem authorityItem = null; - try { - RefNameUtils.AuthorityTermInfo termInfo = - RefNameUtils.parseAuthorityTermInfo(urn); - authorityItem = authorityItemFromTermInfo(termInfo); - } catch (IllegalArgumentException iae) { - return null; - } + + RefNameUtils.AuthorityTermInfo termInfo = RefNameUtils.parseAuthorityTermInfo(urn); + authorityItem = authorityItemFromTermInfo(termInfo); + return authorityItem; } 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 28b749411..e117600b0 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 @@ -203,13 +203,13 @@ public class RefNameUtils { return new AuthorityInfo(refNameTokens); } - public static AuthorityTermInfo parseAuthorityTermInfo(String refName) - throws IllegalArgumentException { - if(refName==null || !refName.startsWith(URN_PREFIX)) - throw new IllegalArgumentException( "Null or invalid refName syntax"); - String[] refNameTokens = refName.substring(URN_PREFIX_LEN).split(SEPARATOR, AUTH_ITEM_REFNAME_TOKENS); - return new AuthorityTermInfo(refNameTokens); - } + public static AuthorityTermInfo parseAuthorityTermInfo(String refName) throws IllegalArgumentException { + if (refName == null || !refName.startsWith(URN_PREFIX)) { + throw new IllegalArgumentException("Null or invalid refName syntax"); + } + String[] refNameTokens = refName.substring(URN_PREFIX_LEN).split(SEPARATOR, AUTH_ITEM_REFNAME_TOKENS); + return new AuthorityTermInfo(refNameTokens); + } public static String stripAuthorityTermDisplayName(String refName) throws IllegalArgumentException { diff --git a/services/common/src/main/java/org/collectionspace/services/common/NuxeoBasedResource.java b/services/common/src/main/java/org/collectionspace/services/common/NuxeoBasedResource.java index 60697a9c4..bdd234001 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/NuxeoBasedResource.java +++ b/services/common/src/main/java/org/collectionspace/services/common/NuxeoBasedResource.java @@ -642,9 +642,9 @@ public abstract class NuxeoBasedResource * for all inheriting resource classes. Just use ServiceContext.getResourceMap() to get * the map, and pass it in. */ - public static DocumentModel getDocModelForRefName(CoreSessionInterface repoSession, String refName, ResourceMap resourceMap) + public static DocumentModel getDocModelForRefName(ServiceContext ctx, String refName, ResourceMap resourceMap) throws Exception, DocumentNotFoundException { - return NuxeoUtils.getDocModelForRefName(repoSession, refName, resourceMap); + return NuxeoUtils.getDocModelForRefName(ctx, refName, resourceMap); } // 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 d26a52c67..52675c71a 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 @@ -425,7 +425,7 @@ public abstract class DocumentModelHandler try { QueryContext queryContext = new QueryContext(ctx, selectClause, whereClause, orderByClause); - result = NuxeoUtils.buildNXQLQuery(ctx, queryContext); + result = NuxeoUtils.buildNXQLQuery(queryContext); } catch (DocumentException de) { throw de; } catch (Exception x) { diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/NuxeoDocumentModelHandler.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/NuxeoDocumentModelHandler.java index 97256338e..064d12d10 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/NuxeoDocumentModelHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/NuxeoDocumentModelHandler.java @@ -278,7 +278,7 @@ public abstract class NuxeoDocumentModelHandler extends RemoteDocumentModelHa + NuxeoUtils.buildWorkflowNotDeletedWhereClause(); QueryContext queryContext = new QueryContext(ctx, whereClause); queryContext.setDocType(IRelationsManager.DOC_TYPE); - String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext); + String query = NuxeoUtils.buildNXQLQuery(queryContext); // Search for 1 relation that matches. 1 is enough to fail // the filter DocumentModelList docList = repoSession.query(query, null, 1, 0, false); 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 3d4b70687..5252a4d50 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 @@ -717,7 +717,7 @@ public abstract class RemoteDocumentModelHandlerImpl // String csid = null; try { - DocumentModel docModel = NuxeoUtils.getDocModelForRefName(this.getRepositorySession(), refName, this.getServiceContext().getResourceMap()); + DocumentModel docModel = NuxeoUtils.getDocModelForRefName(getServiceContext(), refName, getServiceContext().getResourceMap()); csid = NuxeoUtils.getCsid(docModel); } catch (Exception e1) { String msg = String.format("Could not find CSID for authority reference with refname = %s.", refName); @@ -1319,7 +1319,7 @@ public abstract class RemoteDocumentModelHandlerImpl logger.trace("Fetching CSID for child with only refname: "+newChildRefName); } DocumentModel newChildDocModel = - NuxeoBasedResource.getDocModelForRefName(this.getRepositorySession(), + NuxeoBasedResource.getDocModelForRefName(getServiceContext(), newChildRefName, getServiceContext().getResourceMap()); newChildCsid = getCsid(newChildDocModel); } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryClientImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryClientImpl.java index bd6ba7b90..32c7979b9 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryClientImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryClientImpl.java @@ -412,7 +412,7 @@ public class RepositoryClientImpl implements RepositoryClient ctx, QueryContext queryContext) throws Exception { + static public final String buildNXQLQuery(QueryContext queryContext) throws Exception { StringBuilder query = new StringBuilder(queryContext.getSelectClause()); // Since we have a tenant qualification in the WHERE clause, we do not need // tenant-specific doc types @@ -709,7 +709,7 @@ public class NuxeoUtils { // // Since we're doing a query, we get back a list so we need to make sure there is only // a single result since CSID values are supposed to be unique. - String query = buildNXQLQuery(ctx, queryContext); + String query = buildNXQLQuery(queryContext); docModelList = repoSession.query(query); long resultSize = docModelList.totalSize(); if (resultSize == 1) { @@ -725,20 +725,40 @@ public class NuxeoUtils { return null; } - public static DocumentModel getDocModelForRefName(CoreSessionInterface repoSession, String refName, ResourceMap resourceMap) + /** + * The refname could be for an authority, an authority item/term, or a csid-form to an object or procedure record + * @param repoSession + * @param refName + * @param resourceMap + * @return + * @throws DocumentNotFoundException + * @throws Exception + */ + public static DocumentModel getDocModelForRefName(ServiceContext ctx, String refName, ResourceMap resourceMap) throws DocumentNotFoundException, Exception { - RefName.AuthorityItem item = RefName.AuthorityItem.parse(refName); - if (item != null) { - NuxeoBasedResource resource = (NuxeoBasedResource) resourceMap.get(item.inAuthority.resource); - return resource.getDocModelForAuthorityItem(repoSession, item); + RefName.AuthorityItem item = null; + CoreSessionInterface repoSession = (CoreSessionInterface)ctx.getCurrentRepositorySession(); + // + // Let's see if our refname refers to an authority item/term. + // + try { + item = RefName.AuthorityItem.parse(refName); + if (item != null) { + NuxeoBasedResource resource = (NuxeoBasedResource) resourceMap.get(item.inAuthority.resource); + return resource.getDocModelForAuthorityItem(repoSession, item); + } + } catch (IllegalArgumentException e) { + // Ignore exception } - RefName.Authority authority = RefName.Authority.parse(refName); - // Handle case of objects refNames, which must be csid based. - if(authority != null && !Tools.isEmpty(authority.csid)) { + + // + // If we got this far, we know the refname doesn't refer to an authority item/term, so it might refer + // to an authority or an object or procedure. + // + RefName.Authority authority = RefName.Authority.parse(refName); // could be an authority or an object or procedure record + // Handle case of objects refNames, which MUST be csid based. + if (authority != null && !Tools.isEmpty(authority.csid)) { NuxeoBasedResource resource = (NuxeoBasedResource) resourceMap.get(authority.resource); - // Ensure we have the right context. - ServiceContext 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; @@ -780,7 +800,7 @@ public class NuxeoUtils { // // Since we're doing a query, we get back a list so we need to make sure there is only // a single result since CSID values are supposed to be unique. - String query = buildNXQLQuery(ctx, queryContext); + String query = buildNXQLQuery(queryContext); docModelList = repoSession.query(query); long resultSize = docModelList.totalSize(); if (resultSize == 1) { @@ -815,7 +835,7 @@ public class NuxeoUtils { // // Since we're doing a query, we get back a list so we need to make sure there is only // a single result since CSID values are supposed to be unique. - String query = buildNXQLQuery(ctx, queryContext); + String query = buildNXQLQuery(queryContext); docModelList = repoSession.query(query); long resultSize = docModelList.totalSize(); if (resultSize == 1) { 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 745f87f6d..09f2a1d4a 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 @@ -404,7 +404,7 @@ public class RelationDocumentModelHandler try { refName = (String) relationDocModel.getProperty(commonPartLabel, (fSubject?RelationJAXBSchema.SUBJECT_REFNAME:RelationJAXBSchema.OBJECT_REFNAME)); - docModel = NuxeoBasedResource.getDocModelForRefName(repoSession, refName, ctx.getResourceMap()); + docModel = NuxeoBasedResource.getDocModelForRefName(ctx, refName, ctx.getResourceMap()); } catch (Exception e) { throw new InvalidDocumentException( "Relation record must have a CSID or refName to identify the object of the relation.", e); diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java index dff09ae20..90ffd2f0d 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java @@ -102,18 +102,28 @@ public class RelationValidatorHandler extends ValidatorHandlerImplorg.collectionspace.services org.collectionspace.services.common + + org.collectionspace.services + org.collectionspace.services.authority.service + ${project.version} + org.collectionspace.services org.collectionspace.services.servicegroup.client diff --git a/services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/ServiceGroupResource.java b/services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/ServiceGroupResource.java index 7361a90c3..24098d35e 100644 --- a/services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/ServiceGroupResource.java +++ b/services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/ServiceGroupResource.java @@ -207,7 +207,7 @@ public class ServiceGroupResource extends AbstractCollectionSpaceResourceImpl ctx, + public PoxPayloadOut getResourceItemForCsid(ServiceContext ctx, List serviceGroupNames, String csid) throws DocumentException { PoxPayloadOut result = null; @@ -97,14 +119,22 @@ public class ServiceGroupDocumentModelHandler DocumentModelList docList = this.getDocListForGroup(ctx, serviceGroupNames, queriedServiceBindings, repoSession, repoClient); if (docList == null) { // found no authRef fields - nothing to process - //return list; + throw new DocumentNotFoundException(); } DocumentModel docModel = docList.get(0); - TenantBindingConfigReaderImpl bindingReader = ServiceMain.getInstance().getTenantBindingConfigReader(); - String serviceName = ServiceBindingUtils.getServiceNameFromObjectName(bindingReader, ctx.getTenantId(), - docModel.getDocumentType().getName()); - NuxeoBasedResource resource = (NuxeoBasedResource) ctx.getResourceMap().get(serviceName); - result = resource.getWithParentCtx(ctx, csid); + // + // Determine if the docModel is an authority term, object, or some other procedure record. + // + String termRefName = (String) NuxeoUtils.getProperyValue(docModel, CollectionSpaceClient.COLLECTIONSPACE_CORE_REFNAME); + if (isAuthorityTermDocument(termRefName) == true) { + result = getAuthorityItem(ctx, termRefName); + } else { + TenantBindingConfigReaderImpl bindingReader = ServiceMain.getInstance().getTenantBindingConfigReader(); + String serviceName = ServiceBindingUtils.getServiceNameFromObjectName(bindingReader, ctx.getTenantId(), + docModel.getDocumentType().getName()); + NuxeoBasedResource resource = (NuxeoBasedResource) ctx.getResourceMap().get(serviceName); + result = resource.getWithParentCtx(ctx, csid); + } } catch (DocumentException de) { throw de; } catch (Exception e) { @@ -127,7 +157,21 @@ public class ServiceGroupDocumentModelHandler return result; } - private DocumentModelList getDocListForGroup( + private boolean isAuthorityTermDocument(String termRefName) { + boolean result = true; + + try { + //String inAuthorityCsid = (String) NuxeoUtils.getProperyValue(docModel, "inAuthority"); //docModel.getPropertyValue("inAuthority"); // AuthorityItemJAXBSchema.IN_AUTHORITY + //String refName = (String) NuxeoUtils.getProperyValue(docModel, CollectionSpaceClient.COLLECTIONSPACE_CORE_REFNAME); + RefName.AuthorityItem item = RefName.AuthorityItem.parse(termRefName); + } catch (IllegalArgumentException e) { + result = false; + } + + return result; + } + + private DocumentModelList getDocListForGroup( ServiceContext ctx, List serviceGroupNames, Map queriedServiceBindings, @@ -270,8 +314,7 @@ public class ServiceGroupDocumentModelHandler docType = ServiceBindingUtils.getUnqualifiedTenantDocType(docType); ServiceBindingType sb = queriedServiceBindings.get(docType); if (sb == null) { - throw new RuntimeException( - "processDocList: No Service Binding for docType: " + docType); + throw new RuntimeException("processDocList: No Service Binding for docType: " + docType); } String csid = NuxeoUtils.getCsid(docModel); -- 2.47.3