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 org.openehealth.ipf.commons.ihe.xds.core.metadata.Hl7v2Based;
19  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Telecom;
20  
21  import static org.apache.commons.lang3.StringUtils.isBlank;
22  import static org.apache.commons.lang3.StringUtils.isNotBlank;
23  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*;
24  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
25  
26  /**
27   * Validates an XTN value.
28   * @author Dmytro Rud
29   */
30  public class XTNValidator implements ValueValidator {
31  
32      @Override
33      public void validate(String hl7XTN) throws XDSMetaDataException {
34          Telecom telecom = Hl7v2Based.parse(hl7XTN, Telecom.class);
35          metaDataAssert(telecom != null, MISSING_TELECOM_PARAM, hl7XTN);
36  
37          if ("Internet".equals(telecom.getType())) {
38              metaDataAssert(isBlank(telecom.getUse()) || "NET".equals(telecom.getUse()), WRONG_TELECOM_USE, hl7XTN);
39              metaDataAssert(isNotBlank(telecom.getEmail()), MISSING_TELECOM_PARAM, hl7XTN);
40              metaDataAssert(telecom.getCountryCode() == null, INCONSISTENT_TELECOM_PARAM, hl7XTN);
41              metaDataAssert(telecom.getAreaCityCode() == null, INCONSISTENT_TELECOM_PARAM, hl7XTN);
42              metaDataAssert(telecom.getLocalNumber() == null, INCONSISTENT_TELECOM_PARAM, hl7XTN);
43              metaDataAssert(telecom.getExtension() == null, INCONSISTENT_TELECOM_PARAM, hl7XTN);
44              metaDataAssert(isBlank(telecom.getUnformattedPhoneNumber()), INCONSISTENT_TELECOM_PARAM, hl7XTN);
45  
46          } else if ("PH".equals(telecom.getType()) || "CP".equals(telecom.getType())) {
47              metaDataAssert(!"NET".equals(telecom.getUse()), WRONG_TELECOM_USE, hl7XTN);
48              metaDataAssert(isBlank(telecom.getEmail()), INCONSISTENT_TELECOM_PARAM, hl7XTN);
49  
50              boolean localPresent = (telecom.getLocalNumber() != null);
51              boolean unformattedPresent = isNotBlank(telecom.getUnformattedPhoneNumber());
52              metaDataAssert(localPresent || unformattedPresent, MISSING_TELECOM_PARAM, hl7XTN);
53              metaDataAssert(!(localPresent && unformattedPresent), INCONSISTENT_TELECOM_PARAM, hl7XTN);
54  
55          } else {
56              throw new XDSMetaDataException(WRONG_TELECOM_TYPE, hl7XTN);
57          }
58      }
59  }