-
Notifications
You must be signed in to change notification settings - Fork 188
/
PrismContainerValue.java
1536 lines (1386 loc) · 53.3 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-2014 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.prism;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.parser.JaxbDomHack;
import com.evolveum.midpoint.prism.parser.XNodeProcessor;
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.prism.xnode.MapXNode;
import com.evolveum.midpoint.prism.xnode.XNode;
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;
/**
* @author semancik
*
*/
public class PrismContainerValue<T 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
private List<Item<?>> items = null;
private Long id;
// XNode map of all sub-elements in this container value.
private MapXNode rawXNode = null;
// The elements are set during a schema-less parsing, e.g. during a dumb JAXB parsing of the object
// We can't do anything smarter, as we don't have definition nor prism context. We cannot even convert
// this to XNode because no prism context means no parser.
// So we store the raw elements here and process them later (e.g. during applyDefinition).
private List<Object> rawElements = null;
private T containerable = null;
/**
* Concrete type of the containerable value. It is the declared container type or any of its subtypes.
* If null, it is considered to be the declared type itself.
*
* (It is advisable to keep it null when the concrete type is the same as declared type
* in order to prevent the serialized form from having unnecessary type QName declaration,
* as currently this information is directly serialized into value's MapXNode representation
* as type attribute.)
*
* Currently this feature is "half-baked" and experimental. If you need it, you have to set it up explicitly
* when creating your container value.
*/
private QName concreteType = null;
private PrismContainerDefinition concreteTypeDefinition = null; // lazily evaluated
transient private PrismContext prismContext;
public PrismContainerValue() {
super();
// Nothing to do
}
public PrismContainerValue(PrismContext prismContext) {
this();
setPrismContext(prismContext);
}
private void setPrismContext(PrismContext prismContext) {
//Validate.notNull(prismContext, "No prismContext in PrismContainerValue"); // not yet
//if (prismContext == null) {
// LOGGER.warn("No prismContext in PrismContainerValue");
//}
this.prismContext = prismContext;
}
private PrismContainerValue(T containerable) {
super();
this.containerable = containerable;
}
public PrismContainerValue(T containerable, PrismContext prismContext) {
this(containerable);
this.prismContext = prismContext;
}
private PrismContainerValue(OriginType type, Objectable source, PrismContainerable container, Long id, QName concreteType) {
super(type, source, container);
this.id = id;
this.concreteType = concreteType;
}
public PrismContainerValue(OriginType type, Objectable source, PrismContainerable container, Long id, QName concreteType, PrismContext prismContext) {
this(type, source, container, id, concreteType);
setPrismContext(prismContext);
}
@Override
public PrismContext getPrismContext() {
if (prismContext != null) {
return prismContext;
}
if (getParent() != null) {
return getParent().getPrismContext();
}
return null;
}
/**
* 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() {
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();
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){
return 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) {
this.id = id;
}
@SuppressWarnings("unchecked")
public PrismContainerable<T> 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<T>)parent;
}
void setParent(PrismContainerable container) {
super.setParent(container);
}
@SuppressWarnings("unchecked")
public PrismContainer<T> 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<T>)super.getParent();
}
void setParent(PrismContainer<T> container) {
super.setParent(container);
}
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 T getValue() {
return asContainerable();
}
private List<Object> createElement() {
return new ArrayList<Object>();
}
public T asContainerable() {
PrismContainerable parent = getParent();
if (parent == null) {
throw new IllegalStateException("Cannot represent container value without a parent as containerable; value: "+this);
}
Class<T> clazz = null;
if (concreteType != null) {
clazz = resolveConcreteClass(parent);
}
if (clazz == null) {
clazz = parent.getCompileTimeClass();
}
if (clazz == null) {
throw new SystemException("Unknown compile time class of container '" + parent.getElementName() + "'.");
}
if (Modifier.isAbstract(clazz.getModifiers())) {
throw new SystemException("Can't create instance of class '" + clazz.getSimpleName() + "', it's abstract.");
}
return asContainerableInternal(clazz);
}
private Class<T> resolveConcreteClass(PrismContainerable parent) {
Class<T> clazz;
PrismContainerDefinition containerDefinition = parent.getPrismContext().getSchemaRegistry().findContainerDefinitionByType(concreteType);
if (containerDefinition == null) {
throw new IllegalStateException("A definition for an explicit type " + concreteType + " of a container " + parent.getElementName() + " value couldn't be found");
}
clazz = containerDefinition.getCompileTimeClass();
if (clazz == null) {
throw new IllegalStateException("A definition for an explicit type " + concreteType + " of a container " + parent.getElementName() + " value has no compile-time class specified");
}
return clazz;
}
public T asContainerable(Class<T> defaultClazz) {
Class<T> clazz = defaultClazz;
if (concreteType != null) {
PrismContainerable parent = getParent();
if (parent != null) {
clazz = resolveConcreteClass(parent);
} else {
throw new IllegalStateException("Cannot determine compile-time class for concrete type " + concreteType + " because the respective prism container value has no parent");
}
}
return asContainerableInternal(clazz);
}
private T asContainerableInternal(Class<T> clazz) {
if (containerable != null) {
return containerable ;
}
try {
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<QName>();
for (PrismProperty<?> prop: getProperties()) {
names.add(prop.getElementName());
}
return names;
}
/**
* 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 boolean add(Item<?> item) throws SchemaException {
if (item.getElementName() == null) {
throw new IllegalArgumentException("Cannot add item without a name to value of container "+getParent());
}
if (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 (getActualDefinition() != null && item.getDefinition() == null) {
item.applyDefinition(determineItemDefinition(item.getElementName(), getActualDefinition()), false);
}
if (items == null) {
items = new ArrayList<Item<?>>();
}
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 <V extends PrismValue> boolean merge(Item<V> item) throws SchemaException {
Item<V> exisingItem = findItem(item.getElementName(), Item.class);
if (exisingItem == null) {
return add(item);
} else {
boolean changed = false;
for (V newVal: item.getValues()) {
if (exisingItem.add((V) newVal.clone())) {
changed = true;
}
}
return changed;
}
}
/**
* Substract 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 <V extends PrismValue> boolean substract(Item<V> item) throws SchemaException {
Item<V> exisingItem = findItem(item.getElementName(), Item.class);
if (exisingItem == null) {
return false;
} else {
boolean changed = false;
for (V newVal: item.getValues()) {
if (exisingItem.remove(newVal)) {
changed = true;
}
}
return changed;
}
}
/**
* Adds an item to a property container. Existing value will be replaced.
*
* @param item item to add.
*/
public void addReplaceExisting(Item<?> item) throws SchemaException {
if (item == null){
return;
}
Item<?> existingItem = findItem(item.getElementName(), Item.class);
if (existingItem != null && items != null) {
items.remove(existingItem);
existingItem.setParent(null);
}
add(item);
}
public void remove(Item<?> item) {
Validate.notNull(item, "Item must not be null.");
Item<?> existingItem = findItem(item.getElementName(), Item.class);
if (existingItem != null && items != null) {
items.remove(existingItem);
existingItem.setParent(null);
}
}
public void removeAll() {
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 {
// 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 void replace(Item<?> oldItem, Item<?> newItem) throws SchemaException {
remove(oldItem);
add(newItem);
}
// Expects that the "self" path segment is already included in the basePath
// todo treat unqualified names
// is this method used anywhere?
// void addItemPathsToList(ItemPath basePath, Collection<ItemPath> list) {
// if (items != null) {
// for (Item<?> item: items) {
// if (item instanceof PrismProperty) {
// list.add(basePath.subPath(item.getElementName()));
// } else if (item instanceof PrismContainer) {
// ((PrismContainer<?>)item).addItemPathsToList(basePath, list);
// }
// }
// }
// }
public void clear() {
if (items != null) {
items.clear();
}
}
public boolean contains(Item item) {
if (items != null) {
return items.contains(item);
}
return false;
}
@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 <X extends PrismValue> PartiallyResolvedItem<X> 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<X>) 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 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 <I extends Item<?>> 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 Item<?> 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 Item<?> 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")
<I extends Item<?>> I findCreateItem(QName itemName, Class<I> type, ItemDefinition itemDefinition, boolean create) throws SchemaException {
Item<?> 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 <I extends Item<?>> 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")
<I extends Item<?>> I findCreateItem(ItemPath propPath, Class<I> type, ItemDefinition 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();
Item<?> item = 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 (I)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 Item<?> findItemByQName(QName subName) throws SchemaException {
if (items == null) {
return null;
}
Item<?> 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;
}
}
}
return matching;
}
@SuppressWarnings("unchecked")
private <I extends Item<?>> I createSubItem(QName name, Class<I> type, ItemDefinition itemDefinition) throws SchemaException {
// the item with specified name does not exist, create it now
Item<?> newItem = null;
if (itemDefinition == null && getActualDefinition() != null) {
itemDefinition = determineItemDefinition(name, getActualDefinition());
if (itemDefinition == null) {
throw new SchemaException("No definition for item "+name+" in "+getParent());
}
}
if (itemDefinition != null) {
if (StringUtils.isNotBlank(name.getNamespaceURI())){
newItem = itemDefinition.instantiate(name);
} else {
QName computed = new QName(itemDefinition.getNamespace(), name.getLocalPart());
newItem = 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 (I)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 Item<?> findOrCreateItem(QName containerName) throws SchemaException {
return findCreateItem(containerName, Item.class, null, true);
}
public <X extends Item> X findOrCreateItem(QName containerName, Class<X> type) throws SchemaException {
return findCreateItem(containerName, type, null, true);
}
public <X extends Item> X findOrCreateItem(ItemPath path, Class<X> type, ItemDefinition 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(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 {
if (getActualDefinition() == null) {
throw new IllegalStateException("No definition of container "+containerName);
}
PrismContainerDefinition containerDefinition = getActualDefinition().findContainerDefinition(containerName);
if (containerDefinition == null) {
throw new IllegalArgumentException("No definition of container '" + containerName + "' in " + getActualDefinition());
}
PrismContainer<X> container = containerDefinition.instantiate();
add(container);
return container;
}
public <X> PrismProperty<X> createProperty(QName propertyName) throws SchemaException {
PrismPropertyDefinition propertyDefinition = null;
if (getActualDefinition() != null) {
propertyDefinition = getActualDefinition().findPropertyDefinition(propertyName);
if (propertyDefinition == null) {
// container has definition, but there is no property definition. This is either runtime schema
// or an error
if (getParent().getDefinition().isRuntimeSchema) {
// TODO: create opportunistic runtime definition
//propertyDefinition = new PrismPropertyDefinition(propertyName, propertyName, typeName, container.prismContext);
} else {
throw new IllegalArgumentException("No definition for property "+propertyName+" in "+getActualDefinition());
}
}
}
PrismProperty<X> property = null;
if (propertyDefinition == null) {
// Definitionless
property = new PrismProperty<X>(propertyName, prismContext);
} else {
property = propertyDefinition.instantiate();
}
add(property);
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);
}
// Expects that "self" path is NOT present in propPath
<I extends Item<?>> void removeItem(ItemPath propPath, Class<I> itemType) {
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 {
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) {
if (other instanceof PrismContainerValue) {
return representsSameValue((PrismContainerValue<T>)other);
} else {
return false;
}
}
public boolean representsSameValue(PrismContainerValue<T> other) {
if (getParent() != null) {
PrismContainerDefinition definition = getActualDefinition();
if (definition != null) {
if (definition.isSingleValue()) {
// There is only one value, therefore it always represents the same thing
return true;
}
}
}
if (other.getParent() != null) {
PrismContainerDefinition definition = other.getActualDefinition();
if (definition != null) {
if (definition.isSingleValue()) {
// There is only one value, therefore it always represents the same thing
return true;