/
R_Filter.java
433 lines (380 loc) · 16.9 KB
/
R_Filter.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
/*
* Copyright (c) 2010-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.prism.query.builder;
import com.evolveum.midpoint.prism.ComplexTypeDefinition;
import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismReferenceValue;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.query.*;
import com.evolveum.midpoint.util.exception.SchemaException;
import org.apache.commons.lang.Validate;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.List;
/**
* EXPERIMENTAL IMPLEMENTATION.
*
* @author mederly
*/
public class R_Filter implements S_FilterEntryOrEmpty, S_AtomicFilterExit {
final private QueryBuilder queryBuilder;
final private Class<? extends Containerable> currentClass; // object we are working on (changes on Exists filter)
final private OrFilter currentFilter;
final private LogicalSymbol lastLogicalSymbol;
final private boolean isNegated;
final private R_Filter parentFilter;
final private QName typeRestriction;
final private ItemPath existsRestriction;
final private List<ObjectOrdering> orderingList;
public R_Filter(QueryBuilder queryBuilder) {
this.queryBuilder = queryBuilder;
this.currentClass = queryBuilder.getQueryClass();
this.currentFilter = OrFilter.createOr();
this.lastLogicalSymbol = null;
this.isNegated = false;
this.parentFilter = null;
this.typeRestriction = null;
this.existsRestriction = null;
this.orderingList = new ArrayList<>();
}
private R_Filter(QueryBuilder queryBuilder, Class<? extends Containerable> currentClass, OrFilter currentFilter, LogicalSymbol lastLogicalSymbol,
boolean isNegated, R_Filter parentFilter, QName typeRestriction, ItemPath existsRestriction, List<ObjectOrdering> orderingList) {
this.queryBuilder = queryBuilder;
this.currentClass = currentClass;
this.currentFilter = currentFilter;
this.lastLogicalSymbol = lastLogicalSymbol;
this.isNegated = isNegated;
this.parentFilter = parentFilter;
this.typeRestriction = typeRestriction;
this.existsRestriction = existsRestriction;
if (orderingList != null) {
this.orderingList = orderingList;
} else {
this.orderingList = new ArrayList<>();
}
}
public static S_FilterEntryOrEmpty create(QueryBuilder builder) {
return new R_Filter(builder);
}
// subfilter might be null
R_Filter addSubfilter(ObjectFilter subfilter) throws SchemaException {
if (!currentFilter.isEmpty() && lastLogicalSymbol == null) {
throw new IllegalStateException("lastLogicalSymbol is empty but there is already some filter present: " + currentFilter);
}
if (typeRestriction != null && existsRestriction != null) {
throw new IllegalStateException("Both type and exists restrictions present");
}
if (typeRestriction != null) {
if (!currentFilter.isEmpty()) {
throw new IllegalStateException("Type restriction with 2 filters?");
}
if (isNegated) {
subfilter = new NotFilter(subfilter);
}
return parentFilter.addSubfilter(TypeFilter.createType(typeRestriction, subfilter));
} else if (existsRestriction != null) {
if (!currentFilter.isEmpty()) {
throw new IllegalStateException("Exists restriction with 2 filters?");
}
if (isNegated) {
subfilter = new NotFilter(subfilter);
}
return parentFilter.addSubfilter(
ExistsFilter.createExists(
existsRestriction,
parentFilter.currentClass,
queryBuilder.getPrismContext(),
subfilter));
} else {
OrFilter newFilter = appendAtomicFilter(subfilter, isNegated, lastLogicalSymbol);
return new R_Filter(queryBuilder, currentClass, newFilter, null, false, parentFilter, typeRestriction, existsRestriction, orderingList);
}
}
private OrFilter appendAtomicFilter(ObjectFilter subfilter, boolean negated, LogicalSymbol logicalSymbol) {
if (negated) {
subfilter = new NotFilter(subfilter);
}
OrFilter updatedFilter = currentFilter.clone();
if (logicalSymbol == null || logicalSymbol == LogicalSymbol.OR) {
updatedFilter.addCondition(AndFilter.createAnd(subfilter));
} else if (logicalSymbol == LogicalSymbol.AND) {
((AndFilter) updatedFilter.getLastCondition()).addCondition(subfilter);
} else {
throw new IllegalStateException("Unknown logical symbol: " + logicalSymbol);
}
return updatedFilter;
}
private R_Filter setLastLogicalSymbol(LogicalSymbol newLogicalSymbol) {
if (this.lastLogicalSymbol != null) {
throw new IllegalStateException("Two logical symbols in a sequence");
}
return new R_Filter(queryBuilder, currentClass, currentFilter, newLogicalSymbol, isNegated, parentFilter, typeRestriction, existsRestriction, orderingList);
}
private R_Filter setNegated() {
if (isNegated) {
throw new IllegalStateException("Double negation");
}
return new R_Filter(queryBuilder, currentClass, currentFilter, lastLogicalSymbol, true, parentFilter, typeRestriction, existsRestriction, orderingList);
}
private R_Filter addOrdering(ObjectOrdering ordering) {
Validate.notNull(ordering);
List<ObjectOrdering> newList = new ArrayList<>(orderingList);
newList.add(ordering);
return new R_Filter(queryBuilder, currentClass, currentFilter, lastLogicalSymbol, isNegated, parentFilter, typeRestriction, existsRestriction, newList);
}
@Override
public S_AtomicFilterExit all() throws SchemaException {
return addSubfilter(AllFilter.createAll());
}
@Override
public S_AtomicFilterExit none() throws SchemaException {
return addSubfilter(NoneFilter.createNone());
}
@Override
public S_AtomicFilterExit undefined() throws SchemaException {
return addSubfilter(UndefinedFilter.createUndefined());
}
// TODO .............................................
@Override
public S_AtomicFilterExit id(String... identifiers) throws SchemaException {
return addSubfilter(InOidFilter.createInOid(identifiers));
}
@Override
public S_AtomicFilterExit id(long... identifiers) throws SchemaException {
List<String> ids = longsToStrings(identifiers);
return addSubfilter(InOidFilter.createInOid(ids));
}
private List<String> longsToStrings(long[] identifiers) {
List<String> ids = new ArrayList<>(identifiers.length);
for (long id : identifiers) {
ids.add(String.valueOf(id));
}
return ids;
}
@Override
public S_AtomicFilterExit ownerId(String... identifiers) throws SchemaException {
return addSubfilter(InOidFilter.createOwnerHasOidIn(identifiers));
}
@Override
public S_AtomicFilterExit ownerId(long... identifiers) throws SchemaException {
return addSubfilter(InOidFilter.createOwnerHasOidIn(longsToStrings(identifiers)));
}
@Override
public S_AtomicFilterExit isDirectChildOf(PrismReferenceValue value) throws SchemaException {
OrgFilter orgFilter = OrgFilter.createOrg(value, OrgFilter.Scope.ONE_LEVEL);
return addSubfilter(orgFilter);
}
@Override
public S_AtomicFilterExit isChildOf(PrismReferenceValue value) throws SchemaException {
OrgFilter orgFilter = OrgFilter.createOrg(value, OrgFilter.Scope.SUBTREE);
return addSubfilter(orgFilter);
}
@Override
public S_AtomicFilterExit isParentOf(PrismReferenceValue value) throws SchemaException {
OrgFilter orgFilter = OrgFilter.createOrg(value, OrgFilter.Scope.ANCESTORS);
return addSubfilter(orgFilter);
}
@Override
public S_AtomicFilterExit isDirectChildOf(String oid) throws SchemaException {
OrgFilter orgFilter = OrgFilter.createOrg(oid, OrgFilter.Scope.ONE_LEVEL);
return addSubfilter(orgFilter);
}
@Override
public S_AtomicFilterExit isChildOf(String oid) throws SchemaException {
OrgFilter orgFilter = OrgFilter.createOrg(oid, OrgFilter.Scope.SUBTREE);
return addSubfilter(orgFilter);
}
@Override
public S_AtomicFilterExit isParentOf(String oid) throws SchemaException {
OrgFilter orgFilter = OrgFilter.createOrg(oid, OrgFilter.Scope.ANCESTORS);
return addSubfilter(orgFilter);
}
@Override
public S_AtomicFilterExit isRoot() throws SchemaException {
OrgFilter orgFilter = OrgFilter.createRootOrg();
return addSubfilter(orgFilter);
}
@Override
public S_FilterEntryOrEmpty block() {
return new R_Filter(queryBuilder, currentClass, OrFilter.createOr(), null, false, this, null, null, null);
}
@Override
public S_FilterEntry type(Class<? extends Containerable> type) throws SchemaException {
ComplexTypeDefinition ctd = queryBuilder.getPrismContext().getSchemaRegistry().findComplexTypeDefinitionByCompileTimeClass(type);
if (ctd == null) {
throw new SchemaException("Unknown type: " + type);
}
QName typeName = ctd.getTypeName();
if (typeName == null) {
throw new IllegalStateException("No type name for " + ctd);
}
return new R_Filter(queryBuilder, type, OrFilter.createOr(), null, false, this, typeName, null, null);
}
@Override
public S_FilterEntry exists(QName... names) throws SchemaException {
if (existsRestriction != null) {
throw new IllegalStateException("Exists within exists");
}
if (names.length == 0) {
throw new SchemaException("Empty path in exists() filter is not allowed.");
}
ItemPath existsPath = new ItemPath(names);
PrismContainerDefinition pcd = resolveItemPath(existsPath, PrismContainerDefinition.class);
Class<? extends Containerable> clazz = pcd.getCompileTimeClass();
if (clazz == null) {
throw new SchemaException("Item path of '" + existsPath + "' in " + currentClass + " does not point to a valid prism container.");
}
return new R_Filter(queryBuilder, clazz, OrFilter.createOr(), null, false, this, null, existsPath, null);
}
<ID extends ItemDefinition> ID resolveItemPath(ItemPath itemPath, Class<ID> type) throws SchemaException {
Validate.notNull(type, "type");
ComplexTypeDefinition ctd = queryBuilder.getPrismContext().getSchemaRegistry().findComplexTypeDefinitionByCompileTimeClass(currentClass);
if (ctd == null) {
throw new SchemaException("Definition for " + currentClass + " couldn't be found.");
}
ID definition = ctd.findItemDefinition(itemPath, type);
if (definition == null) {
throw new SchemaException("Item path of '" + itemPath + "' in " + currentClass + " does not point to a valid " + type.getSimpleName());
}
return definition;
}
// END OF TODO .............................................
@Override
public S_FilterEntry and() {
return setLastLogicalSymbol(LogicalSymbol.AND);
}
@Override
public S_FilterEntry or() {
return setLastLogicalSymbol(LogicalSymbol.OR);
}
@Override
public S_AtomicFilterEntry not() {
return setNegated();
}
@Override
public S_ConditionEntry item(QName... names) throws SchemaException {
ItemPath itemPath = new ItemPath(names);
ItemDefinition itemDefinition = resolveItemPath(itemPath, ItemDefinition.class);
return item(itemPath, itemDefinition);
}
@Override
public S_ConditionEntry item(ItemPath itemPath, ItemDefinition itemDefinition) throws SchemaException {
return R_AtomicFilter.create(itemPath, itemDefinition, this);
}
@Override
public S_ConditionEntry item(PrismContainerDefinition containerDefinition, QName... names) throws SchemaException {
return item(containerDefinition, new ItemPath(names));
}
@Override
public S_ConditionEntry item(PrismContainerDefinition containerDefinition, ItemPath itemPath) throws SchemaException {
ItemDefinition itemDefinition = containerDefinition.findItemDefinition(itemPath);
if (itemDefinition == null) {
throw new SchemaException("No definition of " + itemPath + " in " + containerDefinition);
}
return item(itemPath, itemDefinition);
}
@Override
public S_AtomicFilterExit endBlock() throws SchemaException {
if (parentFilter == null) {
throw new SchemaException("endBlock() call without preceding block() one");
}
if (currentFilter != null || parentFilter.hasRestriction()) {
ObjectFilter simplified = simplify(currentFilter);
if (simplified != null || parentFilter.hasRestriction()) {
return parentFilter.addSubfilter(simplified);
}
}
return parentFilter;
}
private boolean hasRestriction() {
return existsRestriction != null || typeRestriction != null;
}
@Override
public S_FilterExit asc(QName... names) throws SchemaException {
if (names.length == 0) {
throw new SchemaException("There must be at least one name for asc(...) ordering");
}
return addOrdering(ObjectOrdering.createOrdering(new ItemPath(names), OrderDirection.ASCENDING));
}
@Override
public S_FilterExit asc(ItemPath path) throws SchemaException {
if (ItemPath.isNullOrEmpty(path)) {
throw new SchemaException("There must be non-empty path for asc(...) ordering");
}
return addOrdering(ObjectOrdering.createOrdering(path, OrderDirection.ASCENDING));
}
@Override
public S_FilterExit desc(QName... names) throws SchemaException {
if (names.length == 0) {
throw new SchemaException("There must be at least one name for asc(...) ordering");
}
return addOrdering(ObjectOrdering.createOrdering(new ItemPath(names), OrderDirection.DESCENDING));
}
@Override
public S_FilterExit desc(ItemPath path) throws SchemaException {
if (ItemPath.isNullOrEmpty(path)) {
throw new SchemaException("There must be non-empty path for desc(...) ordering");
}
return addOrdering(ObjectOrdering.createOrdering(path, OrderDirection.DESCENDING));
}
@Override
public ObjectQuery build() throws SchemaException {
if (typeRestriction != null || existsRestriction != null) {
// unfinished empty type restriction or exists restriction
return addSubfilter(null).build();
}
if (parentFilter != null) {
throw new SchemaException("A block in filter definition was probably not closed.");
}
ObjectPaging paging;
if (!orderingList.isEmpty()) {
paging = ObjectPaging.createEmptyPaging();
paging.setOrdering(orderingList);
} else {
paging = null;
}
return ObjectQuery.createObjectQuery(simplify(currentFilter), paging);
}
@Override
public ObjectFilter buildFilter() throws SchemaException {
return build().getFilter();
}
private ObjectFilter simplify(OrFilter filter) {
if (filter == null) {
return null;
}
OrFilter simplified = OrFilter.createOr();
// step 1 - simplify conjunctions
for (ObjectFilter condition : filter.getConditions()) {
AndFilter conjunction = (AndFilter) condition;
if (conjunction.getConditions().size() == 1) {
simplified.addCondition(conjunction.getLastCondition());
} else {
simplified.addCondition(conjunction);
}
}
// step 2 - simplify disjunction
if (simplified.getConditions().size() == 0) {
return null;
} else if (simplified.getConditions().size() == 1) {
return simplified.getLastCondition();
} else {
return simplified;
}
}
}