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.metadata;
17  
18  import ca.uhn.hl7v2.model.v25.datatype.CX;
19  import org.openehealth.ipf.commons.ihe.xds.core.metadata.jaxbadapters.AssigningAuthorityAdapter;
20  
21  import javax.xml.bind.annotation.XmlAccessType;
22  import javax.xml.bind.annotation.XmlAccessorType;
23  import javax.xml.bind.annotation.XmlAttribute;
24  import javax.xml.bind.annotation.XmlType;
25  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
26  import java.util.Objects;
27  
28  /**
29   * Represents a person ID (HL7v2 CX field where only CX.1, CX.4.2 and CX.4.3
30   * are allowed), or an XDS "Coded String".
31   * <p>
32   * All members of this class are allowed to be <code>null</code>. When transforming
33   * to HL7 this indicates that the values are empty. Trailing empty values are 
34   * removed from the HL7 string.
35   * @author Jens Riemschneider
36   * @author Dmytro Rud
37   */
38  @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
39  @XmlType(name = "Identifiable", propOrder = {"id", "assigningAuthority"})
40  public class Identifiable extends Hl7v2Based<CX> {
41      private static final long serialVersionUID = -3392755556068006520L;
42  
43      /**
44       * Constructs an identifiable.
45       */
46      public Identifiable() {
47          super(new CX(MESSAGE));
48      }
49  
50  
51      /**
52       * Constructs an identifiable.
53       */
54      public Identifiable(CX cx) {
55          super(cx);
56      }
57  
58  
59      /**
60       * Constructs an identifiable.
61       * @param id
62       *          person ID (CX.1) / Code.
63       * @param assigningAuthority
64       *          assigning authority (CX.4) / Code System.
65       */
66      public Identifiable(String id, AssigningAuthority assigningAuthority) {
67          this();
68          setId(id);
69          setAssigningAuthority(assigningAuthority);
70      }
71  
72      /**
73       * @return person ID (CX.1) / Code.
74       */
75      @XmlAttribute(name = "extension")
76      public String getId() {
77          return getHapiObject().getCx1_IDNumber().getValue();
78      }
79  
80      /**
81       * @param id
82       *          person ID (CX.1) / Code.
83       */
84      public void setId(String id) {
85          setValue(getHapiObject().getCx1_IDNumber(), id);
86      }
87  
88      /**
89       * @return assigning authority (CX.4) / Code System.
90       */
91      @XmlAttribute(name = "root")
92      @XmlJavaTypeAdapter(value = AssigningAuthorityAdapter.class)
93      public AssigningAuthority getAssigningAuthority() {
94          AssigningAuthority assigningAuthority = new AssigningAuthority(getHapiObject().getCx4_AssigningAuthority());
95          return assigningAuthority.isEmpty() ? null : assigningAuthority;
96      }
97  
98      /**
99       * @param assigningAuthority
100      *          assigning authority (CX.4) / Code System.
101      */
102     public void setAssigningAuthority(AssigningAuthority assigningAuthority) {
103         setAssigningAuthority(assigningAuthority, getHapiObject().getCx4_AssigningAuthority());
104     }
105 
106     @Override
107     public boolean equals(Object o) {
108         if (this == o) return true;
109         if (o == null || getClass() != o.getClass()) return false;
110         Identifiable that = (Identifiable) o;
111         return Objects.equals(getAssigningAuthority(), that.getAssigningAuthority()) &&
112                 Objects.equals(getId(), that.getId());
113     }
114 
115     @Override
116     public int hashCode() {
117         return Objects.hash(getAssigningAuthority(), getId());
118     }
119 
120     @Override
121     public String toString() {
122         return "Identifiable(" +
123                 "id=" + getId() +
124                 ", assigningAuthority=" + getAssigningAuthority() +
125                 ')';
126     }
127 }