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.CE;
19  import org.apache.commons.lang3.StringUtils;
20  import org.openehealth.ipf.commons.ihe.xds.core.metadata.jaxbadapters.LocalizedStringAdapter;
21  
22  import javax.xml.bind.annotation.XmlAccessType;
23  import javax.xml.bind.annotation.XmlAccessorType;
24  import javax.xml.bind.annotation.XmlAttribute;
25  import javax.xml.bind.annotation.XmlType;
26  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
27  import java.util.Objects;
28  
29  /**
30   * Represents a code.
31   * <p> 
32   * All members of this class are allowed to be <code>null</code>.
33   * @author Jens Riemschneider
34   * @author Dmytro Rud
35   */
36  @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
37  @XmlType(name = "Code", propOrder = {"code", "schemeName", "displayName"})
38  public class Code extends Hl7v2Based<CE> {
39      private static final long serialVersionUID = 7603534956639945984L;
40  
41      private LocalizedString localizedString;
42  
43  
44      /**
45       * Constructs a code.
46       */
47      public Code() {
48          super(new CE(MESSAGE));
49      }
50  
51      /**
52       * Constructs a code.
53       */
54      public Code(CE ce) {
55          super(ce);
56      }
57  
58      /**
59       * Constructs a code.
60       * @param code
61       *          the value of the code.
62       * @param displayName
63       *          the display name of the code.
64       * @param schemeName
65       *          the schema of the code.
66       */
67      public Code(String code, LocalizedString displayName, String schemeName) {
68          this();
69          setCode(code);
70          setDisplayName(displayName);
71          setSchemeName(schemeName);
72      }
73  
74      /**
75       * @return the value of this code.
76       */
77      @XmlAttribute
78      public String getCode() {
79          return getHapiObject().getCe1_Identifier().getValue();
80      }
81      
82      /**
83       * @param code 
84       *          the value of this code.
85       */
86      public void setCode(String code) {
87          setValue(getHapiObject().getCe1_Identifier(), code);
88      }
89      
90      /**
91       * @return the display name of this code.
92       */
93      @XmlAttribute
94      @XmlJavaTypeAdapter(value = LocalizedStringAdapter.class)
95      public LocalizedString getDisplayName() {
96          String value = getHapiObject().getCe2_Text().getValue();
97  
98          if (StringUtils.isEmpty(value)) {
99              localizedString = null;
100         }
101         else if (localizedString != null) {
102             localizedString.setValue(value);
103         }
104         else {
105             localizedString = new LocalizedString(value);
106         }
107 
108         return localizedString;
109     }
110     
111     /**
112      * @param displayName
113      *          the display name of this code.
114      */
115     public void setDisplayName(LocalizedString displayName) {
116         this.localizedString = displayName;
117         if (displayName != null) {
118             setValue(getHapiObject().getCe2_Text(), displayName.getValue());
119         } else {
120             getHapiObject().getCe2_Text().clear();
121         }
122     }
123     
124     /**
125      * @return the schema of this code.
126      */
127     @XmlAttribute(name = "codeSystemName")
128     public String getSchemeName() {
129         return getHapiObject().getCe3_NameOfCodingSystem().getValue();
130     }
131     
132     /**
133      * @param schemeName
134      *          the schema of this code.
135      */
136     public void setSchemeName(String schemeName) {
137         setValue(getHapiObject().getCe3_NameOfCodingSystem(), schemeName);
138     }
139 
140     @Override
141     public boolean equals(Object o) {
142         if (this == o) return true;
143         if (o == null || getClass() != o.getClass()) return false;
144         Code that = (Code) o;
145         return Objects.equals(getCode(), that.getCode()) &&
146                 Objects.equals(getDisplayName(), that.getDisplayName()) &&
147                 Objects.equals(getSchemeName(), that.getSchemeName());
148     }
149 
150     @Override
151     public int hashCode() {
152         return Objects.hash(getCode(), getDisplayName(), getSchemeName());
153     }
154 
155     @Override
156     public String toString() {
157         return "Code(" +
158                 "code=" + getCode() +
159                 ", displayName=" + getDisplayName() +
160                 ", schemeName=" + getSchemeName() +
161                 ')';
162     }
163 }