(no commit message)
[cdmlib.git] / cdmlibrary / src / main / java / eu / etaxonomy / cdm / model / common / RelationshipTermBase.java
1 package eu.etaxonomy.cdm.model.common;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import javax.persistence.Entity;
7 import javax.persistence.Inheritance;
8 import javax.persistence.InheritanceType;
9 import javax.persistence.MappedSuperclass;
10 import javax.persistence.OneToMany;
11 import javax.persistence.Transient;
12
13 import org.apache.log4j.Logger;
14 import org.hibernate.annotations.Cascade;
15 import org.hibernate.annotations.CascadeType;
16
17 @MappedSuperclass
18 public abstract class RelationshipTermBase extends OrderedTermBase {
19 static Logger logger = Logger.getLogger(RelationshipTermBase.class);
20
21 private boolean symmetric;
22 private boolean transitive;
23 private Set<Representation> inverseRepresentations = new HashSet();
24
25 public RelationshipTermBase() {
26 super();
27 }
28 public RelationshipTermBase(String term, String label, boolean symmetric, boolean transitive) {
29 super(term, label);
30 setSymmetric(symmetric);
31 setTransitive(transitive);
32 }
33
34
35 public boolean isSymmetric() {
36 return symmetric;
37 }
38 public void setSymmetric(boolean symmetric) {
39 this.symmetric = symmetric;
40 }
41
42 public boolean isTransitive() {
43 return transitive;
44 }
45 public void setTransitive(boolean transitive) {
46 this.transitive = transitive;
47 }
48
49
50 @OneToMany
51 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
52 public Set<Representation> getInverseRepresentations() {
53 return inverseRepresentations;
54 }
55 protected void setInverseRepresentations(
56 Set<Representation> inverseRepresentations) {
57 this.inverseRepresentations = inverseRepresentations;
58 }
59 public void addRepresentation(Representation representation) {
60 this.inverseRepresentations.add(representation);
61 }
62 public void removeRepresentation(Representation representation) {
63 this.inverseRepresentations.remove(representation);
64 }
65
66 @Transient
67 public Representation getInverseRepresentation(Language lang) {
68 Representation result = null;
69 if (this.isSymmetric()){
70 for (Representation repr : this.getRepresentations()){
71 if (repr.getLanguage() == lang){
72 result = repr;
73 }
74 }
75 }else{
76 for (Representation repr : this.getInverseRepresentations()){
77 if (repr.getLanguage() == lang){
78 result = repr;
79 }
80 }
81 }
82 return result;
83 }
84 }