001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thomas Roger
018 */
019package org.nuxeo.ecm.platform.query.api;
020
021import java.io.Serializable;
022import java.util.List;
023import java.util.Map;
024
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.SortInfo;
027
028/**
029 * Basic interface for a page provider, independent of type of items in the list
030 * <p>
031 * Provides APIs to navigate between result pages
032 *
033 * @param <T> any Serializable item
034 * @since 5.4
035 * @author arussel
036 * @author Anahide Tchertchian
037 */
038public interface PageProvider<T> extends Serializable {
039
040    public static final String DEFAULT_MAX_PAGE_SIZE_RUNTIME_PROP = "nuxeo.pageprovider.default-max-page-size";
041
042    /**
043     * Constant to express that the total number of result elements is unknown (usually because the query has not been
044     * done yet).
045     */
046    public static final long UNKNOWN_SIZE = -1;
047
048    /**
049     * Constant to express that the total number of result elements is unknown even after performing a query.
050     *
051     * @since 5.5
052     */
053    public static final long UNKNOWN_SIZE_AFTER_QUERY = -2;
054
055    /**
056     * Default maximum page size value.
057     *
058     * @since 6.0, default value is 1000.
059     */
060    public static final long DEFAULT_MAX_PAGE_SIZE = 1000;
061
062    /**
063     * Page limit unknown.
064     *
065     * @since 5.8
066     */
067    public static final long PAGE_LIMIT_UNKNOWN = -1;
068
069    /**
070     * Returns the provider identifier
071     */
072    String getName();
073
074    /**
075     * Sets the provider identifier
076     */
077    void setName(String name);
078
079    /**
080     * Gets properties set on the provider.
081     * <p>
082     * Useful to retrieve a provider specific field attributes after instantiation. Other contextual parameters can be
083     * passed through API constructing the result provider.
084     */
085    Map<String, Serializable> getProperties();
086
087    /**
088     * Sets properties set on the provider.
089     * <p>
090     * Useful to initialize a provider specific field attributes after instantiation. Other contextual parameters can be
091     * passed through API constructing the result provider.
092     */
093    void setProperties(Map<String, Serializable> properties);
094
095    Object[] getParameters();
096
097    void setParameters(Object[] parameters);
098
099    /**
100     * Returns the number of results per page. 0 means no pagination unless {@link #getMaxPageSize()} is greater than
101     * this value, it will be taken into account instead.
102     */
103    long getPageSize();
104
105    /**
106     * Sets the number of results per page. 0 means no pagination unless {@link #getMaxPageSize()} is greater than this
107     * value, it will be taken into account instead.
108     */
109    void setPageSize(long pageSize);
110
111    /**
112     * Returns the max number of results per page. 0 means no pagination.
113     * <p>
114     * If page size is greater than this maximum value, it will be taken into account instead.
115     *
116     * @since 5.4.2
117     */
118    long getMaxPageSize();
119
120    /**
121     * Sets the max number of results per page. 0 means no pagination.
122     * <p>
123     * If page size is greater than this maximum value, it will be taken into account instead.
124     *
125     * @since 5.4.2
126     */
127    void setMaxPageSize(long pageSize);
128
129    /**
130     * Returns a list of available page size options to display in the page size selector.
131     * <p>
132     * Uses an hardcoded list of values, and adds up the page provider initial and current page sizes.
133     *
134     * @since 7.3
135     */
136    List<Long> getPageSizeOptions();
137
138    /**
139     * Sets the page size options.
140     *
141     * @since 7.3
142     */
143    void setPageSizeOptions(List<Long> options);
144
145    /**
146     * Returns the number of result elements if available or a negative value if it is unknown:
147     * <code>UNKNOWN_SIZE</code> if it is unknown as query was not done, and since 5.5,
148     * <code>UNKNOWN_SIZE_AFTER_QUERY</code> if it is still unknown after query was done.
149     */
150    long getResultsCount();
151
152    /**
153     * Sets the results count.
154     *
155     * @since 5.5
156     */
157    void setResultsCount(long resultsCount);
158
159    /**
160     * Returns the total number of pages or 0 if number of pages is unknown.
161     */
162    long getNumberOfPages();
163
164    /**
165     * Returns the page limit. The n first page we know they exist.
166     *
167     * @since 5.7.3
168     */
169    long getPageLimit();
170
171    /**
172     * Returns the current page of results.
173     * <p>
174     * This method is designed to be called from higher levels. It therefore ensures cheapness of repeated calls, rather
175     * than data consistency. There is a refresh() method for that.
176     * <p>
177     *
178     * @return the current page
179     */
180    List<T> getCurrentPage();
181
182    /**
183     * Returns the current page of results wrapped in a {@link PageSelection} item.
184     * <p>
185     * By default, no entry is selected, unless {@link #setSelectedEntries(List)} has been called before.
186     */
187    PageSelections<T> getCurrentSelectPage();
188
189    /**
190     * Sets the list of selected entries to take into account in {@link #getCurrentSelectPage()}.
191     */
192    void setSelectedEntries(List<T> entries);
193
194    /**
195     * Sets the current page offset.
196     * <p>
197     * If the provider keeps information linked to the current page, they should be reset after calling this method.
198     *
199     * @since 5.5
200     */
201    public void setCurrentPageOffset(long offset);
202
203    /**
204     * Sets the current page of results to the required one.
205     *
206     * @param currentPageIndex the page index, starting from 0
207     * @since 5.7.3
208     */
209    void setCurrentPageIndex(long currentPageIndex);
210
211    /**
212     * Sets the current page of results to the required one and return it.
213     *
214     * @param page the page index, starting from 0
215     */
216    List<T> setCurrentPage(long page);
217
218    /**
219     * Forces refresh of the current page.
220     */
221    void refresh();
222
223    /**
224     * Returns a boolean expressing if there are further pages.
225     */
226    boolean isNextPageAvailable();
227
228    /**
229     * Returns a boolean expressing if the last page can be displayed.
230     *
231     * @since 5.5
232     */
233    boolean isLastPageAvailable();
234
235    /**
236     * Returns a boolean expressing if there is a previous page.
237     */
238    boolean isPreviousPageAvailable();
239
240    /**
241     * Returns the number of elements in current page.
242     */
243    long getCurrentPageSize();
244
245    /**
246     * Returns the offset (starting from 0) of the first element in the current page or <code>UNKNOWN_SIZE</code>.
247     */
248    long getCurrentPageOffset();
249
250    /**
251     * Returns the current page index as a zero-based integer.
252     */
253    long getCurrentPageIndex();
254
255    /**
256     * Returns a simple formatted string for current pagination status.
257     */
258    String getCurrentPageStatus();
259
260    /**
261     * Go to the first page
262     */
263    void firstPage();
264
265    /**
266     * Go to the previous page
267     */
268    void previousPage();
269
270    /**
271     * Go to the next page
272     */
273    void nextPage();
274
275    /**
276     * Go to the last page. Does not do anything if there is only one page displayed, or if the number of results is
277     * unknown.
278     */
279    void lastPage();
280
281    /**
282     * Returns the current entry.
283     */
284    T getCurrentEntry();
285
286    /**
287     * Sets the current entry.
288     */
289    void setCurrentEntry(T entry);
290
291    /**
292     * Sets the current entry index.
293     */
294    void setCurrentEntryIndex(long index);
295
296    /**
297     * Returns true if there is a next entry.
298     * <p>
299     * The next entry might be in next page, except if results count is unknown.
300     */
301    boolean isNextEntryAvailable();
302
303    /**
304     * Returns true if there is a previous entry.
305     * <p>
306     * The previous entry might be in previous page.
307     */
308    boolean isPreviousEntryAvailable();
309
310    /**
311     * Move the current entry to the previous one, if applicable.
312     * <p>
313     * No exception: this method is intended to be plugged directly at the UI layer. In case there's no previous entry,
314     * nothing will happen.
315     */
316    void previousEntry();
317
318    /**
319     * Move the current entry to the next one, if applicable.
320     * <p>
321     * If needed and possible, the provider will forward to next page. No special exceptions: this method is intended to
322     * be plugged directly at the UI layer. In case there's no next entry, nothing happens.
323     */
324    void nextEntry();
325
326    /**
327     * Returns if this provider is sortable
328     */
329    boolean isSortable();
330
331    void setSortable(boolean sortable);
332
333    /**
334     * Returns the complete list of sorting info for this provider
335     */
336    List<SortInfo> getSortInfos();
337
338    /**
339     * Returns the first sorting info for this provider
340     * <p>
341     * Also kept for compatibility with existing code.
342     */
343    SortInfo getSortInfo();
344
345    /**
346     * Sets the complete list of sorting info for this provider
347     */
348    void setSortInfos(List<SortInfo> sortInfo);
349
350    /**
351     * Sets the first and only sorting info for this provider.
352     * <p>
353     * Also kept for compatibility with existing code.
354     */
355    void setSortInfo(SortInfo sortInfo);
356
357    /**
358     * Sets the first and only sorting info for this provider if parameter removeOtherSortInfos is true. Otherwise, adds
359     * or changes the sortAscending information according to given direction.
360     */
361    void setSortInfo(String sortColumn, boolean sortAscending, boolean removeOtherSortInfos);
362
363    /**
364     * Add the given sort info to the list of sorting infos.
365     */
366    void addSortInfo(String sortColumn, boolean sortAscending);
367
368    /**
369     * Returns a positive 0-based integer if given sort information is found on the set sort infos, indicating the sort
370     * index, or -1 if this sort information is not found.
371     */
372    int getSortInfoIndex(String sortColumn, boolean sortAscending);
373
374    DocumentModel getSearchDocumentModel();
375
376    void setSearchDocumentModel(DocumentModel doc);
377
378    boolean hasError();
379
380    String getErrorMessage();
381
382    Throwable getError();
383
384    void setDefinition(PageProviderDefinition providerDefinition);
385
386    PageProviderDefinition getDefinition();
387
388    /**
389     * Sets the {@link PageProviderChangedListener} for this {@code PageProvider}.
390     *
391     * @since 5.7
392     */
393    void setPageProviderChangedListener(PageProviderChangedListener listener);
394
395    /**
396     * Test if provider parameters have changed
397     *
398     * @since 5.7
399     */
400    boolean hasChangedParameters(Object[] parameters);
401
402    /**
403     * @since 6.0
404     */
405    List<AggregateDefinition> getAggregateDefinitions();
406
407    /**
408     * @since 6.0
409     */
410    Map<String, Aggregate<? extends Bucket>> getAggregates();
411
412    /**
413     * @since 6.0
414     */
415    boolean hasAggregateSupport();
416
417    /**
418     * @since 8.4
419     */
420    List<QuickFilter> getQuickFilters();
421
422    /**
423     * @since 8.4
424     */
425    void setQuickFilters(List<QuickFilter> quickFilters);
426
427    /**
428     * @since 8.4
429     */
430    void addQuickFilter(QuickFilter quickFilter);
431
432    /**
433     * @since 8.4
434     */
435    List<QuickFilter> getAvailableQuickFilters();
436
437}