]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-481 Fixed a bug in paginated query, and created a convenience factory method...
authorPatrick Schmitz <pschmitz@berkeley.edu>
Thu, 12 Nov 2009 21:19:09 +0000 (21:19 +0000)
committerPatrick Schmitz <pschmitz@berkeley.edu>
Thu, 12 Nov 2009 21:19:09 +0000 (21:19 +0000)
services/common/src/main/java/org/collectionspace/services/common/repository/DocumentFilter.java
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java
services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/VocabularyResource.java

index 4d65e5876f1facc3788d94d56c260f66cd5e609e..ce8fe70cfca44e12c9010d41e48db83c80f90166 100644 (file)
  */\r
 package org.collectionspace.services.common.repository;\r
 \r
+import java.util.List;\r
+\r
+import javax.ws.rs.core.MultivaluedMap;\r
+\r
 /**\r
  * DocumentFilter bundles simple query filtering parameters. \r
  * It is designed to be used with filtered get and search calls to RepositoryClient.\r
@@ -24,6 +28,8 @@ package org.collectionspace.services.common.repository;
  * fetched by a RepositoryClient when calling filtered get methods.\r
  */\r
 public class DocumentFilter {\r
+       public static final String PAGE_SIZE_PARAM = "pgSz";\r
+       public static final String START_PAGE_PARAM = "pgNum";\r
        public static final int DEFAULT_PAGE_SIZE_INIT = 40;\r
        public static int defaultPageSize = DEFAULT_PAGE_SIZE_INIT;\r
        protected String whereClause;   // Filtering clause. Omit the "WHERE".\r
@@ -40,6 +46,32 @@ public class DocumentFilter {
                this.pageSize = (pageSize>0)?pageSize:defaultPageSize;\r
        }\r
        \r
+       public static DocumentFilter CreatePaginatedDocumentFilter(\r
+                       MultivaluedMap<String,String> queryParams) {\r
+       String startPageStr = null;\r
+       String pageSizeStr = null;\r
+       List<String> list = queryParams.remove(PAGE_SIZE_PARAM);\r
+       if(list!=null)\r
+               pageSizeStr = list.get(0);\r
+       list = queryParams.remove(START_PAGE_PARAM);\r
+       if(list!=null)\r
+               startPageStr = list.get(0);\r
+       DocumentFilter newDocFilter = new DocumentFilter();\r
+       if(pageSizeStr!= null) {\r
+               try{ newDocFilter.pageSize = Integer.valueOf(pageSizeStr); } \r
+               catch(NumberFormatException e){\r
+                       throw new NumberFormatException("Bad value for: "+PAGE_SIZE_PARAM);\r
+               }\r
+       }\r
+       if(startPageStr!= null) {\r
+               try{ newDocFilter.startPage = Integer.valueOf(startPageStr); } \r
+               catch(NumberFormatException e){\r
+                       throw new NumberFormatException("Bad value for: "+START_PAGE_PARAM);\r
+               }\r
+       }\r
+       return newDocFilter;\r
+       }\r
+       \r
        /**\r
         * @return the current default page size for new DocumentFilter instances\r
         */\r
index c03eabaa9a7e37a6a3f4473a375ad3e7f7978ed6..a3b24dc86e25f425fb7f960033686abc6ddf8f82 100644 (file)
@@ -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(
index a210df869ff2d82b5f0623a960fbfdb765b885c3..9a2b00bfd327d8f414c00503bf8633fb873ed68e 100644 (file)
@@ -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<String,String> 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);