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.XCN;
19  import org.apache.commons.beanutils.BeanUtils;
20  
21  import javax.xml.bind.annotation.XmlAccessType;
22  import javax.xml.bind.annotation.XmlAccessorType;
23  import javax.xml.bind.annotation.XmlType;
24  import java.util.Objects;
25  
26  /**
27   * Represents an identifiable person.
28   * <p>
29   * This class contains members from the HL7v2 XCN data type. The XDS profile
30   * imposes some limitations on the XCN type. Most notably the XCN.9
31   * component has the same restrictions as the CX.4 component (as described
32   * in {@link Identifiable}. 
33   * All of this class are allowed to be <code>null</code>. When transforming  
34   * to HL7 this indicates that the values are empty. Trailing empty  
35   * values are removed from the HL7 string.
36   * @author Jens Riemschneider
37   * @author Dmytro Rud
38   */
39  @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
40  @XmlType(name = "Person", propOrder = {"id", "name"})
41  public class Person extends Hl7v2Based<XCN> {
42      private static final long serialVersionUID = 1775227207521668959L;
43      
44      /**
45       * Constructs a person.
46       */
47      public Person() {
48          super(new XCN(MESSAGE));
49      }
50  
51  
52      /**
53       * Constructs a person.
54       */
55      public Person(XCN xcn) {
56          super(xcn);
57      }
58  
59  
60      /**
61       * Constructs a person.
62       * @param id
63       *          the id of the person (XCN.1 and XCN.9).
64       * @param name
65       *          the name of the person (XCN.2.1, XCN.3, XCN.4, XCN.5, XCN.6, XCN.7).
66       */
67      public Person(Identifiable id, Name name) {
68          this();
69          setId(id);
70          setName(name);
71      }
72  
73      /**
74       * @return the id of the person (XCN.1 and XCN.9).
75       */
76      public Identifiable getId() {
77          return new Identifiable(
78                  getHapiObject().getXcn1_IDNumber().getValue(),
79                  new AssigningAuthority(getHapiObject().getXcn9_AssigningAuthority()));
80      }
81  
82      /**
83       * @param id
84       *          the id of the person (XCN.1 and XCN.9).
85       */
86      public void setId(Identifiable id) {
87          if (id != null) {
88              setValue(getHapiObject().getXcn1_IDNumber(), id.getId());
89              setAssigningAuthority(id.getAssigningAuthority(), getHapiObject().getXcn9_AssigningAuthority());
90          } else {
91              getHapiObject().getXcn1_IDNumber().clear();
92              getHapiObject().getXcn9_AssigningAuthority().clear();
93          }
94      }
95  
96      /**
97       * @return the name of the person (XCN.2.1, XCN.3, XCN.4, XCN.5, XCN.6, XCN.7).
98       */
99      public Name getName() {
100         XcnName name = new XcnName(getHapiObject());
101         return name.isEmpty() ? null : name;
102     }
103 
104     /**
105      * @param name
106      *          the name of the person (XCN.2.1, XCN.3, XCN.4, XCN.5, XCN.6, XCN.7).
107      */
108     public void setName(Name name) {
109         if (name != null) {
110             try {
111                 Name thisName = new XcnName(getHapiObject());
112                 BeanUtils.copyProperties(thisName, name);
113             } catch (Exception e) {
114                 throw new RuntimeException("Could not copy properties");
115             }
116         }
117         else {
118             XCN xcn = getHapiObject();
119             xcn.getXcn2_FamilyName().clear();
120             xcn.getXcn3_GivenName().clear();
121             xcn.getXcn4_SecondAndFurtherGivenNamesOrInitialsThereof().clear();
122             xcn.getXcn5_SuffixEgJRorIII().clear();
123             xcn.getXcn6_PrefixEgDR().clear();
124             xcn.getXcn7_DegreeEgMD().clear();
125         }
126     }
127 
128     @Override
129     public boolean equals(Object o) {
130         if (this == o) return true;
131         if (o == null || getClass() != o.getClass()) return false;
132         Person that = (Person) o;
133         return Objects.equals(getId(), that.getId()) &&
134                 Objects.equals(getName(), that.getName());
135     }
136 
137     @Override
138     public int hashCode() {
139         return Objects.hash(getId(), getName());
140     }
141 
142     @Override
143     public String toString() {
144         return "Person(" +
145                 "id=" + getId() +
146                 ", name=" + getName() +
147                 ')';
148     }
149 }