1 package org.collectionspace.services.common.vocabulary;
3 import java.util.Iterator;
7 import org.collectionspace.services.client.PoxPayloadIn;
8 import org.collectionspace.services.client.PoxPayloadOut;
9 import org.collectionspace.services.common.context.ServiceContext;
10 import org.collectionspace.services.common.document.DocumentException;
11 import org.collectionspace.services.common.document.DocumentNotFoundException;
12 import org.collectionspace.services.common.repository.RepositoryClient;
13 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthRefConfigInfo;
14 import org.collectionspace.services.config.service.ServiceBindingType;
15 import org.nuxeo.ecm.core.api.DocumentModel;
16 import org.nuxeo.ecm.core.api.DocumentModelList;
17 import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
18 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
20 import com.google.common.collect.AbstractIterator;
23 * A DocumentModelList representing all of the documents that potentially reference an
24 * authority item, found via full text search. This list must be post-processed to
25 * eliminate false positives.
27 * Documents in this list are lazily fetched one page at a time, as they are accessed through
28 * the list's Iterator, retrieved with the iterator() method. List items may not be accessed
29 * through any other means, including the get() method, and the ListIterator retrieved
30 * with listIterator(). Attempts to do so will result in unspecified behavior.
33 public class LazyAuthorityRefDocList extends DocumentModelListImpl {
34 private static final long serialVersionUID = 1L;
36 private ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx;
37 private RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient;
38 private RepositoryInstance repoSession;
39 private List<String> serviceTypes;
40 private String refName;
41 private String refPropName;
42 private Map<String, ServiceBindingType> queriedServiceBindings;
43 private Map<String, List<AuthRefConfigInfo>> authRefFieldsByService;
44 private String whereClauseAdditions;
45 private String orderByClause;
48 private DocumentModelList firstPageDocList;
51 * Creates a LazyAuthorityRefDocList. The method signature is modeled after
52 * RefNameServiceUtils.findAuthorityRefDocs (the non-lazy way of doing this).
60 * @param queriedServiceBindings
61 * @param authRefFieldsByService
62 * @param whereClauseAdditions
63 * @param orderByClause
64 * @param pageSize The number of documents to retrieve in each page
66 * @throws DocumentException
67 * @throws DocumentNotFoundException
69 public LazyAuthorityRefDocList(
70 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
71 RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient,
72 RepositoryInstance repoSession, List<String> serviceTypes,
75 Map<String, ServiceBindingType> queriedServiceBindings,
76 Map<String, List<AuthRefConfigInfo>> authRefFieldsByService,
77 String whereClauseAdditions,
80 boolean computeTotal) throws DocumentException, DocumentNotFoundException {
83 this.repoClient = repoClient;
84 this.repoSession = repoSession;
85 this.serviceTypes = serviceTypes;
86 this.refName = refName;
87 this.refPropName = refPropName;
88 this.queriedServiceBindings = queriedServiceBindings;
89 this.authRefFieldsByService = authRefFieldsByService;
90 this.whereClauseAdditions = whereClauseAdditions;
91 this.orderByClause = orderByClause;
92 this.pageSize = pageSize;
94 // Fetch the first page immediately. This is necessary so that calls
95 // to totalSize() will work immediately. The computeTotal flag is passed
96 // into this initial page fetch. There's no need to compute totals
97 // when fetching subsequent pages.
99 firstPageDocList = fetchPage(0, computeTotal);
103 * Retrieves a page of authority references.
105 * @param pageNum The page number
106 * @param computeTotal
108 * @throws DocumentNotFoundException
109 * @throws DocumentException
111 private DocumentModelList fetchPage(int pageNum, boolean computeTotal) throws DocumentNotFoundException, DocumentException {
112 return RefNameServiceUtils.findAuthorityRefDocs(ctx, repoClient, repoSession,
113 serviceTypes, refName, refPropName, queriedServiceBindings, authRefFieldsByService,
114 whereClauseAdditions, orderByClause, pageSize, pageNum, computeTotal);
118 public long totalSize() {
119 // Return the totalSize from the first page of documents.
120 return firstPageDocList.totalSize();
124 public Iterator<DocumentModel> iterator() {
125 // Create a new iterator that starts with the first page of documents.
126 return new Itr(0, firstPageDocList);
130 * An iterator over a LazyAuthorityRefDocList. The iterator keeps one
131 * page of documents in memory at a time, and traverses that page until
132 * no items remain. A new page is fetched only when the current page is
136 private class Itr extends AbstractIterator<DocumentModel> {
137 private int currentPageNum = 0;
138 private DocumentModelList currentPageDocList;
139 private Iterator<DocumentModel> currentPageIterator;
142 * Creates a new iterator.
144 * @param currentPageNum The initial page number
145 * @param currentPageDocList The documents in the initial page
147 protected Itr(int pageNum, DocumentModelList pageDocList) {
148 setCurrentPage(pageNum, pageDocList);
152 * Changes the current page.
154 * @param pageNum The new page number
155 * @param pageDocList The documents in the new page
157 private void setCurrentPage(int pageNum, DocumentModelList pageDocList) {
158 this.currentPageNum = pageNum;
159 this.currentPageDocList = pageDocList;
160 this.currentPageIterator = pageDocList.iterator();
164 protected DocumentModel computeNext() {
165 // Find the next document to return, looking first in the current
166 // page. If the current page is exhausted, fetch the next page.
168 if (currentPageIterator.hasNext()) {
169 // There is still an element to return from the current page.
170 return currentPageIterator.next();
173 // The current page is exhausted.
175 if (currentPageDocList.size() < pageSize) {
176 // There are no more pages.
180 // There may be more pages. Try to fetch the next one.
182 int nextPageNum = currentPageNum + 1;
183 DocumentModelList nextPageDocList = null;
186 nextPageDocList = fetchPage(nextPageNum, false);
188 catch(DocumentException e) {}
190 if (nextPageDocList == null || nextPageDocList.size() == 0) {
191 // There are no more pages.
195 // There is another page. Make it the current page.
197 setCurrentPage(nextPageNum, nextPageDocList);
199 if (currentPageIterator.hasNext()) {
200 return currentPageIterator.next();
203 // Shouldn't get here.