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