From: remillet Date: Sat, 11 Nov 2017 00:25:17 +0000 (-0800) Subject: DRYD-126: Added support for 'mkRtSbjOrObj' query param on calls to /servicegroups... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=8d2ea61d1ad79ab18a39573ac06d1d8b085dbd91;p=tmp%2Fjakarta-migration.git DRYD-126: Added support for 'mkRtSbjOrObj' query param on calls to /servicegroups/*/items. --- 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 6a9ebb79d..60d17dfc5 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 @@ -247,9 +247,9 @@ public abstract class NuxeoDocumentModelHandler extends RemoteDocumentModelHa String commonSchema = getServiceContext().getCommonPartLabel(); extractPagingInfo(commonList, wrapDoc); - List resultsFields = getListItemsArray(); - int nFields = resultsFields.size() + NUM_STANDARD_LIST_RESULT_FIELDS; + List resultsFields = getListItemsArray(); // Get additional list result fields defined in the service bindings int baseFields = NUM_STANDARD_LIST_RESULT_FIELDS; + int nFields = resultsFields.size() + NUM_STANDARD_LIST_RESULT_FIELDS; if (markRtSbj != null || markRtSbjOrObj != null) { nFields++; baseFields++; @@ -271,12 +271,18 @@ public abstract class NuxeoDocumentModelHandler extends RemoteDocumentModelHa fields[i] = field.getElement(); } commonList.setFieldsReturned(fields); + Iterator iter = wrapDoc.getWrappedObject().iterator(); HashMap item = new HashMap(); while (iter.hasNext()) { DocumentModel docModel = iter.next(); String id = NuxeoUtils.getCsid(docModel); item.put(STANDARD_LIST_CSID_FIELD, id); + + // + // If the mark-related query param was set, check to see if the doc we're processing + // is related to the value specified in the mark-related query param. + // if (markRtSbj != null || markRtSbjOrObj != null) { String relationClause = RelationsUtils.buildWhereClause(markRtSbj, null, null, id, null, markRtSbjOrObj); String whereClause = relationClause + IQueryManager.SEARCH_QUALIFIER_AND @@ -289,6 +295,7 @@ public abstract class NuxeoDocumentModelHandler extends RemoteDocumentModelHa DocumentModelList docList = repoSession.query(query, null, 1, 0, false); item.put(STANDARD_LIST_MARK_RT_FIELD, docList.isEmpty() ? "false" : "true"); } + String uri = getUri(docModel); item.put(STANDARD_LIST_URI_FIELD, uri); item.put(STANDARD_LIST_REFNAME_FIELD, getRefname(docModel)); 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 d3348b029..2dacf87d8 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 @@ -319,6 +319,31 @@ public abstract class RemoteDocumentModelHandlerImpl return (TL) commonList; } + + /** + * Extract paging info. + * + * @param commonsList the commons list + * @return the tL + * @throws Exception the exception + */ + public TL extractPagingInfo(TL theCommonList, DocumentModelList docList) + throws Exception { + AbstractCommonList commonList = (AbstractCommonList) theCommonList; + + DocumentFilter docFilter = this.getDocumentFilter(); + long pageSize = docFilter.getPageSize(); + long pageNum = pageSize != 0 ? docFilter.getOffset() / pageSize : pageSize; + // set the page size and page number + commonList.setPageNum(pageNum); + commonList.setPageSize(pageSize); + // Set num of items in list. this is useful to our testing framework. + commonList.setItemsInPage(docList.size()); + // set the total result size + commonList.setTotalItems(docList.totalSize()); + + return (TL) commonList; + } /* (non-Javadoc) * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractAllParts(org.collectionspace.services.common.document.DocumentWrapper) diff --git a/services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/nuxeo/ServiceGroupDocumentModelHandler.java b/services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/nuxeo/ServiceGroupDocumentModelHandler.java index f318e359e..d3fba7369 100644 --- a/services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/nuxeo/ServiceGroupDocumentModelHandler.java +++ b/services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/nuxeo/ServiceGroupDocumentModelHandler.java @@ -29,6 +29,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import org.collectionspace.services.nuxeo.client.java.CommonList; @@ -39,6 +40,7 @@ import org.collectionspace.services.nuxeo.util.NuxeoUtils; import org.collectionspace.services.jaxb.AbstractCommonList; import org.collectionspace.services.client.CollectionSpaceClient; import org.collectionspace.services.client.IQueryManager; +import org.collectionspace.services.client.IRelationsManager; import org.collectionspace.services.client.PoxPayloadIn; import org.collectionspace.services.client.PoxPayloadOut; import org.collectionspace.services.common.NuxeoBasedResource; @@ -53,14 +55,19 @@ import org.collectionspace.services.common.api.RefName; import org.collectionspace.services.common.api.RefNameUtils; import org.collectionspace.services.common.api.RefNameUtils.AuthorityTermInfo; import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl; +import org.collectionspace.services.common.context.AbstractServiceContextImpl; import org.collectionspace.services.common.context.ServiceBindingUtils; 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.DocumentNotFoundException; import org.collectionspace.services.common.document.DocumentWrapper; +import org.collectionspace.services.common.document.TransactionException; import org.collectionspace.services.common.security.SecurityUtils; +import org.collectionspace.services.common.query.QueryContext; import org.collectionspace.services.common.query.nuxeo.QueryManagerNuxeoImpl; +import org.collectionspace.services.common.relation.nuxeo.RelationsUtils; +import org.collectionspace.services.config.service.ListResultField; import org.collectionspace.services.config.service.ServiceBindingType; import org.collectionspace.services.config.service.ServiceObjectType; import org.collectionspace.services.servicegroup.ServicegroupsCommon; @@ -77,10 +84,11 @@ public class ServiceGroupDocumentModelHandler protected final Logger logger = LoggerFactory.getLogger(this.getClass()); - protected static final int NUM_META_FIELDS = 3; + protected static final int NUM_META_FIELDS = 4; // the number of meta fields -i.e., DOC_TYPE_FIELD, DOC_NUMBER_FIELD, DOC_NAME_FIELD, STANDARD_LIST_MARK_RT_FIELD protected static final String DOC_TYPE_FIELD = "docType"; protected static final String DOC_NUMBER_FIELD = "docNumber"; protected static final String DOC_NAME_FIELD = "docName"; + protected static final String STANDARD_LIST_MARK_RT_FIELD = "related"; // // Returns a service payload for an authority item @@ -245,7 +253,7 @@ public class ServiceGroupDocumentModelHandler if (docList == null) { // found no authRef fields - nothing to process return list; } - processDocList(ctx.getTenantId(), docList, queriedServiceBindings, commonList); + processDocList(ctx, docList, queriedServiceBindings, commonList); list.setItemsInPage(docList.size()); list.setTotalItems(docList.totalSize()); } catch (DocumentException de) { @@ -291,76 +299,136 @@ public class ServiceGroupDocumentModelHandler return "/" + sb.getName().toLowerCase() + "/" + csid; } - private void processDocList(String tenantId, + private void processDocList(ServiceContext ctx, DocumentModelList docList, Map queriedServiceBindings, - CommonList list) { - String fields[] = new String[NUM_META_FIELDS + NUM_STANDARD_LIST_RESULT_FIELDS]; - fields[0] = STANDARD_LIST_CSID_FIELD; - fields[1] = STANDARD_LIST_URI_FIELD; - fields[2] = STANDARD_LIST_UPDATED_AT_FIELD; - fields[3] = STANDARD_LIST_WORKFLOW_FIELD; - fields[4] = STANDARD_LIST_REFNAME_FIELD; - fields[5] = DOC_NAME_FIELD; - fields[6] = DOC_NUMBER_FIELD; - fields[7] = DOC_TYPE_FIELD; - list.setFieldsReturned(fields); - - Iterator iter = docList.iterator(); - HashMap item = new HashMap(); - while (iter.hasNext()) { - DocumentModel docModel = iter.next(); - String docType = docModel.getDocumentType().getName(); - docType = ServiceBindingUtils.getUnqualifiedTenantDocType(docType); - ServiceBindingType sb = queriedServiceBindings.get(docType); - if (sb == null) { - throw new RuntimeException("processDocList: No Service Binding for docType: " + docType); - } - - String csid = NuxeoUtils.getCsid(docModel); - item.put(STANDARD_LIST_CSID_FIELD, csid); - - UriTemplateRegistry uriTemplateRegistry = ServiceMain.getInstance().getUriTemplateRegistry(); - StoredValuesUriTemplate storedValuesResourceTemplate = uriTemplateRegistry.get(new UriTemplateRegistryKey(tenantId, docType)); - Map additionalValues = new HashMap(); - if (storedValuesResourceTemplate.getUriTemplateType() == UriTemplateFactory.ITEM) { - try { - String inAuthorityCsid = (String) NuxeoUtils.getProperyValue(docModel, "inAuthority"); //docModel.getPropertyValue("inAuthority"); // AuthorityItemJAXBSchema.IN_AUTHORITY - additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, inAuthorityCsid); - additionalValues.put(UriTemplateFactory.ITEM_IDENTIFIER_VAR, csid); - } catch (Exception e) { - String msg = String.format("Could not extract inAuthority property from authority item with CSID = ", docModel.getName()); - logger.warn(msg, e); - } - } else { - additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, csid); - } - - String uriStr = storedValuesResourceTemplate.buildUri(additionalValues); - item.put(STANDARD_LIST_URI_FIELD, uriStr); - try { - item.put(STANDARD_LIST_UPDATED_AT_FIELD, getUpdatedAtAsString(docModel)); - item.put(STANDARD_LIST_WORKFLOW_FIELD, docModel.getCurrentLifeCycleState()); - item.put(STANDARD_LIST_REFNAME_FIELD, getRefname(docModel)); - } catch(Exception e) { - logger.error("Error getting core values for doc ["+csid+"]: "+e.getLocalizedMessage()); - } + CommonList list) throws DocumentException { + + String tenantId = ctx.getTenantId(); + CoreSessionInterface repoSession = null; + RepositoryClientImpl repoClient = null; + boolean releaseRepoSession = false; + + MultivaluedMap queryParams = getServiceContext().getQueryParams(); + String markRtSbj = queryParams.getFirst(IQueryManager.MARK_RELATED_TO_CSID_AS_SUBJECT); + if (markRtSbj != null && markRtSbj.isEmpty()) { + markRtSbj = null; + } + + String markRtSbjOrObj = queryParams.getFirst(IQueryManager.MARK_RELATED_TO_CSID_AS_EITHER); + if (markRtSbjOrObj != null && markRtSbjOrObj.isEmpty()) { + markRtSbjOrObj = null; + } + + try { + if (markRtSbj != null || markRtSbjOrObj != null) { + repoClient = (RepositoryClientImpl) this.getRepositoryClient(ctx); + RepositoryClientImpl nuxeoRepoClient = (RepositoryClientImpl) repoClient; + repoSession = this.getRepositorySession(); + if (repoSession == null) { + repoSession = repoClient.getRepositorySession(ctx); + releaseRepoSession = true; + } + } + + String fields[] = new String[NUM_META_FIELDS + NUM_STANDARD_LIST_RESULT_FIELDS]; + fields[0] = STANDARD_LIST_CSID_FIELD; + fields[1] = STANDARD_LIST_URI_FIELD; + fields[2] = STANDARD_LIST_UPDATED_AT_FIELD; + fields[3] = STANDARD_LIST_WORKFLOW_FIELD; + fields[4] = STANDARD_LIST_REFNAME_FIELD; + fields[5] = DOC_NAME_FIELD; + fields[6] = DOC_NUMBER_FIELD; + fields[7] = DOC_TYPE_FIELD; + if (markRtSbj != null || markRtSbjOrObj != null) { + fields[8] = STANDARD_LIST_MARK_RT_FIELD; + } - String value = ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NUMBER_PROP, docModel); - if (value != null) { - item.put(DOC_NUMBER_FIELD, value); - } - - value = ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NAME_PROP, docModel); - if (value != null) { - item.put(DOC_NAME_FIELD, value); - } - - item.put(DOC_TYPE_FIELD, docType); - // add the item to the list - list.addItem(item); - item.clear(); - } + list.setFieldsReturned(fields); + + Iterator iter = docList.iterator(); + HashMap item = new HashMap(); + while (iter.hasNext()) { + DocumentModel docModel = iter.next(); + String docType = docModel.getDocumentType().getName(); + docType = ServiceBindingUtils.getUnqualifiedTenantDocType(docType); + ServiceBindingType sb = queriedServiceBindings.get(docType); + if (sb == null) { + throw new RuntimeException("processDocList: No Service Binding for docType: " + docType); + } + + String csid = NuxeoUtils.getCsid(docModel); + item.put(STANDARD_LIST_CSID_FIELD, csid); + + // + // If the mark-related query param was set, check to see if the doc we're processing + // is related to the value specified in the mark-related query param. + // + if (markRtSbj != null || markRtSbjOrObj != null) { + String relationClause = RelationsUtils.buildWhereClause(markRtSbj, null, null, csid, null, markRtSbjOrObj); + String whereClause = relationClause + IQueryManager.SEARCH_QUALIFIER_AND + + NuxeoUtils.buildWorkflowNotDeletedWhereClause(); + QueryContext queryContext = new QueryContext(ctx, whereClause); + queryContext.setDocType(IRelationsManager.DOC_TYPE); + String query = NuxeoUtils.buildNXQLQuery(queryContext); + // Search for 1 relation that matches. 1 is enough to fail + // the filter + DocumentModelList queryDocList = repoSession.query(query, null, 1, 0, false); + item.put(STANDARD_LIST_MARK_RT_FIELD, queryDocList.isEmpty() ? "false" : "true"); + } + + UriTemplateRegistry uriTemplateRegistry = ServiceMain.getInstance().getUriTemplateRegistry(); + StoredValuesUriTemplate storedValuesResourceTemplate = uriTemplateRegistry.get(new UriTemplateRegistryKey(tenantId, docType)); + Map additionalValues = new HashMap(); + if (storedValuesResourceTemplate.getUriTemplateType() == UriTemplateFactory.ITEM) { + try { + String inAuthorityCsid = (String) NuxeoUtils.getProperyValue(docModel, "inAuthority"); //docModel.getPropertyValue("inAuthority"); // AuthorityItemJAXBSchema.IN_AUTHORITY + additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, inAuthorityCsid); + additionalValues.put(UriTemplateFactory.ITEM_IDENTIFIER_VAR, csid); + } catch (Exception e) { + String msg = String.format("Could not extract inAuthority property from authority item with CSID = ", docModel.getName()); + logger.warn(msg, e); + } + } else { + additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, csid); + } + + String uriStr = storedValuesResourceTemplate.buildUri(additionalValues); + item.put(STANDARD_LIST_URI_FIELD, uriStr); + try { + item.put(STANDARD_LIST_UPDATED_AT_FIELD, getUpdatedAtAsString(docModel)); + item.put(STANDARD_LIST_WORKFLOW_FIELD, docModel.getCurrentLifeCycleState()); + item.put(STANDARD_LIST_REFNAME_FIELD, getRefname(docModel)); + } catch(Exception e) { + logger.error("Error getting core values for doc ["+csid+"]: "+e.getLocalizedMessage()); + } + + String value = ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NUMBER_PROP, docModel); + if (value != null) { + item.put(DOC_NUMBER_FIELD, value); + } + + value = ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NAME_PROP, docModel); + if (value != null) { + item.put(DOC_NAME_FIELD, value); + } + + item.put(DOC_TYPE_FIELD, docType); + // add the item to the list + list.addItem(item); + item.clear(); + } + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + // If we got/acquired a new session then we're responsible for releasing it. + if (releaseRepoSession && repoSession != null) { + repoClient.releaseRepositorySession(ctx, repoSession); + } + } } }