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 ca.uhn.hl7v2.model.v25.datatype.CX;
19  import ca.uhn.hl7v2.model.v25.datatype.HD;
20  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Hl7v2Based;
21  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Identifiable;
22  
23  import static org.apache.commons.lang3.StringUtils.*;
24  import static org.openehealth.ipf.commons.ihe.xds.core.validate.HL7ValidationUtils.isEmptyField;
25  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.CX_NEEDS_ID;
26  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.CX_TOO_MANY_COMPONENTS;
27  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
28  
29  /**
30   * Validates a CX value.
31   * @author Jens Riemschneider
32   */
33  public class CXValidator implements ValueValidator {
34      private static final HDValidator HD_VALIDATOR = new HDValidator();
35  
36      private final boolean assigningAuthorityRequired;
37  
38      public CXValidator(boolean assigningAuthorityRequired) {
39          this.assigningAuthorityRequired = assigningAuthorityRequired;
40      }
41  
42  
43      @Override
44      public void validate(String hl7CX) throws XDSMetaDataException {
45          Identifiable identifiable = Hl7v2Based.parse(hl7CX, Identifiable.class);
46          metaDataAssert(identifiable != null, CX_NEEDS_ID);
47  
48          CX cx = identifiable.getHapiObject();
49  
50          // prohibited fields
51          metaDataAssert(isEmpty(cx.getCx2_CheckDigit().getValue()), CX_TOO_MANY_COMPONENTS);
52          metaDataAssert(isEmpty(cx.getCx3_CheckDigitScheme().getValue()), CX_TOO_MANY_COMPONENTS);
53          metaDataAssert(isEmpty(cx.getCx5_IdentifierTypeCode().getValue()), CX_TOO_MANY_COMPONENTS);
54          metaDataAssert(isEmptyField(cx.getCx6_AssigningFacility()), CX_TOO_MANY_COMPONENTS);
55          metaDataAssert(isEmpty(cx.getCx7_EffectiveDate().getValue()), CX_TOO_MANY_COMPONENTS);
56          metaDataAssert(isEmpty(cx.getCx8_ExpirationDate().getValue()), CX_TOO_MANY_COMPONENTS);
57          metaDataAssert(isEmptyField(cx.getCx9_AssigningJurisdiction()), CX_TOO_MANY_COMPONENTS);
58          metaDataAssert(isEmptyField(cx.getCx10_AssigningAgencyOrDepartment()), CX_TOO_MANY_COMPONENTS);
59  
60          // required and optional fields
61          metaDataAssert(isNotEmpty(cx.getCx1_IDNumber().getValue()), CX_NEEDS_ID, hl7CX);
62  
63          HD assigningAuthority = cx.getCx4_AssigningAuthority();
64          if (assigningAuthorityRequired || (! isEmptyField(assigningAuthority))) {
65              HD_VALIDATOR.validate(assigningAuthority, hl7CX);
66          }
67      }
68  }