(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / RelationshipTermBase.java
1 package eu.etaxonomy.cdm.model.common;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.Set;
7
8 import javax.persistence.Entity;
9 import javax.persistence.Inheritance;
10 import javax.persistence.InheritanceType;
11 import javax.persistence.JoinTable;
12 import javax.persistence.MappedSuperclass;
13 import javax.persistence.OneToMany;
14 import javax.persistence.Table;
15 import javax.persistence.Transient;
16
17 import org.apache.log4j.Logger;
18 import org.hibernate.annotations.Cascade;
19 import org.hibernate.annotations.CascadeType;
20
21 import au.com.bytecode.opencsv.CSVWriter;
22
23 @MappedSuperclass
24 public abstract class RelationshipTermBase extends OrderedTermBase {
25 static Logger logger = Logger.getLogger(RelationshipTermBase.class);
26
27 private boolean symmetric;
28 private boolean transitive;
29 private Set<Representation> inverseRepresentations = new HashSet();
30
31 public RelationshipTermBase() {
32 super();
33 }
34 public RelationshipTermBase(String term, String label, boolean symmetric, boolean transitive) {
35 super(term, label);
36 setSymmetric(symmetric);
37 setTransitive(transitive);
38 }
39
40
41 public boolean isSymmetric() {
42 return symmetric;
43 }
44 public void setSymmetric(boolean symmetric) {
45 this.symmetric = symmetric;
46 }
47
48 public boolean isTransitive() {
49 return transitive;
50 }
51 public void setTransitive(boolean transitive) {
52 this.transitive = transitive;
53 }
54
55
56 @OneToMany
57 @JoinTable(name="RelationshipTermBase_inverseRepresentation")
58 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
59 public Set<Representation> getInverseRepresentations() {
60 return inverseRepresentations;
61 }
62 protected void setInverseRepresentations(
63 Set<Representation> inverseRepresentations) {
64 this.inverseRepresentations = inverseRepresentations;
65 }
66 public void addInverseRepresentation(Representation inverseRepresentation) {
67 this.inverseRepresentations.add(inverseRepresentation);
68 }
69 public void removeInverseRepresentation(Representation inverseRepresentation) {
70 this.inverseRepresentations.remove(inverseRepresentation);
71 }
72 public void addRepresentation(Representation representation, Representation inverseRepresentation) {
73 this.addRepresentation(representation);
74 this.addInverseRepresentation(inverseRepresentation);
75 }
76
77 @Transient
78 public Representation getInverseRepresentation(Language lang) {
79 Representation result = null;
80 if (this.isSymmetric()){
81 for (Representation repr : this.getRepresentations()){
82 if (repr.getLanguage() == lang){
83 result = repr;
84 }
85 }
86 }else{
87 for (Representation repr : this.getInverseRepresentations()){
88 if (repr.getLanguage() == lang){
89 result = repr;
90 }
91 }
92 }
93 return result;
94 }
95
96 /*
97 * Inverse representation convenience methods similar to TermBase.xxx
98 * @see eu.etaxonomy.cdm.model.common.TermBase#getLabel()
99 */
100 @Transient
101 public String getInverseLabel() {
102 if(getInverseLabel(Language.DEFAULT())!=null){
103 return this.getInverseRepresentation(Language.DEFAULT()).getLabel();
104 }else{
105 for (Representation r : inverseRepresentations){
106 return r.getLabel();
107 }
108 }
109 return super.getUuid().toString();
110 }
111
112 @Transient
113 public String getInverseLabel(Language lang) {
114 Representation r = this.getInverseRepresentation(lang);
115 if(r==null){
116 return null;
117 }else{
118 return r.getLabel();
119 }
120 }
121
122 @Transient
123 public String getInverseDescription() {
124 return this.getInverseRepresentation(Language.DEFAULT()).getDescription();
125 }
126
127 @Transient
128 public String getInverseDescription(Language lang) {
129 return this.getInverseRepresentation(lang).getDescription();
130 }
131
132 public void readCsvLine(List<String> csvLine) {
133 // read UUID, URI, english label+description
134 super.readCsvLine(csvLine);
135 // inverse label + 2 booleans
136 this.addInverseRepresentation(new Representation(csvLine.get(4).trim(), csvLine.get(5).trim(), Language.ENGLISH()) );
137 this.setSymmetric(Boolean.parseBoolean(csvLine.get(6)));
138 this.setTransitive(Boolean.parseBoolean(csvLine.get(7)));
139 }
140
141 public void writeCsvLine(CSVWriter writer) {
142 String [] line = new String[8];
143 line[0] = getUuid().toString();
144 line[1] = getUri();
145 line[2] = getLabel();
146 line[3] = getDescription();
147 line[4] = getInverseLabel();
148 line[5] = getInverseDescription();
149 line[6] = String.valueOf(this.isSymmetric());
150 line[7] = String.valueOf(this.isTransitive());
151 writer.writeNext(line);
152 }
153
154 }