-
Notifications
You must be signed in to change notification settings - Fork 188
/
ObjectDelta.java
1417 lines (1251 loc) · 59.4 KB
/
ObjectDelta.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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010-2014 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.delta;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.path.ItemPath.CompareResult;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.util.PrismAsserts;
import com.evolveum.midpoint.prism.util.PrismUtil;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import javax.xml.namespace.QName;
import org.apache.commons.lang.Validate;
import java.io.Serializable;
import java.util.*;
/**
* Relative difference (delta) of the object.
* <p/>
* This class describes how the object changes. It can describe either object addition, modification of deletion.
* <p/>
* Addition described complete new (absolute) state of the object.
* <p/>
* Modification contains a set property deltas that describe relative changes to individual properties
* <p/>
* Deletion does not contain anything. It only marks object for deletion.
* <p/>
* The OID is mandatory for modification and deletion.
*
* @author Radovan Semancik
* @see PropertyDelta
*/
public class ObjectDelta<T extends Objectable> implements DebugDumpable, Visitable, PathVisitable, Serializable {
private static final long serialVersionUID = -528560467958335366L;
private ChangeType changeType;
/**
* OID of the object that this delta applies to.
*/
private String oid;
/**
* New object to add. Valid only if changeType==ADD
*/
private PrismObject<T> objectToAdd;
/**
* Set of relative property deltas. Valid only if changeType==MODIFY
*/
private Collection<? extends ItemDelta<?,?>> modifications;
/**
* Class of the object that we describe.
*/
private Class<T> objectTypeClass;
transient private PrismContext prismContext;
public ObjectDelta(Class<T> objectTypeClass, ChangeType changeType, PrismContext prismContext) {
Validate.notNull(objectTypeClass,"No objectTypeClass");
Validate.notNull(changeType,"No changeType");
//Validate.notNull(prismContext, "No prismContext");
this.changeType = changeType;
this.objectTypeClass = objectTypeClass;
this.prismContext = prismContext;
objectToAdd = null;
modifications = createEmptyModifications();
}
@Override
public void accept(Visitor visitor){
visitor.visit(this);
if (isAdd()) {
objectToAdd.accept(visitor);
} else if (isModify()) {
for (ItemDelta<?,?> delta : getModifications()){
delta.accept(visitor);
}
}
// Nothing to visit for delete
}
@Override
public void accept(Visitor visitor, ItemPath path, boolean recursive) {
if (path == null || path.isEmpty()) {
if (recursive) {
accept(visitor);
} else {
visitor.visit(this);
}
} else {
ItemDelta.accept(getModifications(), visitor, path, recursive);
}
}
public ChangeType getChangeType() {
return changeType;
}
public void setChangeType(ChangeType changeType) {
this.changeType = changeType;
}
public static boolean isAdd(ObjectDelta<?> objectDelta) {
if (objectDelta == null) {
return false;
}
return objectDelta.isAdd();
}
public boolean isAdd() {
return changeType == ChangeType.ADD;
}
public static boolean isDelete(ObjectDelta<?> objectDelta) {
if (objectDelta == null) {
return false;
}
return objectDelta.isDelete();
}
public boolean isDelete() {
return changeType == ChangeType.DELETE;
}
public static boolean isModify(ObjectDelta<?> objectDelta) {
if (objectDelta == null) {
return false;
}
return objectDelta.isModify();
}
public boolean isModify() {
return changeType == ChangeType.MODIFY;
}
public String getOid() {
return oid;
}
public void setOid(String oid) {
this.oid = oid;
if (objectToAdd != null) {
objectToAdd.setOid(oid);
}
}
public PrismContext getPrismContext() {
return prismContext;
}
public void setPrismContext(PrismContext prismContext) {
this.prismContext = prismContext;
}
public PrismObject<T> getObjectToAdd() {
return objectToAdd;
}
public void setObjectToAdd(PrismObject<T> objectToAdd) {
if (getChangeType() != ChangeType.ADD) {
throw new IllegalStateException("Cannot set object to "+getChangeType()+" delta");
}
this.objectToAdd = objectToAdd;
if (objectToAdd != null) {
this.objectTypeClass = objectToAdd.getCompileTimeClass();
}
}
public Collection<? extends ItemDelta<?,?>> getModifications() {
return modifications;
}
/**
* Adds modification (itemDelta) and returns the modification that was added.
* NOTE: the modification that was added may be different from the modification that was
* passed into this method! E.g. in case if two modifications must be merged to keep the delta
* consistent. Therefore always use the returned modification after this method is invoked.
*/
public <D extends ItemDelta> D addModification(D itemDelta) {
if (getChangeType() != ChangeType.MODIFY) {
throw new IllegalStateException("Cannot add modifications to "+getChangeType()+" delta");
}
ItemPath itemPath = itemDelta.getPath();
D existingModification = (D) findModification(itemPath, itemDelta.getClass());
if (existingModification != null) {
existingModification.merge(itemDelta);
return existingModification;
} else {
((Collection)modifications).add(itemDelta);
return itemDelta;
}
}
public boolean containsModification(ItemDelta itemDelta) {
for (ItemDelta modification: modifications) {
if (modification.contains(itemDelta)) {
return true;
}
}
return false;
}
public void addModifications(Collection<? extends ItemDelta> itemDeltas) {
for (ItemDelta<?,?> modDelta: itemDeltas) {
addModification(modDelta);
}
}
public void addModifications(ItemDelta<?,?>... itemDeltas) {
for (ItemDelta<?,?> modDelta: itemDeltas) {
addModification(modDelta);
}
}
public <IV extends PrismValue,ID extends ItemDefinition> ItemDelta<IV,ID> findItemDelta(ItemPath propertyPath) {
return findItemDelta(propertyPath, ItemDelta.class, Item.class);
}
private <IV extends PrismValue,ID extends ItemDefinition, I extends Item<IV,ID>,DD extends ItemDelta<IV,ID>>
DD findItemDelta(ItemPath propertyPath, Class<DD> deltaType, Class<I> itemType) {
if (changeType == ChangeType.ADD) {
I item = objectToAdd.findItem(propertyPath, itemType);
if (item == null) {
return null;
}
DD itemDelta = createEmptyDelta(propertyPath, item.getDefinition(), deltaType, item.getClass());
itemDelta.addValuesToAdd(item.getClonedValues());
return itemDelta;
} else if (changeType == ChangeType.MODIFY) {
return findModification(propertyPath, deltaType);
} else {
return null;
}
}
public <IV extends PrismValue,ID extends ItemDefinition> Collection<PartiallyResolvedDelta<IV,ID>> findPartial(ItemPath propertyPath) {
if (changeType == ChangeType.ADD) {
PartiallyResolvedItem<IV,ID> partialValue = objectToAdd.findPartial(propertyPath);
if (partialValue == null || partialValue.getItem() == null) {
return new ArrayList<>(0);
}
Item<IV,ID> item = partialValue.getItem();
ItemDelta<IV,ID> itemDelta = item.createDelta();
itemDelta.addValuesToAdd(item.getClonedValues());
Collection<PartiallyResolvedDelta<IV,ID>> deltas = new ArrayList<>(1);
deltas.add(new PartiallyResolvedDelta<IV,ID>(itemDelta, partialValue.getResidualPath()));
return deltas;
} else if (changeType == ChangeType.MODIFY) {
Collection<PartiallyResolvedDelta<IV,ID>> deltas = new ArrayList<>();
for (ItemDelta<?,?> modification: modifications) {
CompareResult compareComplex = modification.getPath().compareComplex(propertyPath);
if (compareComplex == CompareResult.EQUIVALENT) {
deltas.add(new PartiallyResolvedDelta<IV,ID>((ItemDelta<IV,ID>)modification, null));
} else if (compareComplex == CompareResult.SUBPATH) {
deltas.add(new PartiallyResolvedDelta<IV,ID>((ItemDelta<IV,ID>)modification, null));
} else if (compareComplex == CompareResult.SUPERPATH) {
deltas.add(new PartiallyResolvedDelta<IV,ID>((ItemDelta<IV,ID>)modification,
modification.getPath().remainder(propertyPath)));
}
}
return deltas;
} else {
return new ArrayList<>(0);
}
}
public boolean hasItemDelta(ItemPath propertyPath) {
if (changeType == ChangeType.ADD) {
Item item = objectToAdd.findItem(propertyPath, Item.class);
return item != null;
} else if (changeType == ChangeType.MODIFY) {
ItemDelta modification = findModification(propertyPath, ItemDelta.class);
return modification != null;
} else {
return false;
}
}
public boolean hasCompleteDefinition() {
if (isAdd()) {
return getObjectToAdd().hasCompleteDefinition();
} else if (isModify()) {
for (ItemDelta modification: getModifications()) {
if (!modification.hasCompleteDefinition()) {
return false;
}
// return true;
}
return true;
} else if (isDelete()) {
return true;
}
throw new IllegalStateException("Strange things happen");
}
private <D extends ItemDelta, I extends Item> D createEmptyDelta(ItemPath propertyPath, ItemDefinition itemDef,
Class<D> deltaType, Class<I> itemType) {
if (PrismProperty.class.isAssignableFrom(itemType)) {
return (D) new PropertyDelta(propertyPath, (PrismPropertyDefinition)itemDef, prismContext);
} else if (PrismContainer.class.isAssignableFrom(itemType)) {
return (D) new ContainerDelta(propertyPath, (PrismContainerDefinition)itemDef, prismContext);
} else if (PrismReference.class.isAssignableFrom(itemType)) {
return (D) new ReferenceDelta(propertyPath, (PrismReferenceDefinition)itemDef, prismContext);
} else {
throw new IllegalArgumentException("Unknown item type "+itemType);
}
}
public Class<T> getObjectTypeClass() {
return objectTypeClass;
}
public void setObjectTypeClass(Class<T> objectTypeClass) {
this.objectTypeClass = objectTypeClass;
}
/**
* Top-level path is assumed.
*/
public <X> PropertyDelta<X> findPropertyDelta(QName propertyName) {
return findPropertyDelta(new ItemPath(propertyName));
}
public <X> PropertyDelta<X> findPropertyDelta(ItemPath parentPath, QName propertyName) {
return findPropertyDelta(new ItemPath(parentPath, propertyName));
}
@SuppressWarnings("unchecked")
public <X> PropertyDelta<X> findPropertyDelta(ItemPath propertyPath) {
return findItemDelta(propertyPath, PropertyDelta.class, PrismProperty.class);
}
@SuppressWarnings("unchecked")
public <X extends Containerable> ContainerDelta<X> findContainerDelta(ItemPath propertyPath) {
return findItemDelta(propertyPath, ContainerDelta.class, PrismContainer.class);
}
public <X extends Containerable> ContainerDelta<X> findContainerDelta(QName name) {
return findContainerDelta(new ItemPath(name));
}
private <D extends ItemDelta> D findModification(ItemPath propertyPath, Class<D> deltaType) {
return ItemDelta.findItemDelta(modifications, propertyPath, deltaType);
}
private <D extends ItemDelta> D findModification(QName itemName, Class<D> deltaType) {
return findModification(new ItemPath(itemName), deltaType);
}
public ReferenceDelta findReferenceModification(QName itemName) {
return findModification(itemName, ReferenceDelta.class);
}
public ReferenceDelta findReferenceModification(ItemPath itemPath) {
return findModification(itemPath, ReferenceDelta.class);
}
/**
* Returns all item deltas at or below a specified path.
*/
public Collection<? extends ItemDelta<?,?>> findItemDeltasSubPath(ItemPath itemPath) {
return ItemDelta.findItemDeltasSubPath(modifications, itemPath);
}
private <D extends ItemDelta> void removeModification(ItemPath propertyPath, Class<D> deltaType) {
ItemDelta.removeItemDelta(modifications, propertyPath, deltaType);
}
private <D extends ItemDelta> void removeModification(QName itemName, Class<D> deltaType) {
removeModification(new ItemPath(itemName), deltaType);
}
public void removeReferenceModification(QName itemName) {
removeModification(itemName, ReferenceDelta.class);
}
public void removeContainerModification(QName itemName) {
removeModification(itemName, ContainerDelta.class);
}
public void removePropertyModification(QName itemName) {
removeModification(itemName, PropertyDelta.class);
}
public boolean isEmpty() {
if (getChangeType() == ChangeType.DELETE) {
// Delete delta is never empty
return false;
}
if (getChangeType() == ChangeType.ADD) {
return objectToAdd == null || objectToAdd.isEmpty();
}
if (modifications == null || modifications.isEmpty()) {
return true;
}
for (ItemDelta<?,?> mod: modifications) {
if (!mod.isEmpty()) {
return false;
}
}
return true;
}
public void normalize() {
if (objectToAdd != null) {
objectToAdd.normalize();
}
if (modifications != null) {
Iterator<? extends ItemDelta> iterator = modifications.iterator();
while (iterator.hasNext()) {
ItemDelta<?,?> modification = iterator.next();
modification.normalize();
if (modification.isEmpty()) {
iterator.remove();
}
}
}
}
public void applyDefinition(PrismObjectDefinition<T> definition) throws SchemaException {
if (objectToAdd != null) {
objectToAdd.applyDefinition(definition);
}
ItemDelta.applyDefinition(getModifications(), definition);
}
/**
* Deep clone.
*/
public ObjectDelta<T> clone() {
ObjectDelta<T> clone = new ObjectDelta<T>(this.objectTypeClass, this.changeType, this.prismContext);
clone.oid = this.oid;
clone.modifications = createEmptyModifications();
for (ItemDelta<?,?> thisModification: this.modifications) {
((Collection)clone.modifications).add(thisModification.clone());
}
if (this.objectToAdd == null) {
clone.objectToAdd = null;
} else {
clone.objectToAdd = this.objectToAdd.clone();
}
return clone;
}
/**
* Merge provided delta into this delta.
* This delta is assumed to be chronologically earlier, delta in the parameter is assumed to come chronologicaly later.
*/
public void merge(ObjectDelta<T> deltaToMerge) throws SchemaException {
if (deltaToMerge == null) {
return;
}
if (changeType == ChangeType.ADD) {
if (deltaToMerge.changeType == ChangeType.ADD) {
// Maybe we can, be we do not want. This is usually an error anyway.
throw new IllegalArgumentException("Cannot merge two ADD deltas: " + this + ", " + deltaToMerge);
} else if (deltaToMerge.changeType == ChangeType.MODIFY) {
if (objectToAdd == null) {
throw new IllegalStateException("objectToAdd is null");
}
deltaToMerge.applyTo(objectToAdd);
} else if (deltaToMerge.changeType == ChangeType.DELETE) {
this.changeType = ChangeType.DELETE;
}
} else if (changeType == ChangeType.MODIFY) {
if (deltaToMerge.changeType == ChangeType.ADD) {
throw new IllegalArgumentException("Cannot merge 'add' delta to a 'modify' object delta");
} else if (deltaToMerge.changeType == ChangeType.MODIFY) {
mergeModifications(deltaToMerge.modifications);
} else if (deltaToMerge.changeType == ChangeType.DELETE) {
this.changeType = ChangeType.DELETE;
}
} else { // DELETE
if (deltaToMerge.changeType == ChangeType.ADD) {
this.changeType = ChangeType.ADD;
// TODO: clone?
this.objectToAdd = deltaToMerge.objectToAdd;
} else if (deltaToMerge.changeType == ChangeType.MODIFY) {
// Just ignore the modification of a deleted object
} else if (deltaToMerge.changeType == ChangeType.DELETE) {
// Nothing to do
}
}
}
/**
* Returns a delta that is a "sum" of all the deltas in the collection.
* The deltas as processed as an ORDERED sequence. Therefore it correctly processes item overwrites and so on.
* It also means that if there is an ADD delta it has to be first.
*/
public static <T extends Objectable> ObjectDelta<T> summarize(ObjectDelta<T>... deltas) throws SchemaException {
return summarize(Arrays.asList(deltas));
}
/**
* Returns a delta that is a "sum" of all the deltas in the collection.
* The deltas as processed as an ORDERED sequence. Therefore it correctly processes item overwrites and so on.
* It also means that if there is an ADD delta it has to be first.
*/
public static <T extends Objectable> ObjectDelta<T> summarize(List<ObjectDelta<T>> deltas) throws SchemaException {
if (deltas == null || deltas.isEmpty()) {
return null;
}
Iterator<ObjectDelta<T>> iterator = deltas.iterator();
ObjectDelta<T> sumDelta = iterator.next().clone();
while (iterator.hasNext()) {
ObjectDelta<T> nextDelta = iterator.next();
sumDelta.merge(nextDelta);
}
return sumDelta;
}
/**
* Union of several object deltas. The deltas are merged to create a single delta
* that contains changes from all the deltas.
*
* Union works on UNORDERED deltas.
*/
public static <T extends Objectable> ObjectDelta<T> union(ObjectDelta<T>... deltas) throws SchemaException {
List<ObjectDelta<T>> modifyDeltas = new ArrayList<ObjectDelta<T>>(deltas.length);
ObjectDelta<T> addDelta = null;
ObjectDelta<T> deleteDelta = null;
for (ObjectDelta<T> delta : deltas) {
if (delta == null) {
continue;
}
if (delta.changeType == ChangeType.MODIFY) {
modifyDeltas.add(delta);
} else if (delta.changeType == ChangeType.ADD) {
if (addDelta != null) {
// Maybe we can, be we do not want. This is usually an error anyway.
throw new IllegalArgumentException("Cannot merge two add deltas: " + addDelta + ", " + delta);
}
addDelta = delta;
} else if (delta.changeType == ChangeType.DELETE) {
deleteDelta = delta;
}
}
if (deleteDelta != null && addDelta == null) {
// Merging DELETE with anything except ADD is still a DELETE
return deleteDelta.clone();
}
if (deleteDelta != null && addDelta != null) {
throw new IllegalArgumentException("Cannot merge add and delete deltas: " + addDelta + ", " + deleteDelta);
}
if (addDelta != null) {
return mergeToDelta(addDelta, modifyDeltas);
} else {
if (modifyDeltas.size() == 0) {
return null;
}
if (modifyDeltas.size() == 1) {
return modifyDeltas.get(0);
}
return mergeToDelta(modifyDeltas.get(0), modifyDeltas.subList(1, modifyDeltas.size()));
}
}
private static <T extends Objectable> ObjectDelta<T> mergeToDelta(ObjectDelta<T> firstDelta,
List<ObjectDelta<T>> modifyDeltas) throws SchemaException {
if (modifyDeltas.size() == 0) {
return firstDelta;
}
ObjectDelta<T> delta = firstDelta.clone();
for (ObjectDelta<T> modifyDelta : modifyDeltas) {
if (modifyDelta == null) {
continue;
}
if (modifyDelta.changeType != ChangeType.MODIFY) {
throw new IllegalArgumentException("Can only merge MODIFY changes, got " + modifyDelta.changeType);
}
delta.mergeModifications(modifyDelta.modifications);
}
return delta;
}
public void mergeModifications(Collection<? extends ItemDelta> modificationsToMerge) throws SchemaException {
for (ItemDelta<?,?> propDelta : modificationsToMerge) {
mergeModification(propDelta);
}
}
public void mergeModification(ItemDelta<?,?> modificationToMerge) throws SchemaException {
if (changeType == ChangeType.ADD) {
modificationToMerge.applyTo(objectToAdd);
} else if (changeType == ChangeType.MODIFY) {
ItemDelta myDelta = findModification(modificationToMerge.getPath(), ItemDelta.class);
if (myDelta == null) {
addModification(modificationToMerge.clone());
} else {
myDelta.merge(modificationToMerge);
}
} // else it is DELETE. There's nothing to do. Merging anything to delete is still delete
}
/**
* Applies this object delta to specified object, returns updated object.
* It modifies the provided object.
*/
public void applyTo(PrismObject<T> targetObject) throws SchemaException {
if (isEmpty()) {
// nothing to do
return;
}
if (changeType != ChangeType.MODIFY) {
throw new IllegalStateException("Can apply only MODIFY delta to object, got " + changeType + " delta");
}
for (ItemDelta itemDelta : modifications) {
itemDelta.applyTo(targetObject);
}
}
/**
* Applies this object delta to specified object, returns updated object.
* It leaves the original object unchanged.
*
* @param objectOld object before change
* @return object with applied changes or null if the object should not exit (was deleted)
*/
public PrismObject<T> computeChangedObject(PrismObject<T> objectOld) throws SchemaException {
if (objectOld == null) {
if (getChangeType() == ChangeType.ADD) {
objectOld = getObjectToAdd();
return objectOld.clone();
} else {
//throw new IllegalStateException("Cannot apply "+getChangeType()+" delta to a null old object");
// This seems to be quite OK
return null;
}
}
if (getChangeType() == ChangeType.DELETE) {
return null;
}
// MODIFY change
PrismObject<T> objectNew = objectOld.clone();
for (ItemDelta modification : modifications) {
modification.applyTo(objectNew);
}
return objectNew;
}
/**
* Incorporates the property delta into the existing property deltas
* (regardless of the change type).
*/
public void swallow(ItemDelta<?,?> newItemDelta) throws SchemaException {
if (changeType == ChangeType.MODIFY) {
// TODO: check for conflict
addModification(newItemDelta);
} else if (changeType == ChangeType.ADD) {
Item item = objectToAdd.findOrCreateItem(newItemDelta.getPath(), newItemDelta.getItemClass());
newItemDelta.applyTo(item);
}
// nothing to do for DELETE
}
private Collection<? extends ItemDelta<?,?>> createEmptyModifications() {
// Lists are easier to debug
return new ArrayList<ItemDelta<?,?>>();
}
public <X> PropertyDelta<X> createPropertyModification(QName name, PrismPropertyDefinition<X> propertyDefinition) {
PropertyDelta<X> propertyDelta = new PropertyDelta<X>(name, propertyDefinition, prismContext);
return addModification(propertyDelta);
}
public <X> PropertyDelta<X> createPropertyModification(ItemPath path) {
PrismObjectDefinition<T> objDef = getPrismContext().getSchemaRegistry().findObjectDefinitionByCompileTimeClass(getObjectTypeClass());
PrismPropertyDefinition<X> propDef = objDef.findPropertyDefinition(path);
return createPropertyModification(path, propDef);
}
public <C> PropertyDelta<C> createPropertyModification(ItemPath path, PrismPropertyDefinition propertyDefinition) {
PropertyDelta<C> propertyDelta = new PropertyDelta<C>(path, propertyDefinition, prismContext);
// No point in adding the modification to this delta. It will get merged anyway and it may disappear
// it is not reliable and therefore it is better not to add it now.
return propertyDelta;
}
public ReferenceDelta createReferenceModification(QName name, PrismReferenceDefinition referenceDefinition) {
ReferenceDelta referenceDelta = new ReferenceDelta(name, referenceDefinition, prismContext);
return addModification(referenceDelta);
}
public <C extends Containerable> ContainerDelta<C> createContainerModification(ItemPath path) {
PrismObjectDefinition<T> objDef = getPrismContext().getSchemaRegistry().findObjectDefinitionByCompileTimeClass(getObjectTypeClass());
PrismContainerDefinition<C> propDef = objDef.findContainerDefinition(path);
return createContainerModification(path, propDef);
}
public <C extends Containerable> ContainerDelta<C> createContainerModification(ItemPath path, PrismContainerDefinition<C> containerDefinition) {
ContainerDelta<C> containerDelta = new ContainerDelta<C>(path, containerDefinition, prismContext);
return addModification(containerDelta);
}
/**
* Convenience method for quick creation of object deltas that replace a single object property. This is used quite often
* to justify a separate method.
*/
public static <O extends Objectable, X> ObjectDelta<O> createModificationReplaceProperty(Class<O> type, String oid, QName propertyName,
PrismContext prismContext, X... propertyValues) {
ItemPath propertyPath = new ItemPath(propertyName);
return createModificationReplaceProperty(type, oid, propertyPath, prismContext, propertyValues);
}
/**
* Convenience method for quick creation of object deltas that replace a single object property. This is used quite often
* to justify a separate method.
*/
public static <O extends Objectable, X> ObjectDelta<O> createModificationReplaceProperty(Class<O> type, String oid,
ItemPath propertyPath, PrismContext prismContext, X... propertyValues) {
ObjectDelta<O> objectDelta = new ObjectDelta<O>(type, ChangeType.MODIFY, prismContext);
objectDelta.setOid(oid);
fillInModificationReplaceProperty(objectDelta, propertyPath, propertyValues);
return objectDelta;
}
public static <O extends Objectable, X> ObjectDelta<O> createModificationAddProperty(Class<O> type, String oid,
QName propertyName, PrismContext prismContext, X... propertyValues) {
return createModificationAddProperty(type, oid, new ItemPath(propertyName), prismContext, propertyValues);
}
public static <O extends Objectable, X> ObjectDelta<O> createModificationAddProperty(Class<O> type, String oid,
ItemPath propertyPath, PrismContext prismContext, X... propertyValues) {
ObjectDelta<O> objectDelta = new ObjectDelta<O>(type, ChangeType.MODIFY, prismContext);
objectDelta.setOid(oid);
fillInModificationAddProperty(objectDelta, propertyPath, propertyValues);
return objectDelta;
}
public static <O extends Objectable, X> ObjectDelta<O> createModificationDeleteProperty(Class<O> type, String oid,
QName propertyName, PrismContext prismContext, X... propertyValues) {
return createModificationDeleteProperty(type, oid, new ItemPath(propertyName), prismContext, propertyValues);
}
public static <O extends Objectable, X> ObjectDelta<O> createModificationDeleteProperty(Class<O> type, String oid,
ItemPath propertyPath, PrismContext prismContext, X... propertyValues) {
ObjectDelta<O> objectDelta = new ObjectDelta<O>(type, ChangeType.MODIFY, prismContext);
objectDelta.setOid(oid);
fillInModificationDeleteProperty(objectDelta, propertyPath, propertyValues);
return objectDelta;
}
public <X> void addModificationReplaceProperty(QName propertyQName, X... propertyValues) {
addModificationReplaceProperty(new ItemPath(propertyQName), propertyValues);
}
public <X> void addModificationReplaceProperty(ItemPath propertyPath, X... propertyValues) {
fillInModificationReplaceProperty(this, propertyPath, propertyValues);
}
public <X> void addModificationAddProperty(QName propertyQName, X... propertyValues) {
addModificationAddProperty(new ItemPath(propertyQName), propertyValues);
}
public <X> void addModificationAddProperty(ItemPath propertyPath, X... propertyValues) {
fillInModificationAddProperty(this, propertyPath, propertyValues);
}
public <X> void addModificationDeleteProperty(QName propertyQName, X... propertyValues) {
addModificationDeleteProperty(new ItemPath(propertyQName), propertyValues);
}
public <X> void addModificationDeleteProperty(ItemPath propertyPath, X... propertyValues) {
fillInModificationDeleteProperty(this, propertyPath, propertyValues);
}
public <C extends Containerable> void addModificationAddContainer(QName propertyQName, C... containerables) throws SchemaException {
addModificationAddContainer(new ItemPath(propertyQName), containerables);
}
public <C extends Containerable> void addModificationAddContainer(ItemPath propertyPath, C... containerables) throws SchemaException {
fillInModificationAddContainer(this, propertyPath, prismContext, containerables);
}
public <C extends Containerable> void addModificationAddContainer(QName propertyQName, PrismContainerValue<C>... containerValues) {
addModificationAddContainer(new ItemPath(propertyQName), containerValues);
}
public <C extends Containerable> void addModificationAddContainer(ItemPath propertyPath, PrismContainerValue<C>... containerValues) {
fillInModificationAddContainer(this, propertyPath, containerValues);
}
public <C extends Containerable> void addModificationDeleteContainer(QName propertyQName, C... containerables) throws SchemaException {
addModificationDeleteContainer(new ItemPath(propertyQName), containerables);
}
public <C extends Containerable> void addModificationDeleteContainer(ItemPath propertyPath, C... containerables) throws SchemaException {
fillInModificationDeleteContainer(this, propertyPath, prismContext, containerables);
}
public <C extends Containerable> void addModificationDeleteContainer(QName propertyQName, PrismContainerValue<C>... containerValues) {
addModificationDeleteContainer(new ItemPath(propertyQName), containerValues);
}
public <C extends Containerable> void addModificationDeleteContainer(ItemPath propertyPath, PrismContainerValue<C>... containerValues) {
fillInModificationDeleteContainer(this, propertyPath, containerValues);
}
public <C extends Containerable> void addModificationReplaceContainer(QName propertyQName, PrismContainerValue<C>... containerValues) {
addModificationReplaceContainer(new ItemPath(propertyQName), containerValues);
}
public <C extends Containerable> void addModificationReplaceContainer(ItemPath propertyPath, PrismContainerValue<C>... containerValues) {
fillInModificationReplaceContainer(this, propertyPath, containerValues);
}
public static <O extends Objectable, C extends Containerable> ObjectDelta<O> createModificationReplaceContainer(Class<O> type, String oid,
ItemPath propertyPath, PrismContext prismContext, C... containerValues) throws SchemaException {
ObjectDelta<O> objectDelta = new ObjectDelta<O>(type, ChangeType.MODIFY, prismContext);
objectDelta.setOid(oid);
fillInModificationReplaceContainer(objectDelta, propertyPath, containerValues);
return objectDelta;
}
protected static <O extends Objectable, X> void fillInModificationReplaceProperty(ObjectDelta<O> objectDelta,
ItemPath propertyPath, X... propertyValues) {
PropertyDelta<X> propertyDelta = objectDelta.createPropertyModification(propertyPath);
if (propertyValues != null) {
Collection<PrismPropertyValue<X>> valuesToReplace = toPrismPropertyValues(objectDelta.getPrismContext(), propertyValues);
propertyDelta.setValuesToReplace(valuesToReplace);
objectDelta.addModification(propertyDelta);
}
}
protected static <O extends Objectable, X> void fillInModificationAddProperty(ObjectDelta<O> objectDelta,
ItemPath propertyPath, X... propertyValues) {
PropertyDelta<X> propertyDelta = objectDelta.createPropertyModification(propertyPath);
if (propertyValues != null) {
Collection<PrismPropertyValue<X>> valuesToAdd = toPrismPropertyValues(objectDelta.getPrismContext(), propertyValues);
propertyDelta.addValuesToAdd(valuesToAdd);
objectDelta.addModification(propertyDelta);
}
}
protected static <O extends Objectable, X> void fillInModificationDeleteProperty(ObjectDelta<O> objectDelta,
ItemPath propertyPath, X... propertyValues) {
PropertyDelta<X> propertyDelta = objectDelta.createPropertyModification(propertyPath);
if (propertyValues != null) {
Collection<PrismPropertyValue<X>> valuesToDelete = toPrismPropertyValues(objectDelta.getPrismContext(), propertyValues);
propertyDelta.addValuesToDelete(valuesToDelete);
objectDelta.addModification(propertyDelta);
}
}
public static <O extends Objectable, C extends Containerable> ObjectDelta<O> createModificationAddContainer(Class<O> type, String oid,
QName propertyName, PrismContext prismContext, PrismContainerValue<C>... containerValues) {
return createModificationAddContainer(type, oid, new ItemPath(propertyName), prismContext, containerValues);
}
public static <O extends Objectable, C extends Containerable> ObjectDelta<O> createModificationAddContainer(Class<O> type, String oid,
ItemPath propertyPath, PrismContext prismContext, PrismContainerValue<C>... containerValues) {
ObjectDelta<O> objectDelta = new ObjectDelta<O>(type, ChangeType.MODIFY, prismContext);
objectDelta.setOid(oid);
fillInModificationAddContainer(objectDelta, propertyPath, containerValues);
return objectDelta;
}
public static <O extends Objectable, C extends Containerable> ObjectDelta<O> createModificationAddContainer(Class<O> type, String oid,
QName propertyName, PrismContext prismContext, C... containerValues) throws SchemaException {
return createModificationAddContainer(type, oid, new ItemPath(propertyName), prismContext, containerValues);
}
public static <O extends Objectable, C extends Containerable> ObjectDelta<O> createModificationAddContainer(Class<O> type, String oid,
ItemPath propertyPath, PrismContext prismContext, C... containerValues) throws SchemaException {
ObjectDelta<O> objectDelta = new ObjectDelta<O>(type, ChangeType.MODIFY, prismContext);
objectDelta.setOid(oid);
PrismContainerValue<C>[] containerPValues = new PrismContainerValue[containerValues.length];
for (int i=0; i<containerValues.length; i++) {
C containerable = containerValues[i];
prismContext.adopt(containerable, type, propertyPath);
containerPValues[i] = containerable.asPrismContainerValue();
}
fillInModificationAddContainer(objectDelta, propertyPath, containerPValues);
return objectDelta;
}
public static <O extends Objectable, C extends Containerable> ObjectDelta<O> createModificationDeleteContainer(Class<O> type,
String oid, QName containerName, PrismContext prismContext, PrismContainerValue<C>... containerValues) {
return createModificationDeleteContainer(type, oid, new ItemPath(containerName), prismContext, containerValues);
}
public static <O extends Objectable, C extends Containerable> ObjectDelta<O> createModificationDeleteContainer(Class<O> type, String oid, ItemPath containerPath,
PrismContext prismContext, PrismContainerValue<C>... containerValues) {
ObjectDelta<O> objectDelta = new ObjectDelta<O>(type, ChangeType.MODIFY, prismContext);
objectDelta.setOid(oid);
fillInModificationDeleteContainer(objectDelta, containerPath, containerValues);
return objectDelta;
}
public static <O extends Objectable, C extends Containerable> ObjectDelta<O> createModificationDeleteContainer(Class<O> type, String oid,
QName containerName, PrismContext prismContext, C... containerValues) throws SchemaException {
return createModificationDeleteContainer(type, oid, new ItemPath(containerName), prismContext, containerValues);
}
public static <O extends Objectable, C extends Containerable> ObjectDelta<O> createModificationDeleteContainer(Class<O> type, String oid,
ItemPath propertyPath, PrismContext prismContext, C... containerValues) throws SchemaException {
ObjectDelta<O> objectDelta = new ObjectDelta<O>(type, ChangeType.MODIFY, prismContext);
objectDelta.setOid(oid);
PrismContainerValue<C>[] containerPValues = new PrismContainerValue[containerValues.length];
for (int i=0; i<containerValues.length; i++) {
C containerable = containerValues[i];
prismContext.adopt(containerable, type, propertyPath);
containerPValues[i] = containerable.asPrismContainerValue();
}
fillInModificationDeleteContainer(objectDelta, propertyPath, containerPValues);
return objectDelta;
}
protected static <O extends Objectable, C extends Containerable> void fillInModificationDeleteContainer(ObjectDelta<O> objectDelta,
ItemPath propertyPath, PrismContainerValue<C>... containerValues) {
ContainerDelta<C> containerDelta = objectDelta.createContainerModification(propertyPath);
if (containerValues != null && containerValues.length > 0) {
containerDelta.addValuesToDelete(containerValues);
}
}
public static <O extends Objectable, C extends Containerable> ObjectDelta<O> createModificationReplaceContainer(Class<O> type,
String oid, QName containerName, PrismContext prismContext, PrismContainerValue<C>... containerValues) {
return createModificationReplaceContainer(type, oid, new ItemPath(containerName), prismContext, containerValues);
}
public static <O extends Objectable, C extends Containerable> ObjectDelta<O> createModificationReplaceContainer(Class<O> type, String oid, ItemPath containerPath,
PrismContext prismContext, PrismContainerValue<C>... containerValues) {
ObjectDelta<O> objectDelta = new ObjectDelta<O>(type, ChangeType.MODIFY, prismContext);
objectDelta.setOid(oid);
fillInModificationReplaceContainer(objectDelta, containerPath, containerValues);
return objectDelta;
}
public static <O extends Objectable, C extends Containerable> ObjectDelta<O> createModificationReplaceContainer(Class<O> type, String oid,
QName propertyName, PrismContext prismContext, C... containerValues) throws SchemaException {
return createModificationReplaceContainer(type, oid, new ItemPath(propertyName), prismContext, containerValues);
}
protected static <O extends Objectable, C extends Containerable> void fillInModificationAddContainer(ObjectDelta<O> objectDelta,
ItemPath propertyPath, PrismContainerValue<C>... containerValues) {
ContainerDelta<C> containerDelta = objectDelta.createContainerModification(propertyPath);
if (containerValues != null && containerValues.length > 0) {
containerDelta.addValuesToAdd(containerValues);
}
}
protected static <O extends Objectable, C extends Containerable> void fillInModificationAddContainer(ObjectDelta<O> objectDelta,
ItemPath propertyPath, PrismContext prismContext, C... containerables) throws SchemaException {
ContainerDelta<C> containerDelta = objectDelta.createContainerModification(propertyPath);
if (containerables != null) {
for (C containerable: containerables) {
prismContext.adopt(containerable, objectDelta.getObjectTypeClass(), propertyPath);
PrismContainerValue<C> prismContainerValue = containerable.asPrismContainerValue();
containerDelta.addValueToAdd(prismContainerValue);
}
}
}
protected static <O extends Objectable, C extends Containerable> void fillInModificationDeleteContainer(ObjectDelta<O> objectDelta,
ItemPath propertyPath, PrismContext prismContext, C... containerables) throws SchemaException {
ContainerDelta<C> containerDelta = objectDelta.createContainerModification(propertyPath);
if (containerables != null) {
for (C containerable: containerables) {
prismContext.adopt(containerable, objectDelta.getObjectTypeClass(), propertyPath);
PrismContainerValue<C> prismContainerValue = containerable.asPrismContainerValue();
containerDelta.addValueToDelete(prismContainerValue);
}
}
}
protected static <O extends Objectable, C extends Containerable> void fillInModificationReplaceContainer(ObjectDelta<O> objectDelta,
ItemPath propertyPath, PrismContainerValue<C>... containerValues) {
ContainerDelta<C> containerDelta = objectDelta.createContainerModification(propertyPath);