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