From: Patrick Schmitz Date: Thu, 12 Nov 2009 21:19:09 +0000 (+0000) Subject: CSPACE-481 Fixed a bug in paginated query, and created a convenience factory method... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=76429d3b5c2c7cb0a99baf0c941ad59a33b134af;p=tmp%2Fjakarta-migration.git CSPACE-481 Fixed a bug in paginated query, and created a convenience factory method to create DocumentFilter instances with pagination parameters from the Map passed in with the URI context to REST handlers. --- diff --git a/services/common/src/main/java/org/collectionspace/services/common/repository/DocumentFilter.java b/services/common/src/main/java/org/collectionspace/services/common/repository/DocumentFilter.java index 4d65e5876..ce8fe70cf 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/repository/DocumentFilter.java +++ b/services/common/src/main/java/org/collectionspace/services/common/repository/DocumentFilter.java @@ -17,6 +17,10 @@ */ package org.collectionspace.services.common.repository; +import java.util.List; + +import javax.ws.rs.core.MultivaluedMap; + /** * DocumentFilter bundles simple query filtering parameters. * It is designed to be used with filtered get and search calls to RepositoryClient. @@ -24,6 +28,8 @@ package org.collectionspace.services.common.repository; * fetched by a RepositoryClient when calling filtered get methods. */ public class DocumentFilter { + public static final String PAGE_SIZE_PARAM = "pgSz"; + public static final String START_PAGE_PARAM = "pgNum"; public static final int DEFAULT_PAGE_SIZE_INIT = 40; public static int defaultPageSize = DEFAULT_PAGE_SIZE_INIT; protected String whereClause; // Filtering clause. Omit the "WHERE". @@ -40,6 +46,32 @@ public class DocumentFilter { this.pageSize = (pageSize>0)?pageSize:defaultPageSize; } + public static DocumentFilter CreatePaginatedDocumentFilter( + MultivaluedMap queryParams) { + String startPageStr = null; + String pageSizeStr = null; + List list = queryParams.remove(PAGE_SIZE_PARAM); + if(list!=null) + pageSizeStr = list.get(0); + list = queryParams.remove(START_PAGE_PARAM); + if(list!=null) + startPageStr = list.get(0); + DocumentFilter newDocFilter = new DocumentFilter(); + if(pageSizeStr!= null) { + try{ newDocFilter.pageSize = Integer.valueOf(pageSizeStr); } + catch(NumberFormatException e){ + throw new NumberFormatException("Bad value for: "+PAGE_SIZE_PARAM); + } + } + if(startPageStr!= null) { + try{ newDocFilter.startPage = Integer.valueOf(startPageStr); } + catch(NumberFormatException e){ + throw new NumberFormatException("Bad value for: "+START_PAGE_PARAM); + } + } + return newDocFilter; + } + /** * @return the current default page size for new DocumentFilter instances */ diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java index c03eabaa9..a3b24dc86 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java @@ -256,11 +256,13 @@ public class RepositoryJavaClient implements RepositoryClient { String where = docFilter.getWhereClause(); if((null!=where)&&(where.length()>0)) query.append(" WHERE "+where); - if(docFilter.getOffset()>0) - query.append(" OFFSET "+docFilter.getOffset()); - if(docFilter.getPageSize()>0) - query.append(" LIMIT "+docFilter.getPageSize()); - DocumentModelList docList = repoSession.query(query.toString()); + DocumentModelList docList = null; + if((docFilter.getOffset()>0)||(docFilter.getPageSize()>0)) { + docList = repoSession.query(query.toString(), null, + docFilter.getPageSize(), docFilter.getOffset(), false); + } else { + docList = repoSession.query(query.toString()); + } //set repoSession to handle the document ((DocumentModelHandler) handler).setRepositorySession(repoSession); DocumentModelListWrapper wrapDoc = new DocumentModelListWrapper( diff --git a/services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/VocabularyResource.java b/services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/VocabularyResource.java index a210df869..9a2b00bfd 100644 --- a/services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/VocabularyResource.java +++ b/services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/VocabularyResource.java @@ -106,7 +106,7 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { } return docHandler; } - + private DocumentHandler createItemDocumentHandler( RemoteServiceContext ctx, String inVocabulary) throws Exception { @@ -196,16 +196,15 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { VocabulariesCommonList vocabularyObjectList = new VocabulariesCommonList(); try{ RemoteServiceContext ctx = createServiceContext(null); - DocumentHandler handler = createDocumentHandler(ctx); MultivaluedMap queryParams = ui.getQueryParameters(); - if(queryParams.size()>0) { - String nameQ = queryParams.getFirst("name"); - if(nameQ!= null) { - DocumentFilter myFilter = new DocumentFilter( - "vocabularies_common:refName='"+nameQ+"'", 0, 0); - handler.setDocumentFilter(myFilter); - } - } + DocumentHandler handler = createDocumentHandler(ctx); + DocumentFilter myFilter = + DocumentFilter.CreatePaginatedDocumentFilter(queryParams); + String nameQ = queryParams.getFirst("name"); + if(nameQ!= null) { + myFilter.setWhereClause("vocabularies_common:refName='"+nameQ+"'"); + } + handler.setDocumentFilter(myFilter); getRepositoryClient(ctx).getFiltered(ctx, handler); vocabularyObjectList = (VocabulariesCommonList) handler.getCommonPartList(); }catch(Exception e){ @@ -384,24 +383,6 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { // Note that docType defaults to the ServiceName, so we're fine with that. RemoteServiceContext ctx = createServiceContext(null, getItemServiceName()); DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid); - /* - // Note that we replace the getAll() call with a basic search on the parent vocab - handler.prepare(Action.GET_ALL); - client = NuxeoConnector.getInstance().getClient(); - repoSession = client.openRepository(); - if (logger.isDebugEnabled()) { - logger.debug("getVocabularyItemList() repository root: " + repoSession.getRootDocument()); - } - DocumentModelList docList = - repoSession.query("SELECT * FROM Vocabularyitem WHERE vocabularyitems_common:inVocabulary='" - +parentcsid+"'"); - //set repoSession to handle the document - ((DocumentModelHandler) handler).setRepositorySession(repoSession); - DocumentModelListWrapper wrapDoc = new DocumentModelListWrapper( - docList); - handler.handle(Action.GET_ALL, wrapDoc); - handler.complete(Action.GET_ALL, wrapDoc); - */ DocumentFilter myFilter = new DocumentFilter( "vocabularyitems_common:inVocabulary='"+parentcsid+"'", 0, 0); handler.setDocumentFilter(myFilter);