View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.datamodel.objectinfo;
20  
21  import java.time.Instant;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.mycore.datamodel.metadata.MCRObjectID;
26  
27  /**
28   * Used to Query a collection of mycore objects.
29   */
30  public class MCRObjectQuery {
31  
32      private MCRObjectID afterId = null;
33  
34      private int offset = -1;
35  
36      private int limit = -1;
37  
38      private int numberGreater = -1;
39  
40      private int numberLess = -1;
41  
42      private String type = null;
43  
44      private String project = null;
45  
46      private String status = null;
47  
48      private SortBy sortBy;
49  
50      private SortOrder sortOrder;
51  
52      private Instant modifiedBefore;
53  
54      private Instant modifiedAfter;
55  
56      private Instant createdBefore;
57  
58      private Instant createdAfter;
59  
60      private Instant deletedBefore;
61  
62      private Instant deletedAfter;
63  
64      private String createdBy;
65  
66      private String modifiedBy;
67  
68      private String deletedBy;
69  
70      private final List<String> includeCategories = new ArrayList<>();
71  
72      /**
73       * @return the id after which the listing starts
74       */
75      public MCRObjectID afterId() {
76          return afterId;
77      }
78  
79      /**
80       * modifies this query to only return object ids after lastId
81       * should not be used together with the {@link #sort(SortBy, SortOrder)} method
82       * @param lastId
83       * @return this
84       */
85      public MCRObjectQuery afterId(MCRObjectID lastId) {
86          this.afterId = lastId;
87          return this;
88      }
89  
90      /**
91       * @return the amount of objects to skip from the start of the results
92       */
93      public int offset() {
94          return offset;
95      }
96  
97      /**
98       * modifies this query to skip the amount offset objects at the start of the results
99       * @param offset amount
100      * @return this
101      */
102     public MCRObjectQuery offset(int offset) {
103         this.offset = offset;
104         return this;
105     }
106 
107     /**
108      * @return the maximum amount of results
109      */
110     public int limit() {
111         return limit;
112     }
113 
114     /**
115      * modifies this query to limit the amount of results
116      * @param limit amount
117      * @return this
118      */
119     public MCRObjectQuery limit(int limit) {
120         this.limit = limit;
121         return this;
122     }
123 
124     /**
125      * @return the type restriction
126      */
127     public String type() {
128         return type;
129     }
130 
131     /**
132      * modifies this query to restrict the type of the objects
133      * @param type restriction
134      * @return this
135      */
136     public MCRObjectQuery type(String type) {
137         this.type = type;
138         return this;
139     }
140 
141     /**
142      * @return the project restriction
143      */
144     public String project() {
145         return project;
146     }
147 
148     /**
149      * modifies this query to restrict the project of the objects
150      * @param project restriction
151      * @return this
152      */
153     public MCRObjectQuery project(String project) {
154         this.project = project;
155         return this;
156     }
157 
158     /**
159      * @return the status restriction
160      */
161     public String status() {
162         return status;
163     }
164 
165     /**
166      * modifies this query to restrict the status of the objects. e.G. state:submitted
167      * @param status restriction
168      * @return this
169      */
170     public MCRObjectQuery status(String status) {
171         this.status = status;
172         return this;
173     }
174 
175     /**
176      * modifies this query to change the order of the resulting list
177      * @param sortBy the sort field
178      * @param sortOrder the sort direction
179      * @return this
180      */
181     public MCRObjectQuery sort(SortBy sortBy, SortOrder sortOrder) {
182         this.sortOrder = sortOrder;
183         this.sortBy = sortBy;
184         return this;
185     }
186 
187     /**
188      * @return the order field of the resulting list
189      */
190     public SortBy sortBy() {
191         return sortBy;
192     }
193 
194     /**
195      * @return the sort direction of the result list
196      */
197     public SortOrder sortAsc() {
198         return sortOrder;
199     }
200 
201     /**
202      * @return the upper bound limit of the modified date
203      */
204     public Instant modifiedBefore() {
205         return modifiedBefore;
206     }
207 
208     /**
209      * modifies this query to limit the upper bound of the modified date
210      * @param modifiedBefore the upper bound limit
211      * @return the same query
212      */
213     public MCRObjectQuery modifiedBefore(Instant modifiedBefore) {
214         this.modifiedBefore = modifiedBefore;
215         return this;
216     }
217 
218     /**
219      * @return the lower bound limit of the modifed date
220      */
221     public Instant modifiedAfter() {
222         return modifiedAfter;
223     }
224 
225     /**
226      * modifies this query to limit the lower bound of the modified date
227      * @param modifiedAfter the lower bound limit
228      * @return this
229      */
230     public MCRObjectQuery modifiedAfter(Instant modifiedAfter) {
231         this.modifiedAfter = modifiedAfter;
232         return this;
233     }
234 
235     /**
236      * @return the upper bound limit of the created date
237      */
238     public Instant createdBefore() {
239         return createdBefore;
240     }
241 
242     /**
243      * modifies this query to limit the upper bound of the created date
244      * @param createdBefore the upper bound limit
245      * @return this
246      */
247     public MCRObjectQuery createdBefore(Instant createdBefore) {
248         this.createdBefore = createdBefore;
249         return this;
250     }
251 
252     /**
253      * @return the lower bound limit of the created date
254      */
255     public Instant createdAfter() {
256         return createdAfter;
257     }
258 
259     /**
260      * modifies this query to limit the lower bound of the created date
261      * @param createdAfter the lower bound limit
262      * @return this
263      */
264     public MCRObjectQuery createdAfter(Instant createdAfter) {
265         this.createdAfter = createdAfter;
266         return this;
267     }
268 
269     /**
270      * @return the upper bound limit of the deleted date
271      */
272     public Instant deletedBefore() {
273         return deletedBefore;
274     }
275 
276     /**
277      * modifies this query to limit the upper bound of the deleted date
278      * @param deletedBefore the upper bound limit
279      * @return this
280      */
281     public MCRObjectQuery deletedBefore(Instant deletedBefore) {
282         this.deletedBefore = deletedBefore;
283         return this;
284     }
285 
286     /**
287      * @return the lower bound limit of the deleted date
288      */
289     public Instant deletedAfter() {
290         return deletedAfter;
291     }
292 
293     /**
294      * modifies this query to limit the lower bound of the deleted date
295      * @param deletedAfter the lower bound limit
296      * @return this
297      */
298     public MCRObjectQuery deletedAfter(Instant deletedAfter) {
299         this.deletedAfter = deletedAfter;
300         return this;
301     }
302 
303     /**
304      * @return the created by user restriction
305      */
306     public String createdBy() {
307         return createdBy;
308     }
309 
310     /**
311      * modifies this query to restrict the user which created the objects
312      * @param createdBy the user restriction
313      * @return this
314      */
315     public MCRObjectQuery createdBy(String createdBy) {
316         this.createdBy = createdBy;
317         return this;
318     }
319 
320     /**
321      * @return the modified by user restriction
322      */
323     public String modifiedBy() {
324         return modifiedBy;
325     }
326 
327     /**
328      * modifies this query to restrict the user which last modified the objects
329      * @param modifiedBy the user restriction
330      * @return this
331      */
332     public MCRObjectQuery modifiedBy(String modifiedBy) {
333         this.modifiedBy = modifiedBy;
334         return this;
335     }
336 
337     /**
338      * @return the deleted by user restriction
339      */
340     public String deletedBy() {
341         return deletedBy;
342     }
343 
344     /**
345      * modifies this query to restrict the user which deleted the objects
346      * @param deletedBy the user restriction
347      * @return this
348      */
349     public MCRObjectQuery deletedBy(String deletedBy) {
350         this.deletedBy = deletedBy;
351         return this;
352     }
353 
354     /**
355      * @return the lower bound limit to the object id number
356      */
357     public int numberGreater() {
358         return numberGreater;
359     }
360 
361     /**
362      * modifies this query to limit the lower bound of the object id number
363      * @param numberGreater the lower bound limit
364      * @return this
365      */
366     public MCRObjectQuery numberGreater(int numberGreater) {
367         this.numberGreater = numberGreater;
368         return this;
369     }
370 
371     /**
372      * @return the upper bound limit of the object id number
373      */
374     public int numberLess() {
375         return numberLess;
376     }
377 
378     /**
379      * modifies this query to limit the upper bound of the object id number
380      * @param numberLess the upper bound limit
381      * @return this
382      */
383     public MCRObjectQuery numberLess(int numberLess) {
384         this.numberLess = numberLess;
385         return this;
386     }
387 
388     /**
389      * @return a modifiable list of classification category ids. A result object need to be linked to all the
390      * categories.
391      */
392     public List<String> getIncludeCategories() {
393         return includeCategories;
394     }
395 
396     public enum SortBy {
397         id,
398         modified,
399         created
400     }
401 
402     public enum SortOrder {
403         asc,
404         desc
405     }
406 
407     @Override
408     public String toString() {
409         return "MCRObjectQuery{" +
410             "afterId=" + afterId +
411             ", offset=" + offset +
412             ", limit=" + limit +
413             ", numberGreater=" + numberGreater +
414             ", numberLess=" + numberLess +
415             ", type='" + type + '\'' +
416             ", project='" + project + '\'' +
417             ", status='" + status + '\'' +
418             ", sortBy=" + sortBy +
419             ", sortOrder=" + sortOrder +
420             ", modifiedBefore=" + modifiedBefore +
421             ", modifiedAfter=" + modifiedAfter +
422             ", createdBefore=" + createdBefore +
423             ", createdAfter=" + createdAfter +
424             ", deletedBefore=" + deletedBefore +
425             ", deletedAfter=" + deletedAfter +
426             ", createdBy='" + createdBy + '\'' +
427             ", modifiedBy='" + modifiedBy + '\'' +
428             ", deletedBy='" + deletedBy + '\'' +
429             ", includeCategories=" + includeCategories +
430             '}';
431     }
432 }