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
;
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
.XmlRootElement
;
17 import javax
.xml
.bind
.annotation
.XmlTransient
;
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 Logger logger
= Logger
.getLogger(TermBase
.class);
34 @XmlElement(name
= "URI")
37 @XmlElementWrapper(name
= "Representations")
38 @XmlElement(name
= "Representation")
39 private Set
<Representation
> representations
= new HashSet
<Representation
>();
44 public TermBase(String term
, String label
, String labelAbbrev
) {
46 this.addRepresentation(new Representation(term
, label
, labelAbbrev
, Language
.DEFAULT()) );
49 @OneToMany(fetch
=FetchType
.LAZY
)
50 @Cascade( { CascadeType
.SAVE_UPDATE
, CascadeType
.DELETE
})
51 public Set
<Representation
> getRepresentations() {
52 return this.representations
;
55 public void setRepresentations(Set
<Representation
> representations
) {
56 this.representations
= representations
;
59 public void addRepresentation(Representation representation
) {
60 this.representations
.add(representation
);
63 public void removeRepresentation(Representation representation
) {
64 this.representations
.remove(representation
);
68 public Representation
getRepresentation(Language lang
) {
69 for (Representation repr
: representations
){
70 Language reprLanguage
= repr
.getLanguage();
71 if (reprLanguage
!= null && reprLanguage
.equals(lang
)){
79 * @see #getPreferredRepresentation(Language)
84 public Representation
getPreferredRepresentation(Language language
) {
85 Representation repr
= getRepresentation(language
);
87 repr
= getRepresentation(Language
.DEFAULT());
90 repr
= getRepresentations().iterator().next();
96 * Returns the Representation in the preferred language. Preferred languages
97 * are specified by the parameter languages, which receives a list of
98 * Language instances in the order of preference. If no representation in
99 * any preferred languages is found the method falls back to return the
100 * Representation in Language.DEFAULT() and if nessecary further falls back
101 * to return the first element found.
103 * TODO think about this fall-back strategy!
109 public Representation
getPreferredRepresentation(List
<Language
> languages
) {
110 Representation repr
= null;
111 if(languages
!= null){
112 for(Language language
: languages
) {
113 repr
= getRepresentation(language
);
120 repr
= getRepresentation(Language
.DEFAULT());
123 repr
= getRepresentations().iterator().next();
128 public String
getUri() {
132 public void setUri(String uri
) {
137 public String
getLabel() {
138 if(getLabel(Language
.DEFAULT())!=null){
139 Representation repr
= getRepresentation(Language
.DEFAULT());
140 return (repr
== null)?
null :repr
.getLabel();
142 for (Representation r
: representations
){
146 return super.getUuid().toString();
150 public String
getLabel(Language lang
) {
151 Representation repr
= this.getRepresentation(lang
);
152 return (repr
== null) ?
null : repr
.getLabel();
156 public String
getDescription() {
157 return this.getDescription(Language
.DEFAULT());
161 public String
getDescription(Language lang
) {
162 Representation repr
= this.getRepresentation(lang
);
163 return (repr
== null) ?
null :repr
.getDescription();
167 public boolean equals(Object obj
) {
171 if (TermBase
.class.isAssignableFrom(obj
.getClass())){
172 TermBase dtb
= (TermBase
)obj
;
173 if (dtb
.getUuid().equals(this.getUuid())){
181 public String
toString() {
182 //FIXME make toString save as explained in CdmBase.toString
183 return super.toString()+" "+this.getLabel();