From 2711b854bbffcec264e805665c1d18a2ef200d34 Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Tue, 12 Jun 2012 16:07:56 -0700 Subject: [PATCH] CSPACE-5036: Added ORDER BY support to CMIS queries. --- .../services/client/IQueryManager.java | 1 + .../document/AbstractDocumentHandlerImpl.java | 5 +++- .../common/document/DocumentHandler.java | 3 ++- .../client/java/DocumentModelHandler.java | 23 ++++++++++++++----- .../client/java/RepositoryJavaClientImpl.java | 11 ++++----- .../services/nuxeo/util/NuxeoUtils.java | 15 +++++++++--- 6 files changed, 41 insertions(+), 17 deletions(-) diff --git a/services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java b/services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java index a4ff53a82..714559749 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java +++ b/services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java @@ -63,6 +63,7 @@ public interface IQueryManager { // CollectionSpace CMIS property mapping constants final static String CMIS_TARGET_PREFIX = "DOC"; + final static String CMIS_CORESCHEMA_PREFIX = "CORE"; // Relations CMIS property mapping constants final static String CMIS_RELATIONS_PREFIX = "REL"; diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java b/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java index 64b4a7fe9..0b9edbaef 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java @@ -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 * 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; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java index b7a6657bb..52e351eef 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java @@ -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 { * 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(); } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java index 533a4f726..c940797e3 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java @@ -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 * 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 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); } } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java index 0e70d4800..34ad39969 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java @@ -701,7 +701,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient row : queryResult) { logger.debug("" diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java index 6dce9d428..0ba58c3d8 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java @@ -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(); } -- 2.47.3