]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5359: Fix pagination of refObjs in the v2.5 branch.
authorAron Roberts <aron@socrates.berkeley.edu>
Fri, 13 Jul 2012 18:20:58 +0000 (11:20 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Fri, 13 Jul 2012 18:20:58 +0000 (11:20 -0700)
services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java

index 1c86715beb33cd8bed41d710abed5922973c3c3e..ac72506ee8ce99f464bbf270239713937ccff6a6 100644 (file)
@@ -231,8 +231,7 @@ public class RefNameServiceUtils {
         AbstractCommonList commonList = (AbstractCommonList) wrapperList;\r
         int pageNum = filter.getStartPage();\r
         int pageSize = filter.getPageSize();\r
-        commonList.setPageNum(pageNum);\r
-        commonList.setPageSize(pageSize);\r
+        \r
         List<AuthorityRefDocList.AuthorityRefDocItem> list =\r
                 wrapperList.getAuthorityRefDocItem();\r
 \r
@@ -241,17 +240,18 @@ public class RefNameServiceUtils {
 \r
         RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl) repoClient;\r
         try {\r
+            // Ignore any provided page size and number query parameters in\r
+            // the following call, as they pertain to the list of authority\r
+            // references to be returned, not to the list of documents to be\r
+            // scanned for those references.\r
             DocumentModelList docList = findAuthorityRefDocs(ctx, repoClient, repoSession,\r
                     serviceTypes, refName, refPropName, queriedServiceBindings, authRefFieldsByService,\r
-                    filter.getWhereClause(), null, pageSize, pageNum, computeTotal);\r
+                    filter.getWhereClause(), null, 0 /* pageSize */, 0 /* pageNum */, computeTotal);\r
 \r
             if (docList == null) { // found no authRef fields - nothing to process\r
                 return wrapperList;\r
             }\r
-            // Set num of items in list. this is useful to our testing framework.\r
-            commonList.setItemsInPage(docList.size());\r
-            // set the total result size\r
-            commonList.setTotalItems(docList.totalSize());\r
+\r
             // set the fieldsReturned list. Even though this is a fixed schema, app layer treats\r
             // this like other abstract common lists\r
             /*\r
@@ -268,13 +268,76 @@ public class RefNameServiceUtils {
             String fieldList = "docType|docId|docNumber|docName|sourceField|uri|updatedAt|workflowState";\r
             commonList.setFieldsReturned(fieldList);\r
 \r
+            // As a side-effect, the method called below modifies the value of\r
+            // the 'list' variable, which holds the list of references to\r
+            // an authority item.\r
+            //\r
+            // There can be more than one reference to a particular authority\r
+            // item within any individual document scanned, so the number of\r
+            // authority references may potentially exceed the total number\r
+            // of documents scanned.\r
             int nRefsFound = processRefObjsDocList(docList, refName, queriedServiceBindings, authRefFieldsByService, // the actual list size needs to be updated to the size of "list"\r
                     list, null);\r
+\r
+            commonList.setPageSize(pageSize);\r
+            \r
+            // Values returned in the pagination block above the list items\r
+            // need to reflect the number of references to authority items\r
+            // returned, rather than the number of documents originally scanned\r
+            // to find such references.\r
+            commonList.setPageNum(pageNum);\r
+            commonList.setTotalItems(list.size());\r
+\r
+            // Slice the list to return only the specified page of items\r
+            // in the list results.\r
+            //\r
+            // FIXME: There may well be a pattern-based way to do this\r
+            // in our framework, and if we can eliminate much of the\r
+            // non-DRY code below, that would be desirable.\r
+            \r
+            int startIndex = 0;\r
+            int endIndex = 0;\r
+            \r
+            // Return all results if pageSize is 0.\r
+            if (pageSize == 0) {\r
+                startIndex = 0;\r
+                endIndex = list.size();\r
+            } else {\r
+               startIndex = pageNum * pageSize;\r
+            }\r
+            \r
+            // Return an empty list when the start of the requested page is\r
+            // beyond the last item in the list.\r
+            if (startIndex > list.size()) {\r
+                wrapperList.getAuthorityRefDocItem().clear();\r
+                commonList.setItemsInPage(wrapperList.getAuthorityRefDocItem().size());\r
+                return wrapperList;\r
+            }\r
+\r
+            // Otherwise, return a list of items from the start of the specified\r
+            // page through the last item on that page, or otherwise through the\r
+            // last item in the entire list, if that occurs earlier than the end\r
+            // of the specified page.\r
+            if (endIndex == 0) {\r
+                int pageEndIndex = ((startIndex + pageSize));\r
+                endIndex = (pageEndIndex > list.size()) ? list.size() : pageEndIndex;\r
+            }\r
+            \r
+            // Slice the list to return only the specified page of results.\r
+            // Note: the second argument to List.subList(), endIndex, is\r
+            // exclusive of the item at its index position, reflecting the\r
+            // zero-index nature of the list.\r
+            List<AuthorityRefDocList.AuthorityRefDocItem> currentPageList =\r
+                    new ArrayList<AuthorityRefDocList.AuthorityRefDocItem>(list.subList(startIndex, endIndex));\r
+            wrapperList.getAuthorityRefDocItem().clear();\r
+            wrapperList.getAuthorityRefDocItem().addAll(currentPageList);\r
+            commonList.setItemsInPage(currentPageList.size());\r
+            \r
             if (logger.isDebugEnabled() && (nRefsFound < docList.size())) {\r
                 logger.debug("Internal curiosity: got fewer matches of refs than # docs matched..."); // We found a ref to ourself and have excluded it.\r
             }\r
         } catch (Exception e) {\r
-            logger.error("Could not retrieve the Nuxeo repository", e);\r
+            logger.error("Could not retrieve a list of documents referring to the specified authority item", e);\r
             wrapperList = null;\r
         }\r
 \r