View Javadoc
1   /**
2    * This file Copyright (c) 2014-2015 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.List;
40  
41  import com.google.common.base.Preconditions;
42  import com.google.common.net.MediaType;
43  
44  /**
45   * Represents a query to an AssetProvider. Use new AssetQuery.Builder()...build() to construct instances.
46   * @see AssetProvider#list(AssetQuery)
47   */
48  public interface AssetQuery {
49      String getRootPath();
50  
51      Folder getRootFolder();
52  
53      /**
54       * @deprecated since 2.0.5. use {@link #includesDescendants()}
55       */
56      boolean includesChildren();
57  
58      /**
59       * Includes all the descendants of the specified path or folder, not just the direct children.
60       */
61      boolean includesDescendants();
62  
63      List<MediaType> getMediaTypes();
64  
65      String getExtension();
66  
67      String getKeywordSearchTerm();
68  
69      /**
70       * An additional statement, usually provider-specific, which is appended to the underlying query.
71       * There is no common language between providers, so there is no guarantee that this works accross
72       * providers. No guarantee is made by providers on how this is used either. Some providers
73       * might refuse to use this in addition to other criteria.
74       * @deprecated since 2.0. Kept around for backwards compatibility.
75       */
76      String getAdditionalQueryStatement();
77  
78      boolean includesFolders();
79  
80      boolean includesAssets();
81  
82      List<OrderBy> getSorters();
83  
84      long getMaxResults();
85  
86      /**
87       * The number of items to skip before returning the results.
88       * Results that are skipped due to offset do not count against the limit specified by max results.
89       */
90      long getOffset();
91  
92      /**
93       * Enumeration specifying the order of sorting.
94       */
95      static enum Order {
96          ASCENDING,
97  
98          DESCENDING
99      }
100 
101     /**
102      * Represents a sorting rule to be applied to a query.
103      */
104     static class OrderBy {
105 
106         private Class<? extends AssetMetadata> metadataClass;
107         private String propertyId;
108         private Order order;
109 
110         public OrderBy(Class<? extends AssetMetadata> metadataClass, String propertyId, Order order) {
111             this.metadataClass = metadataClass;
112             this.propertyId = propertyId;
113             this.order = order;
114         }
115 
116         public Class<? extends AssetMetadata> getMetadataClass() {
117             return metadataClass;
118         }
119 
120         public String getPropertyId() {
121             return propertyId;
122         }
123 
124         public Order getOrder() {
125             return order;
126         }
127     }
128 
129     /**
130      * A builder for {@link AssetQuery} which provides a fluent API.
131      */
132     public static final class Builder {
133         private String rootPath;
134         private Folder rootFolder;
135         private boolean includeDescendants = true;
136 
137         private List<MediaType> mediaTypes = new ArrayList<MediaType>();
138         private String extension;
139         private String keywordSearchTerm;
140         private String additionalQueryStatement;
141 
142         private boolean includeFolders = false;
143         private boolean includeAssets = true;
144 
145         private long maxResults = 100;
146         private long offset = 0;
147         private List<OrderBy> sorters = new ArrayList<OrderBy>();
148 
149         public Builder() {
150         }
151 
152         public Builder fromPath(String rootPath) {
153             Preconditions.checkState(this.rootFolder == null, "Can't set rootPath, rootFolder is already set");
154             Preconditions.checkState(this.rootPath == null, "Can't set rootPath, it is already set");
155             this.rootPath = rootPath;
156             return this;
157         }
158 
159         public Builder fromFolder(Folder rootFolder) {
160             Preconditions.checkState(this.rootPath == null, "Can't set rootFolder, rootPath is already set");
161             Preconditions.checkState(this.rootFolder == null, "Can't set rootFolder, it is already set");
162             this.rootFolder = rootFolder;
163             return this;
164         }
165 
166         /**
167          * @deprecated since 2.0.5. use {@link #excludeDescendants()}
168          */
169         public Builder excludeChildren() {
170             this.includeDescendants = false;
171             return this;
172         }
173 
174         /**
175          * Do not include all the descendants of the specified path or folder, only the direct children.
176          * Only applies when a Path or Folder were specified.
177          */
178         public Builder excludeDescendants() {
179             this.includeDescendants = false;
180             return this;
181         }
182 
183         public Builder withExtension(String extension) {
184             this.extension = extension;
185             return this;
186         }
187 
188         public Builder withMediaType(MediaType... mediaTypes) {
189             for (MediaType mediaType : mediaTypes) {
190                 this.mediaTypes.add(mediaType);
191             }
192             return this;
193         }
194 
195         public Builder orderBy(Class<? extends AssetMetadata> metadataClass, String propertyId, Order order) {
196             this.sorters.add(new OrderBy(metadataClass, propertyId, order));
197             return this;
198         }
199 
200         public Builder withKeyword(String keyword) {
201             this.keywordSearchTerm = keyword;
202             return this;
203         }
204 
205         public Builder withAdditionalQueryStatement(String additionalQueryStatement) {
206             this.additionalQueryStatement = additionalQueryStatement;
207             return this;
208         }
209 
210         public Builder withMaxResults(long maxResults) {
211             this.maxResults = maxResults;
212             return this;
213         }
214 
215         public Builder withOffset(long offset) {
216             this.offset = offset;
217             return this;
218         }
219 
220         public Builder includeFolders() {
221             this.includeFolders = true;
222             return this;
223         }
224 
225         public Builder excludeAssets() {
226             this.includeAssets = false;
227             return this;
228         }
229 
230         public AssetQuery build() {
231             return new AssetQuery() {
232                 @Override
233                 public String getRootPath() {
234                     return rootPath;
235                 }
236 
237                 @Override
238                 public Folder getRootFolder() {
239                     return rootFolder;
240                 }
241 
242                 @Override
243                 public boolean includesChildren() {
244                     return includeDescendants;
245                 }
246 
247                 @Override
248                 public boolean includesDescendants() {
249                     return includeDescendants;
250                 }
251 
252                 @Override
253                 public List<MediaType> getMediaTypes() {
254                     return mediaTypes;
255                 }
256 
257                 @Override
258                 public List<OrderBy> getSorters() {
259                     return sorters;
260                 }
261 
262                 @Override
263                 public String getExtension() {
264                     return extension;
265                 }
266 
267                 @Override
268                 public String getKeywordSearchTerm() {
269                     return keywordSearchTerm;
270                 }
271 
272                 @Override
273                 public String getAdditionalQueryStatement() {
274                     return additionalQueryStatement;
275                 }
276 
277                 @Override
278                 public boolean includesFolders() {
279                     return includeFolders;
280                 }
281 
282                 @Override
283                 public boolean includesAssets() {
284                     return includeAssets;
285                 }
286 
287                 @Override
288                 public long getMaxResults() {
289                     return maxResults;
290                 }
291 
292                 @Override
293                 public long getOffset() {
294                     return offset;
295                 }
296             };
297         }
298     }
299 }