-
Notifications
You must be signed in to change notification settings - Fork 188
/
PrismReferenceValueImpl.java
722 lines (644 loc) · 25 KB
/
PrismReferenceValueImpl.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
/*
* Copyright (c) 2010-2018 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.prism.impl;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.equivalence.ParameterizedEquivalenceStrategy;
import com.evolveum.midpoint.prism.path.*;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.DebugUtil;
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.prism.xml.ns._public.query_3.SearchFilterType;
import com.evolveum.prism.xml.ns._public.types_3.EvaluationTimeType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
import com.evolveum.prism.xml.ns._public.types_3.ReferentialIntegrityType;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.xml.namespace.QName;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Radovan Semancik
*/
public class PrismReferenceValueImpl extends PrismValueImpl implements PrismReferenceValue {
private static final long serialVersionUID = 1L;
private static final QName F_OID = new QName(PrismConstants.NS_TYPES, "oid");
private static final QName F_TYPE = new QName(PrismConstants.NS_TYPES, "type");
private static final QName F_RELATION = new QName(PrismConstants.NS_TYPES, "relation");
private String oid;
private PrismObject<?> object = null;
private QName targetType = null;
private QName relation = null;
private String description = null;
private SearchFilterType filter = null;
private EvaluationTimeType resolutionTime;
private ReferentialIntegrityType referentialIntegrity;
private PolyString targetName = null;
private Referencable referencable;
public PrismReferenceValueImpl() {
this(null,null,null);
}
public PrismReferenceValueImpl(String oid) {
this(oid, null, null);
}
public PrismReferenceValueImpl(String oid, QName targetType) {
this(oid, null, null);
this.targetType = targetType;
}
public PrismReferenceValueImpl(String oid, OriginType type, Objectable source) {
super(type,source);
this.oid = oid;
}
/**
* OID of the object that this reference refers to (reference target).
*
* May return null, but the reference is in that case incomplete and
* unusable.
*
* @return the target oid
*/
public String getOid() {
if (oid != null) {
return oid;
}
if (object != null) {
return object.getOid();
}
return null;
}
public void setOid(String oid) {
checkMutable();
this.oid = oid;
}
/**
* Returns object that this reference points to. The object is supposed to be used
* for caching and optimizations. Only oid and type of the object really matters for
* the reference.
*
* The object is transient. It will NOT be serialized. Therefore the client must
* expect that the object can disappear when serialization boundary is crossed.
* The client must expect that the object is null.
*/
public PrismObject getObject() {
return object;
}
public void setObject(PrismObject object) {
checkMutable();
this.object = object;
}
/**
* Returns XSD type of the object that this reference refers to. It may be
* used in XPath expressions and similar filters.
*
* May return null if the type name is not set.
*
* @return the target type name
*/
public QName getTargetType() {
if (targetType != null) {
return targetType;
}
if (object != null && object.getDefinition() != null) {
return object.getDefinition().getTypeName();
}
return null;
}
public void setTargetType(QName targetType) {
setTargetType(targetType, false);
}
/**
* @param allowEmptyNamespace This is an ugly hack. See comment in DOMUtil.validateNonEmptyQName.
*/
public void setTargetType(QName targetType, boolean allowEmptyNamespace) {
checkMutable();
// Null value is OK
if (targetType != null) {
// But non-empty is not ..
Itemable item = getParent();
DOMUtil.validateNonEmptyQName(targetType, " in target type in reference "+ (item == null ? "(unknown)" : item.getElementName()), allowEmptyNamespace);
}
this.targetType = targetType;
}
/**
* Returns cached name of the target object.
* This is a ephemeral value. It is usually not stored.
* It may be computed at object retrieval time or it may not be present at all.
* This is NOT an authoritative information. Setting it or changing it will
* not influence the reference meaning. OID is the only authoritative linking
* mechanism.
* @return cached name of the target object.
*/
public PolyString getTargetName() {
if (targetName != null) {
return targetName;
}
if (object != null) {
return object.getName();
}
return null;
}
public void setTargetName(PolyString name) {
checkMutable();
this.targetName = name;
}
public void setTargetName(PolyStringType name) {
checkMutable();
if (name == null) {
this.targetName = null;
} else {
this.targetName = name.toPolyString();
}
}
// The PRV (this object) should have a parent with a prism context
public Class<Objectable> getTargetTypeCompileTimeClass() {
PrismContext prismContext = getPrismContext();
return prismContext != null ? getTargetTypeCompileTimeClass(prismContext) : null;
}
public Class<Objectable> getTargetTypeCompileTimeClass(PrismContext prismContext) {
QName type = getTargetType();
if (type == null) {
return null;
} else {
PrismObjectDefinition<Objectable> objDef = prismContext.getSchemaRegistry().findObjectDefinitionByType(type);
return objDef != null ? objDef.getCompileTimeClass() : null;
}
}
public QName getRelation() {
return relation;
}
public void setRelation(QName relation) {
checkMutable();
this.relation = relation;
}
public PrismReferenceValueImpl relation(QName relation) {
setRelation(relation);
return this;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
checkMutable();
this.description = description;
}
public SearchFilterType getFilter() {
return filter;
}
public void setFilter(SearchFilterType filter) {
checkMutable();
this.filter = filter;
}
public EvaluationTimeType getResolutionTime() {
return resolutionTime;
}
public void setResolutionTime(EvaluationTimeType resolutionTime) {
checkMutable();
this.resolutionTime = resolutionTime;
}
@Override
public ReferentialIntegrityType getReferentialIntegrity() {
return referentialIntegrity;
}
@Override
public void setReferentialIntegrity(ReferentialIntegrityType referentialIntegrity) {
checkMutable();
this.referentialIntegrity = referentialIntegrity;
}
@Override
public PrismReferenceDefinition getDefinition() {
return (PrismReferenceDefinition) super.getDefinition();
}
@Override
public boolean isRaw() {
// Reference value cannot be raw
return false;
}
@Override
public Object find(ItemPath path) {
if (path == null || path.isEmpty()) {
return this;
}
Object first = path.first();
if (!ItemPath.isName(first)) {
throw new IllegalArgumentException("Attempt to resolve inside the reference value using a non-name path "+path+" in "+this);
}
ItemName subName = ItemPath.toName(first);
if (compareLocalPart(F_OID, subName)) {
return this.getOid();
} else if (compareLocalPart(F_TYPE, subName)) {
return this.getTargetType();
} else if (compareLocalPart(F_RELATION, subName)) {
return this.getRelation();
} else {
throw new IllegalArgumentException("Attempt to resolve inside the reference value using a unrecognized path "+path+" in "+this);
}
}
@Override
public <IV extends PrismValue,ID extends ItemDefinition> PartiallyResolvedItem<IV,ID> findPartial(ItemPath path) {
if (path == null || path.isEmpty()) {
//noinspection unchecked
return new PartiallyResolvedItem<>((Item<IV, ID>) getParent(), null);
}
//noinspection unchecked
return new PartiallyResolvedItem<>((Item<IV, ID>) getParent(), path);
}
@SuppressWarnings("SimplifiableIfStatement")
private boolean compareLocalPart(QName a, QName b) {
if (a == null && b == null) {
return true;
}
if (a == null || b == null) {
return false;
}
return a.getLocalPart().equals(b.getLocalPart());
}
@Override
public void applyDefinition(ItemDefinition definition, boolean force) throws SchemaException {
if (!(definition instanceof PrismReferenceDefinition)) {
throw new IllegalArgumentException("Cannot apply "+definition+" to a reference value");
}
applyDefinition((PrismReferenceDefinition)definition, force);
}
public void applyDefinition(PrismReferenceDefinition definition, boolean force) throws SchemaException {
super.applyDefinition(definition, force);
if (object == null) {
return;
}
if (object.getDefinition() != null && !force) {
return;
}
PrismContext prismContext = definition.getPrismContext();
//noinspection ConstantConditions
PrismObjectDefinition<? extends Objectable> objectDefinition = prismContext.getSchemaRegistry()
.findObjectDefinitionByCompileTimeClass(object.getCompileTimeClass());
QName targetTypeName = definition.getTargetTypeName();
if (objectDefinition == null) {
if (targetTypeName == null) {
if (object.getDefinition() != null) {
// Target type is not specified (e.g. as in task.objectRef) but we have at least some definition;
// so let's keep it. TODO reconsider this
return;
} else {
throw new SchemaException("Cannot apply definition to composite object in reference "+getParent()
+": the object has no present definition; it's definition cannot be determined from it's class;"
+ "and target type name is not specified in the reference schema");
}
}
objectDefinition = prismContext.getSchemaRegistry().findObjectDefinitionByType(targetTypeName);
}
if (objectDefinition == null) {
throw new SchemaException("Cannot apply definition to composite object in reference "+getParent()
+": no definition for object type "+targetTypeName);
}
// this should do it
//noinspection unchecked
object.applyDefinition((PrismObjectDefinition)objectDefinition, force);
}
@Override
public void recompute(PrismContext prismContext) {
// Nothing to do
}
@Override
public void checkConsistenceInternal(Itemable rootItem, boolean requireDefinitions, boolean prohibitRaw, ConsistencyCheckScope scope) {
if (!scope.isThorough()) {
return;
}
ItemPath myPath = getPath();
// We allow empty references that contain only the target name (see MID-5489)
if (StringUtils.isBlank(oid) && object == null && filter == null && targetName == null) {
boolean mayBeEmpty = false;
if (getParent() != null && getParent().getDefinition() != null) {
ItemDefinition itemDefinition = getParent().getDefinition();
if (itemDefinition instanceof PrismReferenceDefinition) {
PrismReferenceDefinition prismReferenceDefinition = (PrismReferenceDefinition) itemDefinition;
mayBeEmpty = prismReferenceDefinition.isComposite();
}
}
if (!mayBeEmpty) {
throw new IllegalStateException("Neither OID, object nor filter specified in reference value "+this+" ("+myPath+" in "+rootItem+")");
}
}
if (object != null) {
try {
object.checkConsistence();
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(e.getMessage()+" in reference "+myPath+" in "+rootItem, e);
} catch (IllegalStateException e) {
throw new IllegalStateException(e.getMessage()+" in reference "+myPath+" in "+rootItem, e);
}
}
}
@Override
public boolean isEmpty() {
return oid == null && object == null && filter == null && relation == null && targetType == null;
}
/**
* Returns a version of this value that is canonical, that means it has the minimal form.
* E.g. it will have only OID and no object.
*/
public PrismReferenceValueImpl toCanonical() {
PrismReferenceValueImpl can = new PrismReferenceValueImpl();
can.setOid(getOid());
// do NOT copy object
can.setTargetType(getTargetType());
can.setRelation(getRelation());
can.setFilter(getFilter());
can.setResolutionTime(getResolutionTime());
can.setDescription(getDescription());
return can;
}
public boolean equals(PrismValue other, @NotNull ParameterizedEquivalenceStrategy strategy) {
return other instanceof PrismReferenceValue && equals((PrismReferenceValue) other, strategy);
}
@SuppressWarnings({ "RedundantIfStatement" })
public boolean equals(PrismReferenceValue other, @NotNull ParameterizedEquivalenceStrategy strategy) {
if (!super.equals(other, strategy)) {
return false;
}
if (this.getOid() == null) {
if (other.getOid() != null) {
return false;
}
} else if (!this.getOid().equals(other.getOid())) {
return false;
}
// Special handling: if both oids are null we need to compare embedded objects
boolean bothOidsNull = this.oid == null && other.getOid() == null;
if (bothOidsNull) {
if (this.object != null || other.getObject() != null) {
if (this.object == null || other.getObject() == null) {
// one is null the other is not
return false;
}
if (!this.object.equals(other.getObject())) {
return false;
}
}
}
if (!equalsTargetType(other)) {
return false;
}
if (!relationsEquivalent(relation, other.getRelation(), strategy.isLiteralDomComparison())) {
return false;
}
if ((strategy.isConsideringReferenceFilters() || bothOidsNull) && !filtersEquivalent(filter, other.getFilter())) {
return false;
}
if (strategy.isConsideringReferenceOptions()) {
if (this.getResolutionTime() != other.getResolutionTime()) {
return false;
}
if (this.getReferentialIntegrity() != other.getReferentialIntegrity()) {
return false;
}
}
return true;
}
@SuppressWarnings("SimplifiableIfStatement")
private boolean filtersEquivalent(SearchFilterType filter1, SearchFilterType filter2) {
if (filter1 == null && filter2 == null) {
return true;
} else if (filter1 == null || filter2 == null) {
return false;
} else {
return filter1.equals(filter2);
}
}
private boolean relationsEquivalent(QName r1, QName r2, boolean isLiteral) {
// todo use equals if isLiteral is true
return QNameUtil.match(normalizedRelation(r1, isLiteral), normalizedRelation(r2, isLiteral));
}
private QName normalizedRelation(QName r, boolean isLiteral) {
if (r != null) {
return r;
}
if (isLiteral) {
return null;
}
PrismContext prismContext = getPrismContext();
return prismContext != null ? prismContext.getDefaultRelation() : null;
}
private boolean equalsTargetType(PrismReferenceValue other) {
QName otherTargetType = other.getTargetType();
if (otherTargetType == null && other.getDefinition() != null) {
otherTargetType = other.getDefinition().getTargetTypeName();
}
QName thisTargetType = this.getTargetType();
if (thisTargetType == null && this.getDefinition() != null) {
thisTargetType = this.getDefinition().getTargetTypeName();
}
return QNameUtil.match(thisTargetType, otherTargetType);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!super.equals(obj)) return false;
if (getClass() != obj.getClass()) return false;
PrismReferenceValue other = (PrismReferenceValue) obj;
return equals(other, getEqualsHashCodeStrategy());
}
// Just to make checkstyle happy
@Override
public int hashCode() {
return super.hashCode();
}
// TODO take strategy into account
@Override
public int hashCode(@NotNull ParameterizedEquivalenceStrategy strategy) {
final int prime = 31;
int result = super.hashCode(strategy);
result = prime * result + ((oid == null) ? 0 : oid.hashCode());
QName normalizedRelation = normalizedRelation(relation, false);
if (normalizedRelation != null) {
// Take just the local part to avoid problems with incomplete namespaces
String relationLocal = normalizedRelation.getLocalPart();
if (relationLocal != null) {
result = prime * result + relationLocal.hashCode();
}
}
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("PRV(");
if (object == null) {
sb.append("oid=").append(oid);
sb.append(", targetType=").append(PrettyPrinter.prettyPrint(targetType));
if (targetName != null) {
sb.append(", targetName=").append(PrettyPrinter.prettyPrint(targetName.getOrig()));
}
} else {
sb.append("object=").append(object);
}
if (getRelation() != null) {
sb.append(", relation=").append(PrettyPrinter.prettyPrint(getRelation()));
}
if (getOriginType() != null) {
sb.append(", type=").append(getOriginType());
}
if (getOriginObject() != null) {
sb.append(", source=").append(getOriginObject());
}
if (filter != null) {
sb.append(", filter");
}
if (resolutionTime != null) {
sb.append(", resolutionTime=").append(resolutionTime);
}
if (referentialIntegrity != null) {
sb.append(", RI=").append(referentialIntegrity);
}
sb.append(")");
return sb.toString();
}
public Referencable asReferencable() {
if (referencable != null) {
return referencable;
}
Itemable parent = getParent();
if (parent != null && parent.getDefinition() != null) {
QName xsdType = parent.getDefinition().getTypeName();
Class<?> clazz = getPrismContext() != null ? getPrismContext().getSchemaRegistry().getCompileTimeClass(xsdType) : null;
if (clazz != null) {
try {
referencable = (Referencable) clazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new SystemException("Couldn't create jaxb object instance of '" + clazz + "': " + e.getMessage(),
e);
}
}
referencable.setupReferenceValue(this);
return referencable;
}
// A hack, just to avoid crashes. TODO think about this!
return new DefaultReferencableImpl(this);
}
@SuppressWarnings("unused")
@NotNull
public static List<Referencable> asReferencables(@NotNull Collection<PrismReferenceValue> values) {
return values.stream().map(PrismReferenceValue::asReferencable).collect(Collectors.toList());
}
@SuppressWarnings("unused")
@NotNull
public static List<PrismReferenceValue> asReferenceValues(@NotNull Collection<? extends Referencable> referencables) {
return referencables.stream().map(Referencable::asReferenceValue).collect(Collectors.toList());
}
@Override
public String debugDump() {
return toString();
}
@Override
public String debugDump(int indent) {
return debugDump(indent, false);
}
public String debugDump(int indent, boolean expandObject) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
sb.append(toString());
if (expandObject && object != null) {
sb.append("\n");
sb.append(object.debugDump(indent + 1));
}
return sb.toString();
}
@Override
public PrismReferenceValueImpl clone() {
return cloneComplex(CloneStrategy.LITERAL);
}
@Override
public PrismReferenceValue createImmutableClone() {
return (PrismReferenceValue) super.createImmutableClone();
}
@Override
public PrismReferenceValueImpl cloneComplex(CloneStrategy strategy) {
PrismReferenceValueImpl clone = new PrismReferenceValueImpl(getOid(), getOriginType(), getOriginObject());
copyValues(strategy, clone);
return clone;
}
protected void copyValues(CloneStrategy strategy, PrismReferenceValueImpl clone) {
super.copyValues(strategy, clone);
clone.targetType = this.targetType;
if (this.object != null && strategy == CloneStrategy.LITERAL) {
clone.object = this.object.clone();
}
clone.description = this.description;
clone.filter = this.filter;
clone.resolutionTime = this.resolutionTime;
clone.referentialIntegrity = this.referentialIntegrity;
clone.relation = this.relation;
clone.targetName = this.targetName;
}
@Override
public String toHumanReadableString() {
StringBuilder sb = new StringBuilder();
sb.append("oid=");
shortDump(sb);
return sb.toString();
}
@Override
public Class<?> getRealClass() {
return Referencable.class;
}
@SuppressWarnings("unchecked")
@Nullable
@Override
public Referencable getRealValue() {
return asReferencable();
}
public static boolean containsOid(Collection<PrismReferenceValue> values, @NotNull String oid) {
return values.stream().anyMatch(v -> oid.equals(v.getOid()));
}
@Override
public void revive(PrismContext prismContext) throws SchemaException {
super.revive(prismContext);
if (object != null) {
object.revive(prismContext);
}
}
@Override
public void shortDump(StringBuilder sb) {
sb.append(oid);
if (getTargetType() != null) {
sb.append("(");
sb.append(DebugUtil.formatElementName(getTargetType()));
sb.append(")");
}
if (targetName != null) {
sb.append("('").append(targetName).append("')");
}
if (getRelation() != null) {
sb.append("[");
sb.append(getRelation().getLocalPart());
sb.append("]");
}
if (getObject() != null) {
sb.append('*');
}
if (getReferentialIntegrity() != null) {
switch (getReferentialIntegrity()) {
case STRICT: sb.append(" RI=S"); break;
case RELAXED: sb.append(" RI=R"); break;
case LAX: sb.append(" RI=L"); break;
}
}
}
@Override
public void accept(Visitor visitor) {
super.accept(visitor);
if (object != null && ConfigurableVisitor.shouldVisitEmbeddedObjects(visitor)) {
//noinspection unchecked
object.accept(visitor);
}
}
}