]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5036: Added infrastructure for making CMIS queries and getting results.
authorRichard Millet <remillet@berkeley.edu>
Tue, 8 May 2012 16:52:24 +0000 (09:52 -0700)
committerRichard Millet <remillet@berkeley.edu>
Tue, 8 May 2012 16:52:24 +0000 (09:52 -0700)
services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java
services/common/src/main/java/org/collectionspace/services/common/AbstractMultiPartCollectionSpaceResourceImpl.java
services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java
services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java
services/common/src/main/java/org/collectionspace/services/common/storage/StorageClient.java
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java
services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java
services/movement/service/src/main/java/org/collectionspace/services/movement/MovementResource.java
services/movement/service/src/main/java/org/collectionspace/services/movement/nuxeo/MovementDocumentModelHandler.java

index ceac6ee94d6a0ed56fa877dac2089af549dc9719..a5a143f1295e21018492c53e577bfc194b62ccec 100644 (file)
@@ -42,6 +42,11 @@ public interface IQueryManager {
     final static String SEARCH_TYPE_INVOCATION = "inv";\r
        final static String SEARCH_QUALIFIER_AND = SEARCH_TERM_SEPARATOR + "AND" + SEARCH_TERM_SEPARATOR;\r
        final static String SEARCH_QUALIFIER_OR = SEARCH_TERM_SEPARATOR + "OR" + SEARCH_TERM_SEPARATOR;\r
+       //\r
+       // Query params for CMIS queries on the relationship (Relation) table.\r
+       //\r
+       final static String SEARCH_RELATED_TO_CSID_SUBJECT = "rt_sbj";\r
+       final static String SEARCH_RELATED_TO_CSID_OBJECT = "rd_obj";\r
 \r
        public void execQuery(String queryString);\r
        \r
index 3965252904057ff081425fb331a09baede6264e0..9334399d1822f987e0ffcc6072579a736de69e89 100644 (file)
@@ -71,9 +71,6 @@ public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends Abstr
         return MultipartServiceContextFactory.get();\r
     }\r
 \r
-\r
-\r
-\r
     @Override\r
     public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {\r
         return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass());\r
index f45e0e051f7553ea2dc4bd3844b9c40af1dad6fe..53e556bec58dc5b8686a1aff8375774882fcbdb0 100644 (file)
@@ -412,4 +412,15 @@ public abstract class AbstractDocumentHandlerImpl<T, TL, WT, WTL>
             handler.validate(action, serviceContext);
         }
     }
+    
+    /**
+     * Creates the CMIS query from the service context.  Each document handler is responsible for returning a valid CMIS query using the
+     * information in the current service context -which includes things like the query parameters, etc.
+     */
+    public String getCMISQuery() {
+       //
+       // By default, return nothing.  Child classes can override if they want.
+       //
+       return null;
+    }
 }
index fde044675a1de59820db65f468ef0023753de419..33ef154e1f59bc5f0c21f9b283be967e1758a42c 100644 (file)
@@ -328,4 +328,10 @@ public interface DocumentHandler<T, TL, WT, WTL> {
      * @return unqualified property
      */
     public String getUnQProperty(String qProp);
+    
+    /**
+     * Creates the CMIS query from the service context.  Each document handler is responsible for returning a valid CMIS query using the
+     * information in the current service context -which includes things like the query parameters, etc.
+     */
+    public String getCMISQuery();
 }
index 56108ce2884efc4e61662bcda728c01b525cda1c..8c72ed8fa279e518ad39ce32dbbe51400778d3ad 100644 (file)
@@ -114,7 +114,7 @@ public interface StorageClient {
      * @throws DocumentException
      */
     void getFiltered(ServiceContext ctx, DocumentHandler handler) throws DocumentNotFoundException, DocumentException;
-
+    
     /**
      * update given entity in the persistence store
      * @param ctx service context under which this method is invoked
index 9455097057784fe8342fdad730d28527cab668cf..aad37e2e44b42a6bcde713e096c81d5e72653ad9 100644 (file)
@@ -699,7 +699,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient<PoxPayloadIn,
     /*
      * See CSPACE-5036 - How to make CMISQL queries from Nuxeo
      */
-       private void makeCMISQLQuery(RepositoryInstance repoSession, String query) {
+       private IterableQueryResult makeCMISQLQuery(RepositoryInstance repoSession, String query) {
                // the NuxeoRepository should be constructed only once, then cached
                // (its construction is expensive)
                try {
@@ -756,42 +756,6 @@ public class RepositoryJavaClientImpl implements RepositoryClient<PoxPayloadIn,
         try {
             handler.prepare(Action.GET_ALL);
             repoSession = getRepositorySession();
-            //
-            // CSPACE-5036 - How to make CMISQL queries from Nuxeo
-            //
-            
-            /*
-             * Find all the Dimension records related to record X with csid = aa93c38d-43a1-42cc-ad2a where X is the subject of the relationship and
-             * the dimension records are the 'object' of the relationship
-             */            
-            String targetCsid = "0b5fd53a-41ff-478f-9ed8";
-            String query0 = "SELECT D.cmis:name, D.dc:title, R.dc:title, R.relations_common:subjectCsid FROM Dimension D JOIN Relation R ON R.relations_common:objectCsid = D.cmis:name WHERE R.relations_common:subjectCsid = '0b5fd53a-41ff-478f-9ed8'";
-            makeCMISQLQuery(repoSession, query0); //SHOULD return two dimension records
-            
-                       String query1 = "SELECT A.intakes_common:entryReason FROM Intake A"; // try eaee111c-a8d8-48c7-95cb
-            makeCMISQLQuery(repoSession, query1);
-
-                       String query2 = "SELECT * FROM Intake A where A.intakes_common:entryReason <> 'foo'"; // try eaee111c-a8d8-48c7-95cb
-            makeCMISQLQuery(repoSession, query2);
-
-            String query3 = "SELECT A.cmis:objectId, A.dc:title FROM Intake A where A.intakes_common:entryReason <> 'foo'"; // try eaee111c-a8d8-48c7-95cb
-            makeCMISQLQuery(repoSession, query3);
-
-            String query4 = "SELECT A.cmis:objectId, A.dc:title FROM Intake A where A.intakes_common:entryReason = '38ed4b32-e38f-4f82-adaa'";
-            makeCMISQLQuery(repoSession, query4);
-
-                       String query5 = "SELECT A.cmis:name, A.dc:title, B.intakes_common:entryReason FROM Dimension A JOIN Intake B ON A.cmis:name = B.intakes_common:entryReason"; // try eaee111c-a8d8-48c7-95cb
-            makeCMISQLQuery(repoSession, query5);
-            
-                       String query6 = "SELECT A.cmis:name, A.dc:title, B.intakes_common:entryReason FROM Dimension A JOIN Intake B ON A.dc:title = B.intakes_common:entryReason"; // try eaee111c-a8d8-48c7-95cb
-            makeCMISQLQuery(repoSession, query6);
-            
-            query6 = "SELECT A.cmis:name, A.dc:title, B.intakes_common:entryReason FROM Dimension A JOIN Intake B ON B.intakes_common:entryReason = A.dc:title"; // try eaee111c-a8d8-48c7-95cb
-            makeCMISQLQuery(repoSession, query6);
-            
-            String query7 = "SELECT B.intakes_common:entryReason FROM Intake B"; // try eaee111c-a8d8-48c7-95cb
-            makeCMISQLQuery(repoSession, query7);
-            
             
             DocumentModelList docList = null;
             String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext);
@@ -805,7 +769,9 @@ public class RepositoryJavaClientImpl implements RepositoryClient<PoxPayloadIn,
                Profiler profiler = new Profiler(this, 2);
                profiler.log("Executing NXQL query: " + query.toString());
                profiler.start();
-            if ((queryContext.getDocFilter().getOffset() > 0) || (queryContext.getDocFilter().getPageSize() > 0)) {
+               if (handler.getCMISQuery() != null) {
+                       docList = getFilteredCMIS(ctx, handler, queryContext);
+               } else if ((queryContext.getDocFilter().getOffset() > 0) || (queryContext.getDocFilter().getPageSize() > 0)) {
                 docList = repoSession.query(query, null,
                         queryContext.getDocFilter().getPageSize(), queryContext.getDocFilter().getOffset(), true);
             } else {
@@ -832,6 +798,53 @@ public class RepositoryJavaClientImpl implements RepositoryClient<PoxPayloadIn,
         }
     }
 
+    private DocumentModelList getFilteredCMIS(RepositoryInstance repoSession, ServiceContext ctx, DocumentHandler handler, QueryContext queryContext)
+            throws DocumentNotFoundException, DocumentException {
+
+       DocumentModelList result = new DocumentModelListImpl();
+        try {
+            String query = handler.getCMISQuery();
+
+            if (logger.isDebugEnabled()) {
+                logger.debug("Executing CMIS query: " + query.toString());
+            }
+
+            // If we have limit and/or offset, then pass true to get totalSize
+            // in returned DocumentModelList.
+               Profiler profiler = new Profiler(this, 2);
+               profiler.log("Executing CMIS query: " + query.toString());
+               profiler.start();
+               //
+               IterableQueryResult queryResult = makeCMISQLQuery(repoSession, query);
+                       for (Map<String, Serializable> row : queryResult) {
+                               logger.debug(
+                               // "dc:title is: " + (String)row.get("dc:title")
+                               "" + " Hierarchy Table ID is:" + row.get("cmis:objectId")
+                                               + " cmis:name is: " + row.get("cmis:name")
+                               // + " nuxeo:lifecycleState is: " +
+                               // row.get("nuxeo:lifecycleState")
+                               );
+                               String nuxeoId = (String) row.get("cmis:objectId");
+                               DocumentModel docModel = NuxeoUtils.getDocumentModel(repoSession, nuxeoId);
+                               result.add(docModel);
+                       }               
+               //
+            profiler.stop();
+
+        } catch (Exception e) {
+            if (logger.isDebugEnabled()) {
+                logger.debug("Caught exception ", e);
+            }
+            throw new DocumentException(e);
+        } finally {
+            if (repoSession != null) {
+                releaseRepositorySession(repoSession);
+            }
+        }
+        
+        return result;
+    }
+    
     private String logException(Exception e, String msg) {
        String result = null;
        
index 6daadf926c17f0a201d622dbf039927ed39bd508..3bdf85a34423f2ea92c52f9e83ed7a3fe93cc2e4 100644 (file)
@@ -372,6 +372,17 @@ public class NuxeoUtils {
         return query.toString();
     }
     
+    static public final String buildCMISQuery(ServiceContext ctx, QueryContext queryContext) throws Exception {
+        StringBuilder query = new StringBuilder("SELECT * FROM ");
+
+        /*
+         * This is a place holder for CMIS query creation -see buildNXQLQuery as a reference
+         */
+
+        return query.toString();
+    }
+    
+    
     /**
      * Builds an NXQL SELECT query across multiple document types.
      *
index 77faf3c3ce1eb0a0810e91eaab61991b51b5accd..b6c31705c2bb6f47ef44fcab69f7cbd3c290b93f 100644 (file)
  */
 package org.collectionspace.services.movement;
 
+import org.collectionspace.services.client.IQueryManager;
 import org.collectionspace.services.client.MovementClient;
+import org.collectionspace.services.client.PoxPayloadIn;
+import org.collectionspace.services.client.PoxPayloadOut;
 import org.collectionspace.services.common.ResourceBase;
+import org.collectionspace.services.common.ServiceMessages;
+import org.collectionspace.services.common.context.ServiceContext;
+import org.collectionspace.services.common.document.DocumentHandler;
+import org.collectionspace.services.jaxb.AbstractCommonList;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
+import javax.ws.rs.core.MultivaluedMap;
 
 /**
  * MovementResource.java
@@ -60,5 +68,40 @@ public class MovementResource extends ResourceBase {
     public Class<MovementsCommon> getCommonPartClass() {
         return MovementsCommon.class;
     }
+    
+    private boolean isSet(String key, MultivaluedMap<String, String> queryParams) {
+       boolean result = false;
+       
+       String value = queryParams.getFirst(key);
+       result = value != null && !value.isEmpty();
+       
+       return result;
+    }
+
+    @Override
+    protected AbstractCommonList getList(MultivaluedMap<String, String> queryParams) {
+        if (isSet(IQueryManager.SEARCH_RELATED_TO_CSID_SUBJECT, queryParams) == false) {
+               //
+               // It's not a "related to" query so we can use our normal call to getList and not our CMIS query
+               //
+               return super.getList(queryParams);
+        } else {
+               //
+               // We need to use CMIS query method since we'll be doing a join with the Relation table
+               //
+               try {
+                   ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(queryParams);
+                   DocumentHandler handler = createDocumentHandler(ctx);
+                       String relationToCsid = queryParams.getFirst(IQueryManager.SEARCH_RELATED_TO_CSID_SUBJECT);
+                                           
+                       getRepositoryClient(ctx).getFilteredCMIS(ctx, handler);
 
+                   AbstractCommonList list = (AbstractCommonList) handler.getCommonPartList();
+                   return list;
+               } catch (Exception e) {
+                   throw bigReThrow(e, ServiceMessages.LIST_FAILED);
+               }
+        }
+    }
+    
 }
index 989d15e97ed8468c5b9b1d0e6ec3e8f04e11ad5b..b210545116f861167d39376ccc7c3a93617d5f99 100644 (file)
  */
 package org.collectionspace.services.movement.nuxeo;
 
+import javax.ws.rs.core.MultivaluedMap;
+
+import org.collectionspace.services.client.IQueryManager;
+import org.collectionspace.services.movement.MovementResource;
 import org.collectionspace.services.movement.MovementsCommon;
 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * The Class MovementDocumentModelHandler.
@@ -35,5 +41,38 @@ import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
 public class MovementDocumentModelHandler
         extends DocHandlerBase<MovementsCommon> {
 
+    final Logger logger = LoggerFactory.getLogger(MovementDocumentModelHandler.class);
+       
+    /**
+     * Creates the CMIS query from the service context.  Each document handler is responsible for returning a valid CMIS query using the
+     * information in the current service context -which includes things like the query parameters, etc.
+     */
+    @Override
+    public String getCMISQuery() {
+       String result = null;
+       
+       String subjectCsid = (String)getServiceContext().getQueryParams().get(IQueryManager.SEARCH_RELATED_TO_CSID_SUBJECT);
+       
+       //
+       // For Debugging purposes only
+       //
+        String cmis_movement = System.getenv("CMIS_MOVEMENT");
+
+        if (cmis_movement != null && !cmis_movement.isEmpty()) {
+               result = cmis_movement;
+        } else {
+               result = "SELECT M.cmis:name, M.dc:title, R.dc:title, R.relations_common:subjectCsid "
+                               + "FROM Movement M JOIN Relation R ON R.relations_common:objectCsid = M.cmis:name "
+                               + "WHERE R.relations_common:subjectCsid = "
+                               + "'" + subjectCsid + "'";
+        }
+        
+        if (logger.isDebugEnabled() == true) {
+               logger.debug("The CMIS query for the Movement service is: " + result);
+        }
+        
+        return result;
+    }
+       
 }