-
Notifications
You must be signed in to change notification settings - Fork 188
/
ResourceShadowDiscriminator.java
333 lines (296 loc) · 11 KB
/
ResourceShadowDiscriminator.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
/*
* Copyright (c) 2010-2019 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.schema;
import java.io.Serializable;
import java.util.Objects;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.HumanReadableDescribable;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowDiscriminatorType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowKindType;
import org.apache.commons.lang3.ObjectUtils;
/**
* Aggregate bean containing resource OID, intent and tombstone flag.
* It uniquely identifies an shadow projection (usually account) for a specific user regardless whether it has OID, does not have
* OID yet, it exists of was deleted.
*
* This is used mostly as a key in hashes and for searches.
*
* TODO: split to two objects:
* 1: ResourceShadowCoordinates which will stay in common
* 2: ResourceShadowDiscriminator (subclass) which will go to model. This will contains tombstone and order.
*
* @author Radovan Semancik
*/
public class ResourceShadowDiscriminator implements Serializable, DebugDumpable, HumanReadableDescribable {
private static final long serialVersionUID = 346600684011645741L;
private String resourceOid;
private ShadowKindType kind = ShadowKindType.ACCOUNT;
private String intent;
private String tag;
private QName objectClass;
private boolean tombstone;
private int order = 0;
public ResourceShadowDiscriminator(String resourceOid, ShadowKindType kind, String intent, String tag, boolean tombstone) {
this.resourceOid = resourceOid;
this.tombstone = tombstone;
this.tag = tag;
setIntent(intent);
setKind(kind);
}
public ResourceShadowDiscriminator(ShadowDiscriminatorType accRefType) {
this(accRefType.getResourceRef().getOid(), accRefType.getKind(), accRefType.getIntent(), accRefType.getTag(), false);
}
public ResourceShadowDiscriminator(ShadowDiscriminatorType accRefType, String defaultResourceOid, ShadowKindType defaultKind) {
ShadowKindType kind = accRefType.getKind();
if (kind == null) {
kind = defaultKind;
}
if (accRefType.getResourceRef() == null) {
this.resourceOid = defaultResourceOid;
} else {
this.resourceOid = accRefType.getResourceRef().getOid();
}
this.tombstone = false;
setIntent(accRefType.getIntent());
setKind(kind);
}
public ResourceShadowDiscriminator(String resourceOid) {
this.resourceOid = resourceOid;
}
public ResourceShadowDiscriminator(String resourceOid, QName objectClass) {
this.resourceOid = resourceOid;
this.objectClass = objectClass;
this.kind = null;
}
public String getResourceOid() {
return resourceOid;
}
public void setResourceOid(String resourceOid) {
this.resourceOid = resourceOid;
}
public ShadowKindType getKind() {
return kind;
}
public void setKind(ShadowKindType kind) {
this.kind = kind;
}
public String getIntent() {
return intent;
}
public void setIntent(String intent) {
// if (intent == null) {
// intent = SchemaConstants.INTENT_DEFAULT;
// }
this.intent = intent;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public QName getObjectClass() {
return objectClass;
}
public void setObjectClass(QName objectClass) {
this.objectClass = objectClass;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
/**
* Tombstone flag is true: the account no longer exists. The data we have are the latest metadata we were able to get.
* The projection will be marked as tombstone if we discover that the associated resource object is gone. Or the shadow
* is gone and we can no longer associate the resource object. In any way the tombstoned projection is marked for removal.
* It will be eventually unlinked and the shadow will be deleted. The shadow may stay around in the "dead" state for
* some time for reporting purposes.
*/
public boolean isTombstone() {
return tombstone;
}
public void setTombstone(boolean tombstone) {
this.tombstone = tombstone;
}
public boolean isWildcard() {
return kind == null && objectClass == null;
}
public ShadowDiscriminatorType toResourceShadowDiscriminatorType() {
ShadowDiscriminatorType rsdt = new ShadowDiscriminatorType();
rsdt.setIntent(intent);
rsdt.setKind(kind);
ObjectReferenceType resourceRef = new ObjectReferenceType();
resourceRef.setOid(resourceOid);
resourceRef.setType(ResourceType.COMPLEX_TYPE);
rsdt.setResourceRef(resourceRef);
return rsdt;
}
public static ResourceShadowDiscriminator fromResourceShadowDiscriminatorType(
ShadowDiscriminatorType resourceShadowDiscriminatorType, boolean provideDefaultIntent) {
if (resourceShadowDiscriminatorType == null) {
return null;
}
// For compatibility. Otherwise the kind should be explicitly serialized.
ShadowKindType kind = ObjectUtils.defaultIfNull(resourceShadowDiscriminatorType.getKind(), ShadowKindType.ACCOUNT);
String intent = resourceShadowDiscriminatorType.getIntent() != null || !provideDefaultIntent ?
resourceShadowDiscriminatorType.getIntent() : SchemaConstants.INTENT_DEFAULT;
return new ResourceShadowDiscriminator(
resourceShadowDiscriminatorType.getResourceRef() != null ? resourceShadowDiscriminatorType.getResourceRef().getOid() : null,
kind, intent,
resourceShadowDiscriminatorType.getTag(),
false);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((intent == null) ? 0 : intent.hashCode());
result = prime * result + ((kind == null) ? 0 : kind.hashCode());
result = prime * result + ((objectClass == null) ? 0 : objectClass.hashCode());
result = prime * result + order;
result = prime * result + ((resourceOid == null) ? 0 : resourceOid.hashCode());
result = prime * result + ((tag == null) ? 0 : tag.hashCode());
result = prime * result + (tombstone ? 1231 : 1237);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ResourceShadowDiscriminator other = (ResourceShadowDiscriminator) obj;
if (intent == null) {
if (other.intent != null) {
return false;
}
} else if (!intent.equals(other.intent)) {
return false;
}
if (kind != other.kind) {
return false;
}
if (objectClass == null) {
if (other.objectClass != null) {
return false;
}
} else if (!objectClass.equals(other.objectClass)) {
return false;
}
if (order != other.order) {
return false;
}
if (resourceOid == null) {
if (other.resourceOid != null) {
return false;
}
} else if (!resourceOid.equals(other.resourceOid)) {
return false;
}
if (tag == null) {
if (other.tag != null) {
return false;
}
} else if (!tag.equals(other.tag)) {
return false;
}
if (tombstone != other.tombstone) {
return false;
}
return true;
}
/**
* Similar to equals but ignores the order.
*/
public boolean equivalent(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
ResourceShadowDiscriminator other = (ResourceShadowDiscriminator) obj;
if (intent == null) {
if (other.intent != null) return false;
} else if (!equalsIntent(this.intent, other.intent)) {
return false;
}
if (!Objects.equals(this.tag, other.tag)) {
return false;
}
if (resourceOid == null) {
if (other.resourceOid != null) return false;
} else if (!resourceOid.equals(other.resourceOid)) {
return false;
}
if (tombstone != other.tombstone) return false;
return true;
}
public static boolean equalsIntent(String a, String b) {
if (a == null || b == null) {
return false;
}
return a.equals(b);
}
@Override
public String toString() {
return toHumanReadableDescription();
}
@Override
public String toHumanReadableDescription() {
return toHumanReadableDescription(true);
}
public String toHumanReadableDescription(boolean writeOid) {
StringBuilder sb = new StringBuilder("RSD(");
sb.append(kind==null?"null":kind.value());
sb.append(" (").append(intent);
if (tag != null) {
sb.append("/").append(tag);
}
sb.append(")");
if (objectClass != null) {
sb.append(": ").append(PrettyPrinter.prettyPrint(objectClass));
}
if (writeOid) {
sb.append(" @");
sb.append(resourceOid);
}
if (order != 0) {
sb.append(" order=");
sb.append(order);
}
if (tombstone) {
sb.append(" TOMBSTONE");
}
sb.append(")");
return sb.toString();
}
@Override
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
sb.append("ResourceShadowDiscriminator\n");
DebugUtil.debugDumpWithLabelLn(sb, "resourceOid", resourceOid, indent + 1);
DebugUtil.debugDumpWithLabelToStringLn(sb, "kind", kind, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "intent", indent, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "tag", tag, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "objectClass", objectClass, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "tombstone", tombstone, indent + 1);
DebugUtil.debugDumpWithLabel(sb, "order", order, indent + 1);
return sb.toString();
}
}