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.HD;
19  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Hl7v2Based.Holder;
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 java.util.Objects;
26  
27  /**
28   * Represents an authority that assigns IDs.
29   * <p>
30   * This class is based on the HL7 HD data type.
31   * <p>
32   * Note that most assigning authorities used in XSD only allow the definition of the
33   * universal ID. The ID type must be {@code ISO} and the namespace ID has to be empty.
34   * The constructor {@link #AssigningAuthority(String)} can be used to create such
35   * authorities.
36   * <p>
37   * All members of this class are allowed to be <code>null</code>. When transforming
38   * to HL7 this indicates that the values are empty. Trailing empty values are
39   * removed from the HL7 string.
40   * @author Jens Riemschneider
41   * @author Dmytro Rud
42   */
43  @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
44  @XmlType(name = "AssigningAuthority", propOrder = {"universalId", "universalIdType"})
45  public class AssigningAuthority extends Hl7v2Based<Holder<HD>> {
46      private static final long serialVersionUID = 5350057820250191032L;
47  
48      public AssigningAuthority() {
49          super(new Holder<>(new HD(MESSAGE)));
50      }
51  
52  
53      public AssigningAuthority(Holder<HD> hdHolder) {
54          super(hdHolder);
55      }
56  
57  
58      public AssigningAuthority(HD hd) {
59          super(new Holder<>(hd));
60      }
61  
62      /**
63       * Constructs an assigning authority.
64       * @param universalId
65       *          the universal ID (HD.2).
66       * @param universalIdType
67       *          the type of the universal ID (HD.3).
68       */
69      public AssigningAuthority(String universalId, String universalIdType) {
70          this();
71          setUniversalId(universalId);
72          setUniversalIdType(universalIdType);
73      }
74  
75      /**
76       * Constructs an assigning authority that complies with the rules of the XDS profile.
77       * @param universalId
78       *          the universal ID (HD.2).
79       */
80      public AssigningAuthority(String universalId) {
81          this();
82          setUniversalId(universalId);
83          setUniversalIdType("ISO");
84      }
85  
86      /**
87       * @return the universal ID (HD.2).
88       */
89      @XmlAttribute
90      public String getUniversalId() {
91          return getHapiObject().getInternal().getHd2_UniversalID().getValue();
92      }
93  
94      /**
95       * @param universalId
96       *          the universal ID (HD.2).
97       */
98      public void setUniversalId(String universalId) {
99          setValue(getHapiObject().getInternal().getHd2_UniversalID(), universalId);
100     }
101 
102     /**
103      * @return the universal type ID (HD.3).
104      */
105     @XmlAttribute
106     public String getUniversalIdType() {
107         return getHapiObject().getInternal().getHd3_UniversalIDType().getValue();
108     }
109 
110     /**
111      * @param universalIdType
112      *          the universal type ID (HD.3).
113      */
114     public void setUniversalIdType(String universalIdType) {
115         setValue(getHapiObject().getInternal().getHd3_UniversalIDType(), universalIdType);
116     }
117 
118 
119     @Override
120     public boolean equals(Object o) {
121         if (this == o) return true;
122         if (o == null || getClass() != o.getClass()) return false;
123         AssigningAuthority that = (AssigningAuthority) o;
124         return Objects.equals(getUniversalId(), that.getUniversalId()) &&
125                 Objects.equals(getUniversalIdType(), that.getUniversalIdType());
126     }
127 
128     @Override
129     public int hashCode() {
130         return Objects.hash(getUniversalId(), getUniversalIdType());
131     }
132 
133     @Override
134     public String toString() {
135         return "AssigningAuthority(" +
136                 "universalId=" + getUniversalId() +
137                 ", universalIdType=" + getUniversalIdType() +
138                 ')';
139     }
140 }