2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.model
.common
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
18 import javax
.persistence
.FetchType
;
19 import javax
.persistence
.MappedSuperclass
;
20 import javax
.persistence
.OneToMany
;
21 import javax
.persistence
.Transient
;
22 import javax
.xml
.bind
.annotation
.XmlAccessType
;
23 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
24 import javax
.xml
.bind
.annotation
.XmlElement
;
25 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
26 import javax
.xml
.bind
.annotation
.XmlSeeAlso
;
27 import javax
.xml
.bind
.annotation
.XmlType
;
29 import org
.apache
.log4j
.Logger
;
30 import org
.hibernate
.LazyInitializationException
;
31 import org
.hibernate
.annotations
.Cascade
;
32 import org
.hibernate
.annotations
.CascadeType
;
33 import org
.hibernate
.annotations
.Type
;
34 import org
.hibernate
.search
.annotations
.Analyze
;
35 import org
.hibernate
.search
.annotations
.Field
;
37 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
38 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
39 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
40 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.TermDefaultCacheStrategy
;
42 @XmlAccessorType(XmlAccessType
.FIELD
)
43 @XmlType(name
= "TermBase", propOrder
= {
48 DefinedTermBase
.class,
53 public abstract class TermBase
extends IdentifiableEntity
<IIdentifiableEntityCacheStrategy
>{
54 private static final long serialVersionUID
= 1471561531632115822L;
55 @SuppressWarnings("unused")
56 private static final Logger logger
= Logger
.getLogger(TermBase
.class);
58 @XmlElement(name
= "URI")
59 @Field(analyze
= Analyze
.NO
)
60 @Type(type
="uriUserType")
63 @XmlElementWrapper(name
= "Representations")
64 @XmlElement(name
= "Representation")
65 @OneToMany(fetch
=FetchType
.EAGER
, orphanRemoval
=true)
66 @Cascade( { CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
, CascadeType
.DELETE
})
67 // @IndexedEmbedded no need for embedding since we are using the DefinedTermBaseClassBridge
68 private Set
<Representation
> representations
= new HashSet
<Representation
>();
75 private void initCacheStrategy() {
76 this.cacheStrategy
= new TermDefaultCacheStrategy
<TermBase
>();
78 public TermBase(String term
, String label
, String labelAbbrev
) {
81 this.addRepresentation(new Representation(term
, label
, labelAbbrev
, Language
.DEFAULT()) );
84 public Set
<Representation
> getRepresentations() {
85 return this.representations
;
88 public void addRepresentation(Representation representation
) {
89 this.representations
.add(representation
);
90 // this is just a preliminary solution (see ticket #3148)
91 if(representation
.language
!=null && representation
.language
.equals(Language
.DEFAULT())){
92 this.regenerateTitleCache();
96 public void removeRepresentation(Representation representation
) {
97 this.representations
.remove(representation
);
100 public Representation
getRepresentation(Language lang
) {
101 for (Representation repr
: representations
){
102 Language reprLanguage
= repr
.getLanguage();
103 if (reprLanguage
!= null && reprLanguage
.equals(lang
)){
111 * @see #getPreferredRepresentation(Language)
115 public Representation
getPreferredRepresentation(Language language
) {
116 Representation repr
= getRepresentation(language
);
118 repr
= getRepresentation(Language
.DEFAULT());
121 repr
= getRepresentations().iterator().next();
127 * Returns the Representation in the preferred language. Preferred languages
128 * are specified by the parameter languages, which receives a list of
129 * Language instances in the order of preference. If no representation in
130 * any preferred languages is found the method falls back to return the
131 * Representation in Language.DEFAULT() and if necessary further falls back
132 * to return the first element found if any.
134 * TODO think about this fall-back strategy &
135 * see also {@link TextData#getPreferredLanguageString(List)}
140 public Representation
getPreferredRepresentation(List
<Language
> languages
) {
141 Representation repr
= null;
142 if(languages
!= null){
143 for(Language language
: languages
) {
144 repr
= getRepresentation(language
);
151 repr
= getRepresentation(Language
.DEFAULT());
154 Iterator
<Representation
> it
= getRepresentations().iterator();
156 repr
= getRepresentations().iterator().next();
162 public URI
getUri() {
166 public void setUri(URI uri
) {
171 * @see eu.etaxonomy.cdm.model.common.ITerm#getLabel()
174 public String
getLabel() {
175 if(getLabel(Language
.DEFAULT())!=null){
176 Representation repr
= getRepresentation(Language
.DEFAULT());
177 return (repr
== null)?
null :repr
.getLabel();
179 for (Representation r
: representations
){
183 return super.getUuid().toString();
187 * @see eu.etaxonomy.cdm.model.common.ITerm#getLabel(eu.etaxonomy.cdm.model.common.Language)
189 public String
getLabel(Language lang
) {
190 Representation repr
= this.getRepresentation(lang
);
191 return (repr
== null) ?
null : repr
.getLabel();
194 public void setLabel(String label
){
195 Language lang
= Language
.DEFAULT();
196 setLabel(label
, lang
);
199 public void setLabel(String label
, Language language
){
200 if (language
!= null){
201 Representation repr
= getRepresentation(language
);
203 repr
.setLabel(label
);
205 repr
= Representation
.NewInstance(null, label
, null, language
);
207 this.addRepresentation(repr
);
212 * @see eu.etaxonomy.cdm.model.common.ITerm#getDescription()
215 public String
getDescription() {
216 return this.getDescription(Language
.DEFAULT());
220 * @see eu.etaxonomy.cdm.model.common.ITerm#getDescription(eu.etaxonomy.cdm.model.common.Language)
222 public String
getDescription(Language lang
) {
223 Representation repr
= this.getRepresentation(lang
);
224 return (repr
== null) ?
null :repr
.getDescription();
228 public boolean equals(Object obj
) {
232 if (TermBase
.class.isAssignableFrom(obj
.getClass())){
233 TermBase dtb
= (TermBase
)obj
;
234 if (dtb
.getUuid().equals(this.getUuid())){
242 public String
toString() {
243 //TODO eliminate nasty LazyInitializationException loggings
245 return super.toString();
246 } catch (LazyInitializationException e
) {
247 return super.toString()+" "+this.getUuid();
251 //*********************** CLONE ********************************************************/
254 * Clones <i>this</i> TermBase. This is a shortcut that enables to create
255 * a new instance that differs only slightly from <i>this</i> TermBase by
256 * modifying only some of the attributes.
258 * @see eu.etaxonomy.cdm.model.common.IdentifiableEntity#clone()
259 * @see java.lang.Object#clone()
262 public Object
clone()throws CloneNotSupportedException
{
264 TermBase result
= (TermBase
) super.clone();
266 result
.representations
= new HashSet
<Representation
>();
267 for (Representation rep
: this.representations
){
268 result
.representations
.add((Representation
)rep
.clone());