-
Notifications
You must be signed in to change notification settings - Fork 188
/
GetOperationOptionsBuilderImpl.java
421 lines (349 loc) · 12.8 KB
/
GetOperationOptionsBuilderImpl.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
/*
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.schema;
import static java.util.Collections.singleton;
import static org.apache.commons.collections4.CollectionUtils.emptyIfNull;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import javax.xml.namespace.QName;
import org.jetbrains.annotations.NotNull;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.path.UniformItemPath;
import com.evolveum.midpoint.prism.query.ObjectPaging;
import com.evolveum.midpoint.prism.query.OrderDirection;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FetchErrorHandlingType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FetchErrorReportingMethodType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.IterationMethodType;
public class GetOperationOptionsBuilderImpl
implements GetOperationOptionsBuilder, GetOperationOptionsBuilder.Query {
@NotNull private Set<UniformItemPath> currentPaths;
private RelationalValueSearchQuery relationalValueSearchQuery;
private final Map<UniformItemPath, GetOperationOptions> options = new HashMap<>();
private final PrismContext prismContext;
GetOperationOptionsBuilderImpl(PrismContext prismContext) {
this.prismContext = prismContext;
currentPaths = singleton(prismContext.emptyPath());
}
//region Path setting
@Override
public GetOperationOptionsBuilder root() {
currentPaths = singleton(prismContext.emptyPath());
return this;
}
@Override
public GetOperationOptionsBuilder items(Object... items) {
currentPaths = new HashSet<>();
for (Object item : items) {
currentPaths.add(pathForItem(item));
}
return this;
}
@Override
public GetOperationOptionsBuilder item(ItemPath path) {
currentPaths = singleton(prismContext.toUniformPath(path));
return this;
}
@Override
public GetOperationOptionsBuilder item(Object... components) {
currentPaths = singleton(prismContext.path(components));
return this;
}
//endregion
//region Individual options except Query
@Override
public GetOperationOptionsBuilder retrieve() {
return retrieve(RetrieveOption.INCLUDE);
}
@Override
public GetOperationOptionsBuilder dontRetrieve() {
return retrieve(RetrieveOption.EXCLUDE);
}
@Override
public GetOperationOptionsBuilder retrieve(RetrieveOption value) {
return forPaths(opts -> opts.setRetrieve(value));
}
@Override
public GetOperationOptionsBuilderImpl retrieve(RelationalValueSearchQuery query) {
return forPaths(opts -> {
opts.setRetrieve(RetrieveOption.INCLUDE);
opts.setRelationalValueSearchQuery(query);
});
}
@Override
public Query retrieveQuery() {
if (relationalValueSearchQuery != null) {
throw new IllegalStateException("Already constructing relational value search query");
}
relationalValueSearchQuery = new RelationalValueSearchQuery(null);
return retrieve(relationalValueSearchQuery);
}
@Override
public GetOperationOptionsBuilder resolve() {
return resolve(true);
}
@Override
public GetOperationOptionsBuilder resolve(Boolean value) {
return forPaths(opts -> opts.setResolve(value));
}
@Override
public GetOperationOptionsBuilder resolveNames() {
return resolveNames(true);
}
@Override
public GetOperationOptionsBuilder resolveNames(Boolean value) {
return forPaths(opts -> opts.setResolveNames(value));
}
@Override
public GetOperationOptionsBuilder noFetch() {
return noFetch(true);
}
@Override
public GetOperationOptionsBuilder noFetch(Boolean value) {
return forPaths(opts -> opts.setNoFetch(value));
}
@Override
public GetOperationOptionsBuilder raw() {
return raw(true);
}
@Override
public GetOperationOptionsBuilder raw(Boolean value) {
return forPaths(opts -> opts.setRaw(value));
}
@Override
public GetOperationOptionsBuilder tolerateRawData() {
return tolerateRawData(true);
}
@Override
public GetOperationOptionsBuilder tolerateRawData(Boolean value) {
return forPaths(opts -> opts.setTolerateRawData(value));
}
@Override
public GetOperationOptionsBuilder doNotDiscovery() {
return doNotDiscovery(true);
}
@Override
public GetOperationOptionsBuilder doNotDiscovery(Boolean value) {
return forPaths(opts -> opts.setDoNotDiscovery(value));
}
@Override
public GetOperationOptionsBuilder allowNotFound() {
return allowNotFound(true);
}
@Override
public GetOperationOptionsBuilder allowNotFound(Boolean value) {
return forPaths(opts -> opts.setAllowNotFound(value));
}
@Override
public GetOperationOptionsBuilder readOnly() {
return readOnly(true);
}
@Override
public GetOperationOptionsBuilder readOnly(Boolean value) {
return forPaths(opts -> opts.setReadOnly(value));
}
@Override
public GetOperationOptionsBuilder pointInTime(PointInTimeType value) {
return forPaths(opts -> opts.setPointInTimeType(value));
}
@Override
public GetOperationOptionsBuilder staleness(Long value) {
return forPaths(opts -> opts.setStaleness(value));
}
@Override
public GetOperationOptionsBuilder forceRefresh() {
return forceRefresh(true);
}
@Override
public GetOperationOptionsBuilder forceRefresh(Boolean value) {
return forPaths(opts -> opts.setForceRefresh(value));
}
@Override
public GetOperationOptionsBuilder forceRetry() {
return forceRetry(true);
}
@Override
public GetOperationOptionsBuilder forceRetry(Boolean value) {
return forPaths(opts -> opts.setForceRetry(value));
}
@Override
public GetOperationOptionsBuilder distinct() {
return distinct(true);
}
@Override
public GetOperationOptionsBuilder distinct(Boolean value) {
return forPaths(opts -> opts.setDistinct(value));
}
@Override
public GetOperationOptionsBuilder attachDiagData() {
return attachDiagData(true);
}
@Override
public GetOperationOptionsBuilder attachDiagData(Boolean value) {
return forPaths(opts -> opts.setAttachDiagData(value));
}
@Override
public GetOperationOptionsBuilder definitionProcessing(DefinitionProcessingOption value) {
return forPaths(opts -> opts.setDefinitionProcessing(value));
}
@Override
public GetOperationOptionsBuilder iterationMethod(IterationMethodType value) {
return forPaths(opts -> opts.setIterationMethod(value));
}
@Override
public GetOperationOptionsBuilder executionPhase() {
return executionPhase(true);
}
@Override
public GetOperationOptionsBuilder executionPhase(Boolean value) {
return forPaths(opts -> opts.setExecutionPhase(value));
}
@Override
public GetOperationOptionsBuilder errorHandling(FetchErrorHandlingType errorHandling) {
return forPaths(opts -> opts.setErrorHandling(errorHandling));
}
@Override
public GetOperationOptionsBuilder errorReportingMethod(FetchErrorReportingMethodType method) {
return forPaths(opts -> opts.setErrorReportingMethod(method, prismContext));
}
//endregion
//region Query
@Override
public Query asc(ItemPath path) {
getOrCreatePaging().addOrderingInstruction(path, OrderDirection.ASCENDING);
return this;
}
@Override
public Query asc(Object... components) {
return asc(ItemPath.create(components));
}
@Override
public Query desc(ItemPath path) {
getOrCreatePaging().addOrderingInstruction(path, OrderDirection.DESCENDING);
return this;
}
@Override
public Query desc(Object... components) {
return desc(ItemPath.create(components));
}
@Override
public Query offset(Integer n) {
getOrCreatePaging().setOffset(n);
return this;
}
@Override
public Query maxSize(Integer n) {
getOrCreatePaging().setMaxSize(n);
return this;
}
private ObjectPaging getOrCreatePaging() {
checkRelationalValueSearchQuery();
if (relationalValueSearchQuery.getPaging() == null) {
relationalValueSearchQuery.setPaging(prismContext.queryFactory().createPaging());
}
return relationalValueSearchQuery.getPaging();
}
private void checkRelationalValueSearchQuery() {
if (relationalValueSearchQuery == null) {
throw new IllegalStateException("Currently not constructing relational value search query");
}
}
@Override
public Query item(QName column) {
checkRelationalValueSearchQuery();
relationalValueSearchQuery.setColumn(column);
return this;
}
@Override
public Query eq(String value) {
return comparison(RelationalValueSearchType.EXACT, value);
}
@Override
public Query startsWith(String value) {
return comparison(RelationalValueSearchType.STARTS_WITH, value);
}
@Override
public Query contains(String value) {
return comparison(RelationalValueSearchType.SUBSTRING, value);
}
private Query comparison(RelationalValueSearchType type, String value) {
checkRelationalValueSearchQuery();
relationalValueSearchQuery.setSearchType(type);
relationalValueSearchQuery.setSearchValue(value);
return this;
}
@Override
public GetOperationOptionsBuilder end() {
checkRelationalValueSearchQuery();
return this;
}
//endregion
//region Loading from options
@Override
public GetOperationOptionsBuilder setFrom(Collection<SelectorOptions<GetOperationOptions>> newOptions) {
options.clear();
currentPaths = singleton(prismContext.emptyPath());
relationalValueSearchQuery = null;
for (SelectorOptions<GetOperationOptions> newOption : emptyIfNull(newOptions)) {
if (newOption.getOptions() != null) {
UniformItemPath itemPath = newOption.getItemPath(prismContext.emptyPath());
if (options.containsKey(itemPath)) {
throw new IllegalStateException(
"Options for item path '" + itemPath + "' are defined more than once in " + newOptions);
} else {
options.put(itemPath, newOption.getOptions().clone());
}
}
}
return this;
}
@Override
public GetOperationOptionsBuilder mergeFrom(Collection<SelectorOptions<GetOperationOptions>> newOptions) {
currentPaths = singleton(prismContext.emptyPath());
relationalValueSearchQuery = null;
for (SelectorOptions<GetOperationOptions> newOption : emptyIfNull(newOptions)) {
if (newOption.getOptions() != null) {
UniformItemPath itemPath = newOption.getItemPath(prismContext.emptyPath());
GetOperationOptions currentOptions = options.get(itemPath);
if (currentOptions != null) {
currentOptions.merge(newOption.getOptions());
} else {
options.put(itemPath, newOption.getOptions().clone());
}
}
}
return this;
}
//endregion
//region Aux methods
private UniformItemPath pathForItem(Object item) {
if (item instanceof QName) {
return prismContext.path(item);
} else if (item instanceof UniformItemPath) {
return ((UniformItemPath) item);
} else if (item instanceof ItemPath) {
return prismContext.toUniformPath((ItemPath) item);
} else {
throw new IllegalArgumentException("item has to be QName or ItemPath but is " + item);
}
}
private GetOperationOptionsBuilderImpl forPaths(Consumer<GetOperationOptions> modifier) {
for (UniformItemPath path : currentPaths) {
GetOperationOptions optionsForPath = options.computeIfAbsent(path, (key) -> new GetOperationOptions());
modifier.accept(optionsForPath);
}
return this;
}
@NotNull
public Collection<SelectorOptions<GetOperationOptions>> build() {
return options.entrySet().stream()
.map(e -> new SelectorOptions<>(new ObjectSelector(e.getKey()), e.getValue()))
.collect(Collectors.toList());
}
//endregion
}