(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / TermBase.java
1 package eu.etaxonomy.cdm.model.common;
2
3 import java.util.HashSet;
4 import java.util.List;
5 import java.util.Set;
6
7 import javax.persistence.FetchType;
8 import javax.persistence.MappedSuperclass;
9 import javax.persistence.OneToMany;
10 import javax.persistence.Transient;
11
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;
19
20 import org.apache.log4j.Logger;
21 import org.hibernate.annotations.Cascade;
22 import org.hibernate.annotations.CascadeType;
23
24 @XmlAccessorType(XmlAccessType.FIELD)
25 @XmlType(name = "TermBase", propOrder = {
26 "uri",
27 "representations"
28 })
29 @XmlRootElement(name = "TermBase")
30 @MappedSuperclass
31 public abstract class TermBase extends VersionableEntity {
32 private static final Logger logger = Logger.getLogger(TermBase.class);
33
34 @XmlElement(name = "URI")
35 private String uri;
36
37 @XmlElementWrapper(name = "Representations")
38 @XmlElement(name = "Representation")
39 private Set<Representation> representations = new HashSet<Representation>();
40
41 public TermBase(){
42 super();
43 }
44 public TermBase(String term, String label, String labelAbbrev) {
45 super();
46 this.addRepresentation(new Representation(term, label, labelAbbrev, Language.DEFAULT()) );
47 }
48
49 @OneToMany(fetch=FetchType.LAZY)
50 @Cascade( { CascadeType.SAVE_UPDATE, CascadeType.DELETE })
51 public Set<Representation> getRepresentations() {
52 return this.representations;
53 }
54
55 public void setRepresentations(Set<Representation> representations) {
56 this.representations = representations;
57 }
58
59 public void addRepresentation(Representation representation) {
60 this.representations.add(representation);
61 }
62
63 public void removeRepresentation(Representation representation) {
64 this.representations.remove(representation);
65 }
66
67 @Transient
68 public Representation getRepresentation(Language lang) {
69 for (Representation repr : representations){
70 Language reprLanguage = repr.getLanguage();
71 if (reprLanguage != null && reprLanguage.equals(lang)){
72 return repr;
73 }
74 }
75 return null;
76 }
77
78 /**
79 * @see #getPreferredRepresentation(Language)
80 * @param language
81 * @return
82 */
83 @Transient
84 public Representation getPreferredRepresentation(Language language) {
85 Representation repr = getRepresentation(language);
86 if(repr == null){
87 repr = getRepresentation(Language.DEFAULT());
88 }
89 if(repr == null){
90 repr = getRepresentations().iterator().next();
91 }
92 return repr;
93 }
94
95 /**
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.
102 *
103 * TODO think about this fall-back strategy!
104 *
105 * @param languages
106 * @return
107 */
108 @Transient
109 public Representation getPreferredRepresentation(List<Language> languages) {
110 Representation repr = null;
111 if(languages != null){
112 for(Language language : languages) {
113 repr = getRepresentation(language);
114 if(repr != null){
115 return repr;
116 }
117 }
118 }
119 if(repr == null){
120 repr = getRepresentation(Language.DEFAULT());
121 }
122 if(repr == null){
123 repr = getRepresentations().iterator().next();
124 }
125 return repr;
126 }
127
128 public String getUri() {
129 return this.uri;
130 }
131
132 public void setUri(String uri) {
133 this.uri = uri;
134 }
135
136 @Transient
137 public String getLabel() {
138 if(getLabel(Language.DEFAULT())!=null){
139 Representation repr = getRepresentation(Language.DEFAULT());
140 return (repr == null)? null :repr.getLabel();
141 }else{
142 for (Representation r : representations){
143 return r.getLabel();
144 }
145 }
146 return super.getUuid().toString();
147 }
148
149 @Transient
150 public String getLabel(Language lang) {
151 Representation repr = this.getRepresentation(lang);
152 return (repr == null) ? null : repr.getLabel();
153 }
154
155 @Transient
156 public String getDescription() {
157 return this.getDescription(Language.DEFAULT());
158 }
159
160 @Transient
161 public String getDescription(Language lang) {
162 Representation repr = this.getRepresentation(lang);
163 return (repr == null) ? null :repr.getDescription();
164 }
165
166 @Override
167 public boolean equals(Object obj) {
168 if (obj == null){
169 return false;
170 }
171 if (TermBase.class.isAssignableFrom(obj.getClass())){
172 TermBase dtb = (TermBase)obj;
173 if (dtb.getUuid().equals(this.getUuid())){
174 return true;
175 }
176 }
177 return false;
178 }
179
180 @Override
181 public String toString() {
182 //FIXME make toString save as explained in CdmBase.toString
183 return super.toString()+" "+this.getLabel();
184 }
185
186 }