-
Notifications
You must be signed in to change notification settings - Fork 377
/
CoreQueryDocumentPageProvider.java
412 lines (357 loc) · 15.8 KB
/
CoreQueryDocumentPageProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* (C) Copyright 2010-2018 Nuxeo (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Anahide Tchertchian
* Benoit Delbosc
*/
package org.nuxeo.ecm.platform.query.nxql;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.nuxeo.ecm.core.api.Filter;
import org.nuxeo.ecm.core.api.NuxeoException;
import org.nuxeo.ecm.core.api.SortInfo;
import org.nuxeo.ecm.platform.query.api.AbstractPageProvider;
import org.nuxeo.ecm.platform.query.api.PageProviderDefinition;
import org.nuxeo.ecm.platform.query.api.PageSelections;
import org.nuxeo.ecm.platform.query.api.QuickFilter;
import org.nuxeo.ecm.platform.query.api.WhereClauseDefinition;
import org.nuxeo.runtime.api.Framework;
import org.nuxeo.runtime.services.config.ConfigurationService;
/**
* Page provider performing a query on a core session.
* <p>
* It builds the query at each call so that it can refresh itself when the query changes.
* <p>
* The page provider property named {@link #CORE_SESSION_PROPERTY} is used to pass the {@link CoreSession} instance that
* will perform the query. The optional property {@link #CHECK_QUERY_CACHE_PROPERTY} can be set to "true" to avoid
* performing the query again if it did not change.
* <p>
* Since 6.0, the page provider property named {@link #USE_UNRESTRICTED_SESSION_PROPERTY} allows specifying whether the
* query should be run as unrestricted. When such a property is set to "true", the additional property
* {@link #DETACH_DOCUMENTS_PROPERTY} is used to detach documents (defaults to true when session is unrestricted).
*
* @author Anahide Tchertchian
* @since 5.4
*/
public class CoreQueryDocumentPageProvider extends AbstractPageProvider<DocumentModel> {
public static final String CORE_SESSION_PROPERTY = "coreSession";
public static final String MAX_RESULTS_PROPERTY = "maxResults";
// Special maxResults value used for navigation, can be tuned
public static final String DEFAULT_NAVIGATION_RESULTS_KEY = "DEFAULT_NAVIGATION_RESULTS";
// Special maxResults value that means same as the page size
public static final String PAGE_SIZE_RESULTS_KEY = "PAGE_SIZE";
public static final String DEFAULT_NAVIGATION_RESULTS_PROPERTY = "org.nuxeo.ecm.platform.query.nxql.defaultNavigationResults";
public static final String DEFAULT_NAVIGATION_RESULTS_VALUE = "200";
public static final String CHECK_QUERY_CACHE_PROPERTY = "checkQueryCache";
/**
* Boolean property stating that query should be unrestricted.
*
* @since 6.0
*/
public static final String USE_UNRESTRICTED_SESSION_PROPERTY = "useUnrestrictedSession";
/**
* Boolean property stating that documents should be detached, only useful when property
* {@link #USE_UNRESTRICTED_SESSION_PROPERTY} is set to true.
* <p>
* When an unrestricted session is used, this property defaults to true.
*
* @since 6.0
*/
public static final String DETACH_DOCUMENTS_PROPERTY = "detachDocuments";
private static final Log log = LogFactory.getLog(CoreQueryDocumentPageProvider.class);
private static final long serialVersionUID = 1L;
protected String query;
protected List<DocumentModel> currentPageDocuments;
protected Long maxResults;
@Override
public List<DocumentModel> getCurrentPage() {
long t0 = System.currentTimeMillis();
checkQueryCache();
if (currentPageDocuments == null) {
error = null;
errorMessage = null;
CoreSession coreSession = getCoreSession();
if (query == null) {
buildQuery(coreSession);
}
if (query == null) {
throw new NuxeoException(String.format("Cannot perform null query: check provider '%s'", getName()));
}
currentPageDocuments = new ArrayList<>();
try {
final long minMaxPageSize = getMinMaxPageSize();
final long offset = getCurrentPageOffset();
if (log.isDebugEnabled()) {
log.debug(String.format("Perform query for provider '%s': '%s' with pageSize=%s, offset=%s",
getName(), query, Long.valueOf(minMaxPageSize), Long.valueOf(offset)));
}
final DocumentModelList docs;
final long maxResults = getMaxResults();
final Filter filter = getFilter();
final boolean useUnrestricted = useUnrestrictedSession();
final boolean detachDocs = detachDocuments();
if (maxResults > 0) {
if (useUnrestricted) {
CoreQueryUnrestrictedSessionRunner r = new CoreQueryUnrestrictedSessionRunner(coreSession,
query, filter, minMaxPageSize, offset, false, maxResults, detachDocs);
r.runUnrestricted();
docs = r.getDocs();
} else {
docs = coreSession.query(query, getFilter(), minMaxPageSize, offset, maxResults);
}
} else {
// use a totalCount=true instead of countUpTo=-1 to
// enable global limitation described in NXP-9381
if (useUnrestricted) {
CoreQueryUnrestrictedSessionRunner r = new CoreQueryUnrestrictedSessionRunner(coreSession,
query, filter, minMaxPageSize, offset, true, maxResults, detachDocs);
r.runUnrestricted();
docs = r.getDocs();
} else {
docs = coreSession.query(query, getFilter(), minMaxPageSize, offset, true);
}
}
long resultsCount = docs.totalSize();
if (resultsCount < 0) {
// results count is truncated
setResultsCount(UNKNOWN_SIZE_AFTER_QUERY);
} else {
setResultsCount(resultsCount);
}
currentPageDocuments = docs;
if (log.isDebugEnabled()) {
log.debug(String.format("Performed query for provider '%s': got %s hits (limit %s)", getName(),
Long.valueOf(resultsCount), Long.valueOf(getMaxResults())));
}
if (getResultsCount() < 0) {
// additional info to handle next page when results count
// is unknown
if (currentPageDocuments != null && currentPageDocuments.size() > 0) {
int higherNonEmptyPage = getCurrentHigherNonEmptyPageIndex();
int currentFilledPage = (int) getCurrentPageIndex();
if ((docs.size() >= getPageSize()) && (currentFilledPage > higherNonEmptyPage)) {
setCurrentHigherNonEmptyPageIndex(currentFilledPage);
}
}
}
} catch (NuxeoException e) {
error = e;
errorMessage = e.getMessage();
log.warn(e.getMessage(), e);
}
}
// send event for statistics !
fireSearchEvent(getCoreSession().getPrincipal(), query, currentPageDocuments, System.currentTimeMillis() - t0);
return currentPageDocuments;
}
protected void buildQuery(CoreSession coreSession) {
List<SortInfo> sort = null;
List<QuickFilter> quickFilters = getQuickFilters();
String quickFiltersClause = "";
if (quickFilters != null && !quickFilters.isEmpty()) {
sort = new ArrayList<>();
for (QuickFilter quickFilter : quickFilters) {
String clause = quickFilter.getClause();
if (clause != null) {
if (!quickFiltersClause.isEmpty()) {
quickFiltersClause = NXQLQueryBuilder.appendClause(quickFiltersClause, clause);
} else {
quickFiltersClause = clause;
}
}
sort.addAll(quickFilter.getSortInfos());
}
} else if (sortInfos != null) {
sort = sortInfos;
}
SortInfo[] sortArray = null;
if (sort != null) {
sortArray = sort.toArray(new SortInfo[] {});
}
String newQuery;
PageProviderDefinition def = getDefinition();
WhereClauseDefinition whereClause = def.getWhereClause();
if (whereClause == null) {
String originalPattern = def.getPattern();
String pattern = quickFiltersClause.isEmpty() ? originalPattern
: StringUtils.containsIgnoreCase(originalPattern, " WHERE ")
? NXQLQueryBuilder.appendClause(originalPattern, quickFiltersClause)
: originalPattern + " WHERE " + quickFiltersClause;
newQuery = NXQLQueryBuilder.getQuery(pattern, getParameters(), def.getQuotePatternParameters(),
def.getEscapePatternParameters(), getSearchDocumentModel(), sortArray);
} else {
DocumentModel searchDocumentModel = getSearchDocumentModel();
if (searchDocumentModel == null) {
throw new NuxeoException(String.format(
"Cannot build query of provider '%s': " + "no search document model is set", getName()));
}
newQuery = NXQLQueryBuilder.getQuery(searchDocumentModel, whereClause, quickFiltersClause, getParameters(),
sortArray);
}
if (query != null && newQuery != null && !newQuery.equals(query)) {
// query has changed => refresh
refresh();
}
query = newQuery;
}
protected void checkQueryCache() {
// maybe handle refresh of select page according to query
if (getBooleanProperty(CHECK_QUERY_CACHE_PROPERTY, false)) {
CoreSession coreSession = getCoreSession();
buildQuery(coreSession);
}
}
protected boolean useUnrestrictedSession() {
return getBooleanProperty(USE_UNRESTRICTED_SESSION_PROPERTY, false);
}
protected boolean detachDocuments() {
return getBooleanProperty(DETACH_DOCUMENTS_PROPERTY, true);
}
protected CoreSession getCoreSession() {
Map<String, Serializable> props = getProperties();
CoreSession coreSession = (CoreSession) props.get(CORE_SESSION_PROPERTY);
if (coreSession == null) {
throw new NuxeoException("cannot find core session");
}
return coreSession;
}
/**
* Returns the maximum number of results or <code>0</code> if there is no limit.
*
* @since 5.6
*/
public long getMaxResults() {
if (maxResults == null) {
maxResults = Long.valueOf(0);
String maxResultsStr = (String) getProperties().get(MAX_RESULTS_PROPERTY);
if (maxResultsStr != null) {
if (DEFAULT_NAVIGATION_RESULTS_KEY.equals(maxResultsStr)) {
ConfigurationService cs = Framework.getService(ConfigurationService.class);
maxResultsStr = cs.getString(DEFAULT_NAVIGATION_RESULTS_PROPERTY, DEFAULT_NAVIGATION_RESULTS_VALUE);
} else if (PAGE_SIZE_RESULTS_KEY.equals(maxResultsStr)) {
maxResultsStr = Long.valueOf(getPageSize()).toString();
}
try {
maxResults = Long.valueOf(maxResultsStr);
} catch (NumberFormatException e) {
log.warn(String.format(
"Invalid maxResults property value: %s for page provider: %s, fallback to unlimited.",
maxResultsStr, getName()));
}
}
}
return maxResults.longValue();
}
@Override
public long getResultsCountLimit() {
return getMaxResults();
}
/**
* Returns the page limit. The n first page we know they exist. We don't compute the number of page beyond this
* limit.
*
* @since 5.8
*/
@Override
public long getPageLimit() {
long pageSize = getPageSize();
if (pageSize == 0) {
return 0;
}
return getMaxResults() / pageSize;
}
/**
* Sets the maximum number of result elements.
*
* @since 5.6
*/
public void setMaxResults(long maxResults) {
this.maxResults = Long.valueOf(maxResults);
}
@Override
public PageSelections<DocumentModel> getCurrentSelectPage() {
checkQueryCache();
// fetch last page if current page index is beyond the last page or if there are no results to display
rewindSelectablePage();
return super.getCurrentSelectPage();
}
public String getCurrentQuery() {
return query;
}
/**
* Fetch a page that can be selected. It loads the last page if we're targeting a page beyond the last one or the
* first page if there are no results to show and we're targeting anything other than the first page. Fix for
* NXP-8564.
*/
protected void rewindSelectablePage() {
long pageSize = getPageSize();
if (pageSize != 0) {
if (offset != 0 && currentPageDocuments != null && currentPageDocuments.size() == 0) {
if (resultsCount == 0) {
// fetch first page directly
if (log.isDebugEnabled()) {
log.debug(
String.format(
"Current page %s is not the first one but " + "shows no result and there are "
+ "no results => rewind to first page",
Long.valueOf(getCurrentPageIndex())));
}
firstPage();
} else {
// fetch last page
if (log.isDebugEnabled()) {
log.debug(String.format(
"Current page %s is not the first one but " + "shows no result and there are "
+ "%s results => fetch last page",
Long.valueOf(getCurrentPageIndex()), Long.valueOf(resultsCount)));
}
lastPage();
}
// fetch current page again
getCurrentPage();
}
}
}
/**
* Filter to use when processing results.
* <p>
* Defaults to null (no filter applied), method to be overridden by subclasses.
*
* @since 6.0
*/
protected Filter getFilter() {
return null;
}
@Override
protected void pageChanged() {
currentPageDocuments = null;
super.pageChanged();
}
@Override
public void refresh() {
query = null;
currentPageDocuments = null;
super.refresh();
}
}