/
PrismContainer.java
896 lines (791 loc) · 30.2 KB
/
PrismContainer.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
/*
* Copyright (c) 2010-2013 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;
import com.evolveum.midpoint.prism.delta.ContainerDelta;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.path.IdItemPathSegment;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import javax.xml.namespace.QName;
import java.lang.reflect.Modifier;
import java.util.*;
/**
* <p>
* Property container groups properties into logical blocks.The reason for
* grouping may be as simple as better understandability of data structure. But
* the group usually means different meaning, source or structure of the data.
* For example, the property container is frequently used to hold properties
* that are dynamic, not fixed by a static schema. Such grouping also naturally
* translates to XML and helps to "quarantine" such properties to avoid Unique
* Particle Attribute problems.
* </p><p>
* Property Container contains a set of (potentially multi-valued) properties or inner property containers.
* The order of properties is not significant, regardless of the fact that it
* may be fixed in the XML representation. In the XML representation, each
* element inside Property Container must be either Property or a Property
* Container.
* </p><p>
* Property Container is mutable.
* </p>
*
* @author Radovan Semancik
*/
public class PrismContainer<C extends Containerable> extends Item<PrismContainerValue<C>,PrismContainerDefinition<C>> implements PrismContainerable<C> {
private static final long serialVersionUID = 5206821250098051028L;
private static final Trace LOGGER = TraceManager.getTrace(PrismContainer.class);
protected Class<C> compileTimeClass;
public PrismContainer(QName name) {
super(name);
}
public PrismContainer(QName name, PrismContext prismContext) {
super(name, prismContext);
}
public PrismContainer(QName name, Class<C> compileTimeClass) {
super(name);
if (Modifier.isAbstract(compileTimeClass.getModifiers())) {
throw new IllegalArgumentException("Can't use class '" + compileTimeClass.getSimpleName() + "' as compile-time class for "+name+"; the class is abstract.");
}
this.compileTimeClass = compileTimeClass;
}
public PrismContainer(QName name, Class<C> compileTimeClass, PrismContext prismContext) {
this(name, compileTimeClass);
this.prismContext = prismContext;
}
protected PrismContainer(QName name, PrismContainerDefinition<C> definition, PrismContext prismContext) {
super(name, definition, prismContext);
}
public Class<C> getCompileTimeClass() {
if (this.compileTimeClass != null) {
return compileTimeClass;
}
if (getDefinition() != null) {
return getDefinition().getCompileTimeClass();
}
return null;
}
/**
* Returns true if this object can represent specified compile-time class.
* I.e. this object can be presented in the compile-time form that is an
* instance of a specified class.
*/
public boolean canRepresent(Class<?> compileTimeClass) {
return (compileTimeClass.isAssignableFrom(getCompileTimeClass()));
}
@Override
public List<PrismContainerValue<C>> getValues() {
return (List<PrismContainerValue<C>>) super.getValues();
}
@Override
public Collection<C> getRealValues() {
if (getValues() == null) {
return null;
}
List<C> realValues = new ArrayList<>(getValues().size());
for (PrismContainerValue<C> value : getValues()) {
realValues.add(value.asContainerable());
}
return realValues;
}
@Override
public C getRealValue() {
if (getValue() == null) {
return null;
}
return getValue().asContainerable();
}
public PrismContainerValue<C> getValue() {
if (getValues().size() == 1) {
return getValues().get(0);
}
if (getValues().size() > 1) {
throw new IllegalStateException("Attempt to get single value from a multivalued container "+ getElementName());
}
// We are not sure about multiplicity if there is no definition or the definition is dynamic
if (getDefinition() != null && !getDefinition().isDynamic()) {
if (getDefinition().isSingleValue()) {
// Insert first empty value. This simulates empty single-valued container. It the container exists
// it is clear that it has at least one value (and that value is empty).
PrismContainerValue<C> pValue = new PrismContainerValue<C>(null, null, this, null, null, prismContext);
try {
add(pValue);
} catch (SchemaException e) {
// This should not happen
throw new SystemException("Internal Error: "+e.getMessage(),e);
}
return pValue;
} else {
throw new IllegalStateException("Attempt to get single value from a multivalued container "+ getElementName());
}
} else {
// Insert first empty value. This simulates empty single-valued container. It the container exists
// it is clear that it has at least one value (and that value is empty).
PrismContainerValue<C> pValue = new PrismContainerValue<C>(null, null, this, null, null, prismContext);
try {
add(pValue);
} catch (SchemaException e) {
// This should not happen
throw new SystemException("Internal Error: "+e.getMessage(),e);
}
return pValue;
}
}
public void setValue(PrismContainerValue<C> value) throws SchemaException {
checkMutability();
if (getDefinition() != null) {
if (getDefinition().isSingleValue()) {
clear();
add(value);
} else {
throw new IllegalStateException("Attempt to set single value to a multivalued container "+ getElementName());
}
} else {
clear();
add(value);
}
}
@Override
public boolean add(PrismContainerValue newValue, boolean checkUniqueness) throws SchemaException {
checkMutability();
// when a context-less item is added to a contextful container, it is automatically adopted
if (newValue.getPrismContext() == null && this.prismContext != null) {
prismContext.adopt(newValue);
}
if (newValue.getId() != null) {
for (PrismContainerValue existingValue : getValues()) {
if (existingValue.getId() != null && existingValue.getId().equals(newValue.getId())) {
throw new IllegalStateException("Attempt to add a container value with an id that already exists: " + newValue.getId());
}
}
}
return super.add(newValue, checkUniqueness);
}
@Override
public PrismContainerValue<C> getPreviousValue(PrismValue value) {
return (PrismContainerValue<C>) super.getPreviousValue(value);
}
@Override
public PrismContainerValue<C> getNextValue(PrismValue value) {
return (PrismContainerValue<C>) super.getNextValue(value);
}
private boolean canAssumeSingleValue() {
if (getDefinition() != null) {
return getDefinition().isSingleValue();
} else {
if (getValues().size() <= 1) {
return true;
} else {
return false;
}
}
}
public PrismContainerValue<C> getValue(Long id) {
for (PrismContainerValue<C> pval: getValues()) {
if ((id == null && pval.getId() == null) ||
id.equals(pval.getId())) {
return pval;
}
}
return null;
}
public void setPropertyRealValue(QName propertyName, Object realValue) throws SchemaException {
checkMutability();
PrismProperty<?> property = findOrCreateProperty(propertyName);
property.setRealValue(realValue);
}
public <T> T getPropertyRealValue(QName propertyName, Class<T> type) {
return getPropertyRealValue(new ItemPath(propertyName), type);
}
public <T> T getPropertyRealValue(ItemPath propertyPath, Class<T> type) {
PrismProperty<T> property = findProperty(propertyPath);
if (property == null) {
return null;
}
return property.getRealValue(type);
}
/**
* Convenience method. Works only on single-valued containers.
*/
public void add(Item<?,?> item) throws SchemaException {
checkMutability();
getValue().add(item);
}
public PrismContainerValue<C> createNewValue() {
checkMutability();
PrismContainerValue<C> pValue = new PrismContainerValue<C>(prismContext);
try {
// No need to check uniqueness, we know that this value is new and therefore
// it will change anyway and therefore the check is pointless.
// However, the check is expensive (especially if there are many values).
// So we are happy to avoid it.
add(pValue, false);
} catch (SchemaException e) {
// This should not happen
throw new SystemException("Internal Error: "+e.getMessage(),e);
}
return pValue;
}
public void mergeValues(PrismContainer<C> other) throws SchemaException {
mergeValues(other.getValues());
}
public void mergeValues(Collection<PrismContainerValue<C>> otherValues) throws SchemaException {
for (PrismContainerValue<C> otherValue : otherValues) {
mergeValue(otherValue);
}
}
public void mergeValue(PrismContainerValue<C> otherValue) throws SchemaException {
checkMutability();
Iterator<PrismContainerValue<C>> iterator = getValues().iterator();
while (iterator.hasNext()) {
PrismContainerValue<C> thisValue = iterator.next();
if (thisValue.equals(otherValue)) {
// Same values, nothing to merge
return;
}
if (thisValue.getId() != null && thisValue.getId().equals(otherValue.getId())) {
// Different value but same id. New value overwrites.
iterator.remove();
}
}
PrismContainerValue<C> clonedOtherValue = otherValue.clone();
if (getDefinition() != null) {
clonedOtherValue.applyDefinition(getDefinition());
}
add(clonedOtherValue);
}
/**
* Remove all empty values
*/
public void trim() {
Iterator<PrismContainerValue<C>> iterator = getValues().iterator();
while (iterator.hasNext()) {
PrismContainerValue<C> pval = iterator.next();
if (pval.isEmpty()) {
checkMutability();
iterator.remove();
}
}
}
/**
* Returns applicable property container definition.
* <p/>
* May return null if no definition is applicable or the definition is not
* know.
*
* @return applicable property container definition
*/
public PrismContainerDefinition<C> getDefinition() {
return (PrismContainerDefinition<C>) definition;
}
/**
* Sets applicable property container definition.
*
* @param definition the definition to set
*/
public void setDefinition(PrismContainerDefinition<C> definition) {
checkMutability();
this.definition = definition;
}
@Override
public void applyDefinition(PrismContainerDefinition<C> definition) throws SchemaException {
checkMutability();
if (definition == null) {
return;
}
if (!(definition instanceof PrismContainerDefinition)) {
throw new IllegalArgumentException("Cannot apply "+definition+" to container " + this);
}
this.compileTimeClass = ((PrismContainerDefinition<C>)definition).getCompileTimeClass();
super.applyDefinition(definition);
}
public <IV extends PrismValue,ID extends ItemDefinition,I extends Item<IV,ID>> I findItem(QName itemQName, Class<I> type) {
try {
return findCreateItem(itemQName, type, false);
} catch (SchemaException e) {
// This should not happen
throw new SystemException("Internal Error: "+e.getMessage(),e);
}
}
/**
* Returns true if the object and all contained prisms have proper definition.
*/
@Override
public boolean hasCompleteDefinition() {
if (getDefinition() == null) {
return false;
}
for (PrismContainerValue<C> cval: getValues()) {
if (!cval.hasCompleteDefinition()) {
return false;
}
}
return true;
}
@Override
public Object find(ItemPath path) {
if (path == null || path.isEmpty()) {
return this;
}
IdItemPathSegment idSegment = ItemPath.getFirstIdSegment(path);
PrismContainerValue<C> cval = findValue(idSegment);
if (cval == null) {
return null;
}
// descent to the correct value
ItemPath rest = ItemPath.pathRestStartingWithName(path);
return cval.find(rest);
}
@Override
public <IV extends PrismValue,ID extends ItemDefinition> PartiallyResolvedItem<IV,ID> findPartial(ItemPath path) {
if (path == null || path.isEmpty()) {
return new PartiallyResolvedItem<IV,ID>((Item<IV,ID>) this, null);
}
IdItemPathSegment idSegment = ItemPath.getFirstIdSegment(path);
PrismContainerValue<C> cval = findValue(idSegment);
if (cval == null) {
return null;
}
// descent to the correct value
ItemPath rest = ItemPath.pathRestStartingWithName(path);
return cval.findPartial(rest);
}
public <IV extends PrismValue,ID extends ItemDefinition> Item<IV,ID> findItem(QName itemQName) {
try {
return findCreateItem(itemQName, Item.class, false);
} catch (SchemaException e) {
// This should not happen
throw new SystemException("Internal Error: "+e.getMessage(),e);
}
}
<IV extends PrismValue,ID extends ItemDefinition,I extends Item<IV,ID>> I findCreateItem(QName itemQName, Class<I> type, boolean create) throws SchemaException {
return getValue().findCreateItem(itemQName, type, null, create);
}
public <IV extends PrismValue,ID extends ItemDefinition,I extends Item<IV,ID>> I findItem(ItemPath propPath, Class<I> type) {
try {
return findCreateItem(propPath, type, null, false);
} catch (SchemaException e) {
// This should not happen
throw new SystemException("Internal Error: "+e.getMessage(),e);
}
}
public <IV extends PrismValue,ID extends ItemDefinition> Item<IV,ID> findItem(ItemPath propPath) {
try {
return findCreateItem(propPath, Item.class, null, false);
} catch (SchemaException e) {
// This should not happen
throw new SystemException("Internal Error: "+e.getMessage(),e);
}
}
<IV extends PrismValue,ID extends ItemDefinition,I extends Item<IV,ID>> I findCreateItem(ItemPath itemPath, Class<I> type, ID itemDefinition, boolean create) throws SchemaException {
if (itemPath == null || itemPath.isEmpty()) {
throw new IllegalArgumentException("Empty path specified");
}
if (itemPath.isEmpty()) {
// This is the end ...
if (type.isAssignableFrom(getClass())) {
return (I) this;
} else {
if (create) {
throw new IllegalStateException("The " + type.getSimpleName() + " " + getElementName() + " cannot be created because "
+ this.getClass().getSimpleName() + " with the same name exists");
} else {
return null;
}
}
}
IdItemPathSegment idSegment = ItemPath.getFirstIdSegment(itemPath);
PrismContainerValue<C> cval = findValue(idSegment);
if (cval == null) {
return null;
}
// descent to the correct value
ItemPath rest = ItemPath.pathRestStartingWithName(itemPath);
return cval.findCreateItem(rest, type, itemDefinition, create);
}
public PrismContainerValue<C> findValue(long id) {
for (PrismContainerValue<C> pval : getValues()) {
if (id == pval.getId()) {
return pval;
}
}
return null;
}
private PrismContainerValue<C> findValue(IdItemPathSegment idSegment) {
Long id = null;
if (idSegment != null) {
id = idSegment.getId();
}
// Otherwise descent to the correct value
if (id == null) {
if (canAssumeSingleValue()) {
return getValue();
} else {
throw new IllegalArgumentException("Attempt to get segment without an ID from a multi-valued container "+ getElementName());
}
} else {
for (PrismContainerValue<C> pval : getValues()) {
if (id.equals(pval.getId())) {
return pval;
}
}
return null;
}
}
public <T extends Containerable> PrismContainer<T> findContainer(ItemPath path) {
return findItem(path, PrismContainer.class);
}
public <T extends Containerable> PrismContainer<T> findContainer(QName containerName) {
return findItem(containerName, PrismContainer.class);
}
public <T> PrismProperty<T> findProperty(ItemPath path) {
return findItem(path, PrismProperty.class);
}
public <T> PrismProperty<T> findProperty(QName propertyQName) {
return findItem(propertyQName, PrismProperty.class);
}
public PrismReference findReference(ItemPath path) {
return findItem(path, PrismReference.class);
}
public PrismReference findReference(QName referenceQName) {
return findItem(referenceQName, PrismReference.class);
}
public PrismReference findReferenceByCompositeObjectElementName(QName elementName) {
return getValue().findReferenceByCompositeObjectElementName(elementName);
}
public <IV extends PrismValue,ID extends ItemDefinition,I extends Item<IV,ID>> I findOrCreateItem(ItemPath containerPath, Class<I> type) throws SchemaException {
return findCreateItem(containerPath, type, null, true);
}
// The "definition" parameter provides definition of item to create, in case that the container does not have
// the definition (e.g. in case of "extension" containers)
public <IV extends PrismValue,ID extends ItemDefinition,I extends Item<IV,ID>> I findOrCreateItem(ItemPath containerPath, Class<I> type, ID definition) throws SchemaException {
if (PrismObject.class.isAssignableFrom(type)) {
throw new IllegalArgumentException("It makes no sense to find object in a container (class)");
}
if (definition instanceof PrismObjectDefinition<?>) {
throw new IllegalArgumentException("It makes no sense to find object in a container (definition)");
}
return findCreateItem(containerPath, type, definition, true);
}
public <T extends Containerable> PrismContainer<T> findOrCreateContainer(ItemPath containerPath) throws SchemaException {
return findCreateItem(containerPath, PrismContainer.class, null, true);
}
public <T extends Containerable> PrismContainer<T> findOrCreateContainer(QName containerName) throws SchemaException {
return findCreateItem(containerName, PrismContainer.class, true);
}
public <T> PrismProperty<T> findOrCreateProperty(ItemPath propertyPath) throws SchemaException {
return findCreateItem(propertyPath, PrismProperty.class, null, true);
}
public <T> PrismProperty<T> findOrCreateProperty(QName propertyName) throws SchemaException {
return findCreateItem(propertyName, PrismProperty.class, true);
}
public PrismReference findOrCreateReference(ItemPath propertyPath) throws SchemaException {
return findCreateItem(propertyPath, PrismReference.class, null, true);
}
public PrismReference findOrCreateReference(QName propertyName) throws SchemaException {
return findCreateItem(propertyName, PrismReference.class, true);
}
/**
* Convenience method. Works only on single-valued containers.
*/
public void remove(Item<?,?> item) {
getValue().remove(item);
}
public void removeProperty(QName propertyQName) {
removeItem(new ItemPath(propertyQName), PrismProperty.class);
}
public void removeProperty(ItemPath path) {
removeItem(path, PrismProperty.class);
}
public void removeContainer(QName containerQName) {
removeItem(new ItemPath(containerQName), PrismContainer.class);
}
public void removeContainer(ItemPath path) {
removeItem(path, PrismContainer.class);
}
public void removeReference(QName referenceQName) {
removeItem(new ItemPath(referenceQName), PrismReference.class);
}
public void removeReference(ItemPath path) {
removeItem(path, PrismReference.class);
}
public <IV extends PrismValue,ID extends ItemDefinition,I extends Item<IV,ID>> void removeItem(ItemPath path, Class<I> itemType) {
IdItemPathSegment idSegment = ItemPath.getFirstIdSegment(path);
PrismContainerValue<C> cval = findValue(idSegment);
if (cval == null) {
return;
}
cval.removeItem(ItemPath.pathRestStartingWithName(path.rest()), itemType);
}
// Expects that the "self" path segment is NOT included in the basePath
// is this method used anywhere?
// void addItemPathsToList(ItemPath basePath, Collection<ItemPath> list) {
// boolean addIds = true;
// if (getDefinition() != null) {
// if (getDefinition().isSingleValue()) {
// addIds = false;
// }
// }
// for (PrismContainerValue<V> pval: getValues()) {
// ItemPath subpath = null;
// ItemPathSegment segment = null;
// if (addIds) {
// subpath = basePath.subPath(new IdItemPathSegment(pval.getId())).subPath(new NameItemPathSegment(getElementName()));
// } else {
// subpath = basePath.subPath(new NameItemPathSegment(getElementName()));
// }
// pval.addItemPathsToList(subpath, list);
// }
// }
@Override
public ContainerDelta<C> createDelta() {
return new ContainerDelta<C>(getPath(), getDefinition(), getPrismContext());
}
@Override
public ContainerDelta<C> createDelta(ItemPath path) {
return new ContainerDelta<C>(path, getDefinition(), getPrismContext());
}
public boolean isEmpty() {
for(PrismContainerValue<C> pval : getValues()) {
if (!pval.isEmpty()) {
return false;
}
}
return true;
}
@Override
protected void checkDefinition(PrismContainerDefinition<C> def) {
if (!(def instanceof PrismContainerDefinition<?>)) {
throw new IllegalArgumentException("Definition "+def+" cannot be applied to container "+this);
}
}
@Override
public void checkConsistenceInternal(Itemable rootItem, boolean requireDefinitions,
boolean prohibitRaw, ConsistencyCheckScope scope) {
checkIds();
if (scope.isThorough()) {
// Containers that are from run-time schema cannot have compile-time class.
if (getDefinition() != null && !getDefinition().isRuntimeSchema()) {
if (getCompileTimeClass() == null) {
throw new IllegalStateException("No compile-time class in "+this+" ("+getPath()+" in "+rootItem+")");
}
}
}
super.checkConsistenceInternal(rootItem, requireDefinitions, prohibitRaw, scope);
}
private void checkIds() {
Set<Long> oidsUsed = new HashSet<>();
for (PrismContainerValue value : getValues()) {
Long id = value.getId();
if (id != null) {
if (oidsUsed.contains(id)) {
throw new IllegalArgumentException("There are more container values with the id of " + id + " in " + getElementName());
} else {
oidsUsed.add(id);
}
}
}
}
@Override
public void assertDefinitions(boolean tolarateRaw, String sourceDescription) throws SchemaException {
super.assertDefinitions(tolarateRaw, sourceDescription);
for (PrismContainerValue<C> val: getValues()) {
val.assertDefinitions(tolarateRaw, this.toString()+" in "+sourceDescription);
}
}
public ContainerDelta<C> diff(PrismContainer<C> other) {
return (ContainerDelta<C>) super.diff(other);
}
public ContainerDelta<C> diff(PrismContainer<C> other, boolean ignoreMetadata, boolean isLiteral) {
return (ContainerDelta<C>) super.diff(other, true, false);
}
public List<? extends ItemDelta> diffModifications(PrismContainer<C> other) {
return diffModifications(other, true, false);
}
public List<? extends ItemDelta> diffModifications(PrismContainer<C> other, boolean ignoreMetadata, boolean isLiteral) {
List<? extends ItemDelta> itemDeltas = new ArrayList<>();
diffInternal(other, itemDeltas, ignoreMetadata, isLiteral);
return itemDeltas;
}
@Override
public PrismContainer<C> clone() {
PrismContainer<C> clone = new PrismContainer<C>(getElementName(), getDefinition(), prismContext);
copyValues(clone);
return clone;
}
protected void copyValues(PrismContainer<C> clone) {
super.copyValues(clone);
clone.compileTimeClass = this.compileTimeClass;
for (PrismContainerValue<C> pval : getValues()) {
try {
clone.add(pval.clone());
} catch (SchemaException e) {
// This should not happen
throw new SystemException("Internal Error: "+e.getMessage(),e);
}
}
}
public PrismContainerDefinition<C> deepCloneDefinition(boolean ultraDeep) {
PrismContainerDefinition<C> clonedDef = (PrismContainerDefinition<C>) getDefinition().deepClone(ultraDeep);
propagateDeepCloneDefinition(ultraDeep, clonedDef);
setDefinition(clonedDef);
return clonedDef;
}
@Override
protected void propagateDeepCloneDefinition(boolean ultraDeep, PrismContainerDefinition<C> clonedDef) {
for(PrismContainerValue<C> cval: getValues()) {
cval.deepCloneDefinition(ultraDeep, clonedDef);
}
}
@Override
public boolean containsEquivalentValue(PrismContainerValue<C> value) {
if (value.getId() == null) {
return super.contains(value, true);
} else {
return super.contains(value, false);
}
}
@Override
public void accept(Visitor visitor, ItemPath path, boolean recursive) {
if (path == null || path.isEmpty()) {
// We are at the end of path, continue with regular visits all the way to the bottom
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
for (PrismContainerValue<C> cval: getValues()) {
cval.accept(visitor, rest, recursive);
}
} else {
PrismContainerValue<C> cval = findValue(idSegment);
if (cval != null) {
cval.accept(visitor, rest, recursive);
}
}
}
}
@Override
public int hashCode() {
int result = super.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
return true;
}
/**
* This method ignores some part of the object during comparison (e.g. source demarcation in values)
* These methods compare the "meaningful" parts of the objects.
*/
public boolean equivalent(Object obj) {
// Alibistic implementation for now. But should work well.
if (this == obj)
return true;
if (getClass() != obj.getClass())
return false;
PrismContainer other = (PrismContainer) obj;
//todo probably better comparation (ignore some part of object)
return equals(other);
// ObjectDelta<T> delta = compareTo(other);
// return delta.isEmpty();
}
@Override
public String toString() {
return getDebugDumpClassName() + "(" + getElementName() + "):"
+ getValues();
}
@Override
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < indent; i++) {
sb.append(INDENT_STRING);
}
if (DebugUtil.isDetailedDebugDump()) {
sb.append(getDebugDumpClassName()).append(": ");
}
sb.append(DebugUtil.formatElementName(getElementName()));
sb.append(": ");
sb.append(additionalDumpDescription());
PrismContainerDefinition<C> def = getDefinition();
if (DebugUtil.isDetailedDebugDump()) {
if (def != null) {
sb.append(" def(");
sb.append(PrettyPrinter.prettyPrint(def.getTypeName()));
if (def.isRuntimeSchema()) {
sb.append(",runtime");
}
if (def.isDynamic()) {
sb.append(",dyn");
}
sb.append(")");
}
}
Iterator<PrismContainerValue<C>> i = getValues().iterator();
if (i.hasNext()) {
sb.append("\n");
}
while (i.hasNext()) {
PrismContainerValue<C> pval = i.next();
sb.append(pval.debugDump(indent + 1));
if (i.hasNext()) {
sb.append("\n");
}
}
return sb.toString();
}
protected String additionalDumpDescription() {
return "";
}
public static <V extends Containerable> PrismContainer<V> newInstance(PrismContext prismContext, QName type) throws SchemaException {
PrismContainerDefinition<V> definition = prismContext.getSchemaRegistry().findContainerDefinitionByType(type);
if (definition == null) {
throw new SchemaException("Definition for " + type + " couldn't be found");
}
return definition.instantiate();
}
/**
* Return a human readable name of this class suitable for logs.
*/
@Override
protected String getDebugDumpClassName() {
return "PC";
}
public static <V extends PrismContainerValue> void createParentIfNeeded(V value, ItemDefinition definition) throws SchemaException {
if (value.getParent() != null) {
return;
}
if (!(definition instanceof PrismContainerDefinition)) {
throw new SchemaException("Missing or invalid definition for a PrismContainer: " + definition);
}
PrismContainer<?> rv = (PrismContainer) definition.instantiate();
rv.add(value);
}
}