-
Notifications
You must be signed in to change notification settings - Fork 188
/
PrismReference.java
275 lines (241 loc) · 8.78 KB
/
PrismReference.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
/*
* Copyright (c) 2010-2013 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.prism;
import java.util.List;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.prism.delta.ReferenceDelta;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
/**
* Object Reference is a property that describes reference to an object. It is
* used to represent association between objects. For example reference from
* User object to Account objects that belong to the user. The reference is a
* simple uni-directional link using an OID as an identifier.
*
* This type should be used for all object references so the implementations can
* detect them and automatically resolve them.
*
* @author semancik
*
*/
public class PrismReference extends Item<PrismReferenceValue> {
private static final long serialVersionUID = 1872343401395762657L;
public PrismReference(QName name) {
super(name);
}
PrismReference(QName name, PrismReferenceDefinition definition, PrismContext prismContext) {
super(name, definition, prismContext);
}
/**
* {@inheritDoc}
*/
public PrismReferenceDefinition getDefinition() {
return (PrismReferenceDefinition) super.getDefinition();
}
/**
* Returns reference values.
* <p/>
* The values are returned as set. The order of values is not significant.
*
* @return property values
*/
@Override
public List<PrismReferenceValue> getValues() {
return (List<PrismReferenceValue>) super.getValues();
}
public PrismReferenceValue getValue() {
// We are not sure about multiplicity if there is no definition or the definition is dynamic
if (getDefinition() != null && !getDefinition().isDynamic()) {
if (getDefinition().isMultiValue()) {
throw new IllegalStateException("Attempt to get single value from property " + elementName
+ " with multiple values");
}
}
if (getValues().size() > 1) {
throw new IllegalStateException("Attempt to get single value from property " + elementName
+ " with multiple values");
}
if (getValues().isEmpty()) {
// Insert first empty value. This simulates empty single-valued reference. It the reference exists
// it is clear that it has at least one value (and that value is empty).
PrismReferenceValue rval = new PrismReferenceValue();
add(rval);
return rval;
}
return getValues().iterator().next();
}
private PrismReferenceValue getValue(String oid) {
// We need to tolerate null OIDs here. Because of JAXB.
for (PrismReferenceValue val: getValues()) {
if (MiscUtil.equals(oid, val.getOid())) {
return val;
}
}
return null;
}
public boolean add(PrismReferenceValue value) {
value.setParent(this);
return getValues().add(value);
}
public boolean merge(PrismReferenceValue value) {
String newOid = value.getOid();
// We need to tolerate null OIDs here. Because of JAXB.
PrismReferenceValue existingValue = getValue(newOid);
if (existingValue == null) {
return add(value);
}
// if there is newValue containing object (instead of oid only) and also
// old value containing object (instead of oid only) we need to compare
// these two object if they are equals..this can avoid of bad resolving
// (e.g. creating user and adding two or more accounts at the same time)
if (value.getObject() != null && existingValue.getObject() != null && !value.equalsComplex(existingValue, false, false)){
return add(value);
}
// in the value.getObject() is not null, it it probably only resolving
// of refenrence, so only change oid to object
if (value.getObject() != null) {
existingValue.setObject(value.getObject());
return true;
}
// in the case, if the existing value and new value are not equal, add
// also another reference alhtrough one with the same oid exist. It is
// needed for parent org refs, becasue there can exist more than one
// reference with the same oid, but they should be different (e.g. user
// is member and also manager of the org. unit.)
if (!value.equalsComplex(existingValue, false, false)) {
return add(value);
}
if (value.getTargetType() != null) {
existingValue.setTargetType(value.getTargetType());
// return true;
}
// No need to copy OID as OIDs match
return true;
}
public String getOid() {
return getValue().getOid();
}
public PrismReferenceValue findValueByOid(String oid) {
for (PrismReferenceValue pval: getValues()) {
if (oid.equals(pval.getOid())) {
return pval;
}
}
return null;
}
@Override
public Object find(ItemPath path) {
if (path == null || path.isEmpty()) {
return this;
}
if (!isSingleValue()) {
throw new IllegalStateException("Attempt to resolve sub-path '"+path+"' on multi-value reference " + getElementName());
}
PrismReferenceValue value = getValue();
return value.find(path);
}
@Override
public <X extends PrismValue> PartiallyResolvedItem<X> findPartial(ItemPath path) {
if (path == null || path.isEmpty()) {
return new PartiallyResolvedItem<X>((Item<X>)this, null);
}
if (!isSingleValue()) {
throw new IllegalStateException("Attempt to resolve sub-path '"+path+"' on multi-value reference " + getElementName());
}
PrismReferenceValue value = getValue();
return value.findPartial(path);
}
@Override
public ReferenceDelta createDelta() {
return new ReferenceDelta(getPath(), getDefinition(), prismContext);
}
@Override
public ReferenceDelta createDelta(ItemPath path) {
return new ReferenceDelta(path, getDefinition(), prismContext);
}
@Override
protected void checkDefinition(ItemDefinition def) {
if (!(def instanceof PrismReferenceDefinition)) {
throw new IllegalArgumentException("Cannot apply definition "+def+" to reference "+this);
}
}
@Override
public PrismReference clone() {
PrismReference clone = new PrismReference(getElementName(), getDefinition(), prismContext);
copyValues(clone);
return clone;
}
protected void copyValues(PrismReference clone) {
super.copyValues(clone);
for (PrismReferenceValue value : getValues()) {
clone.add(value.clone());
}
}
@Override
public String toString() {
return getClass().getSimpleName() + "(" + PrettyPrinter.prettyPrint(getElementName()) + "):" + getValues();
}
@Override
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
PrismReferenceDefinition definition = getDefinition();
boolean multiline = true;
if (definition != null) {
multiline = definition.isMultiValue() || definition.isComposite();
}
if (DebugUtil.isDetailedDebugDump()) {
sb.append(getDebugDumpClassName()).append(": ");
}
sb.append(DebugUtil.formatElementName(getElementName()));
sb.append(": ");
List<PrismReferenceValue> values = getValues();
if (getValues() == null) {
sb.append("null");
} else if (values.isEmpty()) {
sb.append("[ ]");
} else {
if (definition != null && DebugUtil.isDetailedDebugDump()) {
sb.append(" def ");
}
for (PrismReferenceValue value : values) {
if (multiline) {
sb.append("\n");
DebugUtil.indentDebugDump(sb, indent + 1);
}
if (DebugUtil.isDetailedDebugDump()) {
if (multiline) {
sb.append(value.debugDump(indent + 1, true));
} else {
sb.append(value.toString());
}
} else {
sb.append(value.toHumanReadableString());
}
}
}
return sb.toString();
}
/**
* Return a human readable name of this class suitable for logs.
*/
@Override
protected String getDebugDumpClassName() {
return "PR";
}
}