/
CollectionProcessor.java
700 lines (625 loc) · 39 KB
/
CollectionProcessor.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
* Copyright (C) 2019-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.model.impl.controller;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.model.api.context.EvaluatedPolicyRule.TargetType;
import com.evolveum.midpoint.schema.util.*;
import com.evolveum.prism.xml.ns._public.query_3.PagingType;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.evolveum.midpoint.model.api.CollectionStats;
import com.evolveum.midpoint.model.api.ModelService;
import com.evolveum.midpoint.model.api.authentication.CompiledObjectCollectionView;
import com.evolveum.midpoint.model.api.context.EvaluatedCollectionStatsTrigger;
import com.evolveum.midpoint.model.api.context.EvaluatedPolicyRule;
import com.evolveum.midpoint.model.api.context.EvaluatedPolicyRuleTrigger;
import com.evolveum.midpoint.model.common.archetypes.ArchetypeManager;
import com.evolveum.midpoint.model.impl.lens.EvaluatedPolicyRuleImpl;
import com.evolveum.midpoint.model.impl.lens.assignments.AssignmentPathImpl;
import com.evolveum.midpoint.model.impl.lens.assignments.AssignmentPathSegmentImpl;
import com.evolveum.midpoint.model.impl.lens.assignments.ConditionState;
import com.evolveum.midpoint.model.impl.lens.assignments.EvaluationOrderImpl;
import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.prism.query.RefFilter;
import com.evolveum.midpoint.repo.common.ObjectResolver;
import com.evolveum.midpoint.repo.common.expression.ExpressionFactory;
import com.evolveum.midpoint.repo.common.expression.ExpressionUtil;
import com.evolveum.midpoint.schema.expression.VariablesMap;
import com.evolveum.midpoint.schema.*;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.LocalizableMessageBuilder;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.query_3.SearchFilterType;
/**
* @author semancik
*/
@Component
public class CollectionProcessor {
private static final String CONSTRAINT_KEY = "collectionStatsConstraint";
private static final Trace LOGGER = TraceManager.getTrace(CollectionProcessor.class);
@Autowired private PrismContext prismContext;
@Autowired private RelationRegistry relationRegistry;
@Autowired private ModelService modelService;
@Autowired @Qualifier("modelObjectResolver") private ObjectResolver objectResolver;
@Autowired private ArchetypeManager archetypeManager;
@Autowired private ExpressionFactory expressionFactory;
@Autowired private SchemaService schemaService;
Collection<EvaluatedPolicyRule> evaluateCollectionPolicyRules(PrismObject<ObjectCollectionType> collection,
CompiledObjectCollectionView collectionView, Class<? extends ObjectType> targetTypeClass,
Task task, OperationResult result)
throws ObjectNotFoundException, SchemaException, SecurityViolationException, CommunicationException,
ConfigurationException, ExpressionEvaluationException {
if (collectionView == null) {
collectionView = new CompiledObjectCollectionView();
compileObjectCollectionView(collectionView, null, collection.asObjectable(), targetTypeClass, task, result);
}
Collection<EvaluatedPolicyRule> evaluatedPolicyRules = new ArrayList<>();
for (AssignmentType assignmentType : collection.asObjectable().getAssignment()) {
PolicyRuleType policyRuleType = assignmentType.getPolicyRule();
if (policyRuleType == null) {
continue;
}
evaluatedPolicyRules.add(evaluatePolicyRule(collection, collectionView, assignmentType, policyRuleType, task, result));
}
return evaluatedPolicyRules;
}
/** Very simple implementation, needs to be extended later. */
@NotNull
private EvaluatedPolicyRule evaluatePolicyRule(
PrismObject<ObjectCollectionType> collection,
CompiledObjectCollectionView collectionView,
@NotNull AssignmentType assignmentType,
@NotNull PolicyRuleType policyRuleType,
Task task,
OperationResult result)
throws SchemaException, ObjectNotFoundException, SecurityViolationException,
ConfigurationException, CommunicationException, ExpressionEvaluationException {
AssignmentPathImpl assignmentPath = new AssignmentPathImpl();
AssignmentPathSegmentImpl assignmentPathSegment = new AssignmentPathSegmentImpl.Builder()
.source(collection.asObjectable())
.sourceDescription("object collection " + collection)
.assignment(assignmentType)
.isAssignment()
.evaluationOrder(EvaluationOrderImpl.zero(relationRegistry))
.evaluationOrderForTarget(EvaluationOrderImpl.zero(relationRegistry))
.direct(true) // to be reconsidered - but assignment path is empty, so we consider this to be directly assigned
.pathToSourceValid(true)
.pathToSourceConditionState(ConditionState.allTrue())
.build();
assignmentPath.add(assignmentPathSegment);
// Generated proforma - actually not much needed for now.
String ruleId = PolicyRuleTypeUtil.createId(collection.getOid(), assignmentType.getId());
EvaluatedPolicyRuleImpl evaluatedPolicyRule =
new EvaluatedPolicyRuleImpl(policyRuleType.clone(), ruleId, assignmentPath, TargetType.OBJECT);
PolicyConstraintsType policyConstraints = policyRuleType.getPolicyConstraints();
if (policyConstraints == null) {
return evaluatedPolicyRule;
}
PolicyThresholdType policyThreshold = policyRuleType.getPolicyThreshold();
for (CollectionStatsPolicyConstraintType collectionStatsPolicy : policyConstraints.getCollectionStats()) {
CollectionStats stats = determineCollectionStats(collectionView, task, result);
if (isThresholdTriggered(stats, collection, policyThreshold)) {
EvaluatedPolicyRuleTrigger<?> trigger = new EvaluatedCollectionStatsTrigger(
PolicyConstraintKindType.COLLECTION_STATS, collectionStatsPolicy,
new LocalizableMessageBuilder()
.key(SchemaConstants.DEFAULT_POLICY_CONSTRAINT_KEY_PREFIX + CONSTRAINT_KEY)
.arg(ObjectTypeUtil.createDisplayInformation(collection, false))
.args(/* TODO */)
.build(),
new LocalizableMessageBuilder()
.key(SchemaConstants.DEFAULT_POLICY_CONSTRAINT_SHORT_MESSAGE_KEY_PREFIX + CONSTRAINT_KEY)
.arg(ObjectTypeUtil.createDisplayInformation(collection, false))
.args(/* TODO */)
.build());
evaluatedPolicyRule.addTrigger(trigger);
}
}
return evaluatedPolicyRule;
}
private boolean isThresholdTriggered(CollectionStats stats, PrismObject<ObjectCollectionType> collection, PolicyThresholdType policyThreshold) throws ExpressionEvaluationException {
if (policyThreshold == null) {
LOGGER.trace("Rule triggered on {} because there is no threshold specification", collection);
return true;
}
WaterMarkType highWaterMark = policyThreshold.getHighWaterMark();
if (highWaterMark != null) {
Integer waterMarkCount = highWaterMark.getCount();
if (waterMarkCount != null) {
if (stats.getObjectCount() > waterMarkCount) {
LOGGER.trace("Rule NOT triggered on {} because high watermark count exceeded (watermark: {}, actual: {})", collection, waterMarkCount, stats.getObjectCount());
return false;
}
}
Float waterMarkPercentage = highWaterMark.getPercentage();
if (waterMarkPercentage != null) {
Float percentage = stats.computePercentage();
if (percentage == null) {
throw new ExpressionEvaluationException("Cannot determine percentage of " + collection);
}
if (percentage > waterMarkPercentage) {
LOGGER.trace("Rule NOT triggered on {} because high watermark percentage exceeded (watermark: {}, actual: {})", collection, waterMarkPercentage, percentage);
return false;
}
}
}
WaterMarkType lowWaterMark = policyThreshold.getLowWaterMark();
if (lowWaterMark != null) {
Integer waterMarkCount = lowWaterMark.getCount();
if (waterMarkCount != null) {
if (stats.getObjectCount() < waterMarkCount) {
LOGGER.trace("Rule NOT triggered on {} because low watermark count not reached (watermark: {}, actual: {})", collection, waterMarkCount, stats.getObjectCount());
return false;
}
}
Float waterMarkPercentage = lowWaterMark.getPercentage();
if (waterMarkPercentage != null) {
Float percentage = stats.computePercentage();
if (percentage == null) {
throw new ExpressionEvaluationException("Cannot determine percentage of " + collection);
}
if (percentage < waterMarkPercentage) {
LOGGER.trace("Rule NOT triggered on {} because low watermark percentage not reached (watermark: {}, actual: {})", collection, waterMarkPercentage, percentage);
return false;
}
}
}
LOGGER.trace("Rule triggered on {}, thresholds reached: {}", collection, stats);
return true;
}
public <O extends ObjectType> CollectionStats determineCollectionStats(CompiledObjectCollectionView collectionView, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, SecurityViolationException, ConfigurationException, CommunicationException, ExpressionEvaluationException {
CollectionStats stats = new CollectionStats();
Class<O> targetClass = collectionView.getTargetClass(prismContext);
stats.setObjectCount(countObjects(targetClass, evaluateExpressionsInFilter(collectionView.getFilter(), result, task), collectionView.getOptions(), task, result));
stats.setDomainCount(countObjects(targetClass, evaluateExpressionsInFilter(collectionView.getDomainFilter(), result, task), collectionView.getDomainOptions(), task, result));
return stats;
}
private <O extends ObjectType> Integer countObjects(Class<O> targetTypeClass, ObjectFilter filter, Collection<SelectorOptions<GetOperationOptions>> options, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, SecurityViolationException, ConfigurationException, CommunicationException, ExpressionEvaluationException {
if (filter == null) {
return null;
}
return modelService.countObjects(targetTypeClass, prismContext.queryFactory().createQuery(filter), options, task, result);
}
public CompiledObjectCollectionView compileObjectCollectionView(CollectionRefSpecificationType collectionRef,
Class<? extends Containerable> targetTypeClass, Task task, OperationResult result)
throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException,
ExpressionEvaluationException, ObjectNotFoundException {
CompiledObjectCollectionView view = new CompiledObjectCollectionView();
compileObjectCollectionView(view, collectionRef, targetTypeClass, task, result);
return view;
}
public void compileObjectCollectionView(CompiledObjectCollectionView existingView, CollectionRefSpecificationType collectionSpec,
Class<? extends Containerable> targetTypeClass, Task task, OperationResult result)
throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException,
ExpressionEvaluationException, ObjectNotFoundException {
ObjectReferenceType collectionRef = collectionSpec.getCollectionRef();
if (collectionRef != null && collectionRef.getOid() != null && collectionSpec.getFilter() != null) {
throw new IllegalArgumentException(
"CollectionRefSpecificationType contains CollectionRef and Filter, please define only one");
}
if (collectionRef != null && collectionRef.getOid() != null) {
QName collectionRefType = collectionRef.getType();
// TODO: support more cases
if (QNameUtil.match(ArchetypeType.COMPLEX_TYPE, collectionRefType)) {
compileArchetypeCollectionView(existingView, targetTypeClass, collectionRef.getOid(), collectionSpec.getBaseCollectionRef(), task, result);
return;
}
if (QNameUtil.match(ObjectCollectionType.COMPLEX_TYPE, collectionRefType)) {
ObjectCollectionType objectCollectionType;
try {
// TODO: caching?
objectCollectionType = objectResolver.resolve(collectionRef, ObjectCollectionType.class, null, "view " + existingView.getViewIdentifier(), task, result);
} catch (ObjectNotFoundException e) {
throw new ConfigurationException(e.getMessage(), e);
}
compileObjectCollectionView(existingView, collectionSpec.getBaseCollectionRef(), objectCollectionType,
targetTypeClass, task, result);
return;
}
// TODO
throw new IllegalArgumentException("Unsupported collection type: " + collectionRefType);
} else if (collectionSpec.getFilter() != null) {
SearchFilterType filter = collectionSpec.getFilter();
CollectionRefSpecificationType baseCollectionSpec = collectionSpec.getBaseCollectionRef();
if (baseCollectionSpec == null) {
if (targetTypeClass == null) {
throw new IllegalArgumentException("UndefinedTypeForCollection type: " + collectionSpec);
}
ObjectFilter objectFilter = prismContext.getQueryConverter().parseFilter(filter, targetTypeClass);
existingView.setFilter(objectFilter);
} else {
compileBaseCollectionSpec(filter, existingView, null, baseCollectionSpec, targetTypeClass, task, result);
}
} else {
// E.g. the case of empty domain specification. Nothing to do. Just return what we have.
//noinspection UnnecessaryReturnStatement
return;
}
}
private void compileArchetypeCollectionView(CompiledObjectCollectionView existingView, Class<? extends Containerable> targetTypeClass, String collectionRefOid, CollectionRefSpecificationType baseCollectionRef, Task task, OperationResult result) throws SchemaException, ExpressionEvaluationException, CommunicationException, SecurityViolationException, ConfigurationException, ObjectNotFoundException {
RefFilter archetypeFilter = (RefFilter) prismContext.queryFor(AssignmentHolderType.class)
.item(AssignmentHolderType.F_ARCHETYPE_REF).ref(collectionRefOid)
.buildFilter();
archetypeFilter.setTargetTypeNullAsAny(true);
if (baseCollectionRef != null) {
compileBaseCollectionSpec(archetypeFilter, existingView, null, baseCollectionRef, targetTypeClass, task, result);
} else {
existingView.setFilter(archetypeFilter);
}
try {
ArchetypeType archetype = archetypeManager.getArchetype(collectionRefOid, result);
ArchetypePolicyType archetypePolicy = archetype.getArchetypePolicy();
if (archetypePolicy != null) {
DisplayType archetypeDisplay = archetypePolicy.getDisplay();
if (archetypeDisplay != null) {
DisplayType viewDisplay = existingView.getDisplay();
if (viewDisplay == null) {
viewDisplay = new DisplayType();
existingView.setDisplay(viewDisplay);
}
MiscSchemaUtil.mergeDisplay(viewDisplay, archetypeDisplay);
}
}
} catch (ObjectNotFoundException e) {
// We do not want to throw exception here. This code takes place at login time.
// We do not want to stop all logins because of missing archetype.
LOGGER.warn("Archetype {} referenced from view {} was not found", collectionRefOid, existingView.getViewIdentifier());
}
}
private Class<? extends Containerable> getContainerTypeClass(QName targetTypeQName, ObjectCollectionType objectCollectionType) throws SchemaException {
if (targetTypeQName == null) {
throw new SchemaException("Target container type not specified in " + objectCollectionType);
}
PrismContainerDefinition<Containerable> def = prismContext.getSchemaRegistry().findContainerDefinitionByType(targetTypeQName);
if (def == null) {
throw new IllegalArgumentException("Unsupported container type " + targetTypeQName);
}
return def.getTypeClass();
}
private void compileObjectCollectionView(CompiledObjectCollectionView existingView, CollectionRefSpecificationType baseCollectionSpec,
@NotNull ObjectCollectionType objectCollectionType, Class<? extends Containerable> targetTypeClass, Task task, OperationResult result)
throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException,
ExpressionEvaluationException, ObjectNotFoundException {
if (targetTypeClass == null) {
if (existingView.getContainerType() == null) {
QName targetTypeQName = objectCollectionType.getType();
targetTypeClass = getContainerTypeClass(targetTypeQName, objectCollectionType);
existingView.setContainerType(targetTypeQName);
} else {
QName targetTypeQName = existingView.getContainerType();
targetTypeClass = getContainerTypeClass(targetTypeQName, objectCollectionType);
}
}
// Used for help text in search panel
if (objectCollectionType.getDescription() != null) {
existingView.setObjectCollectionDescription(objectCollectionType.getDescription());
}
// Do this before we compile main filer. We want domain specification from the first (lowest, most specific) collection in the
// hierarchy. It makes no sense to compile all domain specs from the entire hierarchy just to throw that out.
if (!existingView.hasDomain()) {
CollectionRefSpecificationType domainSpec = objectCollectionType.getDomain();
if (domainSpec != null) {
CompiledObjectCollectionView domainView = new CompiledObjectCollectionView();
compileObjectCollectionView(domainView, domainSpec, targetTypeClass, task, result);
if (domainView.getFilter() == null) {
// We have domain specification, but compilation produced no filter. Which means that the domain is "all"
// Explicitly set "all" filter here. We want to avoid confusion between no domain spec and "all" domain spec.
existingView.setDomainFilter(prismContext.queryFactory().createAll());
} else {
existingView.setDomainFilter(domainView.getFilter());
}
existingView.setDomainOptions(domainView.getOptions());
}
}
SearchFilterType collectionFilterType = objectCollectionType.getFilter();
ObjectFilter collectionFilter;
if (collectionFilterType != null) {
collectionFilter = prismContext.getQueryConverter().parseFilter(collectionFilterType, targetTypeClass);
} else {
collectionFilter = null;
}
List<SelectorOptions<GetOperationOptions>> collectionOptions =
GetOperationOptionsUtil.optionsBeanToOptions(objectCollectionType.getGetOptions());
CollectionRefSpecificationType baseCollectionSpecFromCollection = objectCollectionType.getBaseCollection();
if (baseCollectionSpecFromCollection == null && baseCollectionSpec == null) {
existingView.setFilter(collectionFilter);
existingView.setOptions(collectionOptions);
} else {
if (baseCollectionSpecFromCollection == null || baseCollectionSpec == null) {
compileBaseCollectionSpec(collectionFilter, existingView, collectionOptions,
baseCollectionSpec == null ? baseCollectionSpecFromCollection : baseCollectionSpec, targetTypeClass, task, result);
} else {
compileObjectCollectionView(existingView, baseCollectionSpecFromCollection, targetTypeClass, task, result);
ObjectFilter baseFilterFromCollection = existingView.getFilter();
Collection<SelectorOptions<GetOperationOptions>> baseOptionFromCollection = existingView.getOptions();
compileObjectCollectionView(existingView, baseCollectionSpec, targetTypeClass, task, result);
ObjectFilter baseFilter = existingView.getFilter();
ObjectFilter combinedFilter = ObjectQueryUtil.filterAnd(baseFilterFromCollection, baseFilter, prismContext);
combinedFilter = ObjectQueryUtil.filterAnd(combinedFilter, collectionFilter, prismContext);
existingView.setFilter(combinedFilter);
GetOperationOptionsBuilder optionsBuilder = schemaService.getOperationOptionsBuilder().setFrom(baseOptionFromCollection);
optionsBuilder.mergeFrom(existingView.getOptions());
optionsBuilder.mergeFrom(collectionOptions);
existingView.setOptions(optionsBuilder.build());
}
}
compileView(existingView, objectCollectionType.getDefaultView(), false);
}
private void compileBaseCollectionSpec(ObjectFilter objectFilter, CompiledObjectCollectionView existingView, Collection<SelectorOptions<GetOperationOptions>> options,
CollectionRefSpecificationType baseCollectionRef, Class<? extends Containerable> targetTypeClass, Task task, OperationResult result)
throws CommunicationException, ObjectNotFoundException, SchemaException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
compileObjectCollectionView(existingView, baseCollectionRef, targetTypeClass, task, result);
mergeCollectionFilterAndOptions(objectFilter, existingView, options);
}
private void compileBaseCollectionSpec(SearchFilterType filter, CompiledObjectCollectionView existingView, Collection<SelectorOptions<GetOperationOptions>> options,
CollectionRefSpecificationType baseCollectionRef, Class<? extends Containerable> targetTypeClass, Task task, OperationResult result)
throws CommunicationException, ObjectNotFoundException, SchemaException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
if (targetTypeClass != null) {
ObjectFilter objectFilter = prismContext.getQueryConverter().parseFilter(filter, targetTypeClass);
compileBaseCollectionSpec(objectFilter, existingView, options, baseCollectionRef, targetTypeClass, task, result);
return;
}
compileObjectCollectionView(existingView, baseCollectionRef, targetTypeClass, task, result);
targetTypeClass = existingView.getTargetClass(prismContext);
if (targetTypeClass == null) {
throw new IllegalArgumentException("UndefinedTypeForCollection type: " + baseCollectionRef);
}
ObjectFilter objectFilter = prismContext.getQueryConverter().parseFilter(filter, targetTypeClass);
mergeCollectionFilterAndOptions(objectFilter, existingView, options);
}
private void mergeCollectionFilterAndOptions(ObjectFilter objectFilter, CompiledObjectCollectionView existingView, Collection<SelectorOptions<GetOperationOptions>> options) {
ObjectFilter baseFilter = existingView.getFilter();
ObjectFilter combinedFilter = ObjectQueryUtil.filterAnd(baseFilter, objectFilter, prismContext);
existingView.setFilter(combinedFilter);
GetOperationOptionsBuilder optionsBuilder = schemaService.getOperationOptionsBuilder().setFrom(existingView.getOptions());
optionsBuilder.mergeFrom(options);
existingView.setOptions(optionsBuilder.build());
}
public void compileView(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType) {
compileView(existingView, objectListViewType, true);
}
public void compileView(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType, Task task, OperationResult result)
throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException,
ExpressionEvaluationException, ObjectNotFoundException {
compileView(existingView, objectListViewType);
compileCollection(existingView, objectListViewType, task, result);
}
private void compileCollection(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType, Task task, OperationResult result)
throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException,
ExpressionEvaluationException, ObjectNotFoundException {
CollectionRefSpecificationType collectionSpec = objectListViewType.getCollection();
if (collectionSpec == null) {
return;
}
if (existingView.getCollection() != null) {
LOGGER.debug("Redefining collection in view {}", existingView.getViewIdentifier());
}
existingView.setCollection(collectionSpec);
compileCollection(existingView, collectionSpec, task, result);
}
private void compileCollection(CompiledObjectCollectionView existingView, CollectionRefSpecificationType collectionSpec, Task task, OperationResult result)
throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException,
ExpressionEvaluationException, ObjectNotFoundException {
QName targetObjectType = existingView.getContainerType();
Class<? extends Containerable> targetTypeClass = ObjectType.class;
if (targetObjectType != null) {
PrismContainerDefinition<? extends Containerable> containerDefinition = prismContext.getSchemaRegistry().findContainerDefinitionByType(targetObjectType);
if (containerDefinition != null) {
targetTypeClass = containerDefinition.getTypeClass();
}
}
compileObjectCollectionView(existingView, collectionSpec, targetTypeClass, task, result);
}
private void compileView(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType, boolean replaceIfExist) {
if (objectListViewType == null) {
return;
}
compileObjectType(existingView, objectListViewType);
compileActions(existingView, objectListViewType);
compileColumns(existingView, objectListViewType);
compileDisplay(existingView, objectListViewType, replaceIfExist);
compileDistinct(existingView, objectListViewType, replaceIfExist);
compileSorting(existingView, objectListViewType, replaceIfExist);
compileCounting(existingView, objectListViewType, replaceIfExist);
compileDisplayOrder(existingView, objectListViewType, replaceIfExist);
compileSearchBox(existingView, objectListViewType, replaceIfExist);
compileRefreshInterval(existingView, objectListViewType, replaceIfExist);
compilePaging(existingView, objectListViewType, replaceIfExist);
compileViewIdentifier(existingView, objectListViewType, replaceIfExist);
compileVisibility(existingView, objectListViewType);
compileApplicableForOperation(existingView, objectListViewType);
}
@Nullable
private ObjectFilter evaluateExpressionsInFilter(ObjectFilter filterRaw, OperationResult result, Task task)
throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException,
ConfigurationException, SecurityViolationException {
VariablesMap variables = new VariablesMap(); // do we want to put any variables here?
return ExpressionUtil.evaluateFilterExpressions(filterRaw, variables, MiscSchemaUtil.getExpressionProfile(),
expressionFactory, prismContext, "collection filter", task, result);
}
private void compileObjectType(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType) {
if (existingView.getContainerType() == null) {
existingView.setContainerType(objectListViewType.getType());
}
if (objectListViewType.getType() != null) {
Class<Object> existingType = prismContext.getSchemaRegistry().determineCompileTimeClass(existingView.getContainerType());
Class<Object> newType = prismContext.getSchemaRegistry().determineCompileTimeClass(objectListViewType.getType());
if (existingType != null && newType != null && existingType.isAssignableFrom(newType)) {
existingView.setContainerType(objectListViewType.getType());
}
}
}
private void compileActions(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType) {
List<GuiActionType> newActions = objectListViewType.getAction();
for (GuiActionType newAction : newActions) {
// TODO: check for action duplication/override
existingView.getActions().add(newAction); // No need to clone, CompiledObjectCollectionView is not prism
}
}
private void compileColumns(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType) {
List<GuiObjectColumnType> newColumns = objectListViewType.getColumn();
if (newColumns == null || newColumns.isEmpty()) {
return;
}
existingView.setIncludeDefaultColumns(objectListViewType.isIncludeDefaultColumns());
// Not very efficient algorithm. But must do for now.
List<GuiObjectColumnType> existingColumns = existingView.getColumns();
MiscSchemaUtil.mergeColumns(existingColumns, objectListViewType.getColumn());
List<GuiObjectColumnType> orderedList = MiscSchemaUtil.orderCustomColumns(existingColumns);
existingColumns.clear();
existingColumns.addAll(orderedList);
}
private void compileDisplay(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType, boolean replaceIfExist) {
DisplayType newDisplay = objectListViewType.getDisplay();
if (newDisplay == null) {
return;
}
if (existingView.getDisplay() == null) {
existingView.setDisplay(newDisplay);
} else if (replaceIfExist) {
MiscSchemaUtil.mergeDisplay(existingView.getDisplay(), newDisplay);
}
}
private void compileDistinct(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType, boolean replaceIfExist) {
DistinctSearchOptionType newDistinct = objectListViewType.getDistinct();
if (newDistinct == null) {
return;
}
if (existingView.getDistinct() == null || replaceIfExist) {
existingView.setDistinct(newDistinct);
}
}
private void compileSorting(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType, boolean replaceIfExist) {
Boolean newDisableSorting = objectListViewType.isDisableSorting();
if (newDisableSorting != null && (existingView.getDisableSorting() == null || replaceIfExist)) {
existingView.setDisableSorting(newDisableSorting);
}
}
private void compileRefreshInterval(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType, boolean replaceIfExist) {
Integer refreshInterval = objectListViewType.getRefreshInterval();
if (refreshInterval != null && (existingView.getRefreshInterval() == null || replaceIfExist)) {
existingView.setRefreshInterval(refreshInterval);
}
}
private void compileCounting(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType, boolean replaceIfExist) {
Boolean newDisableCounting = objectListViewType.isDisableCounting();
if (newDisableCounting != null && (existingView.isDisableCounting() == null || replaceIfExist)) {
existingView.setDisableCounting(newDisableCounting);
}
}
private void compileDisplayOrder(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType, boolean replaceIfExist) {
Integer newDisplayOrder = objectListViewType.getDisplayOrder();
if (newDisplayOrder != null && (existingView.getDisplayOrder() == null || replaceIfExist)) {
existingView.setDisplayOrder(newDisplayOrder);
}
}
private void compileSearchBox(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType, boolean replaceIfExist) {
SearchBoxConfigurationType newSearchBoxConfig = objectListViewType.getSearchBoxConfiguration();
if (newSearchBoxConfig == null) {
return;
}
SearchBoxConfigurationType oldSearchBoxConfig = existingView.getSearchBoxConfiguration();
if (oldSearchBoxConfig == null || replaceIfExist) {
if (oldSearchBoxConfig != null) {
if (newSearchBoxConfig.getDefaultObjectType() == null) {
newSearchBoxConfig.setDefaultObjectType(oldSearchBoxConfig.getDefaultObjectType());
}
if (newSearchBoxConfig.getSearchItems() == null) {
newSearchBoxConfig.setSearchItems(oldSearchBoxConfig.getSearchItems());
}
if (CollectionUtils.isEmpty(newSearchBoxConfig.getAvailableFilter())) {
newSearchBoxConfig.getAvailableFilter().addAll(oldSearchBoxConfig.getAvailableFilter());
}
if (newSearchBoxConfig.getDefaultMode() == null) {
newSearchBoxConfig.setDefaultMode(oldSearchBoxConfig.getDefaultMode());
}
if (newSearchBoxConfig.getAllowedMode().isEmpty()) {
newSearchBoxConfig.getAllowedMode().addAll(oldSearchBoxConfig.getAllowedMode());
}
if (newSearchBoxConfig.getIndirectConfiguration() == null) {
newSearchBoxConfig.setIndirectConfiguration(oldSearchBoxConfig.getIndirectConfiguration());
}
if (newSearchBoxConfig.getObjectTypeConfiguration() == null) {
newSearchBoxConfig.setObjectTypeConfiguration(oldSearchBoxConfig.getObjectTypeConfiguration());
}
if (newSearchBoxConfig.getProjectConfiguration() == null) {
newSearchBoxConfig.setProjectConfiguration(oldSearchBoxConfig.getProjectConfiguration());
}
if (newSearchBoxConfig.getRelationConfiguration() == null) {
newSearchBoxConfig.setRelationConfiguration(oldSearchBoxConfig.getRelationConfiguration());
}
if (newSearchBoxConfig.getScopeConfiguration() == null) {
newSearchBoxConfig.setScopeConfiguration(oldSearchBoxConfig.getScopeConfiguration());
}
if (newSearchBoxConfig.getTenantConfiguration() == null) {
newSearchBoxConfig.setTenantConfiguration(oldSearchBoxConfig.getTenantConfiguration());
}
}
existingView.setSearchBoxConfiguration(newSearchBoxConfig);
}
}
private void compilePaging(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType, boolean replaceIfExist) {
PagingType newPaging = objectListViewType.getPaging();
if (newPaging == null) {
return;
}
if (existingView.getPaging() == null) {
existingView.setPaging(newPaging);
} else if (replaceIfExist) {
MiscSchemaUtil.mergePaging(existingView.getPaging(), newPaging);
}
}
private void compileVisibility(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType) {
if (objectListViewType.getVisibility() != null) {
existingView.setVisibility(objectListViewType.getVisibility());
}
}
private void compileApplicableForOperation(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType) {
if (objectListViewType.getApplicableForOperation() != null) {
existingView.setApplicableForOperation(objectListViewType.getApplicableForOperation());
}
}
private void compileViewIdentifier(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType, boolean replaceIfExist) {
String identifier = determineViewIdentifier(objectListViewType);
if (identifier == null) {
return;
}
if (existingView.getViewIdentifier() == null || replaceIfExist) {
existingView.setViewIdentifier(identifier);
}
}
public String determineViewIdentifier(GuiObjectListViewType objectListViewType) {
return determineViewIdentifier(objectListViewType, ""); //todo how to get default view identifier by type? or return null
}
public String determineViewIdentifier(GuiObjectListViewType objectListViewType, String defaultViewIdentifier) {
String viewIdentifier = objectListViewType.getIdentifier();
if (viewIdentifier != null) {
return viewIdentifier;
}
CollectionRefSpecificationType collection = objectListViewType.getCollection();
if (collection != null && collection.getCollectionRef() != null) {
return collection.getCollectionRef().getOid();
}
return defaultViewIdentifier;
}
}