-
Notifications
You must be signed in to change notification settings - Fork 257
/
OMRSRepositoryContentManager.java
3740 lines (3267 loc) · 162 KB
/
OMRSRepositoryContentManager.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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright Contributors to the ODPi Egeria project. */
package org.odpi.openmetadata.repositoryservices.localrepository.repositorycontentmanager;
import org.odpi.openmetadata.frameworks.auditlog.AuditLog;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.OMRSMetadataCollection;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.instances.EnumPropertyValue;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.instances.InstanceProperties;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.instances.InstanceStatus;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.instances.InstanceType;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.typedefs.*;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.utilities.OMRSRepositoryPropertiesUtilities;
import org.odpi.openmetadata.repositoryservices.eventmanagement.OMRSRepositoryEventManager;
import org.odpi.openmetadata.repositoryservices.events.*;
import org.odpi.openmetadata.repositoryservices.ffdc.OMRSAuditCode;
import org.odpi.openmetadata.repositoryservices.ffdc.OMRSErrorCode;
import org.odpi.openmetadata.repositoryservices.ffdc.exception.*;
import org.odpi.openmetadata.repositoryservices.localrepository.repositoryconnector.LocalOMRSRepositoryConnector;
import org.odpi.openmetadata.repositoryservices.rest.server.OMRSRepositoryRESTServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
/**
* OMRSRepositoryContentManager supports an in-memory cache of TypeDefs for the local server. It is used by the OMRS
* components for constructing metadata instances with valid types. It ensures that the TypeDefs used in other
* members of the open metadata repository cohorts that the local server is also a member of are consistent with the
* local repository.
*
* OMRSRepositoryContentManager plays a central role in ensuring the integrity of the metadata in the local repository.
* It is called from multiple components at different points in the processing. It presents a different interface
* to each of these components that is specialized to their needs.
* <ul>
* <li>
* OMRSTypeDefEventProcessor: processes inbound events from remote members of the open metadata
* repository cohorts that the local repository is connected to. These incoming TypeDef events need to
* be validated against the types used locally and either saved or discarded depending on the exchange rule
* setting.
* </li>
* <li>
* OMRSTypeDefManager: provides maintenance methods for managing the TypeDefs in the local cache.
* </li>
* </ul>
*/
public class OMRSRepositoryContentManager extends OMRSTypeDefEventProcessor implements OMRSTypeDefManager
{
private final String localServerUserId; /* initialized in the constructor */
private LocalOMRSRepositoryConnector localRepositoryConnector = null;
private String localServerName = null;
private OMRSRepositoryEventManager outboundRepositoryEventManager = null;
private String openTypesOriginGUID = null;
private final Map<String, TypeDef> knownTypeDefGUIDs = new HashMap<>();
private final Map<String, TypeDef> knownTypeDefNames = new HashMap<>();
private final Map<String, AttributeTypeDef> knownAttributeTypeDefGUIDs = new HashMap<>();
private final Map<String, AttributeTypeDef> knownAttributeTypeDefNames = new HashMap<>();
private final Map<String, TypeDef> activeTypeDefGUIDs = new HashMap<>();
private final Map<String, TypeDef> activeTypeDefNames = new HashMap<>();
private final Map<String, AttributeTypeDef> activeAttributeTypeDefGUIDs = new HashMap<>();
private final Map<String, AttributeTypeDef> activeAttributeTypeDefNames = new HashMap<>();
private final Map<String, List<TypeDefLink>> typeDefSuperTypes = new HashMap<>();
private final Map<String, InstanceType> knownInstanceTypes = new HashMap<>();
private final Map<String, String> metadataCollectionNames = new HashMap<>();
private final Map<String, Set<String>> knownPropertyToTypeDefNames = new HashMap<>();
/*
* The audit log provides a verifiable record of the open metadata archives that have been loaded into
* the open metadata repository. The Logger is for standard debug.
*/
private final AuditLog auditLog;
private static final Logger log = LoggerFactory.getLogger(OMRSRepositoryContentManager.class);
/**
* Default constructor
*
* @param localServerUserId userId to use when processing messages
* @param auditLog audit log for this component.
*/
public OMRSRepositoryContentManager(String localServerUserId,
AuditLog auditLog)
{
super("Local Repository Content (TypeDef) Manager");
this.localServerUserId = localServerUserId;
this.auditLog = auditLog;
}
/**
* Saves all the information necessary to process incoming TypeDef events.
*
* @param localRepositoryConnector connector to the local repository
* @param outboundRepositoryEventManager event manager to call for outbound events used to send out reports
* of conflicting TypeDefs
*/
public void setupEventProcessor(LocalOMRSRepositoryConnector localRepositoryConnector,
OMRSRepositoryEventManager outboundRepositoryEventManager)
{
if (localRepositoryConnector != null)
{
this.localRepositoryConnector = localRepositoryConnector;
this.localServerName = localRepositoryConnector.getLocalServerName();
}
this.outboundRepositoryEventManager = outboundRepositoryEventManager;
}
/**
* Save the unique identifier of the open metadata archive. This is stored in the origin property of
* all the open metadata types. It is needed to support the isOpenType() method.
*
* @param openMetadataTypesGUID unique identifier for the open metadata type's archive
*/
public void setOpenMetadataTypesOriginGUID(String openMetadataTypesGUID)
{
openTypesOriginGUID = openMetadataTypesGUID;
}
/*
* ========================
* OMRSTypeDefManager
*/
/**
* Cache a definition of a new TypeDef. This method assumes the TypeDef has been successfully added to the
* local repository already and all that is needed is to maintain the cached list of types
*
* @param sourceName source of the request (used for logging)
* @param newTypeDef TypeDef structure describing the new TypeDef.
*/
@Override
public void addTypeDef(String sourceName, TypeDef newTypeDef)
{
this.cacheTypeDef(sourceName, newTypeDef,true);
}
/**
* Cache a definition of a TypeDef.
*
* @param sourceName source of the request (used for logging)
* @param newTypeDef TypeDef structure describing the new TypeDef.
* @param isLocallySupported indicates whether the TypeDef is supported by the local repository.
*/
private void cacheTypeDef(String sourceName, TypeDef newTypeDef, boolean isLocallySupported)
{
knownTypeDefGUIDs.put(newTypeDef.getGUID(), newTypeDef);
knownTypeDefNames.put(newTypeDef.getName(), newTypeDef);
if (isLocallySupported)
{
activeTypeDefGUIDs.put(newTypeDef.getGUID(), newTypeDef);
activeTypeDefNames.put(newTypeDef.getName(), newTypeDef);
log.debug("New Active Type {} from {}. Full TypeDef: {}", newTypeDef.getName(), sourceName, newTypeDef);
}
else
{
log.debug("New Known Type {} from {}. Full TypeDef: {}", newTypeDef.getName(), sourceName, newTypeDef);
}
cacheTypeDefPropertyLookup(sourceName, newTypeDef);
}
/**
* An incoming patch for the type is not supported by the local repository and so it has not been processed into a TypeDef.
* This method converts it into the TypeDef and adds it to known types.
*
* @param sourceName source of the request (used for logging)
* @param typeDefPatch received patch
* @param methodName calling method
* @throws InvalidParameterException the original typeDef or typeDefPatch is null
* @throws PatchErrorException the patch is either badly formatted, or does not apply to the supplied TypeDef
*/
private void cacheUnsupportedTypeDef(String sourceName, TypeDefPatch typeDefPatch, String methodName) throws InvalidParameterException,
PatchErrorException
{
TypeDef originalTypeDef = getTypeDefByName(typeDefPatch.getTypeDefName());
OMRSRepositoryPropertiesUtilities utilities = new OMRSRepositoryPropertiesUtilities();
TypeDef updatedTypeDef = utilities.applyPatch(sourceName,
originalTypeDef,
typeDefPatch,
methodName);
this.cacheTypeDef(sourceName, updatedTypeDef, false);
}
/**
* Cache a lookup of the TypeDef's properties by their name, to the names of the TypeDefs in which a property by
* that name is defined.
*
* @param sourceName source of the request (used for logging)
* @param typeDef TypeDef structure describing the new TypeDef.
*/
private void cacheTypeDefPropertyLookup(String sourceName, TypeDef typeDef)
{
// retrieve all properties on the newTypeDef
String typeDefName = typeDef.getName();
List<TypeDefAttribute> propertiesDefinition = typeDef.getPropertiesDefinition();
if (propertiesDefinition != null)
{
// for each property, add this typeDef's GUID as one that defines a property with this name
for (TypeDefAttribute property : propertiesDefinition)
{
String propertyName = property.getAttributeName();
knownPropertyToTypeDefNames.computeIfAbsent(propertyName, k -> new HashSet<>());
knownPropertyToTypeDefNames.get(propertyName).add(typeDefName);
log.debug("Cached property '{}' from {}, for lookup under TypeDef: {}", propertyName, sourceName, typeDefName);
}
}
}
/**
* Remove a definition of a TypeDef.
*
* @param sourceName source of the request (used for logging)
* @param obsoleteTypeDefGUID unique identifier for the type.
* @param obsoleteTypeDefName unique name for the type.
* @param isLocallySupported indicates whether the TypeDef is supported by the local repository.
*/
private void uncacheTypeDef(String sourceName,
String obsoleteTypeDefGUID,
String obsoleteTypeDefName,
boolean isLocallySupported)
{
knownTypeDefGUIDs.remove(obsoleteTypeDefGUID);
knownTypeDefNames.remove(obsoleteTypeDefName);
if (isLocallySupported)
{
activeTypeDefGUIDs.remove(obsoleteTypeDefGUID);
activeTypeDefNames.remove(obsoleteTypeDefName);
}
log.debug("Removed Type {} from {}", obsoleteTypeDefName, sourceName);
uncacheTypeDefPropertyLookup(sourceName, obsoleteTypeDefName);
}
/**
* Remove a TypeDef from the reverse property lookup.
*
* @param sourceName source of the request (used for logging)
* @param typeDefName unique name for the type.
*/
private void uncacheTypeDefPropertyLookup(String sourceName, String typeDefName)
{
// Not much choice but to iterate through the entire Map...
for (String propertyName : knownPropertyToTypeDefNames.keySet())
{
// ... but the removal operation at least is idempotent (no need to first check it is present in the Set)
boolean removed = knownPropertyToTypeDefNames.get(propertyName).remove(typeDefName);
if (removed)
{
log.debug("Removed Type {} from {}, from reverse-lookup of property: {}", typeDefName, sourceName, propertyName);
}
}
}
/**
* Cache a definition of a new AttributeTypeDef.
*
* @param sourceName source of the request (used for logging)
* @param newAttributeTypeDef AttributeTypeDef structure describing the new TypeDef.
*/
@Override
public void addAttributeTypeDef(String sourceName, AttributeTypeDef newAttributeTypeDef)
{
this.cacheAttributeTypeDef(sourceName, newAttributeTypeDef, true);
}
/**
* Cache a definition of a new AttributeTypeDef.
*
* @param sourceName source of the request (used for logging)
* @param newAttributeTypeDef AttributeTypeDef structure describing the new TypeDef.
* @param isLocallySupported indicates whether the TypeDef is supported by the local repository.
*/
private void cacheAttributeTypeDef(String sourceName,
AttributeTypeDef newAttributeTypeDef,
boolean isLocallySupported)
{
knownAttributeTypeDefGUIDs.put(newAttributeTypeDef.getGUID(), newAttributeTypeDef);
knownAttributeTypeDefNames.put(newAttributeTypeDef.getName(), newAttributeTypeDef);
if (isLocallySupported)
{
activeAttributeTypeDefGUIDs.put(newAttributeTypeDef.getGUID(), newAttributeTypeDef);
activeAttributeTypeDefNames.put(newAttributeTypeDef.getName(), newAttributeTypeDef);
if (log.isDebugEnabled())
{
log.debug("New Active Attribute Type " + newAttributeTypeDef.getName() + " from " + sourceName+ ". Full AttributeTypeDef: " + newAttributeTypeDef);
}
else
{
log.debug("New Known Attribute Type " + newAttributeTypeDef.getName() + " from " + sourceName+ ". Full AttributeTypeDef: " + newAttributeTypeDef);
}
}
}
/**
* Update one or more properties of a cached TypeDef. This method assumes the TypeDef has been successfully
* updated in the local repository already and all that is needed is to maintain the cached list of types
*
* @param sourceName source of the request (used for logging)
* @param typeDef TypeDef structure.
*/
@Override
public void updateTypeDef(String sourceName, TypeDef typeDef)
{
this.cacheTypeDef(sourceName, typeDef,true);
}
/**
* Delete a cached TypeDef.
*
* @param sourceName source of the request (used for logging)
* @param obsoleteTypeDefGUID String unique identifier for the TypeDef.
* @param obsoleteTypeDefName String unique name for the TypeDef.
*/
@Override
public void deleteTypeDef(String sourceName,
String obsoleteTypeDefGUID,
String obsoleteTypeDefName)
{
if (this.validTypeId(sourceName, obsoleteTypeDefGUID, obsoleteTypeDefName))
{
knownTypeDefGUIDs.remove(obsoleteTypeDefGUID);
knownTypeDefNames.remove(obsoleteTypeDefName);
if (localRepositoryConnector != null)
{
activeTypeDefGUIDs.remove(obsoleteTypeDefGUID);
activeTypeDefNames.remove(obsoleteTypeDefName);
log.debug("Deleted Active TypeDef " + obsoleteTypeDefName + " from " + sourceName);
}
}
}
/**
* Delete a cached AttributeTypeDef.
*
* @param sourceName source of the request (used for logging)
* @param obsoleteAttributeTypeDefGUID String unique identifier for the AttributeTypeDef.
* @param obsoleteAttributeTypeDefName String unique name for the AttributeTypeDef.
*/
@Override
public void deleteAttributeTypeDef(String sourceName,
String obsoleteAttributeTypeDefGUID,
String obsoleteAttributeTypeDefName)
{
if (this.validTypeId(sourceName, obsoleteAttributeTypeDefGUID, obsoleteAttributeTypeDefName))
{
knownAttributeTypeDefGUIDs.remove(obsoleteAttributeTypeDefGUID);
knownAttributeTypeDefNames.remove(obsoleteAttributeTypeDefName);
if (localRepositoryConnector != null)
{
activeAttributeTypeDefGUIDs.remove(obsoleteAttributeTypeDefGUID);
activeAttributeTypeDefNames.remove(obsoleteAttributeTypeDefName);
if (log.isDebugEnabled())
{
log.debug("Deleted Active AttributeTypeDef " + obsoleteAttributeTypeDefName + " from " + sourceName);
}
}
}
}
/**
* Change the identifiers for a TypeDef.
*
* @param sourceName source of the request (used for logging).
* @param originalTypeDefGUID TypeDef's original unique identifier.
* @param originalTypeDefName TypeDef's original unique name.
* @param newTypeDef updated TypeDef with new identifiers.
*/
@Override
public void reIdentifyTypeDef(String sourceName,
String originalTypeDefGUID,
String originalTypeDefName,
TypeDef newTypeDef)
{
this.deleteTypeDef(sourceName, originalTypeDefGUID, originalTypeDefName);
this.addTypeDef(sourceName, newTypeDef);
}
/**
* Change the identifiers for an AttributeTypeDef.
*
* @param sourceName source of the request (used for logging).
* @param originalAttributeTypeDefGUID AttributeTypeDef's original unique identifier.
* @param originalAttributeTypeDefName AttributeTypeDef's original unique name.
* @param newAttributeTypeDef updated AttributeTypeDef with new identifiers
*/
@Override
public void reIdentifyAttributeTypeDef(String sourceName,
String originalAttributeTypeDefGUID,
String originalAttributeTypeDefName,
AttributeTypeDef newAttributeTypeDef)
{
this.deleteAttributeTypeDef(sourceName, originalAttributeTypeDefGUID, originalAttributeTypeDefName);
this.addAttributeTypeDef(sourceName, newAttributeTypeDef);
}
/**
* Return the list of property names defined for this TypeDef.
*
* @param sourceName source of the request (used for logging)
* @param typeDef type definition to work with.
* @param methodName calling method
* @return list of String property names
* @throws TypeErrorException there is an issue with the TypeDef.
*/
private List<String> getPropertyNames(String sourceName,
TypeDef typeDef,
String methodName) throws TypeErrorException
{
final String thisMethodName = "getPropertyNames";
List<String> propertyNames = null;
if (validTypeDef(sourceName, typeDef))
{
List<TypeDefAttribute> propertiesDefinition = typeDef.getPropertiesDefinition();
if ((propertiesDefinition != null) && (propertiesDefinition.size() > 0))
{
propertyNames = new ArrayList<>();
for (TypeDefAttribute propertyDefinition : propertiesDefinition)
{
if (propertyDefinition != null)
{
String propertyName = propertyDefinition.getAttributeName();
if (propertyName != null)
{
log.debug(typeDef.getName() + " from " + sourceName + " has property " + propertyName);
propertyNames.add(propertyName);
}
else
{
throw new TypeErrorException(OMRSErrorCode.BAD_TYPEDEF_ATTRIBUTE_NAME.getMessageDefinition(sourceName),
this.getClass().getName(),
methodName);
}
}
else
{
throw new TypeErrorException(OMRSErrorCode.NULL_TYPEDEF_ATTRIBUTE.getMessageDefinition(sourceName),
this.getClass().getName(),
methodName);
}
}
/*
* If no property names have been extracted then remove the array.
*/
if (propertyNames.size() == 0)
{
propertyNames = null;
}
}
}
else
{
throw new TypeErrorException(OMRSErrorCode.BAD_TYPEDEF.getMessageDefinition(thisMethodName,
typeDef.getName(),
sourceName,
methodName),
this.getClass().getName(),
methodName);
}
return propertyNames;
}
/**
* Evaluate the superTypes for a type. The results are cached in typeDefSuperTypes.
*
* @param sourceName source of the request (used for logging)
* @param typeName name of type to process
* @param methodName calling method
* @return list of supertype links or null if top level
*/
public List<TypeDefLink> getSuperTypes(String sourceName,
String typeName,
String methodName)
{
final String thisMethodName = "getSuperTypes";
List<TypeDefLink> typeHierarchy = typeDefSuperTypes.get(typeName);
if (typeHierarchy == null)
{
/*
* The type hierarchy is not known at this time (it is evaluated lazily).
*/
typeHierarchy = new ArrayList<>();
TypeDef typeDef = knownTypeDefNames.get(typeName);
if (typeDef != null)
{
TypeDefLink superTypeLink = typeDef.getSuperType();
while (superTypeLink != null)
{
String superTypeName = superTypeLink.getName();
if (superTypeName != null)
{
log.debug(typeName + " has super type " + superTypeName);
typeHierarchy.add(superTypeLink);
/*
* Retrieve the TypeDef for this super type
*/
TypeDef superTypeDef = knownTypeDefNames.get(superTypeName);
if (superTypeDef != null)
{
/*
* Retrieve the super type for this super typeDef. It will be null if the type is top-level.
*/
superTypeLink = superTypeDef.getSuperType();
}
else
{
log.error(superTypeName + " supertype is not known in TypeDef cache");
throwContentManagerLogicError(sourceName, methodName, thisMethodName);
}
}
else
{
log.error("Corrupted TypeDef cache, no name for " + superTypeLink);
throwContentManagerLogicError(sourceName, methodName, thisMethodName);
}
}
/*
* Cache the resulting superType list
*/
typeDefSuperTypes.put(typeName, typeHierarchy);
}
else
{
log.error(typeName + " type is not known in TypeDef cache");
throwContentManagerLogicError(sourceName, methodName, thisMethodName + "(" + typeName + ")");
}
}
if (typeHierarchy.isEmpty())
{
/*
* The type hierarchy has been evaluated and this type has no supertypes.
*/
return null;
}
else
{
/*
* The type hierarchy is known and can be returned directly.
*/
return typeHierarchy;
}
}
/**
* Validate that the entity's type is of the expected/desired value. The actual entity may be a subtype
* of the expected type of course.
*
* @param sourceName source of the request (used for logging)
* @param actualTypeName name of the entity type
* @param expectedTypeName name of the expected type
* @return boolean if they match (a null in actualTypeName results in false; a null in expectedType results in true)
*/
boolean isTypeOf(String sourceName,
String actualTypeName,
String expectedTypeName)
{
final String methodName = "isTypeOf";
log.debug("isTypeOf: sourceName = " + sourceName + "; actualTypeName = " + actualTypeName + "; expectedTypeName = " + expectedTypeName);
if (expectedTypeName == null)
{
/*
* If the expected type name is null, it means that any type is allowed.
*/
return true;
}
/*
* If the actual type is null then the object retrieved is a bit weird. It is treated as not
* matching on type.
*/
if (actualTypeName == null)
{
return false;
}
/*
* Do the obvious first.
*/
if (actualTypeName.equals(expectedTypeName))
{
log.debug("Simple match success");
return true;
}
/*
* Looking for a match in the superTypes.
*/
List<TypeDefLink> typeHierarchy = this.getSuperTypes(sourceName, actualTypeName, methodName);
if (typeHierarchy != null)
{
for (TypeDefLink superType : typeHierarchy)
{
if (superType != null)
{
if (expectedTypeName.equals(superType.getName()))
{
log.debug("SuperType match success");
return true;
}
log.debug("No match with " + superType.getName());
}
}
}
/*
* No match found
*/
return false;
}
/**
* Validate that the entity's type is of the expected/desired value. The actual entity may be a subtype
* of the expected type of course.
*
* @param sourceName source of the request (used for logging)
* @param actualTypeGUID GUID of the entity type
* @param actualTypeName name of the entity type
* @param expectedTypeGUID GUID of the expected type
* @return boolean if they match (a null in either results in false)
*/
boolean isTypeOfByGUID(String sourceName,
String actualTypeGUID,
String actualTypeName,
String expectedTypeGUID)
{
final String methodName = "isTypeOfByGUID";
log.debug("IsTypeOfByGUID: sourceName = " + sourceName + "; actualTypeName = " + actualTypeName + "; expectedTypeGUID = " + expectedTypeGUID);
if (expectedTypeGUID == null)
{
/*
* If the expected type GUID is null, it means that any type is allowed.
*/
log.debug("Any type will do");
return true;
}
/*
* If the actual type is null then the object retrieved is a bit weird. It is treated as not
* matching on type.
*/
if (actualTypeGUID == null)
{
log.debug("No type to test against");
return false;
}
/*
* Do the obvious first.
*/
if (actualTypeGUID.equals(expectedTypeGUID))
{
log.debug("Simple match success");
return true;
}
/*
* Looking for a match in the superTypes.
*/
List<TypeDefLink> typeHierarchy = this.getSuperTypes(sourceName, actualTypeName, methodName);
if (typeHierarchy != null)
{
for (TypeDefLink superType : typeHierarchy)
{
if (superType != null)
{
if (expectedTypeGUID.equals(superType.getGUID()))
{
log.debug("SuperType match success");
return true;
}
log.debug("No match with " + superType.getGUID());
}
}
}
return false;
}
/**
* Return the InstanceType that matches the supplied type name. If the type name is not recognized,
* of the category is incorrect, a logic exception is thrown.
*
* @param sourceName source of the request (used for logging)
* @param category category of the instance type required.
* @param typeName String type name.
* @param methodName name of calling method.
* @return InstanceType object containing TypeDef properties such as unique identifier (guid),
* typeDef name and version name
* @throws TypeErrorException the type name is not a recognized type or is of the wrong category or there is
* a problem with the cached TypeDef.
*/
@Override
public InstanceType getInstanceType(String sourceName,
TypeDefCategory category,
String typeName,
String methodName) throws TypeErrorException
{
final String thisMethodName = "getInstanceType";
if (isValidTypeCategory(sourceName, category, typeName, methodName))
{
InstanceType instanceType = knownInstanceTypes.get(typeName);
if (instanceType != null)
{
return instanceType;
}
/*
* The instance type has not yet been created. (They are created lazily.)
*/
TypeDef typeDef = knownTypeDefNames.get(typeName);
if (typeDef != null)
{
instanceType = new InstanceType();
instanceType.setHeaderVersion(InstanceType.CURRENT_INSTANCE_TYPE_HEADER_VERSION);
instanceType.setTypeDefCategory(category);
instanceType.setTypeDefGUID(typeDef.getGUID());
instanceType.setTypeDefName(typeDef.getName());
instanceType.setTypeDefVersion(typeDef.getVersion());
/*
* Extract the properties for this TypeDef. These will be augmented with property names
* from the super type(s).
*/
List<String> propertyNames = this.getPropertyNames(sourceName, typeDef, methodName);
/*
* If propertyNames is null, it means this TypeDef has no attributes. However, the superType
* may have attributes, and so we need an array to accumulate the attributes into.
*/
if (propertyNames == null)
{
propertyNames = new ArrayList<>();
}
/*
* Work up the TypeDef hierarchy extracting the property names.
*/
TypeDefLink superTypeLink = typeDef.getSuperType();
while (superTypeLink != null)
{
String superTypeName = superTypeLink.getName();
if (superTypeName != null)
{
log.debug(typeName + " from " + sourceName + " has super type " + superTypeName);
/*
* Retrieve the TypeDef for this super type
*/
TypeDef superTypeDef = knownTypeDefNames.get(superTypeName);
if (superTypeDef != null)
{
List<String> superTypePropertyNames = this.getPropertyNames(sourceName, superTypeDef, methodName);
if (superTypePropertyNames != null)
{
propertyNames.addAll(0, superTypePropertyNames);
}
/*
* Retrieve the super type for this typeDef. It will be null if the type is top-level.
*/
superTypeLink = superTypeDef.getSuperType();
}
else
{
/*
* Super type not known so stop processing
*/
log.error(superTypeName + " is not known");
throwContentManagerLogicError(sourceName, methodName, thisMethodName);
}
}
else
{
/*
* Super type is invalid, suggests a corrupted cache
*/
log.error("Corrupted TypeDef cache");
throwContentManagerLogicError(sourceName, methodName, thisMethodName);
}
}
/*
* Cache the instance type for next time
*/
knownInstanceTypes.put(typeName, instanceType);
return instanceType;
}
else
{
log.error("TypeDef " + typeName + " should already have been validated");
throwContentManagerLogicError(sourceName, methodName, thisMethodName);
}
}
else
{
throw new TypeErrorException(OMRSErrorCode.BAD_CATEGORY_FOR_TYPEDEF_ATTRIBUTE.getMessageDefinition(sourceName,
typeName,
category.getName()),
this.getClass().getName(),
methodName);
}
return null;
}
/**
* Return a boolean indicating that the type name matches the category.
*
* @param sourceName source of the request (used for logging)
* @param category TypeDefCategory enum value to test
* @param typeName type name to test
* @return boolean flag indicating that the type name is of the specified category
* @throws TypeErrorException the type name is not a recognized type or there is
* a problem with the cached TypeDef.
*/
@Override
public boolean isValidTypeCategory(String sourceName,
TypeDefCategory category,
String typeName,
String methodName) throws TypeErrorException
{
final String thisMethodName = "isValidTypeCategory";
if (category == null)
{
throwContentManagerLogicError(sourceName, methodName, thisMethodName);
return false;
}
if (typeName == null)
{
throwContentManagerLogicError(sourceName, methodName, thisMethodName);
return false;
}
TypeDef typeDef = knownTypeDefNames.get(typeName);
if (typeDef != null)
{
TypeDefCategory retrievedTypeDefCategory = typeDef.getCategory();
if (retrievedTypeDefCategory != null)
{
return (category.getOrdinal() == retrievedTypeDefCategory.getOrdinal());
}
else
{
throwContentManagerLogicError(sourceName, methodName, thisMethodName);
}
}
else
{
throw new TypeErrorException(OMRSErrorCode.TYPEDEF_NOT_KNOWN_FOR_INSTANCE.getMessageDefinition(typeName,
category.getName(),
methodName,
sourceName),
this.getClass().getName(),
methodName);
}
return false;
}
/**
* Return boolean indicating if a classification type can be applied to a specified entity. This
* uses the list of valid entity types located in the ClassificationDef.
*
* @param sourceName source of the request (used for logging)
* @param classificationTypeName name of the classification's type (ClassificationDef)
* @param entityTypeName name of the entity's type (EntityDef)
* @param methodName name of calling method.
* @return boolean indicating if the classification is valid for the entity.
*/
@Override
public boolean isValidClassificationForEntity(String sourceName,
String classificationTypeName,
String entityTypeName,
String methodName)
{
final String thisMethodName = "isValidClassificationForEntity";
try
{
if ((isValidTypeCategory(sourceName, TypeDefCategory.CLASSIFICATION_DEF, classificationTypeName, methodName)) &&
(isValidTypeCategory(sourceName, TypeDefCategory.ENTITY_DEF, entityTypeName, methodName)))
{
ClassificationDef classificationTypeDef = (ClassificationDef) knownTypeDefNames.get(classificationTypeName);
if (classificationTypeDef != null)
{
List<TypeDefLink> entityDefs = classificationTypeDef.getValidEntityDefs();
if (entityDefs == null || entityDefs.isEmpty())
{
/*
* The classification has no restrictions on which entities it can be attached to.
*/
return true;
}
else
{
/*
* The classification can only be attached to the entities listed. Note an empty list
* means the classification can not be attached to any entity, and it is effectively useless.
* The logic checks the entity types parent types, as the parent types may allow the classification.
*
* The Archive types at this time do not have any ClassificationDefs with supertypes. If we want to support
* ClassificationDefs with supertypes then we need to account for any entities that the ClassificationDef
* supertype can introduce.
*/
Set<String> entityTypes = new HashSet<>();
TypeDef typeDef = getTypeDefByName(entityTypeName);
entityTypes.add(entityTypeName);
while (typeDef.getSuperType() !=null)
{
TypeDefLink superTypeLink=typeDef.getSuperType();
String parentName= superTypeLink.getName();
entityTypes.add(parentName);
typeDef = this.knownTypeDefGUIDs.get(superTypeLink.getGUID());
}
for (TypeDefLink allowedEntityDefLink : entityDefs)
{
if (allowedEntityDefLink != null)
{
String allowedTypeName = allowedEntityDefLink.getName();
if (entityTypes.contains(allowedTypeName))