/
AssetQuery.java
475 lines (401 loc) · 14.2 KB
/
AssetQuery.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* Copyright 2017, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openremote.model.query;
import org.openremote.model.asset.AssetType;
import org.openremote.model.asset.CalendarEventConfiguration;
import org.openremote.model.attribute.MetaItemDescriptor;
import org.openremote.model.query.filter.*;
import java.util.Arrays;
/**
* Encapsulate asset query restriction, projection, and ordering of results.
*/
// TODO: Add AssetQuery support for arbitrary attribute property path amd value (e.g. attributes.consoleProviders.geofence.version == "ORConsole")
public class AssetQuery {
public static class Select {
public String[] attributes;
public String[] meta;
public boolean excludePath = true;
public boolean excludeAttributeMeta = true;
public boolean excludeAttributes = true;
public boolean excludeAttributeValue = true;
public boolean excludeAttributeTimestamp = true;
public boolean excludeAttributeType = true;
public boolean excludeParentInfo;
public boolean excludeRealm;
public Select() {
}
public static Select selectAll() {
return new Select()
.excludeAttributes(false)
.excludeAttributeMeta(false)
.excludeAttributeType(false)
.excludeAttributeTimestamp(false)
.excludeAttributeValue(false)
.excludePath(false)
.excludeParentInfo(false)
.excludeRealm(false);
}
public static Select selectExcludePathAndParentAndRealm() {
return new Select()
.excludeAttributes(false)
.excludeAttributeMeta(false)
.excludeAttributeType(false)
.excludeAttributeTimestamp(false)
.excludeAttributeValue(false)
.excludePath(true)
.excludeParentInfo(true)
.excludeRealm(true);
}
public static Select selectExcludePathAndAttributes() {
return new Select()
.excludePath(true)
.excludeAttributes(true);
}
public static Select selectExcludeAll() {
return new Select()
.excludeAttributes(true)
.excludeAttributeMeta(true)
.excludeAttributeType(true)
.excludeAttributeValue(true)
.excludeAttributeTimestamp(true)
.excludePath(true)
.excludeParentInfo(true)
.excludeRealm(true);
}
public Select attributes(String... attributeNames) {
this.attributes = attributeNames;
return this;
}
public Select meta(String... metaUrns) {
this.meta = metaUrns;
return this;
}
public Select meta(MetaItemDescriptor... meta) {
if (meta == null) {
this.meta = null;
return this;
}
return meta(Arrays.stream(meta).map(MetaItemDescriptor::getUrn).toArray(String[]::new));
}
public Select excludeAttributes(boolean exclude) {
this.excludeAttributes = exclude;
return this;
}
public Select excludeAttributeMeta(boolean exclude) {
this.excludeAttributeMeta = exclude;
return this;
}
public Select excludeAttributeType(boolean exclude) {
this.excludeAttributeType = exclude;
return this;
}
public Select excludeAttributeValue(boolean exclude) {
this.excludeAttributeValue = exclude;
return this;
}
public Select excludeAttributeTimestamp(boolean exclude) {
this.excludeAttributeTimestamp = exclude;
return this;
}
public Select excludePath(boolean exclude) {
this.excludePath = exclude;
return this;
}
public Select excludeParentInfo(boolean exclude) {
this.excludeParentInfo = exclude;
return this;
}
public Select excludeRealm(boolean exclude) {
this.excludeRealm = exclude;
return this;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
", excludeAttributes=" + excludeAttributes +
", excludeAttributeMeta=" + excludeAttributeMeta +
", excludeAttributeValue=" + excludeAttributeValue +
", excludeAttributeTimestamp=" + excludeAttributeTimestamp +
", excludeAttributeType=" + excludeAttributeType +
", excludePath=" + excludePath +
", excludeParentInfo=" + excludeParentInfo +
", excludeRealm=" + excludeRealm +
", attributeNames=" + Arrays.toString(attributes) +
'}';
}
}
public static class OrderBy {
public enum Property {
CREATED_ON,
NAME,
ASSET_TYPE,
PARENT_ID,
REALM
}
public Property property;
public boolean descending;
public OrderBy() {
}
public OrderBy(Property property) {
this.property = property;
}
public OrderBy(Property property, boolean descending) {
this.property = property;
this.descending = descending;
}
public OrderBy property(Property property) {
this.property = property;
return this;
}
public OrderBy descending(boolean descending) {
this.descending = descending;
return this;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"property=" + property +
", descending=" + descending +
'}';
}
}
public enum Access {
PRIVATE,
PROTECTED,
PUBLIC
}
/**
* String matching options
*/
public enum Match {
EXACT,
BEGIN,
END,
CONTAINS;
public String prepare(String string) {
if (string == null)
return null;
switch (this) {
case BEGIN:
return string + "%";
case CONTAINS:
return "%" + string + "%";
case END:
return "%" + string;
}
return string;
}
}
/**
* Binary operators
*/
public enum Operator {
EQUALS,
GREATER_THAN,
GREATER_EQUALS,
LESS_THAN,
LESS_EQUALS,
BETWEEN
}
public enum NumberType {
DOUBLE,
INTEGER
}
public boolean recursive;
// Projection
public Select select;
// Restriction predicates
public Access access = Access.PRIVATE;
public String[] ids;
public StringPredicate[] names;
public ParentPredicate[] parents;
public PathPredicate[] paths;
public TenantPredicate tenant;
public String[] userIds;
public StringPredicate[] types;
public LogicGroup<AttributePredicate> attributes;
public AttributeMetaPredicate[] attributeMeta;
public CalendarEventActivePredicate calendarEventActive;
// Ordering
public OrderBy orderBy;
public int limit;
public AssetQuery() {
}
public AssetQuery recursive(boolean recursive) {
this.recursive = recursive;
return this;
}
public AssetQuery access(Access access) {
this.access = access;
return this;
}
public AssetQuery select(Select select) {
if (select == null) {
select = new Select();
}
this.select = select;
return this;
}
public AssetQuery ids(String... ids) {
this.ids = ids;
return this;
}
public AssetQuery names(String... names) {
if (names == null || names.length == 0) {
this.names = null;
return this;
}
this.names = Arrays.stream(names).map(StringPredicate::new).toArray(StringPredicate[]::new);
return this;
}
public AssetQuery names(StringPredicate... names) {
this.names = names;
return this;
}
public AssetQuery parents(String... ids) {
if (ids == null || ids.length == 0) {
this.names = null;
return this;
}
this.parents = Arrays.stream(ids).map(ParentPredicate::new).toArray(ParentPredicate[]::new);
return this;
}
public AssetQuery parents(AssetType... assetTypes) {
if (assetTypes == null || assetTypes.length == 0) {
this.names = null;
return this;
}
this.parents = Arrays.stream(assetTypes).map(id -> new ParentPredicate().type(id)).toArray(ParentPredicate[]::new);
return this;
}
public AssetQuery parents(ParentPredicate... parentPredicates) {
this.parents = parentPredicates;
return this;
}
public AssetQuery paths(PathPredicate... pathPredicates) {
this.paths = pathPredicates;
return this;
}
public AssetQuery tenant(TenantPredicate tenantPredicate) {
this.tenant = tenantPredicate;
return this;
}
public AssetQuery userIds(String... userIds) {
this.userIds = userIds;
return this;
}
public AssetQuery types(StringPredicate... typePredicates) {
this.types = typePredicates;
return this;
}
public AssetQuery types(String... types) {
if (types == null || types.length == 0) {
this.types = null;
return this;
}
this.types = Arrays.stream(types).map(StringPredicate::new).toArray(StringPredicate[]::new);
return this;
}
public AssetQuery types(AssetType... types) {
if (types == null || types.length == 0) {
this.types = null;
return this;
}
this.types = Arrays.stream(types).map(at -> new StringPredicate(at.getType())).toArray(StringPredicate[]::new);
return this;
}
public AssetQuery attributes(AttributePredicate... attributePredicates) {
if (attributePredicates == null || attributePredicates.length == 0) {
this.attributes = null;
return this;
}
this.attributes = new LogicGroup<>(Arrays.asList(attributePredicates));
return this;
}
public AssetQuery attributes(LogicGroup<AttributePredicate> attributePredicateGroup) {
this.attributes = attributePredicateGroup;
return this;
}
public AssetQuery attributeName(String attributeName) {
return attributes(new AttributePredicate(attributeName));
}
public AssetQuery attributeValue(String name, ValuePredicate valuePredicate) {
return attributes(new AttributePredicate(new StringPredicate(name), valuePredicate));
}
public AssetQuery attributeValue(String name) {
return attributeValue(name, new ValueNotEmptyPredicate());
}
public AssetQuery attributeValue(String name, boolean b) {
return attributeValue(name, new BooleanPredicate(b));
}
public AssetQuery attributeValue(String name, String s) {
return attributeValue(name, new StringPredicate(s));
}
public AssetQuery attributeValue(String name, Match match, String s) {
return attributeValue(name, new StringPredicate(match, s));
}
public AssetQuery attributeValue(String name, Match match, boolean caseSensitive, String s) {
return attributeValue(name, new StringPredicate(match, caseSensitive, s));
}
public AssetQuery attributeValue(String name, double d) {
return attributeValue(name, new NumberPredicate(d));
}
public AssetQuery attributeValue(String name, Operator operator, double d) {
return attributeValue(name, new NumberPredicate(d, operator));
}
public AssetQuery attributeMeta(AttributeMetaPredicate... attributeMetaPredicates) {
this.attributeMeta = attributeMetaPredicates;
return this;
}
public AssetQuery orderBy(OrderBy orderBy) {
this.orderBy = orderBy;
return this;
}
/**
* Will filter out assets that have a {@link CalendarEventConfiguration} attribute that results in the event not
* being 'active/in progress' at the specified time. Assets without a {@link CalendarEventConfiguration} attribute
* are assumed to always be in progress (i.e. will always pass this check).
*
* <b>
* NOTE: This predicate is applied in memory and the results should be limited as much as possible by applying other
* predicates to the query to avoid performance issues.
* </b>
*/
public AssetQuery calendarEventActive(long timestampSeconds) {
calendarEventActive = new CalendarEventActivePredicate(timestampSeconds);
return this;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"select=" + select +
", id='" + ids + '\'' +
", name=" + names +
", parent=" + parents +
", path=" + paths +
", tenant=" + tenant +
", userId='" + userIds + '\'' +
", type=" + types +
", attribute=" + (attributes != null ? attributes.toString() : "null" ) +
", attributeMeta=" + Arrays.toString(attributeMeta) +
", orderBy=" + orderBy +
'}';
}
}