/
ShadowUtil.java
770 lines (686 loc) · 29.1 KB
/
ShadowUtil.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
/*
* Copyright (c) 2010-2017 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.schema.util;
import com.evolveum.midpoint.prism.*;
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.polystring.PolyString;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.schema.ResourceShadowDiscriminator;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.processor.*;
import com.evolveum.midpoint.util.MiscUtil;
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 com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;
import javax.xml.namespace.QName;
import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Methods that would belong to the ResourceObjectShadowType class but cannot go there
* because of JAXB.
*
* @author Radovan Semancik
*/
public class ShadowUtil {
private static final Trace LOGGER = TraceManager.getTrace(ShadowUtil.class);
public static Collection<ResourceAttribute<?>> getPrimaryIdentifiers(ShadowType shadowType) {
return getPrimaryIdentifiers(shadowType.asPrismObject());
}
// TODO: rename to getPrimaryIdentifiers
public static Collection<ResourceAttribute<?>> getPrimaryIdentifiers(PrismObject<? extends ShadowType> shadow) {
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
if (attributesContainer == null) {
return null;
}
return attributesContainer.getPrimaryIdentifiers();
}
public static Collection<ResourceAttribute<?>> getSecondaryIdentifiers(ShadowType shadowType) {
return getSecondaryIdentifiers(shadowType.asPrismObject());
}
public static Collection<ResourceAttribute<?>> getSecondaryIdentifiers(PrismObject<? extends ShadowType> shadow) {
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
if (attributesContainer == null) {
return null;
}
return attributesContainer.getSecondaryIdentifiers();
}
public static ResourceAttribute<String> getSecondaryIdentifier(PrismObject<? extends ShadowType> shadow) throws SchemaException {
Collection<ResourceAttribute<?>> secondaryIdentifiers = getSecondaryIdentifiers(shadow);
if (secondaryIdentifiers == null || secondaryIdentifiers.isEmpty()) {
return null;
}
if (secondaryIdentifiers.size() > 1) {
throw new SchemaException("Too many secondary identifiers in "+shadow+": "+secondaryIdentifiers);
}
return (ResourceAttribute<String>) secondaryIdentifiers.iterator().next();
}
public static Collection<ResourceAttribute<?>> getSecondaryIdentifiers(Collection<? extends ResourceAttribute<?>> identifiers, ObjectClassComplexTypeDefinition objectClassDefinition) throws SchemaException {
if (identifiers == null) {
return null;
}
Collection<ResourceAttribute<?>> secondaryIdentifiers = new ArrayList<>();
for (ResourceAttribute<?> identifier: identifiers) {
if (objectClassDefinition.isSecondaryIdentifier(identifier.getElementName())) {
secondaryIdentifiers.add(identifier);
}
}
return secondaryIdentifiers;
}
public static String getSecondaryIdentifierRealValue(PrismObject<? extends ShadowType> shadow) throws SchemaException {
ResourceAttribute<String> secondaryIdentifier = getSecondaryIdentifier(shadow);
if (secondaryIdentifier == null) {
return null;
}
return secondaryIdentifier.getRealValue();
}
public static ResourceAttribute<?> getSecondaryIdentifier(ObjectClassComplexTypeDefinition objectClassDefinition,
Collection<? extends ResourceAttribute<?>> identifiers) throws SchemaException {
if (identifiers == null) {
return null;
}
ResourceAttribute<?> secondaryIdentifier = null;
for (ResourceAttribute<?> identifier: identifiers) {
if (identifier.getDefinition().isSecondaryIdentifier(objectClassDefinition)) {
if (secondaryIdentifier != null) {
throw new SchemaException("More than one secondary identifier in "+objectClassDefinition);
}
secondaryIdentifier = identifier;
}
}
return secondaryIdentifier;
}
public static Collection<ResourceAttribute<?>> getAllIdentifiers(PrismObject<? extends ShadowType> shadow) {
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
if (attributesContainer == null) {
return null;
}
return attributesContainer.getAllIdentifiers();
}
public static Collection<ResourceAttribute<?>> getAllIdentifiers(ShadowType shadow) {
return getAllIdentifiers(shadow.asPrismObject());
}
public static ResourceAttribute<String> getNamingAttribute(ShadowType shadow){
return getNamingAttribute(shadow.asPrismObject());
}
public static ResourceAttribute<String> getNamingAttribute(PrismObject<? extends ShadowType> shadow) {
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
if (attributesContainer == null) {
return null;
}
return attributesContainer.getNamingAttribute();
}
public static Collection<ResourceAttribute<?>> getAttributes(ShadowType shadowType) {
return getAttributes(shadowType.asPrismObject());
}
public static Collection<ResourceAttribute<?>> getAttributes(PrismObject<? extends ShadowType> shadow) {
return getAttributesContainer(shadow).getAttributes();
}
public static <T> ResourceAttribute<T> getAttribute(PrismObject<? extends ShadowType> shadow, QName attrName) {
return getAttributesContainer(shadow).findAttribute(attrName);
}
public static ResourceAttributeContainer getAttributesContainer(ShadowType shadowType) {
return getAttributesContainer(shadowType.asPrismObject());
}
public static ResourceAttributeContainer getAttributesContainer(PrismObject<? extends ShadowType> shadow) {
return getAttributesContainer(shadow, ShadowType.F_ATTRIBUTES);
}
public static ResourceAttributeContainer getAttributesContainer(PrismObject<? extends ShadowType> shadow, QName containerName) {
return getAttributesContainer(shadow.getValue(), containerName);
}
public static ResourceAttributeContainer getAttributesContainer(PrismContainerValue<?> cval, QName containerName) {
PrismContainer attributesContainer = cval.findContainer(containerName);
if (attributesContainer == null) {
return null;
}
if (attributesContainer instanceof ResourceAttributeContainer) {
return (ResourceAttributeContainer)attributesContainer;
} else {
throw new SystemException("Expected that <"+containerName.getLocalPart()+"> will be ResourceAttributeContainer but it is "+attributesContainer.getClass());
}
}
public static ResourceAttributeContainer getOrCreateAttributesContainer(PrismObject<? extends ShadowType> shadow,
ObjectClassComplexTypeDefinition objectClassDefinition) {
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
if (attributesContainer != null) {
return attributesContainer;
}
ResourceAttributeContainer emptyContainer = ResourceAttributeContainer.createEmptyContainer(ShadowType.F_ATTRIBUTES, objectClassDefinition);
try {
shadow.add(emptyContainer);
} catch (SchemaException e) {
throw new SystemException("Unexpected schema error: "+e.getMessage(), e);
}
return emptyContainer;
}
public static ObjectClassComplexTypeDefinition getObjectClassDefinition(ShadowType shadow) {
// TODO: maybe we can do something more intelligent here
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
return attributesContainer.getDefinition().getComplexTypeDefinition();
}
public static ObjectClassComplexTypeDefinition getObjectClassDefinition(PrismObject<? extends ShadowType> shadow) {
// TODO: maybe we can do something more intelligent here
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
return attributesContainer.getDefinition().getComplexTypeDefinition();
}
public static String getResourceOid(ShadowType shadowType) {
return getResourceOid(shadowType.asPrismObject());
}
public static String getResourceOid(PrismObject<ShadowType> shadow) {
PrismReference resourceRef = shadow.findReference(ShadowType.F_RESOURCE_REF);
if (resourceRef == null) {
return null;
}
return resourceRef.getOid();
}
public static PolyString getResourceName(ShadowType shadowType) {
return getResourceName(shadowType.asPrismObject());
}
public static PolyString getResourceName(PrismObject<ShadowType> shadow) {
PrismReference resourceRef = shadow.findReference(ShadowType.F_RESOURCE_REF);
if (resourceRef == null) {
return null;
}
return resourceRef.getTargetName();
}
public static String getSingleStringAttributeValue(ShadowType shadow, QName attrName) {
return getSingleStringAttributeValue(shadow.asPrismObject(), attrName);
}
public static String getSingleStringAttributeValue(PrismObject<ShadowType> shadow, QName attrName) {
PrismContainer<?> attributesContainer = shadow.findContainer(ShadowType.F_ATTRIBUTES);
if (attributesContainer == null) {
return null;
}
PrismProperty<String> attribute = attributesContainer.findProperty(attrName);
if (attribute == null) {
return null;
}
return attribute.getRealValue();
}
public static String getMultiStringAttributeValueAsSingle(ShadowType shadow, QName attrName) {
return getMultiStringAttributeValueAsSingle(shadow.asPrismObject(), attrName);
}
private static String getMultiStringAttributeValueAsSingle(PrismObject<ShadowType> shadow, QName attrName) {
PrismContainer<?> attributesContainer = shadow.findContainer(ShadowType.F_ATTRIBUTES);
if (attributesContainer == null) {
return null;
}
PrismProperty<String> attribute = attributesContainer.findProperty(attrName);
if (attribute == null) {
return null;
}
Collection<String> realValues = attribute.getRealValues();
if (realValues == null || realValues.isEmpty()) {
return null;
}
if (realValues.size() > 1) {
throw new IllegalStateException("More than one value in attribute "+attrName);
}
return realValues.iterator().next();
}
public static <T> List<T> getAttributeValues(ShadowType shadowType, QName attrName) {
return getAttributeValues(shadowType.asPrismObject(), attrName);
}
public static <T> List<T> getAttributeValues(PrismObject<? extends ShadowType> shadow, QName attrName) {
PrismContainer<?> attributesContainer = shadow.findContainer(ShadowType.F_ATTRIBUTES);
if (attributesContainer == null || attributesContainer.isEmpty()) {
return null;
}
PrismProperty<T> attr = attributesContainer.findProperty(attrName);
if (attr == null) {
return null;
}
List<T> values = new ArrayList<>();
for (PrismPropertyValue<T> pval : attr.getValues()) {
values.add(pval.getValue());
}
if (values.isEmpty()) {
return null;
}
return values;
}
public static <T> T getAttributeValue(ShadowType shadowType, QName attrName) throws SchemaException {
return (T) getAttributeValue(shadowType.asPrismObject(), attrName);
}
public static <T> T getAttributeValue(PrismObject<? extends ShadowType> shadow, QName attrName) throws SchemaException {
Collection<T> values = getAttributeValues(shadow, attrName);
if (values == null || values.isEmpty()) {
return null;
}
if (values.size() > 1) {
throw new SchemaException("Attempt to get single value from multi-valued attribute "+attrName);
}
return values.iterator().next();
}
public static void setPassword(ShadowType shadowType, ProtectedStringType password) {
CredentialsType credentialsType = shadowType.getCredentials();
if (credentialsType == null) {
credentialsType = new CredentialsType();
shadowType.setCredentials(credentialsType);
}
PasswordType passwordType = credentialsType.getPassword();
if (passwordType == null) {
passwordType = new PasswordType();
credentialsType.setPassword(passwordType);
}
passwordType.setValue(password);
}
public static void setPasswordIncomplete(ShadowType shadowType) throws SchemaException {
CredentialsType credentialsType = shadowType.getCredentials();
if (credentialsType == null) {
credentialsType = new CredentialsType();
shadowType.setCredentials(credentialsType);
}
PasswordType passwordType = credentialsType.getPassword();
if (passwordType == null) {
passwordType = new PasswordType();
credentialsType.setPassword(passwordType);
}
PrismContainerValue<PasswordType> passwordContainer = passwordType.asPrismContainerValue();
PrismProperty<ProtectedStringType> valueProperty = passwordContainer.findOrCreateProperty(PasswordType.F_VALUE);
valueProperty.setIncomplete(true);
}
public static ActivationType getOrCreateActivation(ShadowType shadowType) {
ActivationType activation = shadowType.getActivation();
if (activation == null) {
activation = new ActivationType();
shadowType.setActivation(activation);
}
return activation;
}
/**
* This is not supposed to be used in production code! It is just for the tests.
*/
public static void applyResourceSchema(PrismObject<? extends ShadowType> shadow,
ResourceSchema resourceSchema) throws SchemaException {
ShadowType shadowType = shadow.asObjectable();
QName objectClass = shadowType.getObjectClass();
ObjectClassComplexTypeDefinition objectClassDefinition = resourceSchema.findObjectClassDefinition(objectClass);
applyObjectClass(shadow, objectClassDefinition);
}
private static void applyObjectClass(PrismObject<? extends ShadowType> shadow,
ObjectClassComplexTypeDefinition objectClassDefinition) throws SchemaException {
PrismContainer<?> attributesContainer = shadow.findContainer(ShadowType.F_ATTRIBUTES);
ResourceAttributeContainerDefinition racDef = new ResourceAttributeContainerDefinitionImpl(ShadowType.F_ATTRIBUTES,
objectClassDefinition, objectClassDefinition.getPrismContext());
attributesContainer.applyDefinition((PrismContainerDefinition) racDef, true);
}
public static PrismObjectDefinition<ShadowType> applyObjectClass(PrismObjectDefinition<ShadowType> shadowDefinition,
ObjectClassComplexTypeDefinition objectClassDefinition) throws SchemaException {
PrismObjectDefinition<ShadowType> shadowDefClone = shadowDefinition.cloneWithReplacedDefinition(ShadowType.F_ATTRIBUTES,
objectClassDefinition.toResourceAttributeContainerDefinition());
return shadowDefClone;
}
/**
* Returns intent from the shadow. Backwards compatible with older accountType. May also adjust for default
* intent if necessary.
*/
public static String getIntent(ShadowType shadow) {
if (shadow == null) {
return null;
}
String intent = shadow.getIntent();
if (intent != null) {
return intent;
}
return null;
}
public static ShadowKindType getKind(ShadowType shadow) {
if (shadow == null) {
return null;
}
ShadowKindType kind = shadow.getKind();
if (kind != null) {
return kind;
}
return ShadowKindType.ACCOUNT;
}
public static <T> Collection<T> getAttributeValues(ShadowType shadow, QName attributeQname, Class<T> type) {
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
if (attributesContainer == null) {
return null;
}
ResourceAttribute<T> attribute = attributesContainer.findAttribute(attributeQname);
if (attribute == null) {
return null;
}
return attribute.getRealValues(type);
}
public static QName getAttributeName(ItemPath attributePath, String message) throws SchemaException {
if (attributePath == null || attributePath.isEmpty()) {
return null;
}
ItemPathSegment firstPathSegment = attributePath.first();
if (!(firstPathSegment instanceof NameItemPathSegment)) {
throw new SchemaException(message + ": first path segment is not a name segment");
}
if (!QNameUtil.match(ShadowType.F_ATTRIBUTES, ((NameItemPathSegment) firstPathSegment).getName())) {
throw new SchemaException(message + ": first path segment is not "+ShadowType.F_ATTRIBUTES);
}
if (attributePath.size() < 1) {
throw new SchemaException(message + ": path too short ("+attributePath.size()+" segments)");
}
if (attributePath.size() > 2) {
throw new SchemaException(message + ": path too long ("+attributePath.size()+" segments)");
}
ItemPathSegment secondPathSegment = attributePath.getSegments().get(1);
if (!(secondPathSegment instanceof NameItemPathSegment)) {
throw new SchemaException(message + ": second path segment is not a name segment");
}
return ((NameItemPathSegment) secondPathSegment).getName();
}
public static void checkConsistence(PrismObject<? extends ShadowType> shadow, String desc) {
PrismReference resourceRef = shadow.findReference(ShadowType.F_RESOURCE_REF);
if (resourceRef == null) {
throw new IllegalStateException("No resourceRef in "+shadow+" in "+desc);
}
if (StringUtils.isBlank(resourceRef.getOid())) {
throw new IllegalStateException("Null or empty OID in resourceRef in "+desc);
}
ShadowType shadowType = shadow.asObjectable();
if (shadowType.getObjectClass() == null) {
throw new IllegalStateException("Null objectClass in "+desc);
}
PrismContainer<ShadowAttributesType> attributesContainer = shadow.findContainer(ShadowType.F_ATTRIBUTES);
if (attributesContainer != null) {
if (!(attributesContainer instanceof ResourceAttributeContainer)) {
throw new IllegalStateException("The attributes element expected to be ResourceAttributeContainer but it is "
+attributesContainer.getClass()+" instead in "+desc);
}
checkConsistency(attributesContainer.getDefinition(), " container definition in "+desc);
}
PrismContainerDefinition<ShadowAttributesType> attributesDefinition =
shadow.getDefinition().findContainerDefinition(ShadowType.F_ATTRIBUTES);
checkConsistency(attributesDefinition, " object definition in "+desc);
}
public static void checkConsistency(PrismContainerDefinition<ShadowAttributesType> attributesDefinition, String desc) {
if (attributesDefinition == null) {
throw new IllegalStateException("No definition for <attributes> in "+desc);
}
if (!(attributesDefinition instanceof ResourceAttributeContainerDefinition)) {
throw new IllegalStateException("The attributes element definition expected to be ResourceAttributeContainerDefinition but it is "
+attributesDefinition.getClass()+" instead in "+desc);
}
}
// TODO is this correct?
public static boolean isAccount(ShadowType shadowType) {
if (shadowType.getKind() != null) {
return shadowType.getKind() == ShadowKindType.ACCOUNT;
} else {
return true; // ???
}
}
public static boolean isProtected(PrismObject<? extends ShadowType> shadow) {
if (shadow == null) {
return false;
}
ShadowType shadowType = shadow.asObjectable();
Boolean protectedObject = shadowType.isProtectedObject();
return (protectedObject != null && protectedObject);
}
public static boolean isDead(ShadowType shadow) {
Boolean dead = shadow.isDead();
return dead != null && dead;
}
public static boolean isExists(ShadowType shadow) {
Boolean exists = shadow.isExists();
return exists == null || exists;
}
public static boolean matches(ShadowType shadowType, String resourceOid, ShadowKindType kind, String intent) {
if (shadowType == null) {
return false;
}
if (!resourceOid.equals(shadowType.getResourceRef().getOid())) {
return false;
}
if (!MiscUtil.equals(kind, shadowType.getKind())) {
return false;
}
if (intent == null) {
return true;
}
return MiscUtil.equals(intent, shadowType.getIntent());
}
/**
* Strict mathcing. E.g. null discriminator kind is intepreted as ACCOUNT and it must match the kind
* in the shadow.
*/
public static boolean matches(PrismObject<ShadowType> shadow, ResourceShadowDiscriminator discr) {
return matches(shadow.asObjectable(), discr);
}
/**
* Strict mathcing. E.g. null discriminator kind is intepreted as ACCOUNT and it must match the kind
* in the shadow.
*/
public static boolean matches(ShadowType shadowType, ResourceShadowDiscriminator discr) {
if (shadowType == null) {
return false;
}
if (!discr.getResourceOid().equals(shadowType.getResourceRef().getOid())) {
return false;
}
if (!MiscUtil.equals(discr.getKind(), shadowType.getKind())) {
return false;
}
return ResourceShadowDiscriminator.equalsIntent(shadowType.getIntent(), discr.getIntent());
}
/**
* Interprets ResourceShadowDiscriminator as a pattern. E.g. null discriminator kind is
* interpreted to match any shadow kind.
*/
public static boolean matchesPattern(ShadowType shadowType, ShadowDiscriminatorType discr) {
if (shadowType == null) {
return false;
}
if (!discr.getResourceRef().getOid().equals(shadowType.getResourceRef().getOid())) {
return false;
}
if (discr.getKind() != null && !MiscUtil.equals(discr.getKind(), shadowType.getKind())) {
return false;
}
if (discr.getIntent() == null) {
return true;
}
return ResourceShadowDiscriminator.equalsIntent(shadowType.getIntent(), discr.getIntent());
}
public static boolean isConflicting(ShadowType shadow1, ShadowType shadow2) {
if (!shadow1.getResourceRef().getOid().equals(shadow2.getResourceRef().getOid())) {
return false;
}
if (!MiscUtil.equals(getKind(shadow1), getKind(shadow2))) {
return false;
}
return ResourceShadowDiscriminator.equalsIntent(shadow1.getIntent(), shadow2.getIntent());
}
public static String getHumanReadableName(PrismObject<? extends ShadowType> shadow) {
if (shadow == null) {
return "null";
}
StringBuilder sb = new StringBuilder();
ShadowType shadowType = shadow.asObjectable();
ShadowKindType kind = shadowType.getKind();
if (kind != null) {
sb.append(kind).append(" ");
}
sb.append("shadow ");
boolean first = true;
for(ResourceAttribute iattr: getPrimaryIdentifiers(shadow)) {
if (first) {
sb.append("[");
first = false;
} else {
sb.append(",");
}
sb.append(iattr.getElementName().getLocalPart());
sb.append("=");
sb.append(iattr.getRealValue());
}
if (first) {
sb.append("[");
}
sb.append("]");
return shadow.toString();
}
public static String getHumanReadableName(ShadowType shadowType) {
if (shadowType == null) {
return "null";
}
return getHumanReadableName(shadowType.asPrismObject());
}
public static boolean isFullShadow(PrismObject<ShadowType> shadow) {
ShadowType shadowType = shadow.asObjectable();
if (shadowType.getCachingMetadata() == null) {
return false;
}
return shadowType.getCachingMetadata().getRetrievalTimestamp() != null;
}
public static <T extends ShadowType> PolyString determineShadowName(ShadowType shadow)
throws SchemaException {
return determineShadowName(shadow.asPrismObject());
}
public static <T extends ShadowType> PolyString determineShadowName(PrismObject<T> shadow)
throws SchemaException {
String stringName = determineShadowStringName(shadow);
if (stringName == null) {
return null;
}
return new PolyString(stringName);
}
public static <T extends ShadowType> String determineShadowStringName(PrismObject<T> shadow)
throws SchemaException {
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
if (attributesContainer == null) {
return null;
}
ResourceAttribute<String> namingAttribute = attributesContainer.getNamingAttribute();
if (namingAttribute == null || namingAttribute.isEmpty()) {
// No naming attribute defined. Try to fall back to identifiers.
Collection<ResourceAttribute<?>> identifiers = attributesContainer.getPrimaryIdentifiers();
// We can use only single identifiers (not composite)
if (identifiers.size() == 1) {
PrismProperty<?> identifier = identifiers.iterator().next();
// Only single-valued identifiers
Collection<PrismPropertyValue<?>> values = (Collection) identifier.getValues();
if (values.size() == 1) {
PrismPropertyValue<?> value = values.iterator().next();
// and only strings
if (value.getValue() instanceof String) {
return (String) value.getValue();
}
}
} else {
ResourceAttribute<String> nameAttribute = attributesContainer.findAttribute(SchemaConstants.ICFS_NAME);
if (nameAttribute == null) {
throw new SchemaException("Could not determine shadow name.");
}
return nameAttribute.getValue(String.class).getValue();
}
// Identifier is not usable as name
// TODO: better identification of a problem
throw new SchemaException("No naming attribute defined (and identifier not usable)");
}
// TODO: Error handling
List<PrismPropertyValue<String>> possibleValues = namingAttribute.getValues();
if (possibleValues.size() > 1) {
throw new SchemaException(
"Cannot determine name of shadow. Found more than one value for naming attribute (attr: "
+ namingAttribute.getElementName() + ", values: {}" + possibleValues + ")");
}
PrismPropertyValue<String> value = possibleValues.iterator().next();
if (value == null) {
throw new SchemaException("Naming attribute has no value. Could not determine shadow name.");
}
return value.getValue();
// return
// attributesContainer.getNamingAttribute().getValue().getValue();
}
public static ResourceObjectIdentification getResourceObjectIdentification(
PrismObject<ShadowType> shadow, ObjectClassComplexTypeDefinition objectClassDefinition) {
return new ResourceObjectIdentification(objectClassDefinition,
ShadowUtil.getPrimaryIdentifiers(shadow), ShadowUtil.getSecondaryIdentifiers(shadow));
}
public static boolean matchesAttribute(ItemPath path, QName attributeName) {
return (ShadowType.F_ATTRIBUTES.equals(ItemPath.getFirstName(path)) &&
QNameUtil.match(ItemPath.getFirstName(path.rest()), attributeName));
}
public static boolean hasPrimaryIdentifier(Collection<? extends ResourceAttribute<?>> identifiers,
ObjectClassComplexTypeDefinition objectClassDefinition) {
for (ResourceAttribute identifier: identifiers) {
if (objectClassDefinition.isPrimaryIdentifier(identifier.getElementName())) {
return true;
}
}
return false;
}
public static ResourceAttribute<?> fixAttributePath(ResourceAttribute<?> attribute) throws SchemaException {
if (attribute == null) {
return null;
}
if (ShadowType.F_ATTRIBUTES.equals(ItemPath.getFirstName(attribute.getPath()))) {
return attribute;
}
ResourceAttribute<?> fixedAttribute = attribute.clone();
ResourceAttributeContainer container = new ResourceAttributeContainer(ShadowType.F_ATTRIBUTES, null, attribute.getPrismContext());
container.createNewValue().add(fixedAttribute);
return fixedAttribute;
}
// TODO: may be useful to move to ObjectClassComplexTypeDefinition later?
public static void validateAttributeSchema(PrismObject<ShadowType> shadow,
ObjectClassComplexTypeDefinition objectClassDefinition) throws SchemaException {
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
for (ResourceAttribute<?> attribute: attributesContainer.getAttributes()) {
validateAttribute(attribute, objectClassDefinition);
}
}
// TODO: may be useful to move to ResourceAttributeDefinition later?
private static <T> void validateAttribute(ResourceAttribute<T> attribute,
ObjectClassComplexTypeDefinition objectClassDefinition) throws SchemaException {
QName attrName = attribute.getElementName();
ResourceAttributeDefinition<T> attrDef = objectClassDefinition.findAttributeDefinition(attrName);
if (attrDef == null) {
throw new SchemaException("No definition for attribute "+attrName+" in object class "+objectClassDefinition);
}
List<PrismPropertyValue<T>> pvals = attribute.getValues();
if (pvals == null || pvals.isEmpty()) {
if (attrDef.isMandatory()) {
throw new SchemaException("Mandatory attribute "+attrName+" has no value");
} else {
return;
}
}
if (pvals.size() > 1 && attrDef.isSingleValue()) {
throw new SchemaException("Single-value attribute "+attrName+" has "+pvals.size()+" values");
}
Class<T> expectedClass = attrDef.getTypeClass();
for (PrismPropertyValue<T> pval: pvals) {
T val = pval.getValue();
if (val == null) {
throw new SchemaException("Null value in attribute "+attrName);
}
// LOGGER.info("MMMMMMMMMMMM: {}:{}\n {} <-> {}", attrName, attrDef, expectedClass, val.getClass());
if (!XmlTypeConverter.isMatchingType(expectedClass, val.getClass())) {
throw new SchemaException("Wrong value in attribute "+attrName+"; expected class "+attrDef.getTypeClass().getSimpleName()+", but was "+val.getClass());
}
}
}
}