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