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 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
48
49
50 public interface AssetQuery {
51 String getRootPath();
52
53 Folder getRootFolder();
54
55
56
57
58 boolean includesChildren();
59
60
61
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
77
78
79
80
81
82 String getAdditionalQueryStatement();
83
84 boolean includesFolders();
85
86 boolean includesAssets();
87
88 List<OrderBy> getSorters();
89
90 long getMaxResults();
91
92
93
94
95
96 long getOffset();
97
98
99
100
101 static enum Order {
102 ASCENDING,
103
104 DESCENDING
105 }
106
107
108
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
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
175
176 public Builder excludeChildren() {
177 this.includeDescendants = false;
178 return this;
179 }
180
181
182
183
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
219
220
221
222
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 }