(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.Set;
5
6 import javax.persistence.FetchType;
7 import javax.persistence.JoinTable;
8 import javax.persistence.MappedSuperclass;
9 import javax.persistence.OneToMany;
10 import javax.persistence.Transient;
11
12 import org.apache.log4j.Logger;
13 import org.hibernate.annotations.Cascade;
14 import org.hibernate.annotations.CascadeType;
15
16 @MappedSuperclass
17 public abstract class TermBase extends VersionableEntity {
18 static Logger logger = Logger.getLogger(TermBase.class);
19
20 private String uri;
21 private Set<Representation> representations = new HashSet<Representation>();
22
23 public TermBase() {
24 super();
25 }
26 public TermBase(String term, String label) {
27 super();
28 this.addRepresentation(new Representation(term, label, Language.DEFAULT()) );
29 }
30
31 @OneToMany//(fetch=FetchType.EAGER)
32 @Cascade( { CascadeType.SAVE_UPDATE, CascadeType.DELETE })
33 public Set<Representation> getRepresentations() {
34 return this.representations;
35 }
36
37 public void setRepresentations(Set<Representation> representations) {
38 this.representations = representations;
39 }
40
41 public void addRepresentation(Representation representation) {
42 this.representations.add(representation);
43 }
44
45 public void removeRepresentation(Representation representation) {
46 this.representations.remove(representation);
47 }
48
49 @Transient
50 public Representation getRepresentation(Language lang) {
51 for (Representation repr : representations){
52 Language reprLanguage = repr.getLanguage();
53 if (reprLanguage != null && reprLanguage.equals(lang)){
54 return repr;
55 }
56 }
57 return null;
58 }
59
60 public String getUri() {
61 return this.uri;
62 }
63
64 public void setUri(String uri) {
65 this.uri = uri;
66 }
67
68 @Transient
69 public String getLabel() {
70 if(getLabel(Language.DEFAULT())!=null){
71 Representation repr = getRepresentation(Language.DEFAULT());
72 return (repr == null)? null :repr.getLabel();
73 }else{
74 for (Representation r : representations){
75 return r.getLabel();
76 }
77 }
78 return super.getUuid().toString();
79 }
80
81 @Transient
82 public String getLabel(Language lang) {
83 Representation repr = this.getRepresentation(lang);
84 return (repr == null) ? null : repr.getLabel();
85 }
86
87 @Transient
88 public String getDescription() {
89 return this.getDescription(Language.DEFAULT());
90 }
91
92 @Transient
93 public String getDescription(Language lang) {
94 Representation repr = this.getRepresentation(lang);
95 return (repr == null) ? null :repr.getDescription();
96 }
97
98 @Override
99 public boolean equals(Object obj) {
100 if (DefinedTermBase.class.isAssignableFrom(obj.getClass())){
101 DefinedTermBase dtb = (DefinedTermBase)obj;
102 if (dtb.getUuid().equals(this.getUuid())){
103 return true;
104 }
105 }
106 return false;
107 }
108
109 @Override
110 public String toString() {
111 return super.toString()+" "+this.getLabel();
112 }
113
114 }