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.HD;
19  import ca.uhn.hl7v2.model.v25.datatype.XCN;
20  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Hl7v2Based;
21  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Person;
22  
23  import static org.apache.commons.lang3.StringUtils.isNotEmpty;
24  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.PERSON_MISSING_NAME_AND_ID;
25  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.PERSON_HD_MISSING;
26  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
27  import static org.openehealth.ipf.commons.ihe.xds.core.validate.HL7ValidationUtils.isEmptyField;
28  import static org.openehealth.ipf.commons.ihe.xds.core.validate.HL7ValidationUtils.isNotEmptyField;
29  
30  /**
31   * Validates a XCN string.
32   * @author Jens Riemschneider
33   */
34  public class XCNValidator implements ValueValidator {
35      private static final HDValidator HD_VALIDATOR = new HDValidator();
36  
37      @Override
38      public void validate(String hl7xcn) throws XDSMetaDataException {
39          Person person = Hl7v2Based.parse(hl7xcn, Person.class);
40          metaDataAssert(person != null, PERSON_MISSING_NAME_AND_ID, hl7xcn);
41  
42          XCN xcn = person.getHapiObject();
43          metaDataAssert(
44                  isNotEmpty(xcn.getXcn1_IDNumber().getValue()) ||
45                  isNotEmpty(xcn.getXcn2_FamilyName().getFn1_Surname().getValue()),
46                  PERSON_MISSING_NAME_AND_ID, hl7xcn);
47  
48  //        Spec actually allows the assigning authority to be missing:
49  //          "If component 1 (ID Number) is specified, component 9 (Assigning Authority) shall be present if available"
50          HD hd = xcn.getXcn9_AssigningAuthority();
51          boolean condition = !isNotEmpty(xcn.getXcn1_IDNumber().getValue()) || isNotEmptyField(hd);
52          metaDataAssert(condition, PERSON_HD_MISSING, hl7xcn);
53  
54          if (! isEmptyField(hd)) {
55              HD_VALIDATOR.validate(hd, hl7xcn);
56          }
57      }
58  }