Project

General

Profile

Download (9.7 KB) Statistics
| Branch: | Tag: | Revision:
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.TextData;
42
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
43
import eu.etaxonomy.cdm.strategy.cache.common.TermDefaultCacheStrategy;
44

    
45
@XmlAccessorType(XmlAccessType.FIELD)
46
@XmlType(name = "TermBase", propOrder = {
47
    "uri",
48
    "termType",
49
    "representations"
50
})
51
@XmlSeeAlso({
52
    DefinedTermBase.class,
53
    TermVocabulary.class
54
})
55
@MappedSuperclass
56
@Audited
57
public abstract class TermBase extends IdentifiableEntity<IIdentifiableEntityCacheStrategy >{
58
    private static final long serialVersionUID = 1471561531632115822L;
59
    @SuppressWarnings("unused")
60
    private static final Logger logger = Logger.getLogger(TermBase.class);
61

    
62
    @XmlElement(name = "URI")
63
    @Field(analyze = Analyze.NO)
64
    @Type(type="uriUserType")
65
    private URI uri;
66

    
67
	/**
68
	 * The {@link TermType type} of this term. Needs to be the same type in a {@link DefinedTermBase defined term}
69
	 * and in it's {@link TermVocabulary vocabulary}.
70
	 */
71
	@XmlAttribute(name ="TermType")
72
	@Column(name="termType")
73
	@NotNull
74
    @Type(type = "eu.etaxonomy.cdm.hibernate.EnumUserType",
75
        parameters = {@org.hibernate.annotations.Parameter(name  = "enumClass", value = "eu.etaxonomy.cdm.model.common.TermType")}
76
    )
77
	private TermType termType;
78

    
79
    @XmlElementWrapper(name = "Representations")
80
    @XmlElement(name = "Representation")
81
    @OneToMany(fetch=FetchType.EAGER, orphanRemoval=true)
82
    @Cascade( { CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
83
    // @IndexedEmbedded no need for embedding since we are using the DefinedTermBaseClassBridge
84
    private Set<Representation> representations = new HashSet<Representation>();
85

    
86
//******************* CONSTRUCTOR *************************************/
87

    
88
    //for JAXB only, TODO needed?
89
    @Deprecated
90
    protected TermBase(){}
91

    
92
    protected TermBase(TermType type){
93
        super();
94
        if (type == null){
95
        	throw new IllegalArgumentException("TermType must not be null");
96
        }else{
97
        	this.termType = type;
98
        }
99
        initCacheStrategy();
100
    }
101

    
102
    protected TermBase(TermType type, String term, String label, String labelAbbrev) {
103
        this(type);
104
        this.addRepresentation(new Representation(term, label, labelAbbrev, Language.DEFAULT()) );
105
    }
106

    
107
    private void initCacheStrategy() {
108
        this.cacheStrategy = new TermDefaultCacheStrategy<TermBase>();
109
    }
110

    
111
//******************** GETTER /SETTER ********************************/
112

    
113
	public TermType getTermType() {
114
		return termType;
115
	}
116
	public void setTermType(TermType termType) {
117
		this.termType = termType;
118
	}
119

    
120

    
121
    public Set<Representation> getRepresentations() {
122
        return this.representations;
123
    }
124

    
125
    public void addRepresentation(Representation representation) {
126
        this.representations.add(representation);
127
        //reset titleCache
128
        this.titleCache = null;
129
    }
130

    
131
    public void removeRepresentation(Representation representation) {
132
        this.representations.remove(representation);
133
      //reset titlecache
134
        this.titleCache = null;
135
    }
136

    
137
    public Representation getRepresentation(Language lang) {
138
        for (Representation repr : representations){
139
            Language reprLanguage = repr.getLanguage();
140
            if (reprLanguage != null && reprLanguage.equals(lang)){
141
                return repr;
142
            }
143
        }
144
        return null;
145
    }
146

    
147
    /**
148
     * @see #getPreferredRepresentation(Language)
149
     * @param language
150
     * @return
151
     */
152
    public Representation getPreferredRepresentation(Language language) {
153
        Representation repr = getRepresentation(language);
154
        if(repr == null){
155
            repr = getRepresentation(Language.DEFAULT());
156
        }
157
        if(repr == null){
158
            repr = getRepresentations().isEmpty() ? null : getRepresentations().iterator().next();
159
        }
160
        return repr;
161
    }
162

    
163
    /**
164
     * Returns the Representation in the preferred language. Preferred languages
165
     * are specified by the parameter languages, which receives a list of
166
     * Language instances in the order of preference. If no representation in
167
     * any preferred languages is found the method falls back to return the
168
     * Representation in Language.DEFAULT() and if necessary further falls back
169
     * to return the first element found if any.
170
     *
171
     * TODO think about this fall-back strategy &
172
     * see also {@link TextData#getPreferredLanguageString(List)}
173
     *
174
     * @param languages
175
     * @return
176
     */
177
    public Representation getPreferredRepresentation(List<Language> languages) {
178
        Representation repr = null;
179
        if(languages != null){
180
            for(Language language : languages) {
181
                repr = getRepresentation(language);
182
                if(repr != null){
183
                    return repr;
184
                }
185
            }
186
        }
187
        if(repr == null){
188
            repr = getRepresentation(Language.DEFAULT());
189
        }
190
        if(repr == null){
191
            Iterator<Representation> it = getRepresentations().iterator();
192
            if(it.hasNext()){
193
                repr = getRepresentations().iterator().next();
194
            }
195
        }
196
        return repr;
197
    }
198

    
199
    public URI getUri() {
200
        return this.uri;
201
    }
202

    
203
    public void setUri(URI uri) {
204
        this.uri = uri;
205
    }
206

    
207
    /* (non-Javadoc)
208
     * @see eu.etaxonomy.cdm.model.common.ITerm#getLabel()
209
     */
210
    @Transient
211
    public String getLabel() {
212
        if(getLabel(Language.DEFAULT())!=null){
213
            Representation repr = getRepresentation(Language.DEFAULT());
214
            return (repr == null)? null :repr.getLabel();
215
        }else{
216
            for (Representation r : representations){
217
                return r.getLabel();
218
            }
219
        }
220
        return super.getUuid().toString();
221
    }
222

    
223
    /* (non-Javadoc)
224
     * @see eu.etaxonomy.cdm.model.common.ITerm#getLabel(eu.etaxonomy.cdm.model.common.Language)
225
     */
226
    public String getLabel(Language lang) {
227
        Representation repr = this.getRepresentation(lang);
228
        return (repr == null) ? null : repr.getLabel();
229
    }
230

    
231
    public void setLabel(String label){
232
        Language lang = Language.DEFAULT();
233
        setLabel(label, lang);
234
    }
235

    
236
    public void setLabel(String label, Language language){
237
        if (language != null){
238
            Representation repr = getRepresentation(language);
239
            if (repr != null){
240
                repr.setLabel(label);
241
            }else{
242
                repr = Representation.NewInstance(null, label, null, language);
243
            }
244
            this.addRepresentation(repr);
245
        }
246
    }
247

    
248
    /* (non-Javadoc)
249
     * @see eu.etaxonomy.cdm.model.common.ITerm#getDescription()
250
     */
251
    @Transient
252
    public String getDescription() {
253
        return this.getDescription(Language.DEFAULT());
254
    }
255

    
256
    /* (non-Javadoc)
257
     * @see eu.etaxonomy.cdm.model.common.ITerm#getDescription(eu.etaxonomy.cdm.model.common.Language)
258
     */
259
    public String getDescription(Language lang) {
260
        Representation repr = this.getRepresentation(lang);
261
        return (repr == null) ? null :repr.getDescription();
262
    }
263

    
264
    @Override
265
    public boolean equals(Object obj) {
266
        if (obj == null){
267
            return false;
268
        }
269
        if (TermBase.class.isAssignableFrom(obj.getClass())){
270
            TermBase dtb = (TermBase)obj;
271
            if (dtb.getUuid().equals(this.getUuid())){
272
                return true;
273
            }
274
        }
275
        return false;
276
    }
277

    
278
    @Override
279
    public String toString() {
280
        //TODO eliminate nasty LazyInitializationException loggings
281
        try {
282
            return super.toString();
283
        } catch (LazyInitializationException e) {
284
            return super.toString()+" "+this.getUuid();
285
        }
286
    }
287

    
288
//*********************** CLONE ********************************************************/
289

    
290
    /**
291
     * Clones <i>this</i> TermBase. This is a shortcut that enables to create
292
     * a new instance that differs only slightly from <i>this</i> TermBase by
293
     * modifying only some of the attributes.
294
     *
295
     * @see eu.etaxonomy.cdm.model.common.IdentifiableEntity#clone()
296
     * @see java.lang.Object#clone()
297
     */
298
    @Override
299
    public Object clone()throws CloneNotSupportedException {
300

    
301
        TermBase result = (TermBase) super.clone();
302

    
303
        result.representations = new HashSet<Representation>();
304
        for (Representation rep : this.representations){
305
            result.representations.add((Representation)rep.clone());
306
        }
307

    
308
        return result;
309
    }
310

    
311
}
(62-62/72)