/
RefinedObjectClassDefinition.java
939 lines (791 loc) · 35.6 KB
/
RefinedObjectClassDefinition.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
/*
* Copyright (c) 2010-2015 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.common.refinery;
import com.evolveum.midpoint.common.ResourceObjectPattern;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.parser.QueryConvertor;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.prism.util.ItemPathUtil;
import com.evolveum.midpoint.schema.constants.MidPointConstants;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.processor.*;
import com.evolveum.midpoint.schema.util.ResourceTypeUtil;
import com.evolveum.midpoint.schema.util.SchemaDebugUtil;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.ConfigurationException;
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.midpoint.xml.ns._public.resource.capabilities_3.CapabilityType;
import com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.CountObjectsCapabilityType;
import com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.PagedSearchCapabilityType;
import com.evolveum.prism.xml.ns._public.query_3.SearchFilterType;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;
import javax.xml.namespace.QName;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import java.util.*;
/**
* @author semancik
*/
public class RefinedObjectClassDefinition extends ObjectClassComplexTypeDefinition implements DebugDumpable {
private static final Trace LOGGER = TraceManager.getTrace(RefinedObjectClassDefinition.class);
private String intent;
private String displayName;
private String description;
private boolean isDefault;
private ObjectClassComplexTypeDefinition objectClassDefinition;
private ResourceObjectTypeDefinitionType schemaHandlingObjectTypeDefinitionType;
private ResourceType resourceType;
private Collection<? extends RefinedAttributeDefinition<?>> identifiers;
private Collection<? extends RefinedAttributeDefinition<?>> secondaryIdentifiers;
private Collection<ResourceObjectPattern> protectedObjectPatterns;
private List<RefinedAttributeDefinition<?>> attributeDefinitions;
private Collection<RefinedAssociationDefinition> associations = new ArrayList<RefinedAssociationDefinition>();
private ResourceObjectReferenceType baseContext;
/**
* Refined object definition. The "any" parts are replaced with appropriate schema (e.g. resource schema)
*/
PrismObjectDefinition<ShadowType> objectDefinition = null;
private ShadowKindType kind = null;
/**
* This is needed by the LayerRefinedObjectClassDefinition class
*/
protected RefinedObjectClassDefinition(QName typeName, PrismContext prismContext) {
super(typeName, prismContext);
}
private RefinedObjectClassDefinition(PrismContext prismContext, ResourceType resourceType,
ObjectClassComplexTypeDefinition objectClassDefinition) {
super(objectClassDefinition.getTypeName(), prismContext);
Validate.notNull(objectClassDefinition, "ObjectClass definition must not be null");
attributeDefinitions = new ArrayList<>();
this.resourceType = resourceType;
this.objectClassDefinition = objectClassDefinition;
}
/**
* Creates a derived version of this ROCD for a given layer.
* TODO clone if necessary/if specified (currently there is no cloning)
*
* @param layerType
* @return
*/
public LayerRefinedObjectClassDefinition forLayer(LayerType layerType) {
Validate.notNull(layerType);
return LayerRefinedObjectClassDefinition.wrap(this, layerType);
}
@Override
public ResourceAttributeDefinition<?> getDescriptionAttribute() {
return objectClassDefinition.getDescriptionAttribute();
}
@Override
public void setDescriptionAttribute(ResourceAttributeDefinition<?> descriptionAttribute) {
throw new UnsupportedOperationException("Parts of refined account are immutable");
}
@Override
public RefinedAttributeDefinition<?> getNamingAttribute() {
return substituteRefinedAttributeDefinition(objectClassDefinition.getNamingAttribute());
}
@Override
public QName getTypeName() {
return objectClassDefinition.getTypeName();
}
@Override
public String getNativeObjectClass() {
return objectClassDefinition.getNativeObjectClass();
}
@Override
public boolean isDefaultInAKind() {
return isDefault;
}
@Override
public void setDefaultInAKind(boolean defaultAccountType) {
this.isDefault = defaultAccountType;
}
@Override
public String getIntent() {
return intent;
}
@Override
public void setIntent(String intent) {
this.intent = intent;
}
@Override
public ShadowKindType getKind() {
if (kind != null) {
return kind;
}
return getObjectClassDefinition().getKind();
}
@Override
public void setKind(ShadowKindType kind) {
this.kind = kind;
}
@Override
public RefinedAttributeDefinition<?> getDisplayNameAttribute() {
return substituteRefinedAttributeDefinition(objectClassDefinition.getDisplayNameAttribute());
}
@Override
public void setDisplayNameAttribute(QName displayName) {
throw new UnsupportedOperationException("Parts of refined account are immutable");
}
@Override
public Collection<? extends RefinedAttributeDefinition<?>> getIdentifiers() {
if (identifiers == null) {
identifiers = createIdentifiersCollection();
}
return identifiers;
}
@Override
public Collection<? extends RefinedAttributeDefinition<?>> getSecondaryIdentifiers() {
if (secondaryIdentifiers == null) {
secondaryIdentifiers = createIdentifiersCollection();
}
return secondaryIdentifiers;
}
private Collection<? extends RefinedAttributeDefinition<?>> createIdentifiersCollection() {
return new ArrayList<>();
}
public Collection<RefinedAssociationDefinition> getAssociations() {
return associations;
}
public Collection<RefinedAssociationDefinition> getAssociations(ShadowKindType kind) {
Collection<RefinedAssociationDefinition> retAssoc = new ArrayList<RefinedAssociationDefinition>();
for (RefinedAssociationDefinition association: associations) {
if (kind == association.getKind()) {
retAssoc.add(association);
}
}
return retAssoc;
}
public void setAssociations(Collection<RefinedAssociationDefinition> associations) {
this.associations = associations;
}
public RefinedAssociationDefinition findAssociation(QName name) {
for (RefinedAssociationDefinition assocType: getAssociations()) {
if (QNameUtil.match(assocType.getName(), name)) {
return assocType;
}
}
return null;
}
public Collection<RefinedAssociationDefinition> getEntitlementAssociations() {
return getAssociations(ShadowKindType.ENTITLEMENT);
}
public RefinedAssociationDefinition findEntitlementAssociation(QName name) {
for (RefinedAssociationDefinition assocType: getEntitlementAssociations()) {
if (QNameUtil.match(assocType.getName(), name)) {
return assocType;
}
}
return null;
}
public Collection<QName> getNamesOfAssociations() {
Collection<QName> names = new HashSet<QName>();
for (RefinedAssociationDefinition assocDef : getAssociations()) {
names.add(assocDef.getName());
}
return names;
}
public Collection<? extends QName> getNamesOfAssociationsWithOutboundExpressions() {
Collection<QName> names = new HashSet<QName>();
for (RefinedAssociationDefinition assocDef : getAssociations()) {
if (assocDef.getOutboundMappingType() != null) {
names.add(assocDef.getName());
}
}
return names;
}
public Collection<ResourceObjectPattern> getProtectedObjectPatterns() {
if (protectedObjectPatterns == null) {
protectedObjectPatterns = new ArrayList<ResourceObjectPattern>();
}
return protectedObjectPatterns;
}
public PrismContext getPrismContext() {
return resourceType.asPrismObject().getPrismContext();
}
@Override
public RefinedObjectClassDefinition clone() {
RefinedObjectClassDefinition clone = new RefinedObjectClassDefinition(getPrismContext(), resourceType, objectClassDefinition);
copyDefinitionData(clone);
return clone;
}
private void copyDefinitionData(RefinedObjectClassDefinition clone) {
super.copyDefinitionData(clone);
clone.intent = this.intent;
clone.kind = this.kind;
clone.attributeDefinitions = cloneDefinitions(this.attributeDefinitions);
clone.identifiers = cloneDefinitions(this.identifiers);
clone.secondaryIdentifiers = cloneDefinitions(this.secondaryIdentifiers);
clone.associations = cloneAssociations(this.associations);
clone.baseContext = this.baseContext;
clone.description = this.description;
clone.displayName = this.displayName;
clone.isDefault = this.isDefault;
clone.objectClassDefinition = this.objectClassDefinition.clone();
clone.objectDefinition = this.objectDefinition;
clone.resourceType = this.resourceType;
clone.protectedObjectPatterns = this.protectedObjectPatterns;
clone.resourceType = this.resourceType;
clone.schemaHandlingObjectTypeDefinitionType = this.schemaHandlingObjectTypeDefinitionType;
}
private Collection<RefinedAssociationDefinition> cloneAssociations(
Collection<RefinedAssociationDefinition> origAsoc) {
if (origAsoc == null) {
return null;
}
Collection<RefinedAssociationDefinition> cloned = new ArrayList<>();
for (RefinedAssociationDefinition rAsocDef: origAsoc) {
cloned.add(rAsocDef.clone());
}
return cloned;
}
private List<RefinedAttributeDefinition<?>> cloneDefinitions(Collection<? extends RefinedAttributeDefinition<?>> origDefs) {
if (origDefs == null) {
return null;
}
List<RefinedAttributeDefinition<?>> clonedAttributes = new ArrayList<>();
for (RefinedAttributeDefinition<?> attributeDefinition: origDefs) {
clonedAttributes.add(attributeDefinition.clone());
}
return clonedAttributes;
}
@Override
public <X> RefinedAttributeDefinition<X> findAttributeDefinition(QName elementQName) {
return findItemDefinition(elementQName, RefinedAttributeDefinition.class);
}
@Override
public <X> RefinedAttributeDefinition<X> findAttributeDefinition(String elementLocalname) {
QName elementQName = new QName(getResourceNamespace(), elementLocalname);
return findAttributeDefinition(elementQName);
}
protected String getResourceNamespace() {
return ResourceTypeUtil.getResourceNamespace(resourceType);
}
@Override
public String getDisplayName() {
return displayName;
}
@Override
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isDefault() {
return isDefault;
}
public void setDefault(boolean isDefault) {
this.isDefault = isDefault;
}
public ObjectClassComplexTypeDefinition getObjectClassDefinition() {
return objectClassDefinition;
}
public void setObjectClassDefinition(ObjectClassComplexTypeDefinition objectClassDefinition) {
Validate.notNull(objectClassDefinition, "ObjectClass definition must not be null");
this.objectClassDefinition = objectClassDefinition;
}
@Override
public Collection<? extends RefinedAttributeDefinition<?>> getAttributeDefinitions() {
return attributeDefinitions;
}
@Override
public List<? extends ItemDefinition> getDefinitions() {
return attributeDefinitions;
}
public ResourceType getResourceType() {
return resourceType;
}
public PrismObjectDefinition<ShadowType> getObjectDefinition() {
if (objectDefinition == null) {
constructObjectDefinition();
}
return objectDefinition;
}
public ResourceObjectReferenceType getBaseContext() {
return baseContext;
}
public void setBaseContext(ResourceObjectReferenceType baseContext) {
this.baseContext = baseContext;
}
private void constructObjectDefinition() {
// Almost-shallow clone of object definition and complex type
PrismObjectDefinition<ShadowType> originalObjectDefinition =
getSchemaRegistry().findObjectDefinitionByCompileTimeClass(ShadowType.class);
PrismObjectDefinition<ShadowType> refinedObjectDef =
originalObjectDefinition.cloneWithReplacedDefinition(ShadowType.F_ATTRIBUTES,
this.toResourceAttributeContainerDefinition());
this.objectDefinition = refinedObjectDef;
}
public RefinedAttributeDefinition<?> getAttributeDefinition(QName attributeName) {
for (RefinedAttributeDefinition<?> attrDef : attributeDefinitions) {
if (QNameUtil.match(attrDef.getName(), attributeName)) {
return attrDef;
}
}
return null;
}
public void add(RefinedAttributeDefinition<?> refinedAttributeDefinition) {
attributeDefinitions.add(refinedAttributeDefinition);
}
public boolean containsAttributeDefinition(ItemPathType pathType) {
QName segmentQName = ItemPathUtil.getOnlySegmentQName(pathType);
return containsAttributeDefinition(segmentQName);
}
public boolean containsAttributeDefinition(QName attributeName) {
for (RefinedAttributeDefinition<?> rAttributeDef : attributeDefinitions) {
if (QNameUtil.match(rAttributeDef.getName(), attributeName)) {
return true;
}
}
return false;
}
static RefinedObjectClassDefinition parse(ResourceObjectTypeDefinitionType entTypeDefType,
ResourceType resourceType, RefinedResourceSchema rSchema, ShadowKindType impliedKind, PrismContext prismContext,
String contextDescription) throws SchemaException {
ShadowKindType kind = entTypeDefType.getKind();
if (kind == null) {
kind = impliedKind;
}
if (kind == null) {
kind = ShadowKindType.ACCOUNT;
}
String intent = entTypeDefType.getIntent();
RefinedObjectClassDefinition rObjectClassDef = parseRefinedObjectClass(entTypeDefType,
resourceType, rSchema, prismContext, kind, intent, kind.value(), kind.value() + " type definition '"+intent+"' in " + contextDescription);
if (entTypeDefType.getPagedSearches() != null) {
LOGGER.warn("PagedSearches element is no more supported and is ignored. Use PagedSearchCapabilityType instead. In {}", resourceType);
}
return rObjectClassDef;
}
private static void parseProtected(RefinedObjectClassDefinition rAccountDef, ResourceObjectTypeDefinitionType accountTypeDefType) throws SchemaException {
for (ResourceObjectPatternType protectedType: accountTypeDefType.getProtected()) {
ResourceObjectPattern protectedPattern = convertToPattern(protectedType, rAccountDef);
rAccountDef.getProtectedObjectPatterns().add(protectedPattern);
}
}
private static ResourceObjectPattern convertToPattern(ResourceObjectPatternType patternType, RefinedObjectClassDefinition rAccountDef) throws SchemaException {
ResourceObjectPattern resourceObjectPattern = new ResourceObjectPattern(rAccountDef);
SearchFilterType filterType = patternType.getFilter();
if (filterType != null) {
ObjectFilter filter = QueryConvertor.parseFilter(filterType, rAccountDef.getObjectDefinition());
resourceObjectPattern.addFilter(filter);
return resourceObjectPattern;
}
// Deprecated
if (patternType.getName() != null) {
RefinedAttributeDefinition<String> attributeDefinition = rAccountDef.findAttributeDefinition(new QName(SchemaConstants.NS_ICF_SCHEMA,"name"));
ResourceAttribute<String> attr = attributeDefinition.instantiate();
attr.setRealValue(patternType.getName());
resourceObjectPattern.addIdentifier(attr);
} else if (patternType.getUid() != null) {
RefinedAttributeDefinition attributeDefinition = rAccountDef.findAttributeDefinition(new QName(SchemaConstants.NS_ICF_SCHEMA,"uid"));
ResourceAttribute<String> attr = attributeDefinition.instantiate();
attr.setRealValue(patternType.getName());
resourceObjectPattern.addIdentifier(attr);
} else {
throw new SchemaException("No filter and no deprecated name/uid in resource object pattern");
}
return resourceObjectPattern;
}
static RefinedObjectClassDefinition parseFromSchema(ObjectClassComplexTypeDefinition objectClassDef, ResourceType resourceType,
RefinedResourceSchema rSchema,
PrismContext prismContext, String contextDescription) throws SchemaException {
RefinedObjectClassDefinition rAccountDef = new RefinedObjectClassDefinition(prismContext, resourceType, objectClassDef);
String accountTypeName = null;
if (objectClassDef.getIntent() != null) {
accountTypeName = objectClassDef.getIntent();
if (accountTypeName == null) {
accountTypeName = SchemaConstants.INTENT_DEFAULT;
}
rAccountDef.setIntent(accountTypeName);
} else {
if (objectClassDef.isDefaultInAKind()) {
rAccountDef.setIntent(MidPointConstants.DEFAULT_ACCOUNT_TYPE_NAME);
} else {
throw new SchemaException("Account type definition does not have a name, in " + contextDescription);
}
}
if (objectClassDef.getDisplayName() != null) {
rAccountDef.setDisplayName(objectClassDef.getDisplayName());
}
rAccountDef.setDefault(objectClassDef.isDefaultInAKind());
for (ResourceAttributeDefinition attrDef : objectClassDef.getAttributeDefinitions()) {
String attrContextDescription = accountTypeName + ", in " + contextDescription;
RefinedAttributeDefinition rAttrDef = RefinedAttributeDefinition.parse(attrDef, null, objectClassDef, prismContext,
attrContextDescription);
rAccountDef.processIdentifiers(rAttrDef, objectClassDef);
if (rAccountDef.containsAttributeDefinition(rAttrDef.getName())) {
throw new SchemaException("Duplicate definition of attribute " + rAttrDef.getName() + " in " + attrContextDescription);
}
rAccountDef.add(rAttrDef);
}
return rAccountDef;
}
private static RefinedObjectClassDefinition parseRefinedObjectClass(ResourceObjectTypeDefinitionType schemaHandlingObjDefType,
ResourceType resourceType, RefinedResourceSchema rSchema, PrismContext prismContext,
ShadowKindType kind, String intent, String typeDesc, String contextDescription) throws SchemaException {
ObjectClassComplexTypeDefinition objectClassDef;
if (schemaHandlingObjDefType.getObjectClass() != null) {
QName objectClass = schemaHandlingObjDefType.getObjectClass();
objectClassDef = rSchema.getOriginalResourceSchema().findObjectClassDefinition(objectClass);
if (objectClassDef == null) {
throw new SchemaException("Object class " + objectClass + " as specified in "+typeDesc+" type " + schemaHandlingObjDefType.getIntent() + " was not found in the resource schema of " + contextDescription);
}
} else {
throw new SchemaException("Definition of "+typeDesc+" type " + schemaHandlingObjDefType.getIntent() + " does not have objectclass, in " + contextDescription);
}
RefinedObjectClassDefinition rOcDef = new RefinedObjectClassDefinition(prismContext, resourceType, objectClassDef);
rOcDef.setKind(kind);
rOcDef.schemaHandlingObjectTypeDefinitionType = schemaHandlingObjDefType;
if (intent == null && kind == ShadowKindType.ACCOUNT) {
intent = SchemaConstants.INTENT_DEFAULT;
}
if (intent != null) {
rOcDef.setIntent(intent);
} else {
throw new SchemaException(StringUtils.capitalize(typeDesc)+" type definition does not have intent, in " + contextDescription);
}
if (schemaHandlingObjDefType.getDisplayName() != null) {
rOcDef.setDisplayName(schemaHandlingObjDefType.getDisplayName());
} else {
if (objectClassDef.getDisplayName() != null) {
rOcDef.setDisplayName(objectClassDef.getDisplayName());
}
}
if (schemaHandlingObjDefType.getDescription() != null) {
rOcDef.setDescription(schemaHandlingObjDefType.getDescription());
}
if (schemaHandlingObjDefType.isDefault() != null) {
rOcDef.setDefault(schemaHandlingObjDefType.isDefault());
} else {
rOcDef.setDefault(objectClassDef.isDefaultInAKind());
}
if (schemaHandlingObjDefType.getBaseContext() != null) {
rOcDef.setBaseContext(schemaHandlingObjDefType.getBaseContext());
}
for (ResourceAttributeDefinition road : objectClassDef.getAttributeDefinitions()) {
String attrContextDescription = road.getName() + ", in " + contextDescription;
ResourceAttributeDefinitionType attrDefType = findAttributeDefinitionType(road.getName(), schemaHandlingObjDefType,
typeDesc, attrContextDescription);
// We MUST NOT skip ignored attribute definitions here. We must include them in the schema as
// the shadows will still have that attributes and we will need their type definition to work
// well with them. They may also be mandatory. We cannot pretend that they do not exist.
RefinedAttributeDefinition rAttrDef = RefinedAttributeDefinition.parse(road, attrDefType, objectClassDef,
prismContext, "in "+typeDesc+" type " + intent + ", in " + contextDescription);
rOcDef.processIdentifiers(rAttrDef, objectClassDef);
if (rOcDef.containsAttributeDefinition(rAttrDef.getName())) {
throw new SchemaException("Duplicate definition of attribute " + rAttrDef.getName() + " in "+typeDesc+" type " +
intent + ", in " + contextDescription);
}
rOcDef.add(rAttrDef);
}
// Check for extra attribute definitions in the account type
for (ResourceAttributeDefinitionType attrDefType : schemaHandlingObjDefType.getAttribute()) {
if (!rOcDef.containsAttributeDefinition(attrDefType.getRef()) && !RefinedAttributeDefinition.isIgnored(attrDefType)) {
throw new SchemaException("Definition of attribute " + attrDefType.getRef() + " not found in object class " + objectClassDef.getTypeName() + " as defined in " + contextDescription);
}
}
parseProtected(rOcDef, schemaHandlingObjDefType);
return rOcDef;
}
public void parseAssociations(RefinedResourceSchema rSchema) throws SchemaException {
for (ResourceObjectAssociationType resourceObjectAssociationType: schemaHandlingObjectTypeDefinitionType.getAssociation()) {
RefinedAssociationDefinition rAssocDef = new RefinedAssociationDefinition(resourceObjectAssociationType);
ShadowKindType assocKind = rAssocDef.getKind();
RefinedObjectClassDefinition assocTarget = rSchema.getRefinedDefinition(assocKind, rAssocDef.getIntents());
rAssocDef.setAssociationTarget(assocTarget);
associations.add(rAssocDef);
}
}
private void processIdentifiers(RefinedAttributeDefinition rAttrDef, ObjectClassComplexTypeDefinition objectClassDef) {
QName attrName = rAttrDef.getName();
if (objectClassDef.isIdentifier(attrName)) {
((Collection)getIdentifiers()).add(rAttrDef);
}
if (objectClassDef.isSecondaryIdentifier(attrName) || rAttrDef.isSecondaryIdentifier()) {
((Collection)getSecondaryIdentifiers()).add(rAttrDef);
}
}
private RefinedAttributeDefinition substituteRefinedAttributeDefinition(ResourceAttributeDefinition attributeDef) {
RefinedAttributeDefinition rAttrDef = findAttributeDefinition(attributeDef.getName());
return rAttrDef;
}
private static ResourceAttributeDefinitionType findAttributeDefinitionType(QName attrName,
ResourceObjectTypeDefinitionType rOcDefType, String typeDesc, String contextDescription) throws SchemaException {
ResourceAttributeDefinitionType foundAttrDefType = null;
for (ResourceAttributeDefinitionType attrDefType : rOcDefType.getAttribute()) {
if (attrDefType.getRef() != null) {
QName ref = ItemPathUtil.getOnlySegmentQName(attrDefType.getRef());
if (QNameUtil.match(ref, attrName)) {
if (foundAttrDefType == null) {
foundAttrDefType = attrDefType;
} else {
throw new SchemaException("Duplicate definition of attribute " + ref + " in "+typeDesc+" type "
+ rOcDefType.getIntent() + ", in " + contextDescription);
}
}
} else {
throw new SchemaException("Missing reference to the attribute schema definition in definition " + SchemaDebugUtil.prettyPrint(attrDefType) + " during processing of " + contextDescription);
}
}
return foundAttrDefType;
}
public PrismObject<ShadowType> createBlankShadow() {
PrismObject<ShadowType> accountShadow;
try {
accountShadow = prismContext.getSchemaRegistry().instantiate(ShadowType.class);
} catch (SchemaException e) {
// This should not happen
throw new SystemException("Internal error instantiating account shadow: "+e.getMessage(), e);
}
ShadowType accountShadowType = accountShadow.asObjectable();
accountShadowType.setIntent(getIntent());
accountShadowType.setKind(getKind());
accountShadowType.setObjectClass(objectClassDefinition.getTypeName());
accountShadowType.setResourceRef(ObjectTypeUtil.createObjectRef(resourceType));
// Setup definition
PrismObjectDefinition<ShadowType> newDefinition = accountShadow.getDefinition().cloneWithReplacedDefinition(
ShadowType.F_ATTRIBUTES, toResourceAttributeContainerDefinition());
accountShadow.setDefinition(newDefinition);
return accountShadow;
}
public ResourceShadowDiscriminator getShadowDiscriminator() {
return new ResourceShadowDiscriminator(resourceType.getOid(), getKind(), getIntent());
}
public Collection<? extends QName> getNamesOfAttributesWithOutboundExpressions() {
Collection<QName> attrNames = new HashSet<QName>();
for (RefinedAttributeDefinition attrDef : getAttributeDefinitions()) {
if (attrDef.getOutboundMappingType() != null) {
attrNames.add(attrDef.getName());
}
}
return attrNames;
}
public Collection<? extends QName> getNamesOfAttributesWithInboundExpressions() {
Collection<QName> attrNames = new HashSet<QName>();
for (RefinedAttributeDefinition attrDef : getAttributeDefinitions()) {
List<MappingType> inbounds = attrDef.getInboundMappingTypes();
if (inbounds != null && !inbounds.isEmpty()) {
attrNames.add(attrDef.getName());
}
}
return attrNames;
}
public List<MappingType> getCredentialsInbound() {
ResourcePasswordDefinitionType password = getPasswordDefinition();
if (password == null || password.getInbound() == null) {
return null;
}
return password.getInbound();
}
public MappingType getCredentialsOutbound() {
ResourcePasswordDefinitionType password = getPasswordDefinition();
if (password == null || password.getOutbound() == null) {
return null;
}
return password.getOutbound();
}
public AttributeFetchStrategyType getPasswordFetchStrategy() {
ResourcePasswordDefinitionType password = getPasswordDefinition();
if (password == null) {
return AttributeFetchStrategyType.IMPLICIT;
}
if (password.getFetchStrategy() == null) {
return AttributeFetchStrategyType.IMPLICIT;
}
return password.getFetchStrategy();
}
public ObjectReferenceType getPasswordPolicy(){
ResourcePasswordDefinitionType password = getPasswordDefinition();
if (password == null || password.getPasswordPolicyRef() == null){
return null;
}
return password.getPasswordPolicyRef();
}
private ResourcePasswordDefinitionType getPasswordDefinition(){
if (schemaHandlingObjectTypeDefinitionType == null) {
return null;
}
ResourceCredentialsDefinitionType credentials = schemaHandlingObjectTypeDefinitionType.getCredentials();
if (credentials == null) {
return null;
}
return credentials.getPassword();
}
public ResourceActivationDefinitionType getActivationSchemaHandling(){
if (schemaHandlingObjectTypeDefinitionType == null) {
return null;
}
return schemaHandlingObjectTypeDefinitionType.getActivation();
}
public ResourceBidirectionalMappingType getActivationBidirectionalMappingType(QName propertyName) {
ResourceActivationDefinitionType activationSchemaHandling = getActivationSchemaHandling();
if (activationSchemaHandling == null) {
return null;
}
if (QNameUtil.match(ActivationType.F_ADMINISTRATIVE_STATUS, propertyName)) {
return activationSchemaHandling.getAdministrativeStatus();
} else if (QNameUtil.match(ActivationType.F_VALID_FROM, propertyName)) {
return activationSchemaHandling.getValidFrom();
} else if (QNameUtil.match(ActivationType.F_VALID_TO, propertyName)) {
return activationSchemaHandling.getValidTo();
} else if (QNameUtil.match(ActivationType.F_LOCKOUT_STATUS, propertyName)) {
return null; // todo implement this
} else if (QNameUtil.match(ActivationType.F_LOCKOUT_EXPIRATION_TIMESTAMP, propertyName)) {
return null; // todo implement this
} else {
throw new IllegalArgumentException("Unknown activation property "+propertyName);
}
}
public AttributeFetchStrategyType getActivationFetchStrategy(QName propertyName) {
ResourceBidirectionalMappingType biType = getActivationBidirectionalMappingType(propertyName);
if (biType == null) {
return AttributeFetchStrategyType.IMPLICIT;
}
if (biType.getFetchStrategy() == null) {
return AttributeFetchStrategyType.IMPLICIT;
}
return biType.getFetchStrategy();
}
public static RefinedObjectClassDefinition determineObjectClassDefinition(PrismObject<ShadowType> shadow, ResourceType resource) throws SchemaException, ConfigurationException {
ShadowType shadowType = shadow.asObjectable();
RefinedResourceSchema refinedSchema = RefinedResourceSchema.getRefinedSchema(resource, resource.asPrismObject().getPrismContext());
if (refinedSchema == null) {
throw new ConfigurationException("No schema defined for "+resource);
}
RefinedObjectClassDefinition objectClassDefinition = null;
ShadowKindType kind = shadowType.getKind();
String intent = shadowType.getIntent();
QName objectClass = shadow.asObjectable().getObjectClass();
if (kind != null) {
objectClassDefinition = refinedSchema.getRefinedDefinition(kind, intent);
}
if (objectClassDefinition == null) {
// Fallback to objectclass only
if (objectClass == null) {
throw new SchemaException("No kind nor objectclass definied in "+shadow);
}
objectClassDefinition = refinedSchema.findRefinedDefinitionByObjectClassQName(kind, objectClass);
}
if (objectClassDefinition == null) {
throw new SchemaException("Definition for "+shadow+" not found (objectClass=" + PrettyPrinter.prettyPrint(objectClass) +
", kind="+kind+", intent='"+intent+"') in schema of " + resource);
}
return objectClassDefinition;
}
public boolean matches(ShadowType shadowType) {
if (shadowType == null) {
return false;
}
if (!QNameUtil.match(objectClassDefinition.getTypeName(), shadowType.getObjectClass())) {
return false;
}
if (shadowType.getKind() == null) {
if (kind != ShadowKindType.ACCOUNT) {
return false;
}
} else {
if (!MiscUtil.equals(kind, shadowType.getKind())) {
return false;
}
}
if (shadowType.getIntent() != null) {
// if (isDefault) {
// return true;
// } else {
// return false;
// }
// } else {
return MiscUtil.equals(intent, shadowType.getIntent());
}
return true;
}
@Override
public String debugDump() {
return debugDump(0);
}
@Override
public String debugDump(int indent) {
return debugDump(indent, null);
}
protected String debugDump(int indent, LayerType layer) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < indent; i++) {
sb.append(INDENT_STRING);
}
sb.append(getDebugDumpClassName()).append("(");
sb.append(SchemaDebugUtil.prettyPrint(getTypeName()));
if (isDefault()) {
sb.append(",default");
}
if (getKind() != null) {
sb.append(" ").append(getKind().value());
}
sb.append(",");
if (getIntent() != null) {
sb.append("intent=").append(getIntent());
}
if (layer != null) {
sb.append(",layer=").append(layer);
}
sb.append(")");
for (RefinedAttributeDefinition rAttrDef: getAttributeDefinitions()) {
sb.append("\n");
sb.append(rAttrDef.debugDump(indent + 1, layer));
}
return sb.toString();
}
/**
* Return a human readable name of this class suitable for logs.
*/
@Override
protected String getDebugDumpClassName() {
return "rOCD";
}
public String getHumanReadableName() {
if (getDisplayName() != null) {
return getDisplayName();
} else {
return getKind()+":"+getIntent();
}
}
public <T extends CapabilityType> T getEffectiveCapability(Class<T> capabilityClass) {
return ResourceTypeUtil.getEffectiveCapability(resourceType, schemaHandlingObjectTypeDefinitionType, capabilityClass);
}
public PagedSearchCapabilityType getPagedSearches() {
return getEffectiveCapability(PagedSearchCapabilityType.class);
}
public boolean isPagedSearchEnabled() {
return getPagedSearches() != null; // null means nothing or disabled
}
public boolean isObjectCountingEnabled() {
return getEffectiveCapability(CountObjectsCapabilityType.class) != null;
}
}