1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.ihe.xds.core.validate;
17
18 import org.apache.commons.lang3.StringUtils;
19 import org.apache.commons.lang3.Validate;
20 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
21 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryObject;
22 import org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.DisplayNameUsage;
23 import org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.NodeRepresentationUsage;
24
25 import java.util.List;
26
27 import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*;
28 import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
29
30
31
32
33
34
35 public class ClassificationValidation implements RegistryObjectValidator {
36 protected final String classScheme;
37 private final int min;
38 private final int max;
39 private final DisplayNameUsage displayNameUsage;
40 private final NodeRepresentationUsage nodeRepresentationUsage;
41 private final SlotValueValidation[] slotValidations;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public ClassificationValidation(String classScheme, int min, int max, DisplayNameUsage displayNameUsage, SlotValueValidation[] slotValidations) {
60 this(classScheme, min, max, displayNameUsage, NodeRepresentationUsage.REQUIRED, slotValidations);
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public ClassificationValidation(String classScheme, int min, int max, DisplayNameUsage displayNameUsage,
82 NodeRepresentationUsage nodeRepresentationUsage, SlotValueValidation[] slotValidations)
83 {
84 this.classScheme = Validate.notNull(classScheme);
85 this.min = min;
86 this.max = max;
87 this.displayNameUsage = Validate.notNull(displayNameUsage);
88 this.nodeRepresentationUsage = nodeRepresentationUsage;
89 this.slotValidations = slotValidations;
90 }
91
92 @Override
93 public void validate(EbXMLRegistryObject obj) throws XDSMetaDataException {
94 List<EbXMLClassification> classifications = obj.getClassifications(classScheme);
95 metaDataAssert(classifications.size() >= min && classifications.size() <= max,
96 WRONG_NUMBER_OF_CLASSIFICATIONS, classScheme, min, max, classifications.size());
97 for (EbXMLClassification classification : classifications) {
98 metaDataAssert(classification.getClassifiedObject() != null,
99 NO_CLASSIFIED_OBJ, classScheme);
100
101 metaDataAssert(classification.getClassifiedObject().equals(obj.getId()),
102 WRONG_CLASSIFIED_OBJ, obj.getId(), classification.getClassifiedObject());
103
104 switch (nodeRepresentationUsage) {
105 case REQUIRED:
106 metaDataAssert(StringUtils.isNotEmpty(classification.getNodeRepresentation()), NODE_REPRESENTATION_MISSING, classScheme);
107 break;
108 case PROHIBITED:
109 metaDataAssert(StringUtils.isEmpty(classification.getNodeRepresentation()), NODE_REPRESENTATION_PROHIBITED, classScheme);
110 break;
111 default:
112 throw new IllegalArgumentException("Unsupported node representation optionality " + nodeRepresentationUsage);
113 }
114
115 switch(displayNameUsage){
116 case OPTIONAL:
117 break;
118 case REQUIRED:
119 assertDisplayNamePresent(classification, classScheme);
120 break;
121 default :
122 throw new IllegalArgumentException("Unsupported display name usage " + displayNameUsage);
123 }
124
125 if (slotValidations != null) {
126 for (SlotValueValidation slotValidation : slotValidations) {
127 slotValidation.validate(classification);
128 }
129 }
130 }
131 }
132
133 public static void assertDisplayNamePresent(EbXMLClassification classification, String classificationScheme) {
134 metaDataAssert(classification.getName() != null, NO_CLASSIFICATION_NAME_OBJ,
135 classificationScheme, classification.getClassifiedObject());
136 metaDataAssert(classification.getName().getValue() != null, NO_CLASSIFICATION_NAME_OBJ,
137 classificationScheme, classification.getClassifiedObject());
138 }
139
140 }