removing potential null pointer exception from TermBase
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / TermBase.java
1 package eu.etaxonomy.cdm.model.common;
2
3 import java.util.HashSet;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Set;
7
8 import javax.persistence.FetchType;
9 import javax.persistence.MappedSuperclass;
10 import javax.persistence.OneToMany;
11 import javax.persistence.Transient;
12 import javax.xml.bind.annotation.XmlAccessType;
13 import javax.xml.bind.annotation.XmlAccessorType;
14 import javax.xml.bind.annotation.XmlElement;
15 import javax.xml.bind.annotation.XmlElementWrapper;
16 import javax.xml.bind.annotation.XmlIDREF;
17 import javax.xml.bind.annotation.XmlRootElement;
18 import javax.xml.bind.annotation.XmlSchemaType;
19 import javax.xml.bind.annotation.XmlType;
20
21 import org.apache.log4j.Logger;
22 import org.hibernate.LazyInitializationException;
23 import org.hibernate.annotations.Cascade;
24 import org.hibernate.annotations.CascadeType;
25
26 @XmlAccessorType(XmlAccessType.FIELD)
27 @XmlType(name = "TermBase", propOrder = {
28 "uri",
29 "representations"
30 })
31 @XmlRootElement(name = "TermBase")
32 @MappedSuperclass
33 public abstract class TermBase extends VersionableEntity {
34 private static final long serialVersionUID = 1471561531632115822L;
35 @SuppressWarnings("unused")
36 private static final Logger logger = Logger.getLogger(TermBase.class);
37
38 @XmlElement(name = "URI")
39 private String uri;
40
41 @XmlElementWrapper(name = "Representations")
42 @XmlElement(name = "Representation")
43 @OneToMany(fetch=FetchType.LAZY)
44 @Cascade( { CascadeType.SAVE_UPDATE, CascadeType.DELETE })
45 private Set<Representation> representations = new HashSet<Representation>();
46
47 public TermBase(){
48 super();
49 }
50 public TermBase(String term, String label, String labelAbbrev) {
51 super();
52 this.addRepresentation(new Representation(term, label, labelAbbrev, Language.DEFAULT()) );
53 }
54
55 public Set<Representation> getRepresentations() {
56 return this.representations;
57 }
58
59 public void addRepresentation(Representation representation) {
60 this.representations.add(representation);
61 }
62
63 public void removeRepresentation(Representation representation) {
64 this.representations.remove(representation);
65 }
66
67 public Representation getRepresentation(Language lang) {
68 for (Representation repr : representations){
69 Language reprLanguage = repr.getLanguage();
70 if (reprLanguage != null && reprLanguage.equals(lang)){
71 return repr;
72 }
73 }
74 return null;
75 }
76
77 /**
78 * @see #getPreferredRepresentation(Language)
79 * @param language
80 * @return
81 */
82 public Representation getPreferredRepresentation(Language language) {
83 Representation repr = getRepresentation(language);
84 if(repr == null){
85 repr = getRepresentation(Language.DEFAULT());
86 }
87 if(repr == null){
88 repr = getRepresentations().iterator().next();
89 }
90 return repr;
91 }
92
93 /**
94 * Returns the Representation in the preferred language. Preferred languages
95 * are specified by the parameter languages, which receives a list of
96 * Language instances in the order of preference. If no representation in
97 * any preferred languages is found the method falls back to return the
98 * Representation in Language.DEFAULT() and if nessecary further falls back
99 * to return the first element found if any.
100 *
101 * TODO think about this fall-back strategy!
102 *
103 * @param languages
104 * @return
105 */
106 public Representation getPreferredRepresentation(List<Language> languages) {
107 Representation repr = null;
108 if(languages != null){
109 for(Language language : languages) {
110 repr = getRepresentation(language);
111 if(repr != null){
112 return repr;
113 }
114 }
115 }
116 if(repr == null){
117 repr = getRepresentation(Language.DEFAULT());
118 }
119 if(repr == null){
120 Iterator<Representation> it = getRepresentations().iterator();
121 if(it.hasNext()){
122 repr = getRepresentations().iterator().next();
123 }
124 }
125 return repr;
126 }
127
128 public String getUri() {
129 return this.uri;
130 }
131
132 public void setUri(String uri) {
133 this.uri = uri;
134 }
135
136 public String getLabel() {
137 if(getLabel(Language.DEFAULT())!=null){
138 Representation repr = getRepresentation(Language.DEFAULT());
139 return (repr == null)? null :repr.getLabel();
140 }else{
141 for (Representation r : representations){
142 return r.getLabel();
143 }
144 }
145 return super.getUuid().toString();
146 }
147
148 public String getLabel(Language lang) {
149 Representation repr = this.getRepresentation(lang);
150 return (repr == null) ? null : repr.getLabel();
151 }
152
153 public void setLabel(String label){
154 Language lang = Language.DEFAULT();
155 setLabel(label, lang);
156 }
157
158 public void setLabel(String label, Language language){
159 if (language != null){
160 Representation repr = getRepresentation(language);
161 if (repr != null){
162 repr.setLabel(label);
163 }else{
164 repr = Representation.NewInstance(null, label, null, language);
165 }
166 this.addRepresentation(repr);
167 }
168 }
169
170 public String getDescription() {
171 return this.getDescription(Language.DEFAULT());
172 }
173
174 public String getDescription(Language lang) {
175 Representation repr = this.getRepresentation(lang);
176 return (repr == null) ? null :repr.getDescription();
177 }
178
179 @Override
180 public boolean equals(Object obj) {
181 if (obj == null){
182 return false;
183 }
184 if (TermBase.class.isAssignableFrom(obj.getClass())){
185 TermBase dtb = (TermBase)obj;
186 if (dtb.getUuid().equals(this.getUuid())){
187 return true;
188 }
189 }
190 return false;
191 }
192
193 @Override
194 public String toString() {
195 //TODO eliminate nasty LazyInitializationException loggings
196 try {
197 return super.toString()+" "+this.getLabel();
198 } catch (LazyInitializationException e) {
199 return super.toString()+" "+this.getUuid();
200 }
201 }
202
203 }