]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5036: Added ORDER BY support to CMIS queries.
authorRichard Millet <remillet@berkeley.edu>
Tue, 12 Jun 2012 23:07:56 +0000 (16:07 -0700)
committerRichard Millet <remillet@berkeley.edu>
Tue, 12 Jun 2012 23:07:56 +0000 (16:07 -0700)
services/client/src/main/java/org/collectionspace/services/client/IQueryManager.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/nuxeo/client/java/DocumentModelHandler.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

index a4ff53a829216f0e96c0ca43ee9d1cdebcbd53d2..7145597495b64519885c98ecc67a26c99c25a3ea 100644 (file)
@@ -63,6 +63,7 @@ public interface IQueryManager {
        \r
        // CollectionSpace CMIS property mapping constants\r
        final static String CMIS_TARGET_PREFIX = "DOC";\r
+       final static String CMIS_CORESCHEMA_PREFIX = "CORE";\r
        // Relations CMIS property mapping constants\r
        final static String CMIS_RELATIONS_PREFIX = "REL";\r
        \r
index 64b4a7fe9ae7a5d625009f3bbe4564da6b9e8cd0..0b9edbaef05e823f36a698329a4efe9488acfa78 100644 (file)
@@ -29,6 +29,7 @@ import java.util.Map;
 
 import java.util.StringTokenizer;
 import org.collectionspace.services.common.context.ServiceContext;
+import org.collectionspace.services.common.query.QueryContext;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -417,13 +418,15 @@ public abstract class AbstractDocumentHandlerImpl<T, TL, WT, WTL>
      * 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() {
+    @Override
+    public String getCMISQuery(QueryContext queryContext) {
        //
        // By default, return nothing.  Child classes can override if they want.
        //
        return null;
     }
     
+    @Override
     public boolean isCMISQuery() {
        return false;
     }
index b7a6657bb6751fdd24fb2067a16ea5c93e32b60e..52e351eef1a8136335e8c616288308a2ac6e3b4a 100644 (file)
@@ -19,6 +19,7 @@ package org.collectionspace.services.common.document;
 
 import java.util.Map;
 import org.collectionspace.services.common.context.ServiceContext;
+import org.collectionspace.services.common.query.QueryContext;
 import org.collectionspace.services.lifecycle.Lifecycle;
 import org.collectionspace.services.lifecycle.TransitionDef;
 import org.nuxeo.ecm.core.api.DocumentModel;
@@ -334,7 +335,7 @@ public interface DocumentHandler<T, TL, WT, WTL> {
      * 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();
+    public String getCMISQuery(QueryContext queryContext);
     
     public boolean isCMISQuery();
 }
index 533a4f7263ce50ce72b1cbe2344e8dac593a50a5..c940797e39f05112c7ebbf263007d73c3f9ba2a1 100644 (file)
@@ -41,6 +41,7 @@ import org.collectionspace.services.common.document.DocumentFilter;
 import org.collectionspace.services.common.document.DocumentWrapper;
 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
 import org.collectionspace.services.common.profile.Profiler;
+import org.collectionspace.services.common.query.QueryContext;
 import org.collectionspace.services.common.repository.RepositoryClient;
 import org.collectionspace.services.common.repository.RepositoryClientFactory;
 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthRefConfigInfo;
@@ -381,7 +382,7 @@ public abstract class DocumentModelHandler<T, TL>
         *              3. Document 'B' is either the object or the subject of the relationship
         */
     @Override
-    public String getCMISQuery() {
+    public String getCMISQuery(QueryContext queryContext) {
        String result = null;
        
        if (isCMISQuery() == true) {
@@ -429,20 +430,30 @@ public abstract class DocumentModelHandler<T, TL>
                        logger.error("Attempt to make CMIS query failed because the HTTP request was missing valid query parameters.");
                }
                
+               StringBuilder query = new StringBuilder();
                // assemble the query from the string arguments
-               result = "SELECT " + selectFields
-                               + " FROM "      + targetTable + " JOIN " + relTable
-                               + " ON " + theOnClause
-                               + " WHERE " + theWhereClause;
+               query.append("SELECT ");
+               query.append(selectFields);
+               query.append(" FROM " + targetTable + " JOIN " + relTable);
+               query.append(" ON " + theOnClause);
+               query.append(" WHERE " + theWhereClause);
+               
+               try {
+                               NuxeoUtils.appendCMISOrderBy(query, queryContext);
+                       } catch (Exception e) {
+                               logger.error("Could not append ORDER BY clause to CMIS query", e);
+                       }
                
                // An example:
                // 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 = '737527ec-a560-4776-99de'
+               // ORDER BY D.collectionspace_core:updatedAt DESC
                
+               result = query.toString();
                if (logger.isDebugEnabled() == true && result != null) {
-                       logger.debug("The CMIS query for the Movement service is: " + result);
+                       logger.debug("The CMIS query is: " + result);
                }
        }
         
index 0e70d480045e022304eadd0f6a0018b07a904c31..34ad399693274e735d6073d3a7474ef588591f7e 100644 (file)
@@ -701,7 +701,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient<PoxPayloadIn,
     /*
      * See CSPACE-5036 - How to make CMISQL queries from Nuxeo
      */
-       private IterableQueryResult makeCMISQLQuery(RepositoryInstance repoSession, String query) {
+       private IterableQueryResult makeCMISQLQuery(RepositoryInstance repoSession, String query, QueryContext queryContext) {
                IterableQueryResult result = null;
                
                // the NuxeoRepository should be constructed only once, then cached
@@ -720,8 +720,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient<PoxPayloadIn,
                        NuxeoCmisService cmisService = new NuxeoCmisService(repo,
                                        callContext, repoSession);
 
-                       result = repoSession.queryAndFetch(query,
-                                       "CMISQL", cmisService);
+                       result = repoSession.queryAndFetch(query, "CMISQL", cmisService);
                } catch (ClientException e) {
                        // TODO Auto-generated catch block
                        logger.error("Encounter trouble making the following CMIS query: " + query, e);
@@ -745,7 +744,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient<PoxPayloadIn,
         DocumentFilter filter = handler.getDocumentFilter();
         String oldOrderBy = filter.getOrderByClause();
         if (isClauseEmpty(oldOrderBy) == true){
-            filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED);  //per http://issues.collectionspace.org/browse/CSPACE-705 (Doesn't this conflict with what happens with the QueryContext instance that we create below?)
+            filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED);
         }
         QueryContext queryContext = new QueryContext(ctx, handler);
 
@@ -800,7 +799,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient<PoxPayloadIn,
 
        DocumentModelList result = new DocumentModelListImpl();
         try {
-            String query = handler.getCMISQuery();
+            String query = handler.getCMISQuery(queryContext);
 
             if (logger.isDebugEnabled()) {
                 logger.debug("Executing CMIS query: " + query.toString());
@@ -812,7 +811,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient<PoxPayloadIn,
                profiler.log("Executing CMIS query: " + query.toString());
                profiler.start();
                //
-               IterableQueryResult queryResult = makeCMISQLQuery(repoSession, query);
+               IterableQueryResult queryResult = makeCMISQLQuery(repoSession, query, queryContext);
                try {
                                for (Map<String, Serializable> row : queryResult) {
                                        logger.debug(""
index 6dce9d4288072d73fc023263ec3a720a95a3f84f..0ba58c3d8031c0137a9ac99aa96ca0b9de580c27 100644 (file)
@@ -310,11 +310,14 @@ public class NuxeoUtils {
      * @throws DocumentException  if the supplied value of the orderBy clause is not valid.
      *
      */
-    static private final void appendNXQLOrderBy(StringBuilder query, String orderByClause)
+    static private final void appendNXQLOrderBy(StringBuilder query, String orderByClause, String orderByPrefix)
             throws Exception {
         if (orderByClause != null && ! orderByClause.trim().isEmpty()) {
             if (isValidOrderByClause(orderByClause)) {
                 query.append(" ORDER BY ");
+                if (orderByPrefix != null) {
+                       query.append(orderByPrefix);
+                }
                 query.append(orderByClause);
             } else {
                 throw new DocumentException("Invalid format in sort request '" + orderByClause
@@ -335,7 +338,13 @@ public class NuxeoUtils {
     static private final void appendNXQLOrderBy(StringBuilder query, QueryContext queryContext)
             throws Exception {
         String orderByClause = queryContext.getOrderByClause();
-        appendNXQLOrderBy(query, orderByClause);
+        appendNXQLOrderBy(query, orderByClause, null);
+    }
+        
+    static public final void appendCMISOrderBy(StringBuilder query, QueryContext queryContext)
+            throws Exception {
+        String orderByClause = queryContext.getOrderByClause();
+        appendNXQLOrderBy(query, orderByClause, IQueryManager.CMIS_TARGET_PREFIX + ".");
     }
 
     /**
@@ -416,7 +425,7 @@ public class NuxeoUtils {
         }
         appendNXQLWhere(query, queryContext);
         // For a set of DocTypes, there is no sensible ordering other than by updatedAt
-        appendNXQLOrderBy(query, DocumentFilter.ORDER_BY_LAST_UPDATED);
+        appendNXQLOrderBy(query, DocumentFilter.ORDER_BY_LAST_UPDATED, null);
         // FIXME add 'order by' clause here, if appropriate
         return query.toString();
     }