From: Aron Roberts Date: Mon, 19 Jul 2010 22:03:47 +0000 (+0000) Subject: CSPACE-2484: Added initial support to the Services document framework for returning... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=9431d09ea54bfa14b51ed7b0cea7602d1189ba1e;p=tmp%2Fjakarta-migration.git CSPACE-2484: Added initial support to the Services document framework for returning summary lists of records in sorted order. --- diff --git a/services/client/src/main/java/org/collectionspace/services/client/AbstractServiceClientImpl.java b/services/client/src/main/java/org/collectionspace/services/client/AbstractServiceClientImpl.java index c99c3cebe..3ea1e7e21 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/AbstractServiceClientImpl.java +++ b/services/client/src/main/java/org/collectionspace/services/client/AbstractServiceClientImpl.java @@ -312,6 +312,19 @@ public abstract class AbstractServiceClientImpl implements return getProxy().readList(pageSize, pageNumber); } + /* + * (non-Javadoc) + * + * @see + * org.collectionspace.services.client.CollectionSpaceClient#readList(java + * .lang.String, java.lang.String) + */ + @Override + public ClientResponse readList(String sortBy, String pageSize, + String pageNumber) { + return getProxy().readList(sortBy, pageSize, pageNumber); + } + /* (non-Javadoc) * @see org.collectionspace.services.client.CollectionSpaceClient#delete(java.lang.String) */ diff --git a/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java b/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java index 3932f5097..022380679 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java +++ b/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java @@ -148,7 +148,21 @@ public interface CollectionSpaceClient { * @param pageNumber the page number * @return the client response */ - public ClientResponse readList(String pageSize, + public ClientResponse readList( + String pageSize, + String pageNumber); + + /** + * Read list. + * + * @param sortBy the sort order + * @param pageSize the page size + * @param pageNumber the page number + * @return the client response + */ + public ClientResponse readList( + String sortBy, + String pageSize, String pageNumber); /** diff --git a/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceProxy.java b/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceProxy.java index 90807a705..f95894ab7 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceProxy.java +++ b/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceProxy.java @@ -38,6 +38,19 @@ import org.jboss.resteasy.client.ClientResponse; * FIXME: http://issues.collectionspace.org/browse/CSPACE-1684 */ public interface CollectionSpaceProxy { + + /** + * Read list. + * + * @param pageSize the page size + * @param pageNumber the page number + * @return the client response + */ + @GET + @Produces({"application/xml"}) + ClientResponse readList( + @QueryParam(IClientQueryParams.PAGE_SIZE_PARAM) String pageSize, + @QueryParam(IClientQueryParams.START_PAGE_PARAM) String pageNumber); /** * Read list. @@ -49,6 +62,7 @@ public interface CollectionSpaceProxy { @GET @Produces({"application/xml"}) ClientResponse readList( - @QueryParam(IClientQueryParams.PAGE_SIZE_PARAM) String pageSize, - @QueryParam(IClientQueryParams.START_PAGE_PARAM) String pageNumber); + @QueryParam(IClientQueryParams.SORT_BY_PARAM) String sortBy, + @QueryParam(IClientQueryParams.PAGE_SIZE_PARAM) String pageSize, + @QueryParam(IClientQueryParams.START_PAGE_PARAM) String pageNumber); } diff --git a/services/client/src/main/java/org/collectionspace/services/client/IClientQueryParams.java b/services/client/src/main/java/org/collectionspace/services/client/IClientQueryParams.java index 1aeec0a46..7d8d8007c 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/IClientQueryParams.java +++ b/services/client/src/main/java/org/collectionspace/services/client/IClientQueryParams.java @@ -3,5 +3,6 @@ package org.collectionspace.services.client; public interface IClientQueryParams { public static final String PAGE_SIZE_PARAM = "pgSz"; public static final String START_PAGE_PARAM = "pgNum"; - + public static final String SORT_BY_PARAM = "sortBy"; + } diff --git a/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java b/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java index 812120425..1ffe52542 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java +++ b/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java @@ -63,6 +63,7 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements /* Use this to keep track of resources to delete */ protected List allResourceIdsCreated = new ArrayList(); + private String EMPTY_SORT_BY_ORDER = ""; /** * Gets the logger. @@ -388,8 +389,26 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements private AbstractCommonList readList(String testName, CollectionSpaceClient client, long pageSize, long pageNumber) throws Exception { + + return readList(testName, client, EMPTY_SORT_BY_ORDER, pageSize, pageNumber); + + } + /** + * Read list. + * + * @param testName the test name + * @param client the client + * @param sortBy the sort order + * @param pageSize the page size + * @param pageNumber the page number + * @return the abstract common list + * @throws Exception the exception + */ + private AbstractCommonList readList(String testName, + CollectionSpaceClient client, String sortBy, + long pageSize, long pageNumber) throws Exception { ClientResponse response = - client.readList(Long.toString(pageSize), Long.toString(pageNumber)); + client.readList(sortBy, Long.toString(pageSize), Long.toString(pageNumber)); AbstractCommonList result = null; try { int statusCode = response.getStatus(); diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContextImpl.java b/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContextImpl.java index 54eeb9ece..658c14b2e 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContextImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContextImpl.java @@ -449,12 +449,19 @@ public abstract class AbstractServiceContextImpl + DocumentHandler.class.getCanonicalName()); } // - // create a default document filter with pagination if the context - // was created with query params + // Create a default document filter // docHandler.setServiceContext(this); DocumentFilter docFilter = docHandler.createDocumentFilter(); - docFilter.setPagination(this.getQueryParams()); + // + // If the context was created with query parameters, + // reflect the values of those parameters in the document filter + // to specify sort ordering, pagination, etc. + // + if (this.getQueryParams() != null) { + docFilter.setSortOrder(this.getQueryParams()); + docFilter.setPagination(this.getQueryParams()); + } docHandler.setDocumentFilter(docFilter); return docHandler; diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentFilter.java b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentFilter.java index 49cdd67c8..841204551 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentFilter.java +++ b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentFilter.java @@ -40,6 +40,9 @@ public class DocumentFilter { public static int defaultPageSize = DEFAULT_PAGE_SIZE_INIT; /** The where clause. */ protected String whereClause; // Filtering clause. Omit the "WHERE". + /** The order by clause. */ + protected String orderByClause; // Filtering clause. Omit the "ORDER BY". + public static final String EMPTY_ORDER_BY_CLAUSE = ""; /** The start page. */ protected int startPage; // Pagination offset for list results /** The page size. */ @@ -128,12 +131,25 @@ public class DocumentFilter { /** * Instantiates a new document filter. * - * @param theWhereClause the the where clause - * @param theStartPage the the start page - * @param thePageSize the the page size + * @param theWhereClause the where clause + * @param theStartPage the start page + * @param thePageSize the page size */ public DocumentFilter(String theWhereClause, int theStartPage, int thePageSize) { + this(theWhereClause, EMPTY_ORDER_BY_CLAUSE, theStartPage, thePageSize); + } + + /** + * Instantiates a new document filter. + * + * @param theWhereClause the where clause + * @param theOrderByClause the order by clause + * @param theStartPage the start page + * @param thePageSize the page size + */ + public DocumentFilter(String theWhereClause, String theOrderByClause, int theStartPage, int thePageSize) { this.whereClause = theWhereClause; + this.orderByClause = theOrderByClause; this.startPage = (theStartPage > 0) ? theStartPage : 0; this.pageSize = (thePageSize > 0) ? thePageSize : defaultPageSize; } @@ -209,11 +225,12 @@ public class DocumentFilter { /** * Append where clause. * - * @param theWhereClause the the where clause - * @param conjunction the conjunction + * @param theWhereClause the where clause + * @param conjunction the conjunction to insert between the current + * where clause, if any, and the additional where clause to be appended */ public void appendWhereClause(String theWhereClause, String conjunction) { - if (theWhereClause != null && theWhereClause.length() > 0) { + if (theWhereClause != null && ! theWhereClause.trim().isEmpty()) { String currentClause = getWhereClause(); if (currentClause != null) { String newClause = currentClause.concat(conjunction + theWhereClause); @@ -244,6 +261,47 @@ public class DocumentFilter { return new ArrayList(); } + /** + * Sets the sort ordering. + * + * @param theQueryParams the query params + */ + public void setSortOrder(MultivaluedMap theQueryParams) { + // Bail if there are no params + if (theQueryParams == null) { + return; + } + // Set the order by clause + String orderByStr = null; + List list = theQueryParams.get(IClientQueryParams.SORT_BY_PARAM); + if (list != null) { + orderByStr = list.get(0); + } + + // FIXME: Verify the format of the value(s) in the 'sort by' + // query param. + + setOrderByClause(orderByStr); + } + + /** + * Gets the order by clause. + * + * @return the order by clause + */ + public String getOrderByClause() { + return orderByClause; + } + + /** + * Sets the order by clause. + * + * @param theOrderByClause the new order by clause + */ + public void setOrderByClause(String theOrderByClause) { + this.orderByClause = theOrderByClause; + } + /** * Gets the start page. * 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 e51105cdf..1ff686e61 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 @@ -72,6 +72,8 @@ public class RepositoryJavaClientImpl implements RepositoryClient { DocumentFilter docFilter; /** The where clause. */ String whereClause; + /** The order by clause. */ + String orderByClause; /** The domain. */ String domain; /** The tenant id. */ @@ -106,7 +108,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient { * Instantiates a new query context. * * @param ctx the ctx - * @param theWhereClause the the where clause + * @param theWhereClause the where clause * @throws DocumentNotFoundException the document not found exception * @throws DocumentException the document exception */ @@ -137,6 +139,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient { "Document handler has no Filter specified."); } whereClause = docFilter.getWhereClause(); + orderByClause = docFilter.getOrderByClause(); } } /** The logger. */ @@ -621,6 +624,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient { @Override public void getFiltered(ServiceContext ctx, DocumentHandler handler) throws DocumentNotFoundException, DocumentException { + QueryContext queryContext = new QueryContext(ctx, handler); RepositoryInstance repoSession = null; @@ -899,11 +903,10 @@ public class RepositoryJavaClientImpl implements RepositoryClient { } /** - * Append nxql where. + * Append a WHERE clause to the NXQL query. * - * @param query the query - * @param where the where - * @param domain the domain + * @param query The NXQL query to which the WHERE clause will be appended. + * @param querycontext The query context, which provides the WHERE clause to append. */ private final void appendNXQLWhere(StringBuilder query, QueryContext queryContext) { // @@ -923,7 +926,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient { // Finally, append the incoming where clause // String whereClause = queryContext.whereClause; - if (whereClause != null && whereClause.length() > 0) { + if (whereClause != null && ! whereClause.trim().isEmpty()) { // Due to an apparent bug/issue in how Nuxeo translates the NXQL query string // into SQL, we need to parenthesize our 'where' clause query.append(IQueryManager.SEARCH_QUALIFIER_AND + "(" + whereClause + ")"); @@ -935,28 +938,50 @@ public class RepositoryJavaClientImpl implements RepositoryClient { } /** - * Builds the nxql query. + * Append an ORDER BY clause to the NXQL query. + * + * @param query The NXQL query to which the ORDER BY clause will be appended. + * @param querycontext The query context, which provides the ORDER BY clause to append. + */ + private final void appendNXQLOrderBy(StringBuilder query, QueryContext queryContext) { + // Append the incoming ORDER BY clause + String orderByClause = queryContext.orderByClause; + if (orderByClause != null && ! orderByClause.trim().isEmpty()) { + // FIXME Verify whether enclosing parentheses may be required, and add + // them if so, as is being done in appendNXQLWhere. + query.append(" ORDER BY "); + query.append(orderByClause); + } + + // FIXME Determine where and how to handle ASC[ending] and DESC[ending] qualifiers: + // + // Will these be included in the value of the relevant 'order by' query param? + // + // Will the format of the order by clause be verified, including placement of + // the 'order by' qualifiers? + } + + + /** + * Builds an NXQL SELECT query for a single document type. * - * @param docType the doc type - * @param where the where - * @param domain the domain - * @param tenantId the tenant id - * @return the string + * @param queryContext The query context + * @return an NXQL query */ private final String buildNXQLQuery(QueryContext queryContext) { StringBuilder query = new StringBuilder("SELECT * FROM "); query.append(queryContext.docType); appendNXQLWhere(query, queryContext); + appendNXQLOrderBy(query, queryContext); return query.toString(); } /** - * Builds the nxql query. + * Builds an NXQL SELECT query across multiple document types. * - * @param docTypes the doc types - * @param where the where - * @param domain the domain - * @return the string + * @param docTypes a list of document types to be queried + * @param queryContext the query context + * @return an NXQL query */ private final String buildNXQLQuery(List docTypes, QueryContext queryContext) { StringBuilder query = new StringBuilder("SELECT * FROM "); @@ -970,6 +995,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient { query.append(docType); } appendNXQLWhere(query, queryContext); + // FIXME add 'order by' clause here, if appropriate return query.toString(); }