/
PrismContainerValue.java
1575 lines (1420 loc) · 55.1 KB
/
PrismContainerValue.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;
import java.lang.reflect.Modifier;
import java.util.*;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.marshaller.JaxbDomHack;
import com.evolveum.midpoint.prism.path.IdItemPathSegment;
import com.evolveum.midpoint.prism.path.ItemPath;
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.QNameUtil;
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 org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author semancik
*
*/
public class PrismContainerValue<C extends Containerable> extends PrismValue implements DebugDumpable {
private static final Trace LOGGER = TraceManager.getTrace(PrismContainerValue.class);
// This is list. We need to maintain the order internally to provide consistent
// output in DOM and other ordering-sensitive representations
protected List<Item<?,?>> items = null;
private Long id;
private C containerable = null;
// Definition of this value. Usually it is the same as CTD declared in the parent container.
// However, in order to support polymorphism (as well as parent-less values) we distinguish between PC and PCV type definition.
protected ComplexTypeDefinition complexTypeDefinition = null;
public PrismContainerValue() {
}
public PrismContainerValue(C containerable) {
this(containerable, null);
}
public PrismContainerValue(PrismContext prismContext) {
this(null, prismContext);
}
public PrismContainerValue(C containerable, PrismContext prismContext) {
super(prismContext);
this.containerable = containerable;
if (prismContext != null) {
getComplexTypeDefinition(); // to determine CTD (could be also called with null prismContext, but non-null prismContext provides additional information in some cases)
}
}
public PrismContainerValue(OriginType type, Objectable source, PrismContainerable container, Long id, ComplexTypeDefinition complexTypeDefinition, PrismContext prismContext) {
super(prismContext, type, source, container);
this.id = id;
this.complexTypeDefinition = complexTypeDefinition;
}
@Override
public PrismContext getPrismContext() {
if (prismContext != null) {
return prismContext;
}
if (complexTypeDefinition != null && complexTypeDefinition.getPrismContext() != null) {
return complexTypeDefinition.getPrismContext();
}
if (getParent() != null) {
return getParent().getPrismContext();
}
return null;
}
// Primarily for testing
public PrismContext getPrismContextLocal() {
return prismContext;
}
/**
* Returns a set of items that the property container contains. The items may be properties or inner property containers.
* <p/>
* The set may be null. In case there are no properties an empty set is
* returned.
* <p/>
* Returned set is mutable. Live object is returned.
*
* @return set of items that the property container contains.
*/
public List<Item<?,?>> getItems() {
if (isImmutable()) {
return Collections.unmodifiableList(items);
} else {
return items;
}
}
public Item<?,?> getNextItem(Item<?,?> referenceItem) {
if (items == null) {
return null;
}
Iterator<Item<?,?>> iterator = items.iterator();
while (iterator.hasNext()) {
Item<?,?> item = iterator.next();
if (item == referenceItem) {
if (iterator.hasNext()) {
return iterator.next();
} else {
return null;
}
}
}
throw new IllegalArgumentException("Item "+referenceItem+" is not part of "+this);
}
public Item<?,?> getPreviousItem(Item<?,?> referenceItem) {
if (items == null){
return null;
}
Item<?,?> lastItem = null;
Iterator<Item<?,?>> iterator = items.iterator();
//noinspection WhileLoopReplaceableByForEach
while (iterator.hasNext()) {
Item<?,?> item = iterator.next();
if (item == referenceItem) {
return lastItem;
}
lastItem = item;
}
throw new IllegalArgumentException("Item "+referenceItem+" is not part of "+this);
}
/**
* Returns a set of properties that the property container contains.
* <p/>
* The set must not be null. In case there are no properties an empty set is
* returned.
* <p/>
* Returned set is immutable! Any change to it will be ignored.
*
* @return set of properties that the property container contains.
*/
public Set<PrismProperty<?>> getProperties() {
Set<PrismProperty<?>> properties = new HashSet<PrismProperty<?>>();
if (items != null) {
for (Item<?,?> item : getItems()) {
if (item instanceof PrismProperty) {
properties.add((PrismProperty<?>) item);
}
}
}
return properties;
}
public Long getId() {
return id;
}
public void setId(Long id) {
checkMutability();
this.id = id;
}
@SuppressWarnings("unchecked")
public PrismContainerable<C> getParent() {
Itemable parent = super.getParent();
if (parent == null) {
return null;
}
if (!(parent instanceof PrismContainerable)) {
throw new IllegalStateException("Expected that parent of "+PrismContainerValue.class.getName()+" will be "+
PrismContainerable.class.getName()+", but it is "+parent.getClass().getName());
}
return (PrismContainerable<C>)parent;
}
@SuppressWarnings("unchecked")
public PrismContainer<C> getContainer() {
Itemable parent = super.getParent();
if (parent == null) {
return null;
}
if (!(parent instanceof PrismContainer)) {
throw new IllegalStateException("Expected that parent of "+PrismContainerValue.class.getName()+" will be "+
PrismContainer.class.getName()+", but it is "+parent.getClass().getName());
}
return (PrismContainer<C>)super.getParent();
}
@NotNull
public ItemPath getPath() {
Itemable parent = getParent();
ItemPath parentPath = ItemPath.EMPTY_PATH;
if (parent != null) {
parentPath = parent.getPath();
}
if (getId() != null) {
return ItemPath.subPath(parentPath, new IdItemPathSegment(getId()));
} else {
return parentPath;
}
}
// For compatibility with other PrismValue types
public C getValue() {
return asContainerable();
}
@SuppressWarnings("unchecked")
public C asContainerable() {
if (containerable != null) {
return containerable;
}
if (getParent() == null && complexTypeDefinition == null) {
throw new IllegalStateException("Cannot represent container value without a parent and complex type definition as containerable; value: " + this);
}
return asContainerableInternal(resolveClass(null));
}
// returned class must be of type 'requiredClass' (or any of its subtypes)
public C asContainerable(Class<C> requiredClass) {
if (containerable != null) {
return containerable;
}
return asContainerableInternal(resolveClass(requiredClass));
}
private Class<C> resolveClass(@Nullable Class<C> requiredClass) {
if (complexTypeDefinition != null && complexTypeDefinition.getCompileTimeClass() != null) {
Class<?> actualClass = complexTypeDefinition.getCompileTimeClass();
if (requiredClass != null && !requiredClass.isAssignableFrom(actualClass)) {
throw new IllegalStateException("asContainerable was called to produce " + requiredClass
+ ", but the actual class in PCV is " + actualClass);
} else {
return (Class<C>) actualClass;
}
} else {
PrismContainerable parent = getParent();
if (parent != null) {
Class<?> parentClass = parent.getCompileTimeClass();
if (parentClass != null) {
if (requiredClass != null && !requiredClass.isAssignableFrom(parentClass)) {
// mismatch; but this can occur (see ShadowAttributesType vs ShadowIdentifiersType in ShadowAssociationType)
// but TODO maybe this is only a workaround and the problem is in the schema itself (?)
return requiredClass;
} else {
return (Class<C>) parentClass;
}
}
}
}
return requiredClass;
}
private C asContainerableInternal(Class<C> clazz) {
if (clazz == null) {
String elementName = getParent() != null ? String.valueOf(getParent().getElementName()) : String.valueOf(this);
throw new SystemException("Unknown compile time class of container value of '" + elementName + "'.");
}
if (Modifier.isAbstract(clazz.getModifiers())) {
throw new SystemException("Can't create instance of class '" + clazz.getSimpleName() + "', it's abstract.");
}
try {
if (prismContext != null) {
containerable = clazz.getConstructor(PrismContext.class).newInstance(prismContext);
} else {
containerable = clazz.newInstance();
}
containerable.setupContainerValue(this);
return containerable;
} catch (SystemException ex) {
throw ex;
} catch (Exception ex) {
throw new SystemException("Couldn't create jaxb object instance of '" + clazz + "': "+ex.getMessage(), ex);
}
}
public Collection<QName> getPropertyNames() {
Collection<QName> names = new HashSet<>();
for (PrismProperty<?> prop: getProperties()) {
names.add(prop.getElementName());
}
return names;
}
public <IV extends PrismValue,ID extends ItemDefinition> boolean add(Item<IV,ID> item) throws SchemaException {
return add(item, true);
}
/**
* Adds an item to a property container.
*
* @param item item to add.
* @throws SchemaException
* @throws IllegalArgumentException an attempt to add value that already exists
*/
public <IV extends PrismValue,ID extends ItemDefinition> boolean add(Item<IV,ID> item, boolean checkUniqueness) throws SchemaException {
checkMutability();
if (item.getElementName() == null) {
throw new IllegalArgumentException("Cannot add item without a name to value of container "+getParent());
}
if (checkUniqueness && findItem(item.getElementName(), Item.class) != null) {
throw new IllegalArgumentException("Item " + item.getElementName() + " is already present in " + this.getClass().getSimpleName());
}
item.setParent(this);
PrismContext prismContext = getPrismContext();
if (prismContext != null) {
item.setPrismContext(prismContext);
}
if (getComplexTypeDefinition() != null && item.getDefinition() == null) {
item.applyDefinition((ID)determineItemDefinition(item.getElementName(), getComplexTypeDefinition()), false);
}
if (items == null) {
items = new ArrayList<>();
}
return items.add(item);
}
/**
* Merges the provided item into this item. The values are joined together.
* Returns true if new item or value was added.
*/
public <IV extends PrismValue,ID extends ItemDefinition> boolean merge(Item<IV,ID> item) throws SchemaException {
checkMutability();
Item<IV, ID> existingItem = findItem(item.getElementName(), Item.class);
if (existingItem == null) {
return add(item);
} else {
boolean changed = false;
for (IV newVal: item.getValues()) {
if (existingItem.add((IV) newVal.clone())) {
changed = true;
}
}
return changed;
}
}
/**
* Subtract the provided item from this item. The values of the provided item are deleted
* from this item.
* Returns true if this item was changed.
*/
public <IV extends PrismValue,ID extends ItemDefinition> boolean subtract(Item<IV,ID> item) throws SchemaException {
checkMutability();
Item<IV,ID> existingItem = findItem(item.getElementName(), Item.class);
if (existingItem == null) {
return false;
} else {
boolean changed = false;
for (IV newVal: item.getValues()) {
if (existingItem.remove(newVal)) {
changed = true;
}
}
return changed;
}
}
/**
* Adds an item to a property container. Existing value will be replaced.
*
* @param item item to add.
*/
public <IV extends PrismValue,ID extends ItemDefinition> void addReplaceExisting(Item<IV,ID> item) throws SchemaException {
checkMutability();
if (item == null) {
return;
}
Item<IV,ID> existingItem = findItem(item.getElementName(), Item.class);
if (existingItem != null && items != null) {
items.remove(existingItem);
existingItem.setParent(null);
}
add(item);
}
public <IV extends PrismValue,ID extends ItemDefinition> void remove(Item<IV,ID> item) {
Validate.notNull(item, "Item must not be null.");
checkMutability();
Item<IV,ID> existingItem = findItem(item.getElementName(), Item.class);
if (existingItem != null && items != null) {
items.remove(existingItem);
existingItem.setParent(null);
}
}
public void removeAll() {
checkMutability();
if (items == null){
return;
}
Iterator<Item<?,?>> iterator = items.iterator();
while (iterator.hasNext()) {
Item<?,?> item = iterator.next();
item.setParent(null);
iterator.remove();
}
}
/**
* Adds a collection of items to a property container.
*
* @param itemsToAdd items to add
* @throws IllegalArgumentException an attempt to add value that already exists
*/
public void addAll(Collection<? extends Item<?,?>> itemsToAdd) throws SchemaException {
for (Item<?,?> item : itemsToAdd) {
add(item);
}
}
/**
* Adds a collection of items to a property container. Existing values will be replaced.
*
* @param itemsToAdd items to add
*/
public void addAllReplaceExisting(Collection<? extends Item<?,?>> itemsToAdd) throws SchemaException {
checkMutability();
// Check for conflicts, remove conflicting values
for (Item<?,?> item : itemsToAdd) {
Item<?,?> existingItem = findItem(item.getElementName(), Item.class);
if (existingItem != null && items != null) {
items.remove(existingItem);
}
}
addAll(itemsToAdd);
}
public <IV extends PrismValue,ID extends ItemDefinition> void replace(Item<IV,ID> oldItem, Item<IV,ID> newItem) throws SchemaException {
remove(oldItem);
add(newItem);
}
public void clear() {
checkMutability();
if (items != null) {
items.clear();
}
}
public boolean contains(Item item) {
return items != null && items.contains(item);
}
public boolean contains(QName itemName) {
return findItem(itemName) != null;
}
@Override
public Object find(ItemPath path) {
if (path == null || path.isEmpty()) {
return this;
}
ItemPathSegment first = path.first();
if (!(first instanceof NameItemPathSegment)) {
throw new IllegalArgumentException("Attempt to lookup item using a non-name path "+path+" in "+this);
}
QName subName = ((NameItemPathSegment)first).getName();
ItemPath rest = path.rest();
Item<?,?> subItem = findItem(subName);
if (subItem == null) {
return null;
}
return subItem.find(rest);
}
@Override
public <IV extends PrismValue,ID extends ItemDefinition> PartiallyResolvedItem<IV,ID> findPartial(ItemPath path) {
if (path == null || path.isEmpty()) {
// Incomplete path
return null;
}
ItemPathSegment first = path.first();
if (!(first instanceof NameItemPathSegment)) {
throw new IllegalArgumentException("Attempt to lookup item using a non-name path "+path+" in "+this);
}
QName subName = ((NameItemPathSegment)first).getName();
ItemPath rest = path.rest();
Item<?,?> subItem = findItem(subName);
if (subItem == null) {
return null;
}
return subItem.findPartial(rest);
}
@SuppressWarnings("unchecked")
public <X> PrismProperty<X> findProperty(QName propertyQName) {
return findItem(propertyQName, PrismProperty.class);
}
public <X> PrismProperty<X> findProperty(ItemPath propertyPath) {
return (PrismProperty) findItem(propertyPath);
}
/**
* Finds a specific property in the container by definition.
* <p/>
* Returns null if nothing is found.
*
* @param propertyDefinition property definition to find.
* @return found property or null
*/
public <X> PrismProperty<X> findProperty(PrismPropertyDefinition<X> propertyDefinition) {
if (propertyDefinition == null) {
throw new IllegalArgumentException("No property definition");
}
return findProperty(propertyDefinition.getName());
}
public <X extends Containerable> PrismContainer<X> findContainer(QName containerName) {
return findItem(containerName, PrismContainer.class);
}
public PrismReference findReference(QName elementName) {
return findItem(elementName, PrismReference.class);
}
public PrismReference findReferenceByCompositeObjectElementName(QName elementName) {
if (items == null){
return null;
}
for (Item item: items) {
if (item instanceof PrismReference) {
PrismReference ref = (PrismReference)item;
PrismReferenceDefinition refDef = ref.getDefinition();
if (refDef != null) {
if (elementName.equals(refDef.getCompositeObjectElementName())) {
return ref;
}
}
}
}
return null;
}
public <IV extends PrismValue,ID extends ItemDefinition, I extends Item<IV,ID>> I findItem(QName itemName, Class<I> type) {
try {
return findCreateItem(itemName, 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(QName itemName) {
try {
return findCreateItem(itemName, Item.class, 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 itemPath) {
try {
return findCreateItem(itemPath, Item.class, null, false);
} catch (SchemaException e) {
// This should not happen
throw new SystemException("Internal Error: "+e.getMessage(),e);
}
}
@SuppressWarnings("unchecked")
<IV extends PrismValue,ID extends ItemDefinition, I extends Item<IV,ID>> I findCreateItem(QName itemName, Class<I> type, ID itemDefinition, boolean create) throws SchemaException {
Item<IV,ID> item = findItemByQName(itemName);
if (item != null) {
if (type.isAssignableFrom(item.getClass())) {
return (I) item;
} else {
if (create) {
throw new IllegalStateException("The " + type.getSimpleName() + " cannot be created because "
+ item.getClass().getSimpleName() + " with the same name exists ("+item.getElementName()+")");
} else {
return null;
}
}
}
if (create) { // todo treat unqualified names
return createSubItem(itemName, type, itemDefinition);
} else {
return null;
}
}
public <IV extends PrismValue,ID extends ItemDefinition, I extends Item<IV,ID>> I findItem(ItemDefinition itemDefinition, Class<I> type) {
if (itemDefinition == null) {
throw new IllegalArgumentException("No item definition");
}
return findItem(itemDefinition.getName(), type);
}
// Expects that "self" path is NOT present in propPath
@SuppressWarnings("unchecked")
<IV extends PrismValue,ID extends ItemDefinition, I extends Item<IV,ID>> I findCreateItem(ItemPath propPath, Class<I> type, ID itemDefinition, boolean create) throws SchemaException {
ItemPathSegment first = propPath.first();
if (!(first instanceof NameItemPathSegment)) {
throw new IllegalArgumentException("Attempt to lookup item using a non-name path "+propPath+" in "+this);
}
QName subName = ((NameItemPathSegment)first).getName();
ItemPath rest = propPath.rest();
I item = (I) findItemByQName(subName);
if (item != null) {
if (rest.isEmpty()) {
if (type.isAssignableFrom(item.getClass())) {
return (I)item;
} else {
if (create) {
throw new SchemaException("The " + type.getSimpleName() + " cannot be created because "
+ item.getClass().getSimpleName() + " with the same name exists ("+item.getElementName()+")");
} else {
return null;
}
}
} else {
// Go deeper
if (item instanceof PrismContainer) {
return ((PrismContainer<?>)item).findCreateItem(rest, type, itemDefinition, create);
} else {
if (create) {
throw new SchemaException("The " + type.getSimpleName() + " cannot be created because "
+ item.getClass().getSimpleName() + " with the same name exists ("+item.getElementName()+")");
} else {
// Return the item for non-container even if the path is non-empty
// FIXME: This is not the best solution but it is needed to be able to look inside properties
// such as PolyString
if (type.isAssignableFrom(item.getClass())) {
return item;
} else {
return null;
}
}
}
}
}
if (create) { // todo treat unqualified names
if (rest.isEmpty()) {
return createSubItem(subName, type, itemDefinition);
} else {
// Go deeper
PrismContainer<?> subItem = createSubItem(subName, PrismContainer.class, null);
return subItem.findCreateItem(rest, type, itemDefinition, create);
}
} else {
return null;
}
}
private <IV extends PrismValue,ID extends ItemDefinition> Item<IV,ID> findItemByQName(QName subName) throws SchemaException {
if (items == null) {
return null;
}
Item<IV,ID> matching = null;
for (Item<?,?> item : items) {
if (QNameUtil.match(subName, item.getElementName())) {
if (matching != null) {
String containerName = getParent() != null ? DebugUtil.formatElementName(getParent().getElementName()) : "";
throw new SchemaException("More than one items matching " + subName + " in container " + containerName);
} else {
matching = (Item<IV, ID>) item;
}
}
}
return matching;
}
private <IV extends PrismValue,ID extends ItemDefinition,I extends Item<IV,ID>> I createSubItem(QName name, Class<I> type, ID itemDefinition) throws SchemaException {
checkMutability();
// the item with specified name does not exist, create it now
I newItem = null;
if (itemDefinition == null) {
ComplexTypeDefinition ctd = getComplexTypeDefinition();
itemDefinition = determineItemDefinition(name, ctd);
if (ctd != null && itemDefinition == null) {
throw new SchemaException("No definition for item "+name+" in "+getParent());
}
}
if (itemDefinition != null) {
if (StringUtils.isNotBlank(name.getNamespaceURI())){
newItem = (I) itemDefinition.instantiate(name);
} else {
QName computed = new QName(itemDefinition.getNamespace(), name.getLocalPart());
newItem = (I) itemDefinition.instantiate(computed);
}
if (newItem instanceof PrismObject) {
throw new IllegalStateException("PrismObject instantiated as a subItem in "+this+" from definition "+itemDefinition);
}
} else {
newItem = Item.createNewDefinitionlessItem(name, type, prismContext);
if (newItem instanceof PrismObject) {
throw new IllegalStateException("PrismObject instantiated as a subItem in "+this+" as definitionless instance of class "+type);
}
}
if (type.isAssignableFrom(newItem.getClass())) {
add(newItem);
return newItem;
} else {
throw new IllegalStateException("The " + type.getSimpleName() + " cannot be created because the item should be of type "
+ newItem.getClass().getSimpleName() + " ("+newItem.getElementName()+")");
}
}
public <T extends Containerable> PrismContainer<T> findOrCreateContainer(QName containerName) throws SchemaException {
return findCreateItem(containerName, PrismContainer.class, null, true);
}
public PrismReference findOrCreateReference(QName referenceName) throws SchemaException {
return findCreateItem(referenceName, PrismReference.class, null, true);
}
public <IV extends PrismValue,ID extends ItemDefinition> Item<IV,ID> findOrCreateItem(QName containerName) throws SchemaException {
return findCreateItem(containerName, Item.class, null, true);
}
public <IV extends PrismValue,ID extends ItemDefinition,I extends Item<IV,ID>> I findOrCreateItem(QName containerName, Class<I> type) throws SchemaException {
return findCreateItem(containerName, type, null, true);
}
public <IV extends PrismValue,ID extends ItemDefinition,I extends Item<IV,ID>> I findOrCreateItem(ItemPath path, Class<I> type, ID definition) throws SchemaException {
return findCreateItem(path, type, definition, true);
}
public <X> PrismProperty<X> findOrCreateProperty(QName propertyQName) throws SchemaException {
PrismProperty<X> property = findItem(propertyQName, PrismProperty.class);
if (property != null) {
return property;
}
return createProperty(propertyQName);
}
public <X> PrismProperty<X> findOrCreateProperty(ItemPath propertyPath) throws SchemaException {
return findOrCreateItem(propertyPath, PrismProperty.class, null);
}
public <X> PrismProperty<X> findOrCreateProperty(PrismPropertyDefinition propertyDef) throws SchemaException {
PrismProperty<X> property = findItem(propertyDef.getName(), PrismProperty.class);
if (property != null) {
return property;
}
return createProperty(propertyDef);
}
// public PrismProperty findOrCreateProperty(PropertyPath parentPath, QName propertyQName, Class<?> valueClass) {
// PrismContainer container = findOrCreatePropertyContainer(parentPath);
// if (container == null) {
// throw new IllegalArgumentException("No container");
// }
// return container.findOrCreateProperty(propertyQName, valueClass);
// }
public <X extends Containerable> PrismContainer<X> createContainer(QName containerName) throws SchemaException {
checkMutability();
ComplexTypeDefinition complexTypeDefinition = getComplexTypeDefinition();
if (complexTypeDefinition == null) {
throw new IllegalStateException("No definition of container "+containerName);
}
PrismContainerDefinition<X> containerDefinition = complexTypeDefinition.findContainerDefinition(containerName);
if (containerDefinition == null) {
throw new IllegalArgumentException("No definition of container '" + containerName + "' in " + complexTypeDefinition);
}
PrismContainer<X> container = containerDefinition.instantiate();
add(container);
return container;
}
public <X> PrismProperty<X> createProperty(QName propertyName) throws SchemaException {
checkMutability();
PrismPropertyDefinition propertyDefinition = determineItemDefinition(propertyName, getComplexTypeDefinition());
if (propertyDefinition == null) {
// container has definition, but there is no property definition. This is either runtime schema
// or an error
if (getParent() != null && getDefinition() != null && !getDefinition().isRuntimeSchema()) { // TODO clean this up
throw new IllegalArgumentException("No definition for property "+propertyName+" in "+complexTypeDefinition);
}
}
PrismProperty<X> property;
if (propertyDefinition == null) {
property = new PrismProperty<X>(propertyName, prismContext); // Definitionless
} else {
property = propertyDefinition.instantiate();
}
add(property, false);
return property;
}
public <X> PrismProperty<X> createProperty(PrismPropertyDefinition propertyDefinition) throws SchemaException {
PrismProperty<X> property = propertyDefinition.instantiate();
add(property);
return property;
}
public void removeProperty(QName propertyName) {
removeProperty(new ItemPath(propertyName));
}
public void removeProperty(ItemPath propertyPath) {
removeItem(propertyPath, PrismProperty.class);
}
public void removeContainer(QName containerName) {
removeContainer(new ItemPath(containerName));
}
public void removeContainer(ItemPath itemPath) {
removeItem(itemPath, PrismContainer.class);
}
public void removeReference(QName name) {
removeReference(new ItemPath(name));
}
public void removeReference(ItemPath path) {
removeItem(path, PrismReference.class);
}
// Expects that "self" path is NOT present in propPath
<IV extends PrismValue,ID extends ItemDefinition,I extends Item<IV,ID>> void removeItem(ItemPath propPath, Class<I> itemType) {
checkMutability();
if (items == null){
return;
}
ItemPathSegment first = propPath.first();
if (!(first instanceof NameItemPathSegment)) {
throw new IllegalArgumentException("Attempt to remove item using a non-name path "+propPath+" in "+this);
}
QName subName = ((NameItemPathSegment)first).getName();
ItemPath rest = propPath.rest();
Iterator<Item<?,?>> itemsIterator = items.iterator();
while(itemsIterator.hasNext()) {
Item<?,?> item = itemsIterator.next();
if (subName.equals(item.getElementName())) {
if (!rest.isEmpty() && item instanceof PrismContainer) {
((PrismContainer<?>)item).removeItem(propPath, itemType);
return;
} else {
if (itemType.isAssignableFrom(item.getClass())) {
itemsIterator.remove();
} else {
throw new IllegalArgumentException("Attempt to remove item "+subName+" from "+this+
" of type "+itemType+" while the existing item is of incompatible type "+item.getClass());
}
}
}
}
}
public void setPropertyRealValue(QName propertyName, Object realValue, PrismContext prismContext) throws SchemaException {
checkMutability();
PrismProperty<?> property = findOrCreateProperty(propertyName);
property.setRealValue(realValue);
if (property.getPrismContext() == null) {
property.setPrismContext(prismContext);
}
}
public <T> T getPropertyRealValue(QName propertyName, Class<T> type) {
PrismProperty<T> property = findProperty(propertyName);
if (property == null) { // when using sql repo, even non-existing properties do not have 'null' here
return null;
}
return property.getRealValue(type);
}
@Override
public void recompute(PrismContext prismContext) {
// Nothing to do. The subitems should be already recomputed as they are added to this container.
}
@Override
public void accept(Visitor visitor) {
super.accept(visitor);
if (items != null) {
for (Item<?,?> item : getItems()) {
item.accept(visitor);
}
}
}
@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 {
ItemPathSegment first = path.first();
if (!(first instanceof NameItemPathSegment)) {
throw new IllegalArgumentException("Attempt to lookup item using a non-name path "+path+" in "+this);
}
QName subName = ((NameItemPathSegment)first).getName();
ItemPath rest = path.rest();
if (items != null) {
for (Item<?,?> item : items) { // todo unqualified names!
if (first.isWildcard() || subName.equals(item.getElementName())) {
item.accept(visitor, rest, recursive);
}
}
}
}
}
public boolean hasCompleteDefinition() {
if (items != null) {
for (Item<?,?> item : getItems()) {
if (!item.hasCompleteDefinition()) {
return false;
}
}
}
return true;
}
@Override
public boolean representsSameValue(PrismValue other, boolean lax) {
if (other instanceof PrismContainerValue) {
return representsSameValue((PrismContainerValue<C>)other, lax);
} else {
return false;
}
}
@SuppressWarnings("Duplicates")
private boolean representsSameValue(PrismContainerValue<C> other, boolean lax) {
if (lax && getParent() != null) {
PrismContainerDefinition definition = getDefinition();
if (definition != null) {
if (definition.isSingleValue()) {
// There is only one value, therefore it always represents the same thing
return true;
}
}
}
if (lax && other.getParent() != null) {
PrismContainerDefinition definition = other.getDefinition();
if (definition != null) {
if (definition.isSingleValue()) {
// There is only one value, therefore it always represents the same thing
return true;
}
}
}
if (this.getId() != null && other.getId() != null) {
return this.getId().equals(other.getId());
}
return false;
}
@Override
void diffMatchingRepresentation(PrismValue otherValue,
Collection<? extends ItemDelta> deltas, boolean ignoreMetadata, boolean isLiteral) {
if (otherValue instanceof PrismContainerValue) {
diffRepresentation((PrismContainerValue)otherValue, deltas, ignoreMetadata, isLiteral);
} else {
throw new IllegalStateException("Comparing incompatible values "+this+" - "+otherValue);
}
}
void diffRepresentation(PrismContainerValue<C> otherValue,
Collection<? extends ItemDelta> deltas, boolean ignoreMetadata, boolean isLiteral) {
diffItems(this, otherValue, deltas, ignoreMetadata, isLiteral);