View Javadoc
1   /*
2    * Copyright 2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Validates a classification.
32   * @author Jens Riemschneider
33   * @author Mitko Kolev
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       * Constructs the validation for classifications with the given <code>classScheme</code>.
45       *
46       * @param classScheme
47       *          the class scheme of the classification to check.
48       * @param min
49       *          the minimum number of classifications allowed for the given scheme.
50       * @param max
51       *          the maximum number of classifications allowed for the given scheme.
52       * @param displayNameUsage
53       *          the usage of the display name element.
54       * @param slotValidations
55       *          validations to apply to the slots of the classification.
56       *
57       * @see DisplayNameUsage
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       * Constructs the validation for classifications with the given <code>classScheme</code>.
65       *
66       * @param classScheme
67       *          the class scheme of the classification to check.
68       * @param min
69       *          the minimum number of classifications allowed for the given scheme.
70       * @param max
71       *          the maximum number of classifications allowed for the given scheme.
72       * @param displayNameUsage
73       *          the usage of the display name element.
74       * @param nodeRepresentationUsage
75       *          optionality of the attribute {@code nodeRepresentation}.
76       * @param slotValidations
77       *          validations to apply to the slots of the classification.
78       *
79       * @see DisplayNameUsage
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 }