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
.validation
.constraints
.Pattern
;
23 import javax
.xml
.bind
.annotation
.XmlAccessType
;
24 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
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
;
30 import org
.apache
.log4j
.Logger
;
31 import org
.hibernate
.LazyInitializationException
;
32 import org
.hibernate
.annotations
.Cascade
;
33 import org
.hibernate
.annotations
.CascadeType
;
34 import org
.hibernate
.annotations
.Type
;
35 import org
.hibernate
.search
.annotations
.Field
;
36 import org
.hibernate
.search
.annotations
.IndexedEmbedded
;
37 import org
.hibernate
.validator
.constraints
.Length
;
39 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
40 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
41 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.TermDefaultCacheStrategy
;
42 import eu
.etaxonomy
.cdm
.validation
.Level2
;
43 import eu
.etaxonomy
.cdm
.validation
.annotation
.NullOrNotEmpty
;
45 @XmlAccessorType(XmlAccessType
.FIELD
)
46 @XmlType(name
= "TermBase", propOrder
= {
51 DefinedTermBase
.class,
56 public abstract class TermBase
extends IdentifiableEntity
{
57 private static final long serialVersionUID
= 1471561531632115822L;
58 @SuppressWarnings("unused")
59 private static final Logger logger
= Logger
.getLogger(TermBase
.class);
61 @XmlElement(name
= "URI")
62 @Field(index
=org
.hibernate
.search
.annotations
.Index
.UN_TOKENIZED
)
65 @Type(type
="uriUserType")
68 @XmlElementWrapper(name
= "Representations")
69 @XmlElement(name
= "Representation")
70 @OneToMany(fetch
=FetchType
.EAGER
)
71 @Cascade( { CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
, CascadeType
.DELETE
, CascadeType
.DELETE_ORPHAN
})
72 @IndexedEmbedded(depth
= 2)
73 private Set
<Representation
> representations
= new HashSet
<Representation
>();
80 private void initCacheStrategy() {
81 this.cacheStrategy
= new TermDefaultCacheStrategy
<TermBase
>();
83 public TermBase(String term
, String label
, String labelAbbrev
) {
85 this.addRepresentation(new Representation(term
, label
, labelAbbrev
, Language
.DEFAULT()) );
89 public Set
<Representation
> getRepresentations() {
90 return this.representations
;
93 public void addRepresentation(Representation representation
) {
94 this.representations
.add(representation
);
97 public void removeRepresentation(Representation representation
) {
98 this.representations
.remove(representation
);
101 public Representation
getRepresentation(Language lang
) {
102 for (Representation repr
: representations
){
103 Language reprLanguage
= repr
.getLanguage();
104 if (reprLanguage
!= null && reprLanguage
.equals(lang
)){
112 * @see #getPreferredRepresentation(Language)
116 public Representation
getPreferredRepresentation(Language language
) {
117 Representation repr
= getRepresentation(language
);
119 repr
= getRepresentation(Language
.DEFAULT());
122 repr
= getRepresentations().iterator().next();
128 * Returns the Representation in the preferred language. Preferred languages
129 * are specified by the parameter languages, which receives a list of
130 * Language instances in the order of preference. If no representation in
131 * any preferred languages is found the method falls back to return the
132 * Representation in Language.DEFAULT() and if necessary further falls back
133 * to return the first element found if any.
135 * TODO think about this fall-back strategy &
136 * see also {@link TextData#getPreferredLanguageString(List)}
141 public Representation
getPreferredRepresentation(List
<Language
> languages
) {
142 Representation repr
= null;
143 if(languages
!= null){
144 for(Language language
: languages
) {
145 repr
= getRepresentation(language
);
152 repr
= getRepresentation(Language
.DEFAULT());
155 Iterator
<Representation
> it
= getRepresentations().iterator();
157 repr
= getRepresentations().iterator().next();
163 public URI
getUri() {
167 public void setUri(URI uri
) {
172 public String
getLabel() {
173 if(getLabel(Language
.DEFAULT())!=null){
174 Representation repr
= getRepresentation(Language
.DEFAULT());
175 return (repr
== null)?
null :repr
.getLabel();
177 for (Representation r
: representations
){
181 return super.getUuid().toString();
184 public String
getLabel(Language lang
) {
185 Representation repr
= this.getRepresentation(lang
);
186 return (repr
== null) ?
null : repr
.getLabel();
189 public void setLabel(String label
){
190 Language lang
= Language
.DEFAULT();
191 setLabel(label
, lang
);
194 public void setLabel(String label
, Language language
){
195 if (language
!= null){
196 Representation repr
= getRepresentation(language
);
198 repr
.setLabel(label
);
200 repr
= Representation
.NewInstance(null, label
, null, language
);
202 this.addRepresentation(repr
);
207 public String
getDescription() {
208 return this.getDescription(Language
.DEFAULT());
211 public String
getDescription(Language lang
) {
212 Representation repr
= this.getRepresentation(lang
);
213 return (repr
== null) ?
null :repr
.getDescription();
217 public boolean equals(Object obj
) {
221 if (TermBase
.class.isAssignableFrom(obj
.getClass())){
222 TermBase dtb
= (TermBase
)obj
;
223 if (dtb
.getUuid().equals(this.getUuid())){
231 public String
toString() {
232 //TODO eliminate nasty LazyInitializationException loggings
234 return super.toString();
235 } catch (LazyInitializationException e
) {
236 return super.toString()+" "+this.getUuid();
240 //*********************** CLONE ********************************************************/
243 * Clones <i>this</i> TermBase. This is a shortcut that enables to create
244 * a new instance that differs only slightly from <i>this</i> TermBase by
245 * modifying only some of the attributes.
247 * @see eu.etaxonomy.cdm.model.common.IdentifiableEntity#clone()
248 * @see java.lang.Object#clone()
251 public Object
clone()throws CloneNotSupportedException
{
253 TermBase result
= (TermBase
) super.clone();
255 result
.representations
= new HashSet
<Representation
>();
256 for (Representation rep
: this.representations
){
257 result
.representations
.add((Representation
)rep
.clone());