JAXB annotations - first drop
[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.Set;
5
6 import javax.persistence.FetchType;
7 import javax.persistence.MappedSuperclass;
8 import javax.persistence.OneToMany;
9 import javax.persistence.Transient;
10
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.XmlRootElement;
16 import javax.xml.bind.annotation.XmlTransient;
17 import javax.xml.bind.annotation.XmlType;
18
19 import org.apache.log4j.Logger;
20 import org.hibernate.annotations.Cascade;
21 import org.hibernate.annotations.CascadeType;
22
23 @XmlAccessorType(XmlAccessType.FIELD)
24 @XmlType(name = "TermBase", propOrder = {
25 "uri",
26 "representations",
27 })
28 @XmlRootElement(name = "TermBase")
29 @MappedSuperclass
30 public abstract class TermBase extends VersionableEntity {
31 private static final Logger logger = Logger.getLogger(TermBase.class);
32
33 @XmlElement(name = "URI")
34 private String uri;
35
36 @XmlElementWrapper(name = "Representations")
37 @XmlElement(name = "Representation")
38 private Set<Representation> representations = new HashSet<Representation>();
39
40 public TermBase(){
41 super();
42 }
43 public TermBase(String term, String label, String labelAbbrev) {
44 super();
45 this.addRepresentation(new Representation(term, label, labelAbbrev, Language.DEFAULT()) );
46 }
47
48 @OneToMany(fetch=FetchType.LAZY)
49 @Cascade( { CascadeType.SAVE_UPDATE, CascadeType.DELETE })
50 public Set<Representation> getRepresentations() {
51 return this.representations;
52 }
53
54 public void setRepresentations(Set<Representation> representations) {
55 this.representations = representations;
56 }
57
58 public void addRepresentation(Representation representation) {
59 this.representations.add(representation);
60 }
61
62 public void removeRepresentation(Representation representation) {
63 this.representations.remove(representation);
64 }
65
66 @Transient
67 public Representation getRepresentation(Language lang) {
68 for (Representation repr : representations){
69 Language reprLanguage = repr.getLanguage();
70 if (reprLanguage != null && reprLanguage.equals(lang)){
71 return repr;
72 }
73 }
74 return null;
75 }
76
77 public String getUri() {
78 return this.uri;
79 }
80
81 public void setUri(String uri) {
82 this.uri = uri;
83 }
84
85 @Transient
86 public String getLabel() {
87 if(getLabel(Language.DEFAULT())!=null){
88 Representation repr = getRepresentation(Language.DEFAULT());
89 return (repr == null)? null :repr.getLabel();
90 }else{
91 for (Representation r : representations){
92 return r.getLabel();
93 }
94 }
95 return super.getUuid().toString();
96 }
97
98 @Transient
99 public String getLabel(Language lang) {
100 Representation repr = this.getRepresentation(lang);
101 return (repr == null) ? null : repr.getLabel();
102 }
103
104 @Transient
105 public String getDescription() {
106 return this.getDescription(Language.DEFAULT());
107 }
108
109 @Transient
110 public String getDescription(Language lang) {
111 Representation repr = this.getRepresentation(lang);
112 return (repr == null) ? null :repr.getDescription();
113 }
114
115 @Override
116 public boolean equals(Object obj) {
117 if (obj == null){
118 return false;
119 }
120 if (TermBase.class.isAssignableFrom(obj.getClass())){
121 TermBase dtb = (TermBase)obj;
122 if (dtb.getUuid().equals(this.getUuid())){
123 return true;
124 }
125 }
126 return false;
127 }
128
129 @Override
130 public String toString() {
131 //FIXME make toString save as explained in CdmBase.toString
132 return super.toString()+" "+this.getLabel();
133 }
134
135 }