1 package eu
.etaxonomy
.cdm
.model
.common
;
3 import java
.util
.HashSet
;
4 import java
.util
.Iterator
;
8 import javax
.persistence
.FetchType
;
9 import javax
.persistence
.MappedSuperclass
;
10 import javax
.persistence
.OneToMany
;
11 import javax
.persistence
.Transient
;
12 import javax
.xml
.bind
.annotation
.XmlAccessType
;
13 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
14 import javax
.xml
.bind
.annotation
.XmlElement
;
15 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
16 import javax
.xml
.bind
.annotation
.XmlIDREF
;
17 import javax
.xml
.bind
.annotation
.XmlRootElement
;
18 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
19 import javax
.xml
.bind
.annotation
.XmlType
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.hibernate
.LazyInitializationException
;
23 import org
.hibernate
.annotations
.Cascade
;
24 import org
.hibernate
.annotations
.CascadeType
;
26 @XmlAccessorType(XmlAccessType
.FIELD
)
27 @XmlType(name
= "TermBase", propOrder
= {
31 @XmlRootElement(name
= "TermBase")
33 public abstract class TermBase
extends VersionableEntity
{
34 private static final long serialVersionUID
= 1471561531632115822L;
35 @SuppressWarnings("unused")
36 private static final Logger logger
= Logger
.getLogger(TermBase
.class);
38 @XmlElement(name
= "URI")
41 @XmlElementWrapper(name
= "Representations")
42 @XmlElement(name
= "Representation")
43 @OneToMany(fetch
=FetchType
.LAZY
)
44 @Cascade( { CascadeType
.SAVE_UPDATE
, CascadeType
.DELETE
})
45 private Set
<Representation
> representations
= new HashSet
<Representation
>();
50 public TermBase(String term
, String label
, String labelAbbrev
) {
52 this.addRepresentation(new Representation(term
, label
, labelAbbrev
, Language
.DEFAULT()) );
55 public Set
<Representation
> getRepresentations() {
56 return this.representations
;
59 public void addRepresentation(Representation representation
) {
60 this.representations
.add(representation
);
63 public void removeRepresentation(Representation representation
) {
64 this.representations
.remove(representation
);
67 public Representation
getRepresentation(Language lang
) {
68 for (Representation repr
: representations
){
69 Language reprLanguage
= repr
.getLanguage();
70 if (reprLanguage
!= null && reprLanguage
.equals(lang
)){
78 * @see #getPreferredRepresentation(Language)
82 public Representation
getPreferredRepresentation(Language language
) {
83 Representation repr
= getRepresentation(language
);
85 repr
= getRepresentation(Language
.DEFAULT());
88 repr
= getRepresentations().iterator().next();
94 * Returns the Representation in the preferred language. Preferred languages
95 * are specified by the parameter languages, which receives a list of
96 * Language instances in the order of preference. If no representation in
97 * any preferred languages is found the method falls back to return the
98 * Representation in Language.DEFAULT() and if nessecary further falls back
99 * to return the first element found if any.
101 * TODO think about this fall-back strategy!
106 public Representation
getPreferredRepresentation(List
<Language
> languages
) {
107 Representation repr
= null;
108 if(languages
!= null){
109 for(Language language
: languages
) {
110 repr
= getRepresentation(language
);
117 repr
= getRepresentation(Language
.DEFAULT());
120 Iterator
<Representation
> it
= getRepresentations().iterator();
122 repr
= getRepresentations().iterator().next();
128 public String
getUri() {
132 public void setUri(String uri
) {
136 public String
getLabel() {
137 if(getLabel(Language
.DEFAULT())!=null){
138 Representation repr
= getRepresentation(Language
.DEFAULT());
139 return (repr
== null)?
null :repr
.getLabel();
141 for (Representation r
: representations
){
145 return super.getUuid().toString();
148 public String
getLabel(Language lang
) {
149 Representation repr
= this.getRepresentation(lang
);
150 return (repr
== null) ?
null : repr
.getLabel();
153 public void setLabel(String label
){
154 Language lang
= Language
.DEFAULT();
155 setLabel(label
, lang
);
158 public void setLabel(String label
, Language language
){
159 if (language
!= null){
160 Representation repr
= getRepresentation(language
);
162 repr
.setLabel(label
);
164 repr
= Representation
.NewInstance(null, label
, null, language
);
166 this.addRepresentation(repr
);
170 public String
getDescription() {
171 return this.getDescription(Language
.DEFAULT());
174 public String
getDescription(Language lang
) {
175 Representation repr
= this.getRepresentation(lang
);
176 return (repr
== null) ?
null :repr
.getDescription();
180 public boolean equals(Object obj
) {
184 if (TermBase
.class.isAssignableFrom(obj
.getClass())){
185 TermBase dtb
= (TermBase
)obj
;
186 if (dtb
.getUuid().equals(this.getUuid())){
194 public String
toString() {
195 //TODO eliminate nasty LazyInitializationException loggings
197 return super.toString()+" "+this.getLabel();
198 } catch (LazyInitializationException e
) {
199 return super.toString()+" "+this.getUuid();