-
Notifications
You must be signed in to change notification settings - Fork 188
/
LensElementContext.java
859 lines (737 loc) · 32.5 KB
/
LensElementContext.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
* Copyright (c) 2010-2018 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.lens;
import java.util.*;
import java.util.function.Consumer;
import com.evolveum.midpoint.prism.ConsistencyCheckScope;
import com.evolveum.midpoint.prism.Objectable;
import com.evolveum.midpoint.prism.delta.*;
import com.evolveum.midpoint.prism.delta.builder.S_ItemEntry;
import com.evolveum.midpoint.prism.equivalence.EquivalenceStrategy;
import com.evolveum.midpoint.prism.util.ObjectDeltaObject;
import com.evolveum.midpoint.schema.DeltaConvertor;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.types_3.ChangeTypeType;
import com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import com.evolveum.midpoint.common.crypto.CryptoUtil;
import com.evolveum.midpoint.model.api.context.EvaluatedPolicyRule;
import com.evolveum.midpoint.model.api.context.EvaluatedPolicyRuleTrigger;
import com.evolveum.midpoint.model.api.context.ModelElementContext;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.schema.util.ShadowUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import org.jetbrains.annotations.NotNull;
/**
* @author semancik
*
*/
public abstract class LensElementContext<O extends ObjectType> implements ModelElementContext<O> {
private static final long serialVersionUID = 1649567559396392861L;
private static final Trace LOGGER = TraceManager.getTrace(LensElementContext.class);
private PrismObject<O> objectOld;
private transient PrismObject<O> objectCurrent;
private PrismObject<O> objectNew;
private ObjectDelta<O> primaryDelta;
@NotNull private final List<LensObjectDeltaOperation<O>> executedDeltas = new ArrayList<>();
@NotNull private final Class<O> objectTypeClass;
private String oid = null;
private int iteration;
private String iterationToken;
transient private SecurityPolicyType securityPolicy;
/**
* These are policy state modifications that should be applied.
* Currently we apply them in ChangeExecutor.executeChanges only.
*
* In the future we plan to be able to apply some state modifications even
* if the clockwork is exited in non-standard way (e.g. in primary state or with an exception).
* But we must be sure what policy state to store, because some constraints might be triggered
* because of expectation of future state (like conflicting assignment is added etc.)
* ---
* Although placed in LensElementContext, support for this data is currently implemented only for focus, not for projections.
*/
@NotNull private final List<ItemDelta<?,?>> pendingObjectPolicyStateModifications = new ArrayList<>();
/**
* Policy state modifications for assignments.
*
* Although we put here also deltas for assignments that are to be deleted, we do not execute these
* (because we implement execution only for the standard exit-path from the clockwork).
*/
@NotNull private final Map<AssignmentSpec, List<ItemDelta<?,?>>> pendingAssignmentPolicyStateModifications = new HashMap<>();
/**
* Initial intent regarding the account. It indicated what the initiator of the operation WANTS TO DO with the
* context.
* If set to null then the decision is left to "the engine". Null is also a typical value
* when the context is created. It may be pre-set under some circumstances, e.g. if an account is being unlinked.
*/
private SynchronizationIntent synchronizationIntent;
private transient boolean isFresh = false;
private LensContext<? extends ObjectType> lensContext;
private transient PrismObjectDefinition<O> objectDefinition = null;
private final Collection<EvaluatedPolicyRule> policyRules = new ArrayList<>();
private final Collection<String> policySituations = new ArrayList<>();
public LensElementContext(@NotNull Class<O> objectTypeClass, LensContext<? extends ObjectType> lensContext) {
super();
Validate.notNull(objectTypeClass, "Object class is null");
Validate.notNull(lensContext, "Lens context is null");
this.lensContext = lensContext;
this.objectTypeClass = objectTypeClass;
}
public int getIteration() {
return iteration;
}
public void setIteration(int iteration) {
this.iteration = iteration;
}
public String getIterationToken() {
return iterationToken;
}
public void setIterationToken(String iterationToken) {
this.iterationToken = iterationToken;
}
public SynchronizationIntent getSynchronizationIntent() {
return synchronizationIntent;
}
public void setSynchronizationIntent(SynchronizationIntent synchronizationIntent) {
this.synchronizationIntent = synchronizationIntent;
}
public LensContext<? extends ObjectType> getLensContext() {
return lensContext;
}
protected PrismContext getNotNullPrismContext() {
return getLensContext().getNotNullPrismContext();
}
@Override
public Class<O> getObjectTypeClass() {
return objectTypeClass;
}
public boolean represents(Class<?> type) {
return type.isAssignableFrom(objectTypeClass);
}
public PrismContext getPrismContext() {
return lensContext.getPrismContext();
}
@Override
public PrismObject<O> getObjectOld() {
return objectOld;
}
public void setObjectOld(PrismObject<O> objectOld) {
this.objectOld = objectOld;
}
@Override
public PrismObject<O> getObjectCurrent() {
return objectCurrent;
}
public void setObjectCurrent(PrismObject<O> objectCurrent) {
this.objectCurrent = objectCurrent;
}
public PrismObject<O> getObjectAny() {
if (objectNew != null) {
return objectNew;
}
if (objectCurrent != null) {
return objectCurrent;
}
return objectOld;
}
/**
* Sets current and possibly also old object. This method is used with
* freshly loaded object. The object is set as current object.
* If the old object was not initialized yet (and if it should be initialized)
* then the object is also set as old object.
*/
public void setLoadedObject(PrismObject<O> object) {
setObjectCurrent(object);
if (objectOld == null && !isAdd()) {
setObjectOld(object.clone());
}
}
@Override
public PrismObject<O> getObjectNew() {
return objectNew;
}
public void setObjectNew(PrismObject<O> objectNew) {
this.objectNew = objectNew;
}
@Override
public ObjectDelta<O> getPrimaryDelta() {
return primaryDelta;
}
public boolean hasPrimaryDelta() {
return primaryDelta != null && !primaryDelta.isEmpty();
}
/**
* As getPrimaryDelta() but caters for the possibility that an object already exists.
* So, if the primary delta is ADD and object already exists, it should be changed somehow,
* e.g. to MODIFY delta or to null.
*
* Actually, the question is what to do with the attribute values if changed to MODIFY.
* (a) Should they become REPLACE item deltas? (b) ADD ones?
* (c) Or should we compute a difference from objectCurrent to objectToAdd, hoping that
* secondary deltas will re-add everything that might be unknowingly removed by this step?
* (d) Or should we simply ignore ADD delta altogether, hoping that it was executed
* so it need not be repeated?
*
* And, should not we report AlreadyExistingException instead?
*
* It seems that (c) i.e. reverting back to objectToAdd is not a good idea at all. For example, this
* may erase linkRefs for good.
*
* For the time being let us proceed with (d), i.e. ignoring such a delta.
*
* TODO is this OK???? [med]
*
* @return
*/
public ObjectDelta<O> getFixedPrimaryDelta() {
if (primaryDelta == null || !primaryDelta.isAdd() || objectCurrent == null) {
return primaryDelta; // nothing to do
}
// Object does exist. Let's ignore the delta - see description above.
return null;
}
public void setPrimaryDelta(ObjectDelta<O> primaryDelta) {
this.primaryDelta = primaryDelta;
}
public void addPrimaryDelta(ObjectDelta<O> delta) throws SchemaException {
if (primaryDelta == null) {
primaryDelta = delta;
} else {
primaryDelta.merge(delta);
}
}
public void swallowToPrimaryDelta(ItemDelta<?,?> itemDelta) throws SchemaException {
modifyOrCreatePrimaryDelta(
delta -> delta.swallow(itemDelta),
() -> {
ObjectDelta<O> newPrimaryDelta = getPrismContext().deltaFactory().object().create(getObjectTypeClass(), ChangeType.MODIFY);
newPrimaryDelta.setOid(oid);
newPrimaryDelta.addModification(itemDelta);
return newPrimaryDelta;
});
}
@FunctionalInterface
private interface DeltaModifier<O extends Objectable> {
void modify(ObjectDelta<O> delta) throws SchemaException;
}
@FunctionalInterface
private interface DeltaCreator<O extends Objectable> {
ObjectDelta<O> create() throws SchemaException;
}
private void modifyOrCreatePrimaryDelta(DeltaModifier<O> modifier, DeltaCreator<O> creator) throws SchemaException {
if (primaryDelta == null) {
primaryDelta = creator.create();
} else if (!primaryDelta.isImmutable()) {
modifier.modify(primaryDelta);
} else {
primaryDelta = primaryDelta.clone();
modifier.modify(primaryDelta);
primaryDelta.freeze();
}
}
public abstract void swallowToSecondaryDelta(ItemDelta<?,?> itemDelta) throws SchemaException;
// TODO deduplicate with swallowToSecondaryDelta in LensFocusContext
public ObjectDelta<O> swallowToDelta(ObjectDelta<O> originalDelta, ItemDelta<?,?> propDelta) throws SchemaException {
if (originalDelta == null) {
originalDelta = getPrismContext().deltaFactory().object().create(getObjectTypeClass(), ChangeType.MODIFY);
originalDelta.setOid(getOid());
} else if (originalDelta.containsModification(propDelta, EquivalenceStrategy.LITERAL_IGNORE_METADATA)) { // todo why literal?
return originalDelta;
}
originalDelta.swallow(propDelta);
return originalDelta;
}
/**
* Returns collection of all deltas that this element context contains.
* This is a nice method to use if we want to inspect all deltas (e.g. look for a changed item)
* but we want to avoid the overhead of merging all the deltas together.
*/
public abstract Collection<ObjectDelta<O>> getAllDeltas();
@NotNull
public List<ItemDelta<?, ?>> getPendingObjectPolicyStateModifications() {
return pendingObjectPolicyStateModifications;
}
public void clearPendingObjectPolicyStateModifications() {
pendingObjectPolicyStateModifications.clear();
}
public void addToPendingObjectPolicyStateModifications(ItemDelta<?, ?> modification) {
pendingObjectPolicyStateModifications.add(modification);
}
@NotNull
public Map<AssignmentSpec, List<ItemDelta<?, ?>>> getPendingAssignmentPolicyStateModifications() {
return pendingAssignmentPolicyStateModifications;
}
public void clearPendingAssignmentPolicyStateModifications() {
pendingAssignmentPolicyStateModifications.clear();
}
public void addToPendingAssignmentPolicyStateModifications(@NotNull AssignmentType assignment, @NotNull PlusMinusZero mode, @NotNull ItemDelta<?, ?> modification) {
AssignmentSpec spec = new AssignmentSpec(assignment, mode);
pendingAssignmentPolicyStateModifications.computeIfAbsent(spec, k -> new ArrayList<>()).add(modification);
}
public abstract boolean isAdd();
public boolean isModify() {
// TODO I'm not sure why isModify checks both primary and secondary deltas for focus context, while
// isAdd and isDelete care only for the primary delta.
return ObjectDelta.isModify(getPrimaryDelta()) || ObjectDelta.isModify(getSecondaryDelta());
}
public abstract boolean isDelete();
@NotNull
public SimpleOperationName getOperation() {
if (isAdd()) {
return SimpleOperationName.ADD;
} else if (isDelete()) {
return SimpleOperationName.DELETE;
} else {
return SimpleOperationName.MODIFY;
}
}
@NotNull
@Override
public List<LensObjectDeltaOperation<O>> getExecutedDeltas() {
return executedDeltas;
}
List<LensObjectDeltaOperation<O>> getExecutedDeltas(Boolean audited) {
if (audited == null) {
return executedDeltas;
}
List<LensObjectDeltaOperation<O>> deltas = new ArrayList<>();
for (LensObjectDeltaOperation<O> delta: executedDeltas) {
if (delta.isAudited() == audited) {
deltas.add(delta);
}
}
return deltas;
}
public void markExecutedDeltasAudited() {
for(LensObjectDeltaOperation<O> executedDelta: executedDeltas) {
executedDelta.setAudited(true);
}
}
public void addToExecutedDeltas(LensObjectDeltaOperation<O> executedDelta) {
executedDeltas.add(executedDelta.clone()); // must be cloned because e.g. for ADD deltas the object gets modified afterwards
}
/**
* Returns user delta, both primary and secondary (merged together).
* The returned object is (kind of) immutable. Changing it may do strange things (but most likely the changes will be lost).
*/
public ObjectDelta<O> getDelta() throws SchemaException {
return ObjectDeltaCollectionsUtil.union(primaryDelta, getSecondaryDelta());
}
public ObjectDelta<O> getFixedDelta() throws SchemaException {
return ObjectDeltaCollectionsUtil.union(getFixedPrimaryDelta(), getSecondaryDelta());
}
public <F extends FocusType> boolean wasAddExecuted() {
for (LensObjectDeltaOperation<O> executedDeltaOperation : getExecutedDeltas()){
ObjectDelta<O> executedDelta = executedDeltaOperation.getObjectDelta();
if (!executedDelta.isAdd()){
continue;
} else if (executedDelta.getObjectToAdd() != null && executedDelta.getObjectTypeClass().equals(getObjectTypeClass())){
return true;
}
}
return false;
}
abstract public ObjectDeltaObject<O> getObjectDeltaObject() throws SchemaException;
@Override
public String getOid() {
if (oid == null) {
oid = determineOid();
}
return oid;
}
public String determineOid() {
if (getObjectOld() != null && getObjectOld().getOid() != null) {
return getObjectOld().getOid();
}
if (getObjectCurrent() != null && getObjectCurrent().getOid() != null) {
return getObjectCurrent().getOid();
}
if (getObjectNew() != null && getObjectNew().getOid() != null) {
return getObjectNew().getOid();
}
if (getPrimaryDelta() != null && getPrimaryDelta().getOid() != null) {
return getPrimaryDelta().getOid();
}
if (getSecondaryDelta() != null && getSecondaryDelta().getOid() != null) {
return getSecondaryDelta().getOid();
}
return null;
}
/**
* Sets oid to the field but also to the deltas (if applicable).
*/
public void setOid(String oid) {
this.oid = oid;
if (primaryDelta != null && !primaryDelta.isImmutable()) {
primaryDelta.setOid(oid);
}
// TODO What if primary delta is immutable ADD delta and objectNew was taken from it?
// It would be immutable as well in that case. We will see.
if (objectNew != null) {
objectNew.setOid(oid);
}
}
public PrismObjectDefinition<O> getObjectDefinition() {
if (objectDefinition == null) {
if (objectOld != null) {
objectDefinition = objectOld.getDefinition();
} else if (objectCurrent != null) {
objectDefinition = objectCurrent.getDefinition();
} else if (objectNew != null) {
objectDefinition = objectNew.getDefinition();
} else {
objectDefinition = getNotNullPrismContext().getSchemaRegistry().findObjectDefinitionByCompileTimeClass(getObjectTypeClass());
}
}
return objectDefinition;
}
public boolean isFresh() {
return isFresh;
}
public void setFresh(boolean isFresh) {
this.isFresh = isFresh;
}
@NotNull
public Collection<EvaluatedPolicyRule> getPolicyRules() {
return policyRules;
}
public void addPolicyRule(EvaluatedPolicyRule policyRule) {
this.policyRules.add(policyRule);
}
public void clearPolicyRules() {
policyRules.clear();
}
public void triggerRule(@NotNull EvaluatedPolicyRule rule, Collection<EvaluatedPolicyRuleTrigger<?>> triggers) {
LensUtil.triggerRule(rule, triggers, policySituations);
}
@NotNull
public Collection<String> getPolicySituations() {
return policySituations;
}
/**
* Returns security policy applicable to the object. This means security policy
* applicable directory to focus or projection. It will NOT return global
* security policy.
*/
public SecurityPolicyType getSecurityPolicy() {
return securityPolicy;
}
public void setSecurityPolicy(SecurityPolicyType securityPolicy) {
this.securityPolicy = securityPolicy;
}
public CredentialsPolicyType getCredentialsPolicy() {
return securityPolicy != null ? securityPolicy.getCredentials() : null;
}
public void recompute() throws SchemaException, ConfigurationException {
PrismObject<O> base = getObjectCurrentOrOld();
ObjectDelta<O> delta = getDelta();
if (delta == null) {
// No change
objectNew = base;
return;
}
objectNew = delta.computeChangedObject(base);
}
public void checkConsistence() {
checkConsistence(null);
}
public void checkConsistence(String contextDesc) {
if (getObjectOld() != null) {
checkConsistence(getObjectOld(), "old "+getElementDesc() , contextDesc);
}
if (getObjectCurrent() != null) {
checkConsistence(getObjectCurrent(), "current "+getElementDesc() , contextDesc);
}
if (primaryDelta != null) {
checkConsistence(primaryDelta, false, getElementDesc()+" primary delta in "+this + (contextDesc == null ? "" : " in " +contextDesc));
}
if (getObjectNew() != null) {
checkConsistence(getObjectNew(), "new "+getElementDesc(), contextDesc);
}
}
protected void checkConsistence(ObjectDelta<O> delta, boolean requireOid, String contextDesc) {
try {
delta.checkConsistence(requireOid, true, true, ConsistencyCheckScope.THOROUGH);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(e.getMessage()+"; in "+contextDesc, e);
} catch (IllegalStateException e) {
throw new IllegalStateException(e.getMessage()+"; in "+contextDesc, e);
}
if (delta.isAdd()) {
checkConsistence(delta.getObjectToAdd(), "add object", contextDesc);
}
}
protected boolean isRequireSecondaryDeltaOid() {
return primaryDelta == null;
}
protected void checkConsistence(PrismObject<O> object, String elementDesc, String contextDesc) {
String desc = elementDesc+" in "+this + (contextDesc == null ? "" : " in " +contextDesc);
try {
object.checkConsistence(true, ConsistencyCheckScope.THOROUGH);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(e.getMessage()+"; in "+desc, e);
} catch (IllegalStateException e) {
throw new IllegalStateException(e.getMessage()+"; in "+desc, e);
}
if (object.getDefinition() == null) {
throw new IllegalStateException("No "+getElementDesc()+" definition "+desc);
}
O objectType = object.asObjectable();
if (objectType instanceof ShadowType) {
ShadowUtil.checkConsistence((PrismObject<? extends ShadowType>) object, desc);
}
}
/**
* Cleans up the contexts by removing some of the working state.
*/
public abstract void cleanup();
public void normalize() {
if (objectNew != null) {
objectNew.normalize();
}
if (objectOld != null) {
objectOld.normalize();
}
if (objectCurrent != null) {
objectCurrent.normalize();
}
if (primaryDelta != null && !primaryDelta.isImmutable()) {
primaryDelta.normalize();
}
}
public void adopt(PrismContext prismContext) throws SchemaException {
if (objectNew != null) {
prismContext.adopt(objectNew);
}
if (objectOld != null) {
prismContext.adopt(objectOld);
}
if (objectCurrent != null) {
prismContext.adopt(objectCurrent);
}
if (primaryDelta != null) {
prismContext.adopt(primaryDelta);
}
// TODO: object definition?
}
public abstract LensElementContext<O> clone(LensContext<? extends ObjectType> lensContext);
void copyValues(LensElementContext<O> clone, LensContext lensContext) {
//noinspection unchecked
clone.lensContext = lensContext;
// This is de-facto immutable
clone.objectDefinition = this.objectDefinition;
clone.objectNew = cloneObject(this.objectNew);
clone.objectOld = cloneObject(this.objectOld);
clone.objectCurrent = cloneObject(this.objectCurrent);
clone.oid = this.oid;
clone.primaryDelta = cloneDelta(this.primaryDelta);
clone.isFresh = this.isFresh;
clone.iteration = this.iteration;
clone.iterationToken = this.iterationToken;
clone.securityPolicy = this.securityPolicy;
}
protected ObjectDelta<O> cloneDelta(ObjectDelta<O> thisDelta) {
if (thisDelta == null) {
return null;
}
return thisDelta.clone();
}
private PrismObject<O> cloneObject(PrismObject<O> thisObject) {
if (thisObject == null) {
return null;
}
return thisObject.clone();
}
void storeIntoLensElementContextType(LensElementContextType lensElementContextType, LensContext.ExportType exportType) throws SchemaException {
if (objectOld != null && exportType != LensContext.ExportType.MINIMAL) {
if (exportType == LensContext.ExportType.REDUCED) {
lensElementContextType.setObjectOldRef(ObjectTypeUtil.createObjectRef(objectOld, getPrismContext()));
} else {
lensElementContextType.setObjectOld(objectOld.asObjectable().clone());
}
}
if (objectCurrent != null && exportType == LensContext.ExportType.TRACE) {
lensElementContextType.setObjectCurrent(objectCurrent.asObjectable().clone());
}
if (objectNew != null && exportType != LensContext.ExportType.MINIMAL) {
if (exportType == LensContext.ExportType.REDUCED) {
lensElementContextType.setObjectNewRef(ObjectTypeUtil.createObjectRef(objectNew, getPrismContext()));
} else {
lensElementContextType.setObjectNew(objectNew.asObjectable().clone());
}
}
if (exportType != LensContext.ExportType.MINIMAL) {
lensElementContextType.setPrimaryDelta(primaryDelta != null ? DeltaConvertor.toObjectDeltaType(primaryDelta) : null);
for (LensObjectDeltaOperation<?> executedDelta : executedDeltas) {
lensElementContextType.getExecutedDeltas()
.add(LensContext.simplifyExecutedDelta(executedDelta).toLensObjectDeltaOperationType());
}
lensElementContextType.setObjectTypeClass(objectTypeClass != null ? objectTypeClass.getName() : null);
lensElementContextType.setOid(oid);
lensElementContextType.setIteration(iteration);
lensElementContextType.setIterationToken(iterationToken);
lensElementContextType.setSynchronizationIntent(
synchronizationIntent != null ? synchronizationIntent.toSynchronizationIntentType() : null);
}
lensElementContextType.setFresh(isFresh);
}
public void retrieveFromLensElementContextType(LensElementContextType lensElementContextType, Task task, OperationResult result) throws SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, ExpressionEvaluationException {
ObjectType objectTypeOld = lensElementContextType.getObjectOld();
this.objectOld = objectTypeOld != null ? (PrismObject) objectTypeOld.asPrismObject() : null;
fixProvisioningTypeInObject(this.objectOld, task, result);
ObjectType objectTypeNew = lensElementContextType.getObjectNew();
this.objectNew = objectTypeNew != null ? (PrismObject) objectTypeNew.asPrismObject() : null;
fixProvisioningTypeInObject(this.objectNew, task, result);
ObjectType object = objectTypeNew != null ? objectTypeNew : objectTypeOld;
ObjectDeltaType primaryDeltaType = lensElementContextType.getPrimaryDelta();
this.primaryDelta = primaryDeltaType != null ? (ObjectDelta) DeltaConvertor.createObjectDelta(primaryDeltaType, lensContext.getPrismContext()) : null;
fixProvisioningTypeInDelta(this.primaryDelta, object, task, result);
for (LensObjectDeltaOperationType eDeltaOperationType : lensElementContextType.getExecutedDeltas()) {
LensObjectDeltaOperation objectDeltaOperation = LensObjectDeltaOperation.fromLensObjectDeltaOperationType(eDeltaOperationType, lensContext.getPrismContext());
if (objectDeltaOperation.getObjectDelta() != null) {
fixProvisioningTypeInDelta(objectDeltaOperation.getObjectDelta(), object, task, result);
}
this.executedDeltas.add(objectDeltaOperation);
}
this.oid = lensElementContextType.getOid();
this.iteration = lensElementContextType.getIteration() != null ? lensElementContextType.getIteration() : 0;
this.iterationToken = lensElementContextType.getIterationToken();
this.synchronizationIntent = SynchronizationIntent.fromSynchronizationIntentType(lensElementContextType.getSynchronizationIntent());
// note: objectTypeClass is already converted (used in the constructor)
}
protected void fixProvisioningTypeInDelta(ObjectDelta<O> delta, Objectable object, Task task, OperationResult result) {
if (delta != null && delta.getObjectTypeClass() != null && (ShadowType.class.isAssignableFrom(delta.getObjectTypeClass()) || ResourceType.class.isAssignableFrom(delta.getObjectTypeClass()))) {
try {
lensContext.getProvisioningService().applyDefinition(delta, object, task, result);
} catch (Exception e) {
LOGGER.warn("Error applying provisioning definitions to delta {}: {}", delta, e.getMessage());
// In case of error just go on. Maybe we do not have correct definition here. But at least we can
// display the GUI pages and maybe we can also salvage the operation.
result.recordWarning(e);
}
}
}
private void fixProvisioningTypeInObject(PrismObject<O> object, Task task, OperationResult result) {
if (object != null && object.getCompileTimeClass() != null && (ShadowType.class.isAssignableFrom(object.getCompileTimeClass()) || ResourceType.class.isAssignableFrom(object.getCompileTimeClass()))) {
try {
lensContext.getProvisioningService().applyDefinition(object, task, result);
} catch (Exception e) {
LOGGER.warn("Error applying provisioning definitions to object {}: {}", object, e.getMessage());
// In case of error just go on. Maybe we do not have correct definition here. But at least we can
// display the GUI pages and maybe we can also salvage the operation.
result.recordWarning(e);
}
}
}
public void checkEncrypted() {
if (objectNew != null) {
CryptoUtil.checkEncrypted(objectNew);
}
if (objectOld != null) {
CryptoUtil.checkEncrypted(objectOld);
}
if (objectCurrent != null) {
CryptoUtil.checkEncrypted(objectCurrent);
}
if (primaryDelta != null) {
CryptoUtil.checkEncrypted(primaryDelta);
}
}
public boolean operationMatches(ChangeTypeType operation) {
switch (operation) {
case ADD:
return getOperation() == SimpleOperationName.ADD;
case MODIFY:
return getOperation() == SimpleOperationName.MODIFY;
case DELETE:
return getOperation() == SimpleOperationName.DELETE;
}
throw new IllegalArgumentException("Unknown operation "+operation);
}
protected abstract String getElementDefaultDesc();
protected String getElementDesc() {
PrismObject<O> object = getObjectNew();
if (object == null) {
object = getObjectOld();
}
if (object == null) {
object = getObjectCurrent();
}
if (object == null) {
return getElementDefaultDesc();
}
return object.toDebugType();
}
protected String getDebugDumpTitle() {
return StringUtils.capitalize(getElementDesc());
}
protected String getDebugDumpTitle(String suffix) {
return getDebugDumpTitle()+" "+suffix;
}
public abstract String getHumanReadableName();
public String getObjectReadVersion() {
// Do NOT use version from object current.
// Current object may be re-read, but the computation
// might be based on older data (objectOld).
// if (getObjectCurrent() != null) {
// return getObjectCurrent().getVersion();
// }
if (getObjectOld() != null) {
return getObjectOld().getVersion();
}
return null;
}
public PrismObject<O> getObjectCurrentOrOld() {
return objectCurrent != null ? objectCurrent : objectOld;
}
@Override
public boolean isOfType(Class<?> aClass) {
if (objectTypeClass != null && aClass.isAssignableFrom(objectTypeClass)) {
return true;
}
PrismObject<O> object = getObjectAny();
return object != null && aClass.isAssignableFrom(object.asObjectable().getClass());
}
public abstract void deleteSecondaryDeltas();
public S_ItemEntry deltaBuilder() throws SchemaException {
return getPrismContext().deltaFor(getObjectTypeClass());
}
public void forEachObject(Consumer<PrismObject<O>> consumer) {
if (objectCurrent != null) {
consumer.accept(objectCurrent);
}
if (objectOld != null) {
consumer.accept(objectOld);
}
if (objectNew != null) {
consumer.accept(objectNew);
}
}
public void forEachDelta(Consumer<ObjectDelta<O>> consumer) {
if (primaryDelta != null) {
consumer.accept(primaryDelta);
}
}
public void finishBuild() {
if (primaryDelta != null) {
primaryDelta.normalize();
primaryDelta.freeze();
}
}
}