1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.model.common;
|
11
|
|
12
|
import java.util.HashSet;
|
13
|
import java.util.Iterator;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import javax.persistence.Column;
|
20
|
import javax.persistence.Entity;
|
21
|
import javax.persistence.FetchType;
|
22
|
import javax.persistence.JoinTable;
|
23
|
import javax.persistence.OneToMany;
|
24
|
import javax.persistence.Transient;
|
25
|
import javax.xml.bind.annotation.XmlAccessType;
|
26
|
import javax.xml.bind.annotation.XmlAccessorType;
|
27
|
import javax.xml.bind.annotation.XmlElement;
|
28
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
29
|
import javax.xml.bind.annotation.XmlSeeAlso;
|
30
|
import javax.xml.bind.annotation.XmlType;
|
31
|
|
32
|
import org.apache.log4j.Logger;
|
33
|
import org.hibernate.annotations.Cascade;
|
34
|
import org.hibernate.annotations.CascadeType;
|
35
|
import org.hibernate.envers.Audited;
|
36
|
import org.hibernate.search.annotations.Field;
|
37
|
import org.hibernate.search.annotations.Index;
|
38
|
import org.hibernate.search.annotations.Indexed;
|
39
|
import org.hibernate.search.annotations.IndexedEmbedded;
|
40
|
|
41
|
import au.com.bytecode.opencsv.CSVWriter;
|
42
|
import eu.etaxonomy.cdm.model.description.TextData;
|
43
|
import eu.etaxonomy.cdm.model.name.HybridRelationshipType;
|
44
|
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
|
45
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
|
46
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
47
|
|
48
|
@XmlAccessorType(XmlAccessType.FIELD)
|
49
|
@XmlType(name = "RelationshipTermBase", propOrder = {
|
50
|
"symmetric",
|
51
|
"transitive",
|
52
|
"inverseRepresentations"
|
53
|
})
|
54
|
@XmlSeeAlso({
|
55
|
HybridRelationshipType.class,
|
56
|
NameRelationshipType.class,
|
57
|
SynonymRelationshipType.class,
|
58
|
TaxonRelationshipType.class
|
59
|
})
|
60
|
@Entity
|
61
|
@Indexed(index = "eu.etaxonomy.cdm.model.common.DefinedTermBase")
|
62
|
@Audited
|
63
|
public abstract class RelationshipTermBase<T extends RelationshipTermBase> extends OrderedTermBase<T> {
|
64
|
private static final long serialVersionUID = 5497187985269083971L;
|
65
|
@SuppressWarnings("unused")
|
66
|
private static final Logger logger = Logger.getLogger(RelationshipTermBase.class);
|
67
|
|
68
|
@XmlElement(name = "Symmetrical")
|
69
|
@Field(index=Index.UN_TOKENIZED)
|
70
|
@Column(name="symmetrical") //to be compatible with PostGreSQL
|
71
|
private boolean symmetric;
|
72
|
|
73
|
@XmlElement(name = "Transitive")
|
74
|
@Field(index=Index.UN_TOKENIZED)
|
75
|
private boolean transitive;
|
76
|
|
77
|
@XmlElementWrapper(name = "InverseRepresentations")
|
78
|
@XmlElement(name = "Representation")
|
79
|
@OneToMany(fetch = FetchType.LAZY)
|
80
|
@JoinTable(name="RelationshipTermBase_inverseRepresentation")
|
81
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
|
82
|
@IndexedEmbedded(depth = 2)
|
83
|
private Set<Representation> inverseRepresentations = new HashSet<Representation>();
|
84
|
|
85
|
public RelationshipTermBase() {
|
86
|
}
|
87
|
public RelationshipTermBase(String term, String label, String labelAbbrev, boolean symmetric, boolean transitive) {
|
88
|
super(term, label, labelAbbrev);
|
89
|
setSymmetric(symmetric);
|
90
|
setTransitive(transitive);
|
91
|
}
|
92
|
|
93
|
|
94
|
public boolean isSymmetric() {
|
95
|
return symmetric;
|
96
|
}
|
97
|
public void setSymmetric(boolean symmetric) {
|
98
|
this.symmetric = symmetric;
|
99
|
}
|
100
|
|
101
|
public boolean isTransitive() {
|
102
|
return transitive;
|
103
|
}
|
104
|
public void setTransitive(boolean transitive) {
|
105
|
this.transitive = transitive;
|
106
|
}
|
107
|
|
108
|
public Set<Representation> getInverseRepresentations() {
|
109
|
return inverseRepresentations;
|
110
|
}
|
111
|
|
112
|
public void addInverseRepresentation(Representation inverseRepresentation) {
|
113
|
this.inverseRepresentations.add(inverseRepresentation);
|
114
|
}
|
115
|
public void removeInverseRepresentation(Representation inverseRepresentation) {
|
116
|
this.inverseRepresentations.remove(inverseRepresentation);
|
117
|
}
|
118
|
public void addRepresentation(Representation representation, Representation inverseRepresentation) {
|
119
|
this.addRepresentation(representation);
|
120
|
this.addInverseRepresentation(inverseRepresentation);
|
121
|
}
|
122
|
|
123
|
public Representation getInverseRepresentation(Language lang) {
|
124
|
Representation result = null;
|
125
|
if (this.isSymmetric()){
|
126
|
for (Representation repr : this.getRepresentations()){
|
127
|
if (lang.equals(repr.getLanguage())){
|
128
|
result = repr;
|
129
|
}
|
130
|
}
|
131
|
}else{
|
132
|
for (Representation repr : this.getInverseRepresentations()){
|
133
|
if (lang.equals(repr.getLanguage())){
|
134
|
result = repr;
|
135
|
}
|
136
|
}
|
137
|
}
|
138
|
return result;
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* Returns the InverseRepresentation in the preferred language. Preferred languages
|
143
|
* are specified by the parameter languages, which receives a list of
|
144
|
* Language instances in the order of preference. If no representation in
|
145
|
* any preferred languages is found the method falls back to return the
|
146
|
* Representation in Language.DEFAULT() and if necessary further falls back
|
147
|
* to return the first element found if any.
|
148
|
*
|
149
|
* TODO think about this fall-back strategy &
|
150
|
* see also {@link TextData#getPreferredLanguageString(List)}
|
151
|
* see also {@link TermBase#getPreferredRepresentation(List)}
|
152
|
*
|
153
|
* @param languages
|
154
|
* @return
|
155
|
*/
|
156
|
public Representation getPreferredInverseRepresentation(List<Language> languages) {
|
157
|
Representation repr = null;
|
158
|
if(languages != null){
|
159
|
for(Language language : languages) {
|
160
|
repr = getInverseRepresentation(language);
|
161
|
if(repr != null){
|
162
|
return repr;
|
163
|
}
|
164
|
}
|
165
|
}
|
166
|
if(repr == null){
|
167
|
repr = getInverseRepresentation(Language.DEFAULT());
|
168
|
}
|
169
|
if(repr == null){
|
170
|
Iterator<Representation> it = getInverseRepresentations().iterator();
|
171
|
if(it.hasNext()){
|
172
|
repr = getInverseRepresentations().iterator().next();
|
173
|
}
|
174
|
}
|
175
|
return repr;
|
176
|
}
|
177
|
|
178
|
/*
|
179
|
* Inverse representation convenience methods similar to TermBase.xxx
|
180
|
* @see eu.etaxonomy.cdm.model.common.TermBase#getLabel()
|
181
|
*/
|
182
|
@Transient
|
183
|
public String getInverseLabel() {
|
184
|
if(getInverseLabel(Language.DEFAULT())!=null){
|
185
|
return this.getInverseRepresentation(Language.DEFAULT()).getLabel();
|
186
|
}else{
|
187
|
for (Representation r : inverseRepresentations){
|
188
|
return r.getLabel();
|
189
|
}
|
190
|
}
|
191
|
return super.getUuid().toString();
|
192
|
}
|
193
|
|
194
|
public String getInverseLabel(Language lang) {
|
195
|
Representation r = this.getInverseRepresentation(lang);
|
196
|
if(r==null){
|
197
|
return null;
|
198
|
}else{
|
199
|
return r.getLabel();
|
200
|
}
|
201
|
}
|
202
|
|
203
|
@Transient
|
204
|
public String getInverseDescription() {
|
205
|
return this.getInverseRepresentation(Language.DEFAULT()).getDescription();
|
206
|
}
|
207
|
|
208
|
public String getInverseDescription(Language lang) {
|
209
|
return this.getInverseRepresentation(lang).getDescription();
|
210
|
}
|
211
|
|
212
|
@Override
|
213
|
public T readCsvLine(Class<T> termClass, List<String> csvLine, Map<UUID,DefinedTermBase> terms) {
|
214
|
T newInstance = super.readCsvLine(termClass, csvLine, terms);
|
215
|
|
216
|
String inverseText = csvLine.get(5).trim();
|
217
|
String inverseLabel = csvLine.get(4).trim();
|
218
|
String inverseLabelAbbrev = null;
|
219
|
newInstance.addInverseRepresentation(new Representation(inverseText, inverseLabel, inverseLabelAbbrev, Language.CSV_LANGUAGE()) );
|
220
|
newInstance.setSymmetric(Boolean.parseBoolean(csvLine.get(6)));
|
221
|
newInstance.setTransitive(Boolean.parseBoolean(csvLine.get(7)));
|
222
|
return newInstance;
|
223
|
}
|
224
|
|
225
|
@Override
|
226
|
public void writeCsvLine(CSVWriter writer,T term) {
|
227
|
String [] line = new String[8];
|
228
|
line[0] = term.getUuid().toString();
|
229
|
line[1] = term.getUri();
|
230
|
line[2] = term.getLabel();
|
231
|
line[3] = term.getDescription();
|
232
|
line[4] = term.getInverseLabel();
|
233
|
line[5] = term.getInverseDescription();
|
234
|
line[6] = String.valueOf(term.isSymmetric());
|
235
|
line[7] = String.valueOf(term.isTransitive());
|
236
|
writer.writeNext(line);
|
237
|
}
|
238
|
|
239
|
}
|