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