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
.Field
;
35 import org
.hibernate
.search
.annotations
.IndexedEmbedded
;
36 import org
.hibernate
.validator
.constraints
.Length
;
38 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
39 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
40 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.TermDefaultCacheStrategy
;
41 import eu
.etaxonomy
.cdm
.validation
.annotation
.NullOrNotEmpty
;
43 @XmlAccessorType(XmlAccessType
.FIELD
)
44 @XmlType(name
= "TermBase", propOrder
= {
49 DefinedTermBase
.class,
54 public abstract class TermBase
extends IdentifiableEntity
{
55 private static final long serialVersionUID
= 1471561531632115822L;
56 @SuppressWarnings("unused")
57 private static final Logger logger
= Logger
.getLogger(TermBase
.class);
59 @XmlElement(name
= "URI")
60 @Field(index
=org
.hibernate
.search
.annotations
.Index
.UN_TOKENIZED
)
63 @Type(type
="uriUserType")
66 @XmlElementWrapper(name
= "Representations")
67 @XmlElement(name
= "Representation")
68 @OneToMany(fetch
=FetchType
.EAGER
)
69 @Cascade( { CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
, CascadeType
.DELETE
, CascadeType
.DELETE_ORPHAN
})
70 // @IndexedEmbedded no need for embedding since we are using the DefinedTermBaseClassBridge
71 private Set
<Representation
> representations
= new HashSet
<Representation
>();
78 private void initCacheStrategy() {
79 this.cacheStrategy
= new TermDefaultCacheStrategy
<TermBase
>();
81 public TermBase(String term
, String label
, String labelAbbrev
) {
84 this.addRepresentation(new Representation(term
, label
, labelAbbrev
, Language
.DEFAULT()) );
87 public Set
<Representation
> getRepresentations() {
88 return this.representations
;
91 public void addRepresentation(Representation representation
) {
92 this.representations
.add(representation
);
93 // this is just a preliminary solution (see ticket #3148)
94 if(representation
.language
!=null && representation
.language
.equals(Language
.DEFAULT())){
95 this.regenerateTitleCache();
99 public void removeRepresentation(Representation representation
) {
100 this.representations
.remove(representation
);
103 public Representation
getRepresentation(Language lang
) {
104 for (Representation repr
: representations
){
105 Language reprLanguage
= repr
.getLanguage();
106 if (reprLanguage
!= null && reprLanguage
.equals(lang
)){
114 * @see #getPreferredRepresentation(Language)
118 public Representation
getPreferredRepresentation(Language language
) {
119 Representation repr
= getRepresentation(language
);
121 repr
= getRepresentation(Language
.DEFAULT());
124 repr
= getRepresentations().iterator().next();
130 * Returns the Representation in the preferred language. Preferred languages
131 * are specified by the parameter languages, which receives a list of
132 * Language instances in the order of preference. If no representation in
133 * any preferred languages is found the method falls back to return the
134 * Representation in Language.DEFAULT() and if necessary further falls back
135 * to return the first element found if any.
137 * TODO think about this fall-back strategy &
138 * see also {@link TextData#getPreferredLanguageString(List)}
143 public Representation
getPreferredRepresentation(List
<Language
> languages
) {
144 Representation repr
= null;
145 if(languages
!= null){
146 for(Language language
: languages
) {
147 repr
= getRepresentation(language
);
154 repr
= getRepresentation(Language
.DEFAULT());
157 Iterator
<Representation
> it
= getRepresentations().iterator();
159 repr
= getRepresentations().iterator().next();
165 public URI
getUri() {
169 public void setUri(URI uri
) {
174 * @see eu.etaxonomy.cdm.model.common.ITerm#getLabel()
177 public String
getLabel() {
178 if(getLabel(Language
.DEFAULT())!=null){
179 Representation repr
= getRepresentation(Language
.DEFAULT());
180 return (repr
== null)?
null :repr
.getLabel();
182 for (Representation r
: representations
){
186 return super.getUuid().toString();
190 * @see eu.etaxonomy.cdm.model.common.ITerm#getLabel(eu.etaxonomy.cdm.model.common.Language)
192 public String
getLabel(Language lang
) {
193 Representation repr
= this.getRepresentation(lang
);
194 return (repr
== null) ?
null : repr
.getLabel();
197 public void setLabel(String label
){
198 Language lang
= Language
.DEFAULT();
199 setLabel(label
, lang
);
202 public void setLabel(String label
, Language language
){
203 if (language
!= null){
204 Representation repr
= getRepresentation(language
);
206 repr
.setLabel(label
);
208 repr
= Representation
.NewInstance(null, label
, null, language
);
210 this.addRepresentation(repr
);
215 * @see eu.etaxonomy.cdm.model.common.ITerm#getDescription()
218 public String
getDescription() {
219 return this.getDescription(Language
.DEFAULT());
223 * @see eu.etaxonomy.cdm.model.common.ITerm#getDescription(eu.etaxonomy.cdm.model.common.Language)
225 public String
getDescription(Language lang
) {
226 Representation repr
= this.getRepresentation(lang
);
227 return (repr
== null) ?
null :repr
.getDescription();
231 public boolean equals(Object obj
) {
235 if (TermBase
.class.isAssignableFrom(obj
.getClass())){
236 TermBase dtb
= (TermBase
)obj
;
237 if (dtb
.getUuid().equals(this.getUuid())){
245 public String
toString() {
246 //TODO eliminate nasty LazyInitializationException loggings
248 return super.toString();
249 } catch (LazyInitializationException e
) {
250 return super.toString()+" "+this.getUuid();
254 //*********************** CLONE ********************************************************/
257 * Clones <i>this</i> TermBase. This is a shortcut that enables to create
258 * a new instance that differs only slightly from <i>this</i> TermBase by
259 * modifying only some of the attributes.
261 * @see eu.etaxonomy.cdm.model.common.IdentifiableEntity#clone()
262 * @see java.lang.Object#clone()
265 public Object
clone()throws CloneNotSupportedException
{
267 TermBase result
= (TermBase
) super.clone();
269 result
.representations
= new HashSet
<Representation
>();
270 for (Representation rep
: this.representations
){
271 result
.representations
.add((Representation
)rep
.clone());