1 package eu
.etaxonomy
.cdm
.model
.common
;
3 import java
.util
.HashSet
;
7 import javax
.persistence
.FetchType
;
8 import javax
.persistence
.MappedSuperclass
;
9 import javax
.persistence
.OneToMany
;
10 import javax
.persistence
.Transient
;
11 import javax
.xml
.bind
.annotation
.XmlAccessType
;
12 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
13 import javax
.xml
.bind
.annotation
.XmlElement
;
14 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
15 import javax
.xml
.bind
.annotation
.XmlIDREF
;
16 import javax
.xml
.bind
.annotation
.XmlRootElement
;
17 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
18 import javax
.xml
.bind
.annotation
.XmlType
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.hibernate
.annotations
.Cascade
;
22 import org
.hibernate
.annotations
.CascadeType
;
24 @XmlAccessorType(XmlAccessType
.FIELD
)
25 @XmlType(name
= "TermBase", propOrder
= {
29 @XmlRootElement(name
= "TermBase")
31 public abstract class TermBase
extends VersionableEntity
{
32 private static final long serialVersionUID
= 1471561531632115822L;
33 @SuppressWarnings("unused")
34 private static final Logger logger
= Logger
.getLogger(TermBase
.class);
36 @XmlElement(name
= "URI")
39 @XmlElementWrapper(name
= "Representations")
40 @XmlElement(name
= "Representation")
42 @XmlSchemaType(name
= "IDREF")
43 private Set
<Representation
> representations
= new HashSet
<Representation
>();
48 public TermBase(String term
, String label
, String labelAbbrev
) {
50 this.addRepresentation(new Representation(term
, label
, labelAbbrev
, Language
.DEFAULT()) );
53 @OneToMany(fetch
=FetchType
.LAZY
)
54 @Cascade( { CascadeType
.SAVE_UPDATE
, CascadeType
.DELETE
})
55 public Set
<Representation
> getRepresentations() {
56 return this.representations
;
59 public void setRepresentations(Set
<Representation
> representations
) {
60 this.representations
= representations
;
63 public void addRepresentation(Representation representation
) {
64 this.representations
.add(representation
);
67 public void removeRepresentation(Representation representation
) {
68 this.representations
.remove(representation
);
72 public Representation
getRepresentation(Language lang
) {
73 for (Representation repr
: representations
){
74 Language reprLanguage
= repr
.getLanguage();
75 if (reprLanguage
!= null && reprLanguage
.equals(lang
)){
83 * @see #getPreferredRepresentation(Language)
88 public Representation
getPreferredRepresentation(Language language
) {
89 Representation repr
= getRepresentation(language
);
91 repr
= getRepresentation(Language
.DEFAULT());
94 repr
= getRepresentations().iterator().next();
100 * Returns the Representation in the preferred language. Preferred languages
101 * are specified by the parameter languages, which receives a list of
102 * Language instances in the order of preference. If no representation in
103 * any preferred languages is found the method falls back to return the
104 * Representation in Language.DEFAULT() and if nessecary further falls back
105 * to return the first element found.
107 * TODO think about this fall-back strategy!
113 public Representation
getPreferredRepresentation(List
<Language
> languages
) {
114 Representation repr
= null;
115 if(languages
!= null){
116 for(Language language
: languages
) {
117 repr
= getRepresentation(language
);
124 repr
= getRepresentation(Language
.DEFAULT());
127 repr
= getRepresentations().iterator().next();
132 public String
getUri() {
136 public void setUri(String uri
) {
141 public String
getLabel() {
142 if(getLabel(Language
.DEFAULT())!=null){
143 Representation repr
= getRepresentation(Language
.DEFAULT());
144 return (repr
== null)?
null :repr
.getLabel();
146 for (Representation r
: representations
){
150 return super.getUuid().toString();
154 public String
getLabel(Language lang
) {
155 Representation repr
= this.getRepresentation(lang
);
156 return (repr
== null) ?
null : repr
.getLabel();
160 public void setLabel(String label
){
161 Language lang
= Language
.DEFAULT();
162 setLabel(label
, lang
);
166 public void setLabel(String label
, Language language
){
167 if (language
!= null){
168 Representation repr
= getRepresentation(language
);
170 repr
.setLabel(label
);
172 repr
= Representation
.NewInstance(null, label
, null, language
);
174 this.addRepresentation(repr
);
180 public String
getDescription() {
181 return this.getDescription(Language
.DEFAULT());
185 public String
getDescription(Language lang
) {
186 Representation repr
= this.getRepresentation(lang
);
187 return (repr
== null) ?
null :repr
.getDescription();
191 public boolean equals(Object obj
) {
195 if (TermBase
.class.isAssignableFrom(obj
.getClass())){
196 TermBase dtb
= (TermBase
)obj
;
197 if (dtb
.getUuid().equals(this.getUuid())){
205 public String
toString() {
206 //FIXME make toString save as explained in CdmBase.toString
207 return super.toString()+" "+this.getLabel();