View Javadoc
1   /**
2    * This file Copyright (c) 2014-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.dam.api;
35  
36  import info.magnolia.dam.api.metadata.AssetMetadata;
37  
38  import java.util.ArrayList;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Map;
42  
43  import com.google.common.base.Preconditions;
44  import com.google.common.net.MediaType;
45  
46  /**
47   * Represents a query to an AssetProvider. Use new AssetQuery.Builder()...build() to construct instances.
48   * @see AssetProvider#list(AssetQuery)
49   */
50  public interface AssetQuery {
51      String getRootPath();
52  
53      Folder getRootFolder();
54  
55      /**
56       * @deprecated since 2.0.5. use {@link #includesDescendants()}
57       */
58      boolean includesChildren();
59  
60      /**
61       * Includes all the descendants of the specified path or folder, not just the direct children.
62       */
63      boolean includesDescendants();
64  
65      List<MediaType> getMediaTypes();
66  
67      String getExtension();
68  
69      String getKeywordSearchTerm();
70  
71      default Map<String, Object> getProperties() {
72          return new HashMap<>();
73      }
74  
75      /**
76       * An additional statement, usually provider-specific, which is appended to the underlying query.
77       * There is no common language between providers, so there is no guarantee that this works accross
78       * providers. No guarantee is made by providers on how this is used either. Some providers
79       * might refuse to use this in addition to other criteria.
80       * @deprecated since 2.0. Kept around for backwards compatibility.
81       */
82      String getAdditionalQueryStatement();
83  
84      boolean includesFolders();
85  
86      boolean includesAssets();
87  
88      List<OrderBy> getSorters();
89  
90      long getMaxResults();
91  
92      /**
93       * The number of items to skip before returning the results.
94       * Results that are skipped due to offset do not count against the limit specified by max results.
95       */
96      long getOffset();
97  
98      /**
99       * Enumeration specifying the order of sorting.
100      */
101     static enum Order {
102         ASCENDING,
103 
104         DESCENDING
105     }
106 
107     /**
108      * Represents a sorting rule to be applied to a query.
109      */
110     static class OrderBy {
111 
112         private Class<? extends AssetMetadata> metadataClass;
113         private String propertyId;
114         private Order order;
115 
116         public OrderBy(Class<? extends AssetMetadata> metadataClass, String propertyId, Order order) {
117             this.metadataClass = metadataClass;
118             this.propertyId = propertyId;
119             this.order = order;
120         }
121 
122         public Class<? extends AssetMetadata> getMetadataClass() {
123             return metadataClass;
124         }
125 
126         public String getPropertyId() {
127             return propertyId;
128         }
129 
130         public Order getOrder() {
131             return order;
132         }
133     }
134 
135     /**
136      * A builder for {@link AssetQuery} which provides a fluent API.
137      */
138     public static final class Builder {
139         private String rootPath;
140         private Folder rootFolder;
141         private boolean includeDescendants = true;
142 
143         private List<MediaType> mediaTypes = new ArrayList<MediaType>();
144         private String extension;
145         private String keywordSearchTerm;
146         private String additionalQueryStatement;
147         private Map<String, Object> properties = new HashMap<>();
148 
149         private boolean includeFolders = false;
150         private boolean includeAssets = true;
151 
152         private long maxResults = 100;
153         private long offset = 0;
154         private List<OrderBy> sorters = new ArrayList<OrderBy>();
155 
156         public Builder() {
157         }
158 
159         public Builder fromPath(String rootPath) {
160             Preconditions.checkState(this.rootFolder == null, "Can't set rootPath, rootFolder is already set");
161             Preconditions.checkState(this.rootPath == null, "Can't set rootPath, it is already set");
162             this.rootPath = rootPath;
163             return this;
164         }
165 
166         public Builder fromFolder(Folder rootFolder) {
167             Preconditions.checkState(this.rootPath == null, "Can't set rootFolder, rootPath is already set");
168             Preconditions.checkState(this.rootFolder == null, "Can't set rootFolder, it is already set");
169             this.rootFolder = rootFolder;
170             return this;
171         }
172 
173         /**
174          * @deprecated since 2.0.5. use {@link #excludeDescendants()}
175          */
176         public Builder excludeChildren() {
177             this.includeDescendants = false;
178             return this;
179         }
180 
181         /**
182          * Do not include all the descendants of the specified path or folder, only the direct children.
183          * Only applies when a Path or Folder were specified.
184          */
185         public Builder excludeDescendants() {
186             this.includeDescendants = false;
187             return this;
188         }
189 
190         public Builder withExtension(String extension) {
191             this.extension = extension;
192             return this;
193         }
194 
195         public Builder withMediaType(MediaType... mediaTypes) {
196             for (MediaType mediaType : mediaTypes) {
197                 this.mediaTypes.add(mediaType);
198             }
199             return this;
200         }
201 
202         public Builder orderBy(Class<? extends AssetMetadata> metadataClass, String propertyId, Order order) {
203             this.sorters.add(new OrderBy(metadataClass, propertyId, order));
204             return this;
205         }
206 
207         public Builder withKeyword(String keyword) {
208             this.keywordSearchTerm = keyword;
209             return this;
210         }
211 
212         public Builder withProperty(String name, Object value) {
213             properties.put(name, value);
214             return this;
215         }
216 
217         /**
218          * An additional statement, usually provider-specific, which is appended to the underlying query.
219          * There is no common language between providers, so there is no guarantee that this works accross
220          * providers. No guarantee is made by providers on how this is used either. Some providers
221          * might refuse to use this in addition to other criteria.
222          * @deprecated since 2.0. Kept around for backwards compatibility.
223          */
224         @Deprecated
225         public Builder withAdditionalQueryStatement(String additionalQueryStatement) {
226             this.additionalQueryStatement = additionalQueryStatement;
227             return this;
228         }
229 
230         public Builder withMaxResults(long maxResults) {
231             this.maxResults = maxResults;
232             return this;
233         }
234 
235         public Builder withOffset(long offset) {
236             this.offset = offset;
237             return this;
238         }
239 
240         public Builder includeFolders() {
241             this.includeFolders = true;
242             return this;
243         }
244 
245         public Builder excludeAssets() {
246             this.includeAssets = false;
247             return this;
248         }
249 
250         public AssetQuery build() {
251             return new AssetQuery() {
252                 @Override
253                 public String getRootPath() {
254                     return rootPath;
255                 }
256 
257                 @Override
258                 public Folder getRootFolder() {
259                     return rootFolder;
260                 }
261 
262                 @Override
263                 public boolean includesChildren() {
264                     return includeDescendants;
265                 }
266 
267                 @Override
268                 public boolean includesDescendants() {
269                     return includeDescendants;
270                 }
271 
272                 @Override
273                 public List<MediaType> getMediaTypes() {
274                     return mediaTypes;
275                 }
276 
277                 @Override
278                 public List<OrderBy> getSorters() {
279                     return sorters;
280                 }
281 
282                 @Override
283                 public String getExtension() {
284                     return extension;
285                 }
286 
287                 @Override
288                 public String getKeywordSearchTerm() {
289                     return keywordSearchTerm;
290                 }
291 
292                 @Override
293                 public Map<String, Object> getProperties() {
294                     return properties;
295                 }
296 
297                 @Override
298                 public String getAdditionalQueryStatement() {
299                     return additionalQueryStatement;
300                 }
301 
302                 @Override
303                 public boolean includesFolders() {
304                     return includeFolders;
305                 }
306 
307                 @Override
308                 public boolean includesAssets() {
309                     return includeAssets;
310                 }
311 
312                 @Override
313                 public long getMaxResults() {
314                     return maxResults;
315                 }
316 
317                 @Override
318                 public long getOffset() {
319                     return offset;
320                 }
321             };
322         }
323     }
324 }