]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-126: Added support for 'mkRtSbjOrObj' query param on calls to /servicegroups...
authorremillet <remillet@yahoo.com>
Sat, 11 Nov 2017 00:25:17 +0000 (16:25 -0800)
committerremillet <remillet@yahoo.com>
Sat, 11 Nov 2017 00:25:17 +0000 (16:25 -0800)
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/NuxeoDocumentModelHandler.java
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java
services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/nuxeo/ServiceGroupDocumentModelHandler.java

index 6a9ebb79d8ddfdf23b44a5c8197e31dbb3ac9cdc..60d17dfc5e0bb9a8d6faebb3d97826ede91eb1e4 100644 (file)
@@ -247,9 +247,9 @@ public abstract class NuxeoDocumentModelHandler<T> extends RemoteDocumentModelHa
 
                        String commonSchema = getServiceContext().getCommonPartLabel();
                        extractPagingInfo(commonList, wrapDoc);
-                       List<ListResultField> resultsFields = getListItemsArray();
-                       int nFields = resultsFields.size() + NUM_STANDARD_LIST_RESULT_FIELDS;
+                       List<ListResultField> 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<T> extends RemoteDocumentModelHa
                                fields[i] = field.getElement();
                        }
                        commonList.setFieldsReturned(fields);
+                       
                        Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
                        HashMap<String, Object> item = new HashMap<String, Object>();
                        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<T> 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));
index d3348b029dfa7476dd2db1a4c6aecc893ccaedc3..2dacf87d8085e4e6540ac1503fdcb16f71aa585a 100644 (file)
@@ -319,6 +319,31 @@ public abstract class   RemoteDocumentModelHandlerImpl<T, TL>
 
         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)
index f318e359edbdac8d52981964f243a17fbd312c30..d3fba73699141487af555427cc0deb69f847d365 100644 (file)
@@ -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<PoxPayloadIn, PoxPayloadOut> ctx,
                        DocumentModelList docList,
                        Map<String, ServiceBindingType> 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<DocumentModel> iter = docList.iterator();
-               HashMap<String, Object> item = new HashMap<String, Object>();
-        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<String, String> additionalValues = new HashMap<String, String>();
-                   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<String, String> 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<DocumentModel> iter = docList.iterator();
+                       HashMap<String, Object> item = new HashMap<String, Object>();
+               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<String, String> additionalValues = new HashMap<String, String>();
+                           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);
+                       }
+               }
     }
 }