/
ItemDelta.java
1660 lines (1491 loc) · 49.9 KB
/
ItemDelta.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-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.prism.delta;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.parser.XPathHolder;
import com.evolveum.midpoint.prism.path.IdItemPathSegment;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.path.ItemPath.CompareResult;
import com.evolveum.midpoint.prism.path.ItemPathSegment;
import com.evolveum.midpoint.prism.path.NameItemPathSegment;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.SchemaException;
/**
* @author Radovan Semancik
*
*/
public abstract class ItemDelta<V extends PrismValue,D extends ItemDefinition> implements Itemable, DebugDumpable, Visitable, PathVisitable, Serializable {
/**
* Name of the property
*/
protected QName elementName;
/**
* Parent path of the property (path to the property container)
*/
protected ItemPath parentPath;
protected D definition;
protected Collection<V> valuesToReplace = null;
protected Collection<V> valuesToAdd = null;
protected Collection<V> valuesToDelete = null;
protected Collection<V> estimatedOldValues = null;
transient private PrismContext prismContext;
protected ItemDelta(D itemDefinition, PrismContext prismContext) {
if (itemDefinition == null) {
throw new IllegalArgumentException("Attempt to create item delta without a definition");
}
//checkPrismContext(prismContext, itemDefinition);
this.prismContext = prismContext;
this.elementName = itemDefinition.getName();
this.parentPath = new ItemPath();
this.definition = itemDefinition;
}
protected ItemDelta(QName elementName, D itemDefinition, PrismContext prismContext) {
//checkPrismContext(prismContext, itemDefinition);
this.prismContext = prismContext;
this.elementName = elementName;
this.parentPath = new ItemPath();
this.definition = itemDefinition;
}
protected ItemDelta(ItemPath parentPath, QName elementName, D itemDefinition, PrismContext prismContext) {
//checkPrismContext(prismContext, itemDefinition);
ItemPath.checkNoReferences(parentPath);
this.prismContext = prismContext;
this.elementName = elementName;
this.parentPath = parentPath;
this.definition = itemDefinition;
}
protected ItemDelta(ItemPath path, D itemDefinition, PrismContext prismContext) {
//checkPrismContext(prismContext, itemDefinition);
ItemPath.checkNoReferences(path);
this.prismContext = prismContext;
if (path == null) {
throw new IllegalArgumentException("Null path specified while creating item delta");
}
if (path.isEmpty()) {
this.elementName = null;
} else {
ItemPathSegment last = path.last();
if (!(last instanceof NameItemPathSegment)) {
throw new IllegalArgumentException("Invalid delta path "+path+". Delta path must always point to item, not to value");
}
this.elementName = ((NameItemPathSegment)last).getName();
this.parentPath = path.allExceptLast();
}
this.definition = itemDefinition;
}
// currently unused; we allow deltas without prismContext, except for some operations (e.g. serialization to ItemDeltaType)
// private void checkPrismContext(PrismContext prismContext, ItemDefinition itemDefinition) {
// if (prismContext == null) {
// throw new IllegalStateException("No prismContext in delta for " + itemDefinition);
// }
// }
public QName getElementName() {
return elementName;
}
public void setElementName(QName elementName) {
this.elementName = elementName;
}
public ItemPath getParentPath() {
return parentPath;
}
public void setParentPath(ItemPath parentPath) {
this.parentPath = parentPath;
}
@Override
public ItemPath getPath() {
if (getParentPath() == null) {
throw new IllegalStateException("No parent path in "+this);
}
return getParentPath().subPath(elementName);
}
public D getDefinition() {
return definition;
}
public void setDefinition(D definition) {
this.definition = definition;
}
@Override
public void accept(Visitor visitor) {
accept(visitor, true);
}
public void accept(Visitor visitor, boolean includeOldValues) {
visitor.visit(this);
if (getValuesToAdd() != null) {
for (V pval : getValuesToAdd()) {
pval.accept(visitor);
}
}
if (getValuesToDelete() != null) {
for (V pval : getValuesToDelete()) {
pval.accept(visitor);
}
}
if (getValuesToReplace() != null) {
for (V pval : getValuesToReplace()) {
pval.accept(visitor);
}
}
if (includeOldValues && getEstimatedOldValues() != null) {
for (V pval : getEstimatedOldValues()) {
pval.accept(visitor);
}
}
}
// TODO think if estimated old values have to be visited as well
@Override
public void accept(Visitor visitor, ItemPath path, boolean recursive) {
if (path == null || path.isEmpty()) {
if (recursive) {
accept(visitor);
} else {
visitor.visit(this);
}
} else {
IdItemPathSegment idSegment = ItemPath.getFirstIdSegment(path);
ItemPath rest = ItemPath.pathRestStartingWithName(path);
if (idSegment == null || idSegment.isWildcard()) {
// visit all values
if (getValuesToAdd() != null) {
for (V pval : getValuesToAdd()) {
pval.accept(visitor, rest, recursive);
}
}
if (getValuesToDelete() != null) {
for (V pval : getValuesToDelete()) {
pval.accept(visitor, rest, recursive);
}
}
if (getValuesToReplace() != null) {
for (V pval : getValuesToReplace()) {
pval.accept(visitor, rest, recursive);
}
}
} else {
Long id = idSegment.getId();
acceptSet(getValuesToAdd(), id, visitor, rest, recursive);
acceptSet(getValuesToDelete(), id, visitor, rest, recursive);
acceptSet(getValuesToReplace(), id, visitor, rest, recursive);
}
}
}
private void acceptSet(Collection<V> set, Long id, Visitor visitor, ItemPath rest, boolean recursive) {
if (set == null) {
return;
}
for (V pval : set) {
if (pval instanceof PrismContainerValue<?>) {
PrismContainerValue<?> cval = (PrismContainerValue<?>)pval;
if (id == null || id.equals(cval.getId())) {
pval.accept(visitor, rest, recursive);
}
} else {
throw new IllegalArgumentException("Attempt to fit container id to "+pval.getClass());
}
}
}
public void applyDefinition(D definition) throws SchemaException {
this.definition = definition;
if (getValuesToAdd() != null) {
for (V pval : getValuesToAdd()) {
pval.applyDefinition(definition);
}
}
if (getValuesToDelete() != null) {
for (V pval : getValuesToDelete()) {
pval.applyDefinition(definition);
}
}
if (getValuesToReplace() != null) {
for (V pval : getValuesToReplace()) {
pval.applyDefinition(definition);
}
}
}
public static void applyDefinition(Collection<? extends ItemDelta> deltas,
PrismObjectDefinition definition) throws SchemaException {
for (ItemDelta itemDelta : deltas) {
ItemPath path = itemDelta.getPath();
ItemDefinition itemDefinition = definition.findItemDefinition(path, ItemDefinition.class);
if (itemDefinition == null) {
throw new SchemaException("Object type " + definition.getTypeName() + " doesn't contain definition for path " + new XPathHolder(path).getXPathWithDeclarations(false));
}
itemDelta.applyDefinition(itemDefinition);
}
}
public boolean hasCompleteDefinition() {
return getDefinition() != null;
}
public PrismContext getPrismContext() {
return prismContext;
}
public abstract Class<? extends Item> getItemClass();
public Collection<V> getValuesToAdd() {
return valuesToAdd;
}
public void clearValuesToAdd() {
valuesToAdd = null;
}
public Collection<V> getValuesToDelete() {
return valuesToDelete;
}
public void clearValuesToDelete() {
valuesToDelete = null;
}
public Collection<V> getValuesToReplace() {
return valuesToReplace;
}
public void clearValuesToReplace() {
valuesToReplace = null;
}
public void addValuesToAdd(Collection<V> newValues) {
for (V val : newValues) {
addValueToAdd(val);
}
}
public void addValuesToAdd(V... newValues) {
for (V val : newValues) {
addValueToAdd(val);
}
}
public void addValueToAdd(V newValue) {
if (valuesToReplace != null) {
throw new IllegalStateException("Delta " + this
+ " already has values to replace ("+valuesToReplace+"), attempt to add value ("+newValue+") is an error");
}
if (valuesToAdd == null) {
valuesToAdd = newValueCollection();
}
if (PrismValue.containsRealValue(valuesToAdd,newValue)) {
return;
}
valuesToAdd.add(newValue);
newValue.setParent(this);
newValue.recompute();
}
public boolean removeValueToAdd(V valueToRemove) {
return removeValue(valueToRemove, valuesToAdd);
}
public boolean removeValueToDelete(V valueToRemove) {
return removeValue(valueToRemove, valuesToDelete);
}
public boolean removeValueToReplace(V valueToRemove) {
return removeValue(valueToRemove, valuesToReplace);
}
private boolean removeValue(V valueToRemove, Collection<V> set) {
boolean removed = false;
if (set == null) {
return removed;
}
Iterator<V> valuesToReplaceIterator = set.iterator();
while (valuesToReplaceIterator.hasNext()) {
V valueToReplace = valuesToReplaceIterator.next();
if (valueToReplace.equalsRealValue(valueToRemove)) {
valuesToReplaceIterator.remove();
removed = true;
}
}
return removed;
}
public void mergeValuesToAdd(Collection<V> newValues) {
for (V val : newValues) {
mergeValueToAdd(val);
}
}
public void mergeValuesToAdd(V[] newValues) {
for (V val : newValues) {
mergeValueToAdd(val);
}
}
public void mergeValueToAdd(V newValue) {
if (valuesToReplace != null) {
if (!PrismValue.containsRealValue(valuesToReplace, newValue)) {
valuesToReplace.add(newValue);
newValue.setParent(this);
}
} else {
if (!removeValueToDelete(newValue)) {
addValueToAdd(newValue);
}
}
}
public void addValuesToDelete(Collection<V> newValues) {
for (V val : newValues) {
addValueToDelete(val);
}
}
public void addValuesToDelete(V... newValues) {
for (V val : newValues) {
addValueToDelete(val);
}
}
public void addValueToDelete(V newValue) {
if (valuesToReplace != null) {
throw new IllegalStateException("Delta " + this
+ " already has values to replace ("+valuesToReplace+"), attempt to set value to delete ("+newValue+")");
}
if (valuesToDelete == null) {
valuesToDelete = newValueCollection();
}
if (containsEquivalentValue(valuesToDelete, newValue)) {
return;
}
valuesToDelete.add(newValue);
newValue.setParent(this);
newValue.recompute();
}
protected boolean containsEquivalentValue(Collection<V> collection, V value) {
if (collection == null) {
return false;
}
for (V colVal: collection) {
if (isValueEquivalent(colVal, value)) {
return true;
}
}
return false;
}
protected boolean isValueEquivalent(V a, V b) {
return a.equalsRealValue(b);
}
public void mergeValuesToDelete(Collection<V> newValues) {
for (V val : newValues) {
mergeValueToDelete(val);
}
}
public void mergeValuesToDelete(V[] newValues) {
for (V val : newValues) {
mergeValueToDelete(val);
}
}
public void mergeValueToDelete(V newValue) {
if (valuesToReplace != null) {
removeValueToReplace(newValue);
} else {
if (!removeValueToAdd(newValue)) {
addValueToDelete(newValue);
}
}
}
public void resetValuesToAdd() {
valuesToAdd = null;
}
public void resetValuesToDelete() {
valuesToDelete = null;
}
public void setValuesToReplace(Collection<V> newValues) {
if (valuesToAdd != null) {
throw new IllegalStateException("Delta " + this
+ " already has values to add ("+valuesToAdd+"), attempt to set value to replace ("+newValues+")");
}
if (valuesToDelete != null) {
throw new IllegalStateException("Delta " + this
+ " already has values to delete, attempt to set value to replace");
}
if (valuesToReplace == null) {
valuesToReplace = newValueCollection();
} else {
valuesToReplace.clear();
}
for (V val : newValues) {
valuesToReplace.add(val);
val.setParent(this);
val.recompute();
}
}
public void setValuesToReplace(V... newValues) {
if (valuesToAdd != null) {
throw new IllegalStateException("Delta " + this
+ " already has values to add, attempt to set value to replace");
}
if (valuesToDelete != null) {
throw new IllegalStateException("Delta " + this
+ " already has values to delete, attempt to set value to replace");
}
if (valuesToReplace == null) {
valuesToReplace = newValueCollection();
} else {
valuesToReplace.clear();
}
for (V val : newValues) {
valuesToReplace.add(val);
val.setParent(this);
val.recompute();
}
}
/**
* Sets empty value to replace. This efficiently means removing all values.
*/
public void setValueToReplace() {
if (valuesToReplace == null) {
valuesToReplace = newValueCollection();
} else {
valuesToReplace.clear();
}
}
public void setValueToReplace(V newValue) {
if (valuesToAdd != null) {
throw new IllegalStateException("Delta " + this
+ " already has values to add, attempt to set value to replace");
}
if (valuesToDelete != null) {
throw new IllegalStateException("Delta " + this
+ " already has values to delete, attempt to set value to replace");
}
if (valuesToReplace == null) {
valuesToReplace = newValueCollection();
} else {
valuesToReplace.clear();
}
if (newValue != null) {
valuesToReplace.add(newValue);
newValue.setParent(this);
newValue.recompute();
}
}
public void mergeValuesToReplace(Collection<V> newValues) {
// No matter what type the delta was before. We are just discarding all the previous
// state as the replace that we are applying will overwrite that anyway.
valuesToAdd = null;
valuesToDelete = null;
setValuesToReplace(newValues);
}
public void mergeValuesToReplace(V[] newValues) {
// No matter what type the delta was before. We are just discarding all the previous
// state as the replace that we are applying will overwrite that anyway.
valuesToAdd = null;
valuesToDelete = null;
setValuesToReplace(newValues);
}
public void mergeValueToReplace(V newValue) {
// No matter what type the delta was before. We are just discarding all the previous
// state as the replace that we are applying will overwrite that anyway.
valuesToAdd = null;
valuesToDelete = null;
setValueToReplace(newValue);
}
private Collection<V> newValueCollection() {
return new ArrayList<V>();
}
public boolean isValueToAdd(V value) {
return isValueSet(value, false, valuesToAdd);
}
public boolean isValueToAdd(V value, boolean ignoreMetadata) {
return isValueSet(value, ignoreMetadata, valuesToAdd);
}
public boolean isValueToDelete(V value) {
return isValueSet(value, false, valuesToDelete);
}
public boolean isValueToDelete(V value, boolean ignoreMetadata) {
return isValueSet(value, ignoreMetadata, valuesToDelete);
}
public boolean isValueToReplace(V value) {
return isValueSet(value, false, valuesToReplace);
}
public boolean isValueToReplace(V value, boolean ignoreMetadata) {
return isValueSet(value, ignoreMetadata, valuesToReplace);
}
private boolean isValueSet(V value, boolean ignoreMetadata, Collection<V> set) {
if (set == null) {
return false;
}
for (V myVal: set) {
if (myVal.equals(value, ignoreMetadata)) {
return true;
}
}
return false;
}
public V getAnyValue() {
V anyValue = getAnyValue(valuesToAdd);
if (anyValue != null) {
return anyValue;
}
anyValue = getAnyValue(valuesToDelete);
if (anyValue != null) {
return anyValue;
}
anyValue = getAnyValue(valuesToReplace);
if (anyValue != null) {
return anyValue;
}
return null;
}
private V getAnyValue(Collection<V> set) {
if (set == null || set.isEmpty()) {
return null;
}
return set.iterator().next();
}
public boolean isEmpty() {
if (valuesToAdd == null && valuesToDelete == null && valuesToReplace == null) {
return true;
}
return false;
}
public boolean addsAnyValue() {
return hasAnyValue(valuesToAdd) || hasAnyValue(valuesToReplace);
}
private boolean hasAnyValue(Collection<V> set) {
return (set != null && !set.isEmpty());
}
/**
* Returns estimated state of the old value before the delta is applied.
* This information is not entirely reliable. The state might change
* between the value is read and the delta is applied. This is property
* is optional and even if provided it is only for for informational
* purposes.
*
* If this method returns null then it should be interpreted as "I do not know".
* In that case the delta has no information about the old values.
* If this method returns empty collection then it should be interpreted that
* we know that there were no values in this item before the delta was applied.
*
* @return estimated state of the old value before the delta is applied (may be null).
*/
public Collection<V> getEstimatedOldValues() {
return estimatedOldValues;
}
public void setEstimatedOldValues(Collection<V> estimatedOldValues) {
this.estimatedOldValues = estimatedOldValues;
}
public void addEstimatedOldValues(Collection<V> newValues) {
for (V val : newValues) {
addEstimatedOldValue(val);
}
}
public void addEstimatedOldValues(V... newValues) {
for (V val : newValues) {
addEstimatedOldValue(val);
}
}
public void addEstimatedOldValue(V newValue) {
if (estimatedOldValues == null) {
estimatedOldValues = newValueCollection();
}
if (PrismValue.containsRealValue(estimatedOldValues,newValue)) {
return;
}
estimatedOldValues.add(newValue);
newValue.setParent(this);
newValue.recompute();
}
public void normalize() {
normalize(valuesToAdd);
normalize(valuesToDelete);
normalize(valuesToReplace);
}
private void normalize(Collection<V> set) {
if (set == null) {
return;
}
Iterator<V> iterator = set.iterator();
while (iterator.hasNext()) {
V value = iterator.next();
value.normalize();
if (value.isEmpty()) {
iterator.remove();
}
}
}
public boolean isReplace() {
return (valuesToReplace != null);
}
public boolean isAdd() {
return (valuesToAdd != null && !valuesToAdd.isEmpty());
}
public boolean isDelete() {
return (valuesToDelete != null && !valuesToDelete.isEmpty());
}
public void clear() {
valuesToReplace = null;
valuesToAdd = null;
valuesToDelete = null;
}
public static <T> PropertyDelta<T> findPropertyDelta(Collection<? extends ItemDelta> deltas, QName propertyName) {
return findPropertyDelta(deltas, new ItemPath(propertyName));
}
public static <T> PropertyDelta<T> findPropertyDelta(Collection<? extends ItemDelta> deltas, ItemPath parentPath, QName propertyName) {
return findPropertyDelta(deltas, new ItemPath(parentPath, propertyName));
}
public static <T> PropertyDelta<T> findPropertyDelta(Collection<? extends ItemDelta> deltas, ItemPath propertyPath) {
return findItemDelta(deltas, propertyPath, PropertyDelta.class);
}
public static <X extends Containerable> ContainerDelta<X> findContainerDelta(Collection<? extends ItemDelta> deltas, ItemPath propertyPath) {
return findItemDelta(deltas, propertyPath, ContainerDelta.class);
}
public static <X extends Containerable> ContainerDelta<X> findContainerDelta(Collection<? extends ItemDelta> deltas, QName name) {
return findContainerDelta(deltas, new ItemPath(name));
}
public static <DD extends ItemDelta> DD findItemDelta(Collection<? extends ItemDelta> deltas, ItemPath propertyPath, Class<DD> deltaType) {
if (deltas == null) {
return null;
}
for (ItemDelta<?,?> delta : deltas) {
if (deltaType.isAssignableFrom(delta.getClass()) && delta.getPath().equivalent(propertyPath)) {
return (DD) delta;
}
}
return null;
}
public static Collection<? extends ItemDelta<?,?>> findItemDeltasSubPath(Collection<? extends ItemDelta<?,?>> deltas, ItemPath itemPath) {
Collection<ItemDelta<?,?>> foundDeltas = new ArrayList<ItemDelta<?,?>>();
if (deltas == null) {
return foundDeltas;
}
for (ItemDelta<?,?> delta : deltas) {
if (itemPath.isSubPath(delta.getPath())) {
foundDeltas.add(delta);
}
}
return foundDeltas;
}
public static <D extends ItemDelta> D findItemDelta(Collection<? extends ItemDelta> deltas, QName itemName, Class<D> deltaType) {
return findItemDelta(deltas, new ItemPath(itemName), deltaType);
}
public static ReferenceDelta findReferenceModification(Collection<? extends ItemDelta> deltas, QName itemName) {
return findItemDelta(deltas, itemName, ReferenceDelta.class);
}
public static <D extends ItemDelta> void removeItemDelta(Collection<? extends ItemDelta> deltas, ItemPath propertyPath, Class<D> deltaType) {
if (deltas == null) {
return;
}
Iterator<? extends ItemDelta> deltasIterator = deltas.iterator();
while (deltasIterator.hasNext()) {
ItemDelta<?,?> delta = deltasIterator.next();
if (deltaType.isAssignableFrom(delta.getClass()) && delta.getPath().equivalent(propertyPath)) {
deltasIterator.remove();
}
}
}
public static <D extends ItemDelta> void removeItemDelta(Collection<? extends ItemDelta> deltas, ItemDelta deltaToRemove) {
if (deltas == null) {
return;
}
Iterator<? extends ItemDelta> deltasIterator = deltas.iterator();
while (deltasIterator.hasNext()) {
ItemDelta<?,?> delta = deltasIterator.next();
if (delta.equals(deltaToRemove)) {
deltasIterator.remove();
}
}
}
/**
* Filters out all delta values that are meaningless to apply. E.g. removes all values to add that the property already has,
* removes all values to delete that the property does not have, etc.
*/
public ItemDelta<V,D> narrow(PrismObject<? extends Objectable> object) {
return narrow(object, null);
}
/**
* Filters out all delta values that are meaningless to apply. E.g. removes all values to add that the property already has,
* removes all values to delete that the property does not have, etc.
*/
public ItemDelta<V,D> narrow(PrismObject<? extends Objectable> object, Comparator<V> comparator) {
Item<V,D> currentItem = (Item<V,D>) object.findItem(getPath());
if (currentItem == null) {
if (valuesToDelete != null) {
ItemDelta<V,D> clone = clone();
clone.valuesToDelete = null;
return clone;
} else {
// Nothing to narrow
return this;
}
} else {
ItemDelta<V,D> clone = clone();
if (clone.valuesToDelete != null) {
Iterator<V> iterator = clone.valuesToDelete.iterator();
while (iterator.hasNext()) {
V valueToDelete = iterator.next();
if (!currentItem.contains(valueToDelete, true, comparator)) {
iterator.remove();
}
}
if (clone.valuesToDelete.isEmpty()) {
clone.valuesToDelete = null;
}
}
if (clone.valuesToAdd != null) {
Iterator<V> iterator = clone.valuesToAdd.iterator();
while (iterator.hasNext()) {
V valueToDelete = iterator.next();
if (currentItem.contains(valueToDelete, true, comparator)) {
iterator.remove();
}
}
if (clone.valuesToAdd.isEmpty()) {
clone.valuesToAdd = null;
}
}
return clone;
}
}
/**
* Checks if the delta is redundant w.r.t. current state of the object.
* I.e. if it changes the current object state.
*/
public boolean isRedundant(PrismObject<? extends Objectable> object) {
Comparator<V> comparator = new Comparator<V>() {
@Override
public int compare(V o1, V o2) {
if (o1.equalsComplex(o2, true, false)) {
return 0;
} else {
return 1;
}
}
};
return isRedundant(object, comparator);
}
public boolean isRedundant(PrismObject<? extends Objectable> object, Comparator<V> comparator) {
Item<V,D> currentItem = (Item<V,D>) object.findItem(getPath());
if (currentItem == null) {
if (valuesToReplace != null) {
return valuesToReplace.isEmpty();
}
return !hasAnyValue(valuesToAdd);
} else {
if (valuesToReplace != null) {
return MiscUtil.unorderedCollectionEquals(valuesToReplace, currentItem.getValues(), comparator);
}
ItemDelta<V,D> narrowed = narrow(object, comparator);
boolean narrowedNotEmpty = narrowed.hasAnyValue(narrowed.valuesToAdd) || narrowed.hasAnyValue(narrowed.valuesToDelete);
return narrowedNotEmpty;
}
}
public void validate() throws SchemaException {
validate(null);
}
public void validate(String contextDescription) throws SchemaException {
if (definition == null) {
throw new IllegalStateException("Attempt to validate delta without a definition: "+this);
}
if (definition.isSingleValue()) {
if (valuesToAdd != null && valuesToAdd.size() > 1) {
throw new SchemaException("Attempt to add "+valuesToAdd.size()+" values to a single-valued item "+getPath() +
(contextDescription == null ? "" : " in "+contextDescription) + "; values: "+valuesToAdd);
}
if (valuesToReplace != null && valuesToReplace.size() > 1) {
throw new SchemaException("Attempt to replace "+valuesToReplace.size()+" values to a single-valued item "+getPath() +
(contextDescription == null ? "" : " in "+contextDescription) + "; values: "+valuesToReplace);
}
}
if (definition.isMandatory()) {
if (valuesToReplace != null && valuesToReplace.isEmpty()) {
throw new SchemaException("Attempt to clear all values of a mandatory item "+getPath() +
(contextDescription == null ? "" : " in "+contextDescription));
}
}
}
public static void checkConsistence(Collection<? extends ItemDelta> deltas) {
checkConsistence(deltas, ConsistencyCheckScope.THOROUGH);
}
public static void checkConsistence(Collection<? extends ItemDelta> deltas, ConsistencyCheckScope scope) {
checkConsistence(deltas, false, false, scope);
}
public static void checkConsistence(Collection<? extends ItemDelta> deltas, boolean requireDefinition, boolean prohibitRaw, ConsistencyCheckScope scope) {
Map<ItemPath,ItemDelta<?,?>> pathMap = new HashMap<>();
for (ItemDelta<?,?> delta : deltas) {
delta.checkConsistence(requireDefinition, prohibitRaw, scope);
int matches = 0;
for (ItemDelta<?,?> other : deltas) {
if (other == delta) {
matches++;
} else if (other.equals(delta)) {
throw new IllegalStateException("Duplicate item delta: "+delta+" and "+other);
}
}
if (matches > 1) {
throw new IllegalStateException("The delta "+delta+" appears multiple times in the modification list");
}
}
}
public void checkConsistence() {
checkConsistence(ConsistencyCheckScope.THOROUGH);
}
public void checkConsistence(ConsistencyCheckScope scope) {
checkConsistence(false, false, scope);
}
public void checkConsistence(boolean requireDefinition, boolean prohibitRaw, ConsistencyCheckScope scope) {
if (scope.isThorough() && parentPath == null) {
throw new IllegalStateException("Null parent path in " + this);
}
if (scope.isThorough() && requireDefinition && definition == null) {
throw new IllegalStateException("Null definition in "+this);
}
if (scope.isThorough() && valuesToReplace != null && (valuesToAdd != null || valuesToDelete != null)) {
throw new IllegalStateException(
"The delta cannot be both 'replace' and 'add/delete' at the same time");
}
assertSetConsistence(valuesToReplace, "replace", requireDefinition, prohibitRaw, scope);
assertSetConsistence(valuesToAdd, "add", requireDefinition, prohibitRaw, scope);
assertSetConsistence(valuesToDelete, "delete", requireDefinition, prohibitRaw, scope);
}
private void assertSetConsistence(Collection<V> values, String type, boolean requireDefinitions, boolean prohibitRaw, ConsistencyCheckScope scope) {
if (values == null) {
return;
}
// This may be not be 100% correct but we can tolerate it now
// if (values.isEmpty()) {
// throw new
// IllegalStateException("The "+type+" values set in "+this+" is not-null but it is empty");
// }
for (V val : values) {
if (scope.isThorough()) {
if (val == null) {
throw new IllegalStateException("Null value in the " + type + " values set in " + this);
}
if (val.getParent() != this) {
throw new IllegalStateException("Wrong parent for " + val + " in " + type + " values set in " + this + ": " + val.getParent());
}
}
val.checkConsistenceInternal(this, requireDefinitions, prohibitRaw, scope);
}
}
/**
* Distributes the replace values of this delta to add and delete with
* respect to provided existing values.
*/
public void distributeReplace(Collection<V> existingValues) {
Collection<V> origValuesToReplace = getValuesToReplace();
// We have to clear before we distribute, otherwise there will be replace/add or replace/delete conflict
clearValuesToReplace();
if (existingValues != null) {
for (V existingVal : existingValues) {
if (!isIn(origValuesToReplace, existingVal)) {
addValueToDelete((V) existingVal.clone());
}
}
}
for (V replaceVal : origValuesToReplace) {
if (!isIn(existingValues, replaceVal) && !isIn(getValuesToAdd(), replaceVal)) {
addValueToAdd((V) replaceVal.clone());
}
}
}