View Javadoc
1   /*
2    * Copyright 2013 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.ReferenceId;
22  
23  import static org.apache.commons.lang3.StringUtils.isEmpty;
24  import static org.apache.commons.lang3.StringUtils.isNotEmpty;
25  import static org.openehealth.ipf.commons.ihe.xds.core.validate.HL7ValidationUtils.isEmptyField;
26  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*;
27  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
28  
29  /**
30   * Validates a CXi value (special CX used for Reference IDs).
31   * @author Dmytro Rud
32   */
33  public class CXiValidator implements ValueValidator {
34  
35      @Override
36      public void validate(String hl7CX) throws XDSMetaDataException {
37          ReferenceId referenceId = Hl7v2Based.parse(hl7CX, ReferenceId.class);
38          metaDataAssert(referenceId != null, CX_NEEDS_ID);
39  
40          CX cx = referenceId.getHapiObject();
41  
42          // prohibited fields
43          metaDataAssert(isEmpty(cx.getCx2_CheckDigit().getValue()), CXI_TOO_MANY_COMPONENTS);
44          metaDataAssert(isEmpty(cx.getCx3_CheckDigitScheme().getValue()), CXI_TOO_MANY_COMPONENTS);
45          metaDataAssert(isEmptyField(cx.getCx6_AssigningFacility()), CXI_TOO_MANY_COMPONENTS);
46          metaDataAssert(isEmpty(cx.getCx7_EffectiveDate().getValue()), CXI_TOO_MANY_COMPONENTS);
47          metaDataAssert(isEmpty(cx.getCx8_ExpirationDate().getValue()), CXI_TOO_MANY_COMPONENTS);
48          metaDataAssert(isEmptyField(cx.getCx9_AssigningJurisdiction()), CXI_TOO_MANY_COMPONENTS);
49          metaDataAssert(isEmptyField(cx.getCx10_AssigningAgencyOrDepartment()), CXI_TOO_MANY_COMPONENTS);
50  
51          // required and optional fields
52          metaDataAssert(isNotEmpty(cx.getCx1_IDNumber().getValue()), CX_NEEDS_ID, hl7CX);
53          metaDataAssert(isNotEmpty(cx.getCx5_IdentifierTypeCode().getValue()), CXI_NEEDS_ID_TYPE_CODE, hl7CX);
54  
55          HD assigningAuthority = cx.getCx4_AssigningAuthority();
56          if (! isEmptyField(assigningAuthority)) {
57              boolean cx41filled = isNotEmpty(assigningAuthority.getHd1_NamespaceID().getValue());
58              boolean cx42filled = isNotEmpty(assigningAuthority.getHd2_UniversalID().getValue());
59              boolean cx43filled = isNotEmpty(assigningAuthority.getHd3_UniversalIDType().getValue());
60              metaDataAssert(cx41filled || (cx42filled && cx43filled), CXI_INCOMPLETE_ASSIGNING_AUTHORITY);
61          }
62      }
63  
64  }