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 static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*;
19  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.*;
20  import static org.openehealth.ipf.commons.ihe.xds.core.validate.HL7ValidationUtils.isEmptyField;
21  import static org.apache.commons.lang3.StringUtils.*;
22  
23  import ca.uhn.hl7v2.model.v25.datatype.HD;
24  import ca.uhn.hl7v2.model.v25.datatype.XON;
25  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Hl7v2Based;
26  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Organization;
27  
28  /**
29   * Validates a XON string.
30   * @author Jens Riemschneider
31   */
32  public class XONValidator implements ValueValidator {
33      private static final OIDValidator OID_VALIDATOR = new OIDValidator();
34      private static final HDValidator HD_VALIDATOR = new HDValidator();
35  
36      @Override
37      public void validate(String hl7XON) throws XDSMetaDataException {
38          Organization organization = Hl7v2Based.parse(hl7XON, Organization.class);
39          metaDataAssert(organization != null, ORGANIZATION_NAME_MISSING, hl7XON);
40  
41          XON xon = organization.getHapiObject();
42          metaDataAssert(isNotEmpty(xon.getXon1_OrganizationName().getValue()), ORGANIZATION_NAME_MISSING, hl7XON);
43  
44          HD hd = xon.getXon6_AssigningAuthority();
45          if (isEmptyField(hd)) {
46              String idNumber = xon.getXon10_OrganizationIdentifier().getValue();
47              if (isNotEmpty(idNumber)) {
48                  OID_VALIDATOR.validate(idNumber);
49              }
50          } else {
51              HD_VALIDATOR.validate(hd, hl7XON);
52          }
53  
54          metaDataAssert(countMatches(hl7XON, "^") <= 9, ORGANIZATION_TOO_MANY_COMPONENTS);
55          metaDataAssert(isEmpty(xon.getXon2_OrganizationNameTypeCode().getValue()), ORGANIZATION_TOO_MANY_COMPONENTS);
56          metaDataAssert(isEmpty(xon.getXon3_IDNumber().getValue()), ORGANIZATION_TOO_MANY_COMPONENTS);
57          metaDataAssert(isEmpty(xon.getXon4_CheckDigit().getValue()), ORGANIZATION_TOO_MANY_COMPONENTS);
58          metaDataAssert(isEmpty(xon.getXon5_CheckDigitScheme().getValue()), ORGANIZATION_TOO_MANY_COMPONENTS);
59          metaDataAssert(isEmpty(xon.getXon7_IdentifierTypeCode().getValue()), ORGANIZATION_TOO_MANY_COMPONENTS);
60          metaDataAssert(isEmptyField(xon.getXon8_AssigningFacility()), ORGANIZATION_TOO_MANY_COMPONENTS);
61          metaDataAssert(isEmpty(xon.getXon9_NameRepresentationCode().getValue()), ORGANIZATION_TOO_MANY_COMPONENTS);
62      }
63  }