1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.model.common;
|
11
|
|
12
|
import java.net.URI;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.List;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import javax.persistence.Column;
|
19
|
import javax.persistence.FetchType;
|
20
|
import javax.persistence.MappedSuperclass;
|
21
|
import javax.persistence.OneToMany;
|
22
|
import javax.persistence.Transient;
|
23
|
import javax.validation.constraints.NotNull;
|
24
|
import javax.xml.bind.annotation.XmlAccessType;
|
25
|
import javax.xml.bind.annotation.XmlAccessorType;
|
26
|
import javax.xml.bind.annotation.XmlAttribute;
|
27
|
import javax.xml.bind.annotation.XmlElement;
|
28
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
29
|
import javax.xml.bind.annotation.XmlSeeAlso;
|
30
|
import javax.xml.bind.annotation.XmlType;
|
31
|
|
32
|
import org.apache.log4j.Logger;
|
33
|
import org.hibernate.LazyInitializationException;
|
34
|
import org.hibernate.annotations.Cascade;
|
35
|
import org.hibernate.annotations.CascadeType;
|
36
|
import org.hibernate.annotations.Type;
|
37
|
import org.hibernate.envers.Audited;
|
38
|
import org.hibernate.search.annotations.Analyze;
|
39
|
import org.hibernate.search.annotations.Field;
|
40
|
|
41
|
import eu.etaxonomy.cdm.model.description.FeatureTree;
|
42
|
import eu.etaxonomy.cdm.model.description.TextData;
|
43
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
44
|
import eu.etaxonomy.cdm.strategy.cache.common.TermDefaultCacheStrategy;
|
45
|
|
46
|
@XmlAccessorType(XmlAccessType.FIELD)
|
47
|
@XmlType(name = "TermBase", propOrder = {
|
48
|
"uri",
|
49
|
"termType",
|
50
|
"representations"
|
51
|
})
|
52
|
@XmlSeeAlso({
|
53
|
DefinedTermBase.class,
|
54
|
TermVocabulary.class,
|
55
|
FeatureTree.class
|
56
|
})
|
57
|
@MappedSuperclass
|
58
|
@Audited
|
59
|
public abstract class TermBase extends IdentifiableEntity<IIdentifiableEntityCacheStrategy >{
|
60
|
private static final long serialVersionUID = 1471561531632115822L;
|
61
|
@SuppressWarnings("unused")
|
62
|
private static final Logger logger = Logger.getLogger(TermBase.class);
|
63
|
|
64
|
@XmlElement(name = "URI")
|
65
|
@Field(analyze = Analyze.NO)
|
66
|
@Type(type="uriUserType")
|
67
|
private URI uri;
|
68
|
|
69
|
/**
|
70
|
* The {@link TermType type} of this term. Needs to be the same type in a {@link DefinedTermBase defined term}
|
71
|
* and in it's {@link TermVocabulary vocabulary}.
|
72
|
*/
|
73
|
@XmlAttribute(name ="TermType")
|
74
|
@Column(name="termType")
|
75
|
@NotNull
|
76
|
@Type(type = "eu.etaxonomy.cdm.hibernate.EnumUserType",
|
77
|
parameters = {@org.hibernate.annotations.Parameter(name = "enumClass", value = "eu.etaxonomy.cdm.model.common.TermType")}
|
78
|
)
|
79
|
private TermType termType;
|
80
|
|
81
|
@XmlElementWrapper(name = "Representations")
|
82
|
@XmlElement(name = "Representation")
|
83
|
@OneToMany(fetch=FetchType.EAGER, orphanRemoval=true)
|
84
|
@Cascade( { CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
|
85
|
// @IndexedEmbedded no need for embedding since we are using the DefinedTermBaseClassBridge
|
86
|
private Set<Representation> representations = new HashSet<Representation>();
|
87
|
|
88
|
//******************* CONSTRUCTOR *************************************/
|
89
|
|
90
|
//for JAXB only, TODO needed?
|
91
|
@Deprecated
|
92
|
protected TermBase(){}
|
93
|
|
94
|
protected TermBase(TermType type){
|
95
|
super();
|
96
|
if (type == null){
|
97
|
throw new IllegalArgumentException("TermType must not be null");
|
98
|
}else{
|
99
|
this.termType = type;
|
100
|
}
|
101
|
initCacheStrategy();
|
102
|
}
|
103
|
|
104
|
protected TermBase(TermType type, String term, String label, String labelAbbrev) {
|
105
|
this(type);
|
106
|
this.addRepresentation(new Representation(term, label, labelAbbrev, Language.DEFAULT()) );
|
107
|
}
|
108
|
|
109
|
private void initCacheStrategy() {
|
110
|
this.cacheStrategy = new TermDefaultCacheStrategy<TermBase>();
|
111
|
}
|
112
|
|
113
|
//******************** GETTER /SETTER ********************************/
|
114
|
|
115
|
public TermType getTermType() {
|
116
|
return termType;
|
117
|
}
|
118
|
public void setTermType(TermType termType) {
|
119
|
this.termType = termType;
|
120
|
}
|
121
|
|
122
|
|
123
|
public Set<Representation> getRepresentations() {
|
124
|
return this.representations;
|
125
|
}
|
126
|
|
127
|
public void addRepresentation(Representation representation) {
|
128
|
this.representations.add(representation);
|
129
|
// this is just a preliminary solution (see ticket #3148)
|
130
|
if(representation.language !=null && representation.language.equals(Language.DEFAULT())){
|
131
|
this.regenerateTitleCache();
|
132
|
}
|
133
|
}
|
134
|
|
135
|
public void removeRepresentation(Representation representation) {
|
136
|
this.representations.remove(representation);
|
137
|
}
|
138
|
|
139
|
public Representation getRepresentation(Language lang) {
|
140
|
for (Representation repr : representations){
|
141
|
Language reprLanguage = repr.getLanguage();
|
142
|
if (reprLanguage != null && reprLanguage.equals(lang)){
|
143
|
return repr;
|
144
|
}
|
145
|
}
|
146
|
return null;
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* @see #getPreferredRepresentation(Language)
|
151
|
* @param language
|
152
|
* @return
|
153
|
*/
|
154
|
public Representation getPreferredRepresentation(Language language) {
|
155
|
Representation repr = getRepresentation(language);
|
156
|
if(repr == null){
|
157
|
repr = getRepresentation(Language.DEFAULT());
|
158
|
}
|
159
|
if(repr == null){
|
160
|
repr = getRepresentations().iterator().next();
|
161
|
}
|
162
|
return repr;
|
163
|
}
|
164
|
|
165
|
/**
|
166
|
* Returns the Representation in the preferred language. Preferred languages
|
167
|
* are specified by the parameter languages, which receives a list of
|
168
|
* Language instances in the order of preference. If no representation in
|
169
|
* any preferred languages is found the method falls back to return the
|
170
|
* Representation in Language.DEFAULT() and if necessary further falls back
|
171
|
* to return the first element found if any.
|
172
|
*
|
173
|
* TODO think about this fall-back strategy &
|
174
|
* see also {@link TextData#getPreferredLanguageString(List)}
|
175
|
*
|
176
|
* @param languages
|
177
|
* @return
|
178
|
*/
|
179
|
public Representation getPreferredRepresentation(List<Language> languages) {
|
180
|
Representation repr = null;
|
181
|
if(languages != null){
|
182
|
for(Language language : languages) {
|
183
|
repr = getRepresentation(language);
|
184
|
if(repr != null){
|
185
|
return repr;
|
186
|
}
|
187
|
}
|
188
|
}
|
189
|
if(repr == null){
|
190
|
repr = getRepresentation(Language.DEFAULT());
|
191
|
}
|
192
|
if(repr == null){
|
193
|
Iterator<Representation> it = getRepresentations().iterator();
|
194
|
if(it.hasNext()){
|
195
|
repr = getRepresentations().iterator().next();
|
196
|
}
|
197
|
}
|
198
|
return repr;
|
199
|
}
|
200
|
|
201
|
public URI getUri() {
|
202
|
return this.uri;
|
203
|
}
|
204
|
|
205
|
public void setUri(URI uri) {
|
206
|
this.uri = uri;
|
207
|
}
|
208
|
|
209
|
/* (non-Javadoc)
|
210
|
* @see eu.etaxonomy.cdm.model.common.ITerm#getLabel()
|
211
|
*/
|
212
|
@Transient
|
213
|
public String getLabel() {
|
214
|
if(getLabel(Language.DEFAULT())!=null){
|
215
|
Representation repr = getRepresentation(Language.DEFAULT());
|
216
|
return (repr == null)? null :repr.getLabel();
|
217
|
}else{
|
218
|
for (Representation r : representations){
|
219
|
return r.getLabel();
|
220
|
}
|
221
|
}
|
222
|
return super.getUuid().toString();
|
223
|
}
|
224
|
|
225
|
/* (non-Javadoc)
|
226
|
* @see eu.etaxonomy.cdm.model.common.ITerm#getLabel(eu.etaxonomy.cdm.model.common.Language)
|
227
|
*/
|
228
|
public String getLabel(Language lang) {
|
229
|
Representation repr = this.getRepresentation(lang);
|
230
|
return (repr == null) ? null : repr.getLabel();
|
231
|
}
|
232
|
|
233
|
public void setLabel(String label){
|
234
|
Language lang = Language.DEFAULT();
|
235
|
setLabel(label, lang);
|
236
|
}
|
237
|
|
238
|
public void setLabel(String label, Language language){
|
239
|
if (language != null){
|
240
|
Representation repr = getRepresentation(language);
|
241
|
if (repr != null){
|
242
|
repr.setLabel(label);
|
243
|
}else{
|
244
|
repr = Representation.NewInstance(null, label, null, language);
|
245
|
}
|
246
|
this.addRepresentation(repr);
|
247
|
}
|
248
|
}
|
249
|
|
250
|
/* (non-Javadoc)
|
251
|
* @see eu.etaxonomy.cdm.model.common.ITerm#getDescription()
|
252
|
*/
|
253
|
@Transient
|
254
|
public String getDescription() {
|
255
|
return this.getDescription(Language.DEFAULT());
|
256
|
}
|
257
|
|
258
|
/* (non-Javadoc)
|
259
|
* @see eu.etaxonomy.cdm.model.common.ITerm#getDescription(eu.etaxonomy.cdm.model.common.Language)
|
260
|
*/
|
261
|
public String getDescription(Language lang) {
|
262
|
Representation repr = this.getRepresentation(lang);
|
263
|
return (repr == null) ? null :repr.getDescription();
|
264
|
}
|
265
|
|
266
|
@Override
|
267
|
public boolean equals(Object obj) {
|
268
|
if (obj == null){
|
269
|
return false;
|
270
|
}
|
271
|
if (TermBase.class.isAssignableFrom(obj.getClass())){
|
272
|
TermBase dtb = (TermBase)obj;
|
273
|
if (dtb.getUuid().equals(this.getUuid())){
|
274
|
return true;
|
275
|
}
|
276
|
}
|
277
|
return false;
|
278
|
}
|
279
|
|
280
|
@Override
|
281
|
public String toString() {
|
282
|
//TODO eliminate nasty LazyInitializationException loggings
|
283
|
try {
|
284
|
return super.toString();
|
285
|
} catch (LazyInitializationException e) {
|
286
|
return super.toString()+" "+this.getUuid();
|
287
|
}
|
288
|
}
|
289
|
|
290
|
//*********************** CLONE ********************************************************/
|
291
|
|
292
|
/**
|
293
|
* Clones <i>this</i> TermBase. This is a shortcut that enables to create
|
294
|
* a new instance that differs only slightly from <i>this</i> TermBase by
|
295
|
* modifying only some of the attributes.
|
296
|
*
|
297
|
* @see eu.etaxonomy.cdm.model.common.IdentifiableEntity#clone()
|
298
|
* @see java.lang.Object#clone()
|
299
|
*/
|
300
|
@Override
|
301
|
public Object clone()throws CloneNotSupportedException {
|
302
|
|
303
|
TermBase result = (TermBase) super.clone();
|
304
|
|
305
|
result.representations = new HashSet<Representation>();
|
306
|
for (Representation rep : this.representations){
|
307
|
result.representations.add((Representation)rep.clone());
|
308
|
}
|
309
|
|
310
|
return result;
|
311
|
}
|
312
|
|
313
|
}
|