]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a67fa584d7fcbd50021d8459efa7d1fcc0ad69ec
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.common.vocabulary;
2
3 import java.util.Iterator;
4 import java.util.List;
5 import java.util.Map;
6
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.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
16
17 import org.nuxeo.ecm.core.api.DocumentModel;
18 import org.nuxeo.ecm.core.api.DocumentModelList;
19 import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
20
21 import com.google.common.collect.AbstractIterator;
22
23 /**
24  * A DocumentModelList representing all of the documents that potentially reference an
25  * authority item, found via full text search. This list must be post-processed to
26  * eliminate false positives.
27  *
28  * Documents in this list are lazily fetched one page at a time, as they are accessed through
29  * the list's Iterator, retrieved with the iterator() method. List items may not be accessed
30  * through any other means, including the get() method, and the ListIterator retrieved
31  * with listIterator(). Attempts to do so will result in unspecified behavior.
32  * 
33  */
34 public class LazyAuthorityRefDocList extends DocumentModelListImpl {
35         private static final long serialVersionUID = 1L;
36         
37         private ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx;
38         private RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient;
39         private CoreSessionInterface repoSession;
40         private List<String> serviceTypes;
41         private String refName;
42         private String refPropName;
43         private Map<String, ServiceBindingType> queriedServiceBindings;
44         private Map<String, List<AuthRefConfigInfo>> authRefFieldsByService;
45         private String whereClauseAdditions;
46         private String orderByClause;
47         private int pageSize;
48         
49         private DocumentModelList firstPageDocList;
50         
51         /**
52          * Creates a LazyAuthorityRefDocList. The method signature is modeled after
53          * RefNameServiceUtils.findAuthorityRefDocs (the non-lazy way of doing this).
54          * 
55          * @param ctx
56          * @param repoClient
57          * @param repoSession
58          * @param serviceTypes
59          * @param refName
60          * @param refPropName
61          * @param queriedServiceBindings
62          * @param authRefFieldsByService
63          * @param whereClauseAdditions
64          * @param orderByClause
65          * @param pageSize                                              The number of documents to retrieve in each page
66          * @param computeTotal
67          * @throws DocumentException
68          * @throws DocumentNotFoundException
69          */
70         public LazyAuthorityRefDocList(
71                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
72                 RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient,
73                 CoreSessionInterface repoSession, List<String> serviceTypes,
74                 String refName,
75                 String refPropName,
76                 Map<String, ServiceBindingType> queriedServiceBindings,
77                 Map<String, List<AuthRefConfigInfo>> authRefFieldsByService,
78                 String whereClauseAdditions,
79                 String orderByClause,
80                 int pageSize,
81                 boolean useDefaultOrderByClause,
82                 boolean computeTotal) throws DocumentException, DocumentNotFoundException {
83
84                 this.ctx = ctx;
85                 this.repoClient = repoClient;
86                 this.repoSession = repoSession;
87                 this.serviceTypes = serviceTypes;
88                 this.refName = refName;
89                 this.refPropName = refPropName;
90                 this.queriedServiceBindings = queriedServiceBindings;
91                 this.authRefFieldsByService = authRefFieldsByService;
92                 this.whereClauseAdditions = whereClauseAdditions;
93                 this.orderByClause = orderByClause;
94                 this.pageSize = pageSize;
95
96                 // Fetch the first page immediately. This is necessary so that calls
97                 // to totalSize() will work immediately. The computeTotal flag is passed
98                 // into this initial page fetch. There's no need to compute totals
99                 // when fetching subsequent pages.
100                 
101                 firstPageDocList = fetchPage(0, computeTotal, useDefaultOrderByClause);
102         }
103
104         /**
105          * Retrieves a page of authority references.
106          * 
107          * @param pageNum               The page number
108          * @param computeTotal  
109          * @return
110          * @throws DocumentNotFoundException
111          * @throws DocumentException
112          */
113         private DocumentModelList fetchPage(int pageNum, boolean computeTotal, boolean useDefaultOrderByClause) throws DocumentNotFoundException, DocumentException {
114                 return RefNameServiceUtils.findAuthorityRefDocs(ctx, 
115                         repoClient, 
116                         repoSession,
117                         serviceTypes, 
118                         refName, 
119                         refPropName, 
120                         queriedServiceBindings, 
121                         authRefFieldsByService,
122                         whereClauseAdditions, 
123                         orderByClause, 
124                         pageNum, 
125                         pageSize,
126                         useDefaultOrderByClause,
127                         computeTotal);
128         }
129                 
130         @Override
131         public long totalSize() {
132                 // Return the totalSize from the first page of documents.
133                 return firstPageDocList.totalSize();
134         }
135
136         @Override
137         public Iterator<DocumentModel> iterator() {
138                 // Create a new iterator that starts with the first page of documents.
139                 return new Itr(0, firstPageDocList);
140         }
141         
142         /**
143          * An iterator over a LazyAuthorityRefDocList. The iterator keeps one
144          * page of documents in memory at a time, and traverses that page until
145          * no items remain. A new page is fetched only when the current page is
146          * exhausted.
147          *
148          */
149         private class Itr extends AbstractIterator<DocumentModel> {
150                 private int currentPageNum = 0;
151                 private DocumentModelList currentPageDocList;
152                 private Iterator<DocumentModel> currentPageIterator;
153                 
154                 /**
155                  * Creates a new iterator.
156                  * 
157                  * @param currentPageNum                The initial page number
158                  * @param currentPageDocList    The documents in the initial page
159                  */
160                 protected Itr(int pageNum, DocumentModelList pageDocList) {
161                         setCurrentPage(pageNum, pageDocList);
162                 }
163
164                 /**
165                  * Changes the current page.
166                  * 
167                  * @param pageNum               The new page number
168                  * @param pageDocList   The documents in the new page
169                  */
170                 private void setCurrentPage(int pageNum, DocumentModelList pageDocList) {
171                         this.currentPageNum = pageNum;
172                         this.currentPageDocList = pageDocList;
173                         this.currentPageIterator = pageDocList.iterator();
174                 }
175                 
176                 @Override
177                 protected DocumentModel computeNext() {
178                         // Find the next document to return, looking first in the current
179                         // page. If the current page is exhausted, fetch the next page.
180                         
181                         if (currentPageIterator.hasNext()) {
182                                 // There is still an element to return from the current page.
183                                 return currentPageIterator.next();
184                         }
185                         
186                         // The current page is exhausted.
187                         
188                         if (pageSize == 0 || (currentPageDocList.size() < pageSize)) { 
189                                 // There are no more pages.
190                                 return endOfData();
191                         }
192                         
193                         // There may be more pages. Try to fetch the next one.
194                         
195                         int nextPageNum = currentPageNum + 1;
196                         DocumentModelList nextPageDocList = null;
197                         
198                         try {
199                                 nextPageDocList = fetchPage(nextPageNum, false, true);
200                         }
201                         catch(DocumentException e) {}
202                         
203                         if (nextPageDocList == null || nextPageDocList.size() == 0) {
204                                 // There are no more pages.
205                                 return endOfData();
206                         }
207
208                         // There is another page. Make it the current page.
209                         
210                         setCurrentPage(nextPageNum, nextPageDocList);
211                         
212                         if (currentPageIterator.hasNext()) {
213                                 return currentPageIterator.next();
214                         }
215
216                         // Shouldn't get here.
217
218                         return endOfData();
219                 }
220         }
221 }