]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5381 Added support to pass a new query param, which will mark list results...
authorPatrick Schmitz <pschmitz@berkeley.edu>
Mon, 23 Jul 2012 23:26:24 +0000 (16:26 -0700)
committerPatrick Schmitz <pschmitz@berkeley.edu>
Mon, 23 Jul 2012 23:26:24 +0000 (16:26 -0700)
services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java

index accc0cbbb0b4d3e211bb264ee45ed13eec7d97e6..c2e9c0c5423be598e35ff56d60af585dab4eda0d 100644 (file)
@@ -51,6 +51,7 @@ public interface IQueryManager {
        final static String SEARCH_RELATED_MATCH_OBJ_DOCTYPES = "rtObjDocTypes";\r
        final static String SELECT_DOC_TYPE_FIELD = "selectDocType";\r
        \r
+       final static String MARK_RELATED_TO_CSID_AS_SUBJECT = "mkRtSbj";\r
        //\r
        // Generic CMIS property mapping constants\r
        //\r
index 9b81c82102468f4dcac0cd2c2a4c0d6c81612384..c8f4efbc7566775d6bc13fa3b4cf0587a0ae40e9 100644 (file)
@@ -30,13 +30,20 @@ import java.util.HashMap;
 import java.util.Iterator;\r
 import java.util.List;\r
 \r
+import javax.ws.rs.core.MultivaluedMap;\r
+\r
 import org.collectionspace.services.client.CollectionSpaceClient;\r
+import org.collectionspace.services.client.IQueryManager;\r
+import org.collectionspace.services.client.IRelationsManager;\r
 import org.collectionspace.services.common.ReflectionMapper;\r
 import org.collectionspace.services.common.api.Tools;\r
+import org.collectionspace.services.common.context.AbstractServiceContextImpl;\r
 import org.collectionspace.services.common.context.MultipartServiceContext;\r
 import org.collectionspace.services.common.datetime.DateTimeFormatUtils;\r
 import org.collectionspace.services.common.document.DocumentException;\r
 import org.collectionspace.services.common.document.DocumentWrapper;\r
+import org.collectionspace.services.common.query.QueryContext;\r
+import org.collectionspace.services.common.relation.nuxeo.RelationsUtils;\r
 import org.collectionspace.services.config.service.DocHandlerParams;\r
 import org.collectionspace.services.config.service.ListResultField;\r
 import org.collectionspace.services.config.service.ServiceBindingType;\r
@@ -46,6 +53,7 @@ import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandler
 import org.collectionspace.services.nuxeo.util.NuxeoUtils;\r
 import org.nuxeo.ecm.core.api.DocumentModel;\r
 import org.nuxeo.ecm.core.api.DocumentModelList;\r
+import org.nuxeo.ecm.core.api.repository.RepositoryInstance;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
@@ -71,6 +79,7 @@ public abstract class DocHandlerBase<T> extends RemoteDocumentModelHandlerImpl<T
     protected static final String STANDARD_LIST_URI_FIELD = CollectionSpaceClient.COLLECTIONSPACE_CORE_URI;\r
     protected static final String STANDARD_LIST_UPDATED_AT_FIELD = CollectionSpaceClient.COLLECTIONSPACE_CORE_UPDATED_AT;\r
     protected static final String STANDARD_LIST_WORKFLOW_FIELD = CollectionSpaceClient.COLLECTIONSPACE_CORE_WORKFLOWSTATE;\r
+    protected static final String STANDARD_LIST_MARK_RT_FIELD = "relTo";\r
 \r
     @Override\r
     public AbstractCommonList getCommonPartList() {\r
@@ -179,56 +188,106 @@ public abstract class DocHandlerBase<T> extends RemoteDocumentModelHandlerImpl<T
         if (!Tools.isBlank(classname)){\r
              return extractCommonPartListLaramieHACK(wrapDoc);\r
         }\r
-        \r
-       String commonSchema = getServiceContext().getCommonPartLabel();\r
        CommonList commonList = new CommonList();\r
-        extractPagingInfo(commonList, wrapDoc);\r
-        List<ListResultField> resultsFields = getListItemsArray();\r
-        int nFields = resultsFields.size() + NUM_STANDARD_LIST_RESULT_FIELDS;\r
-        String fields[] = new String[nFields];\r
-        fields[0] = STANDARD_LIST_CSID_FIELD;\r
-        fields[1] = STANDARD_LIST_URI_FIELD;\r
-        fields[2] = STANDARD_LIST_UPDATED_AT_FIELD;\r
-        fields[3] = STANDARD_LIST_WORKFLOW_FIELD;\r
-        for(int i = NUM_STANDARD_LIST_RESULT_FIELDS; i < nFields; i++) {\r
-               ListResultField field = resultsFields.get(i - NUM_STANDARD_LIST_RESULT_FIELDS); \r
-               fields[i] = field.getElement();\r
-        }\r
-               commonList.setFieldsReturned(fields);\r
-               Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();\r
-               HashMap<String, Object> item = new HashMap<String, Object>();\r
-               while (iter.hasNext()) {\r
-                       DocumentModel docModel = iter.next();\r
-                       String id = NuxeoUtils.getCsid(docModel);\r
-                       item.put(STANDARD_LIST_CSID_FIELD, id);\r
-                       String uri = getUri(docModel);\r
-                       item.put(STANDARD_LIST_URI_FIELD, uri);\r
-                       item.put(STANDARD_LIST_UPDATED_AT_FIELD,\r
-                                       getUpdatedAtAsString(docModel));\r
-                       item.put(STANDARD_LIST_WORKFLOW_FIELD,\r
-                                       docModel.getCurrentLifeCycleState());\r
-\r
-                       for (ListResultField field : resultsFields) {\r
-                               String schema = field.getSchema();\r
-                               if (schema == null || schema.trim().isEmpty()) {\r
-                                       schema = commonSchema;\r
-                               }\r
-                               Object value = getListResultValue(docModel, schema, field);\r
-                               if (value != null && value instanceof String) {\r
-                                       String strValue = (String) value;\r
-                                       if (strValue.trim().isEmpty() == true) {\r
-                                               value = null;\r
+        String markRtSbj = null;\r
+       RepositoryInstance repoSession = null;\r
+       RepositoryJavaClientImpl repoClient = null;\r
+       RepositoryJavaClientImpl nuxeoRepoClient = null;\r
+       boolean releaseRepoSession = false;\r
+       \r
+       AbstractServiceContextImpl sc = (AbstractServiceContextImpl)getServiceContext();\r
+       MultivaluedMap<String, String> queryParams = getServiceContext().getQueryParams();\r
+       markRtSbj = queryParams.getFirst(IQueryManager.MARK_RELATED_TO_CSID_AS_SUBJECT);\r
+       if(markRtSbj!=null && markRtSbj.isEmpty())\r
+               markRtSbj = null;\r
+  \r
+       try {\r
+            if(markRtSbj!=null) {\r
+                       repoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(sc);\r
+                   nuxeoRepoClient = (RepositoryJavaClientImpl) repoClient;\r
+                       repoSession = this.getRepositorySession();\r
+                       if (repoSession == null) {\r
+                               repoSession = repoClient.getRepositorySession(sc);\r
+                               releaseRepoSession = true;\r
+                       }\r
+            }\r
+            \r
+               String commonSchema = getServiceContext().getCommonPartLabel();\r
+               extractPagingInfo(commonList, wrapDoc);\r
+               List<ListResultField> resultsFields = getListItemsArray();\r
+               int nFields = resultsFields.size() + NUM_STANDARD_LIST_RESULT_FIELDS;\r
+               int baseFields = NUM_STANDARD_LIST_RESULT_FIELDS;\r
+               if(markRtSbj!=null) {\r
+                       nFields++;\r
+                       baseFields++;\r
+               }\r
+               String fields[] = new String[nFields];\r
+               fields[0] = STANDARD_LIST_CSID_FIELD;\r
+               fields[1] = STANDARD_LIST_URI_FIELD;\r
+               fields[2] = STANDARD_LIST_UPDATED_AT_FIELD;\r
+               fields[3] = STANDARD_LIST_WORKFLOW_FIELD;\r
+               if(markRtSbj!=null) {\r
+                   fields[4] = STANDARD_LIST_MARK_RT_FIELD;\r
+               }\r
+               for(int i = baseFields; i < nFields; i++) {\r
+                       ListResultField field = resultsFields.get(i - baseFields); \r
+                       fields[i] = field.getElement();\r
+               }\r
+                       commonList.setFieldsReturned(fields);\r
+                       Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();\r
+                       HashMap<String, Object> item = new HashMap<String, Object>();\r
+                       while (iter.hasNext()) {\r
+                               DocumentModel docModel = iter.next();\r
+                               String id = NuxeoUtils.getCsid(docModel);\r
+                               item.put(STANDARD_LIST_CSID_FIELD, id);\r
+                       if(markRtSbj!=null) {\r
+                           String relationClause = RelationsUtils.buildWhereClause(markRtSbj, null, null, id, null);\r
+                           QueryContext queryContext = new QueryContext(sc, relationClause);\r
+                           queryContext.setDocType(IRelationsManager.DOC_TYPE);\r
+                           String query = NuxeoUtils.buildNXQLQuery(sc, queryContext);\r
+                           // Search for 1 relation that matches. 1 is enough to fail the filter\r
+                           DocumentModelList docList = repoSession.query(query, null, 1, 0, false);\r
+                       item.put(STANDARD_LIST_MARK_RT_FIELD, docList.isEmpty()?"false":"true");\r
+                       }\r
+                               String uri = getUri(docModel);\r
+                               item.put(STANDARD_LIST_URI_FIELD, uri);\r
+                               item.put(STANDARD_LIST_UPDATED_AT_FIELD,\r
+                                               getUpdatedAtAsString(docModel));\r
+                               item.put(STANDARD_LIST_WORKFLOW_FIELD,\r
+                                               docModel.getCurrentLifeCycleState());\r
+       \r
+                               for (ListResultField field : resultsFields) {\r
+                                       String schema = field.getSchema();\r
+                                       if (schema == null || schema.trim().isEmpty()) {\r
+                                               schema = commonSchema;\r
+                                       }\r
+                                       Object value = getListResultValue(docModel, schema, field);\r
+                                       if (value != null && value instanceof String) {\r
+                                               String strValue = (String) value;\r
+                                               if (strValue.trim().isEmpty() == true) {\r
+                                                       value = null;\r
+                                               }\r
+                                       }\r
+                                       if (value != null) {\r
+                                               item.put(field.getElement(), value);\r
                                        }\r
                                }\r
-                               if (value != null) {\r
-                                       item.put(field.getElement(), value);\r
-                               }\r
+                               commonList.addItem(item);\r
+                               item.clear();\r
                        }\r
-                       commonList.addItem(item);\r
-                       item.clear();\r
-               }\r
+       } catch (Exception e) {\r
+               if (logger.isDebugEnabled()) {\r
+                       logger.debug("Caught exception ", e);\r
+               }\r
+               throw new DocumentException(e);\r
+               } finally {\r
+                       // If we got/aquired a new seesion then we're responsible for releasing it.\r
+                       if (releaseRepoSession && repoSession != null) {\r
+                               repoClient.releaseRepositorySession(sc, repoSession);\r
+                       }\r
+       }\r
 \r
-        return commonList;\r
+       return commonList;\r
     }\r
 \r
     // TODO - get rid of this if we can - appears to be unused.\r