]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5397: Initial take on returning just the appropriate set of list results on...
authorAron Roberts <aron@socrates.berkeley.edu>
Fri, 13 Jul 2012 00:00:41 +0000 (17:00 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Fri, 13 Jul 2012 00:00:41 +0000 (17:00 -0700)
services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java

index 1c86715beb33cd8bed41d710abed5922973c3c3e..48490fa2a5b1335d1b7d4b081ea26fb8898fcc11 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
@@ -248,10 +247,7 @@ public class RefNameServiceUtils {
             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,8 +264,48 @@ 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 in our framework.\r
+            int startIndex = 0;\r
+            int endIndex = 0;\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
+               int pageEndIndex = ((startIndex + pageSize) - 1);\r
+               endIndex = (pageEndIndex > list.size()) ? list.size() : pageEndIndex;\r
+            }\r
+            // Adjust for the second argument to List.subList() being exclusive\r
+            // of the last item in the slice\r
+            endIndex++;\r
+            list = new ArrayList<AuthorityRefDocList.AuthorityRefDocItem>\r
+                    (list.subList(startIndex, endIndex));\r
+            commonList.setItemsInPage(list.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
@@ -319,11 +355,11 @@ public class RefNameServiceUtils {
             DocumentModelList docList;\r
             boolean morePages = true;\r
             while (morePages) {\r
-                \r
+\r
                 docList = findAuthorityRefDocs(ctx, repoClient, repoSession,\r
                         getRefNameServiceTypes(), oldRefName, refPropName,\r
                         queriedServiceBindings, authRefFieldsByService, WHERE_CLAUSE_ADDITIONS_VALUE, ORDER_BY_VALUE, pageSize, currentPage, false);\r
-                \r
+\r
                 if (docList == null) {\r
                     logger.debug("updateAuthorityRefDocs: no documents could be found that referenced the old refName");\r
                     break;\r
@@ -345,7 +381,7 @@ public class RefNameServiceUtils {
                     ((RepositoryJavaClientImpl) repoClient).saveDocListWithoutHandlerProcessing(ctx, repoSession, docList, true);\r
                     nRefsFound += nRefsFoundThisPage;\r
                 }\r
-                \r
+\r
                 // FIXME: Per REM, set a limit of num objects - something like\r
                 // 1000K objects - and also add a log Warning after some threshold\r
                 docsScanned += docsInCurrentPage;\r
@@ -399,7 +435,7 @@ public class RefNameServiceUtils {
         // Additional qualifications, like workflow state\r
         if (Tools.notBlank(whereClauseAdditions)) {\r
             query += " AND " + whereClauseAdditions;\r
-        }        \r
+        }\r
         // Now we have to issue the search\r
         RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl) repoClient;\r
         DocumentWrapper<DocumentModelList> docListWrapper = nuxeoRepoClient.findDocs(ctx, repoSession,\r