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.List;
40
41 import com.google.common.base.Preconditions;
42 import com.google.common.net.MediaType;
43
44
45
46
47
48 public interface AssetQuery {
49 String getRootPath();
50
51 Folder getRootFolder();
52
53
54
55
56 boolean includesChildren();
57
58
59
60
61 boolean includesDescendants();
62
63 List<MediaType> getMediaTypes();
64
65 String getExtension();
66
67 String getKeywordSearchTerm();
68
69
70
71
72
73
74
75
76 String getAdditionalQueryStatement();
77
78 boolean includesFolders();
79
80 boolean includesAssets();
81
82 List<OrderBy> getSorters();
83
84 long getMaxResults();
85
86
87
88
89
90 long getOffset();
91
92
93
94
95 static enum Order {
96 ASCENDING,
97
98 DESCENDING
99 }
100
101
102
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
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
168
169 public Builder excludeChildren() {
170 this.includeDescendants = false;
171 return this;
172 }
173
174
175
176
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 }