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.JoinColumn;
|
23
|
import javax.persistence.JoinTable;
|
24
|
import javax.persistence.OneToMany;
|
25
|
import javax.persistence.Transient;
|
26
|
import javax.xml.bind.annotation.XmlAccessType;
|
27
|
import javax.xml.bind.annotation.XmlAccessorType;
|
28
|
import javax.xml.bind.annotation.XmlElement;
|
29
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
30
|
import javax.xml.bind.annotation.XmlSeeAlso;
|
31
|
import javax.xml.bind.annotation.XmlType;
|
32
|
|
33
|
import org.apache.log4j.Logger;
|
34
|
import org.hibernate.annotations.Cascade;
|
35
|
import org.hibernate.annotations.CascadeType;
|
36
|
import org.hibernate.envers.Audited;
|
37
|
import org.hibernate.search.annotations.Analyze;
|
38
|
import org.hibernate.search.annotations.Field;
|
39
|
import org.hibernate.search.annotations.IndexedEmbedded;
|
40
|
|
41
|
import au.com.bytecode.opencsv.CSVWriter;
|
42
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
43
|
import eu.etaxonomy.cdm.model.description.TextData;
|
44
|
import eu.etaxonomy.cdm.model.name.HybridRelationshipType;
|
45
|
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
|
46
|
import eu.etaxonomy.cdm.model.taxon.SynonymType;
|
47
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
48
|
|
49
|
@XmlAccessorType(XmlAccessType.FIELD)
|
50
|
@XmlType(name = "RelationshipTermBase", propOrder = {
|
51
|
"symmetric",
|
52
|
"transitive",
|
53
|
"inverseRepresentations",
|
54
|
"inverseSymbol"
|
55
|
})
|
56
|
@XmlSeeAlso({
|
57
|
HybridRelationshipType.class,
|
58
|
NameRelationshipType.class,
|
59
|
SynonymType.class,
|
60
|
TaxonRelationshipType.class
|
61
|
})
|
62
|
@Entity
|
63
|
@Audited
|
64
|
public abstract class RelationshipTermBase<T extends RelationshipTermBase<T>>
|
65
|
extends OrderedTermBase<T> {
|
66
|
private static final long serialVersionUID = 5497187985269083971L;
|
67
|
@SuppressWarnings("unused")
|
68
|
private static final Logger logger = Logger.getLogger(RelationshipTermBase.class);
|
69
|
|
70
|
@XmlElement(name = "Symmetrical")
|
71
|
@Field(analyze = Analyze.NO)
|
72
|
@Column(name="symmetrical") //to be compatible with PostGreSQL
|
73
|
private boolean symmetric;
|
74
|
|
75
|
@XmlElement(name = "Transitive")
|
76
|
@Field(analyze = Analyze.NO)
|
77
|
private boolean transitive;
|
78
|
|
79
|
@XmlElementWrapper(name = "InverseRepresentations")
|
80
|
@XmlElement(name = "Representation")
|
81
|
@OneToMany(fetch = FetchType.EAGER, orphanRemoval=true) //eager loading same as TermBase.representations
|
82
|
@JoinTable(name="DefinedTermBase_InverseRepresentation", //see also Feature.inverseRepresentations
|
83
|
joinColumns=@JoinColumn(name="DefinedTermBase_id")
|
84
|
// inverseJoinColumns
|
85
|
)
|
86
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
|
87
|
@IndexedEmbedded(depth = 2)
|
88
|
private Set<Representation> inverseRepresentations = new HashSet<>();
|
89
|
|
90
|
@XmlElement(name = "inverseSymbol")
|
91
|
@Column(length=30)
|
92
|
//the symbol to be used in String representations for the reverse representation of this term #5734
|
93
|
//this term can be changed by the database instance even if the term is not managed by this instance
|
94
|
//as it is only for representation and has no semantic or identifying character
|
95
|
//empty string is explicitly allowed and should be distinguished from NULL!
|
96
|
private String inverseSymbol;
|
97
|
|
98
|
//******************** CONSTRUCTOR ************************/
|
99
|
|
100
|
//for JAXB only, TODO needed?
|
101
|
@Deprecated
|
102
|
private RelationshipTermBase(){super();}
|
103
|
|
104
|
|
105
|
protected RelationshipTermBase(TermType type) {
|
106
|
super(type);
|
107
|
}
|
108
|
public RelationshipTermBase(TermType type, String term, String label, String labelAbbrev, boolean symmetric, boolean transitive) {
|
109
|
super(type, term, label, labelAbbrev);
|
110
|
setSymmetric(symmetric);
|
111
|
setTransitive(transitive);
|
112
|
}
|
113
|
|
114
|
//************************** METHODS ********************************
|
115
|
|
116
|
public boolean isSymmetric() {
|
117
|
return symmetric;
|
118
|
}
|
119
|
public void setSymmetric(boolean symmetric) {
|
120
|
this.symmetric = symmetric;
|
121
|
}
|
122
|
|
123
|
public boolean isTransitive() {
|
124
|
return transitive;
|
125
|
}
|
126
|
public void setTransitive(boolean transitive) {
|
127
|
this.transitive = transitive;
|
128
|
}
|
129
|
|
130
|
public Set<Representation> getInverseRepresentations() {
|
131
|
return inverseRepresentations;
|
132
|
}
|
133
|
|
134
|
public void addInverseRepresentation(Representation inverseRepresentation) {
|
135
|
this.inverseRepresentations.add(inverseRepresentation);
|
136
|
}
|
137
|
public void removeInverseRepresentation(Representation inverseRepresentation) {
|
138
|
this.inverseRepresentations.remove(inverseRepresentation);
|
139
|
}
|
140
|
public void addRepresentation(Representation representation, Representation inverseRepresentation) {
|
141
|
this.addRepresentation(representation);
|
142
|
this.addInverseRepresentation(inverseRepresentation);
|
143
|
}
|
144
|
|
145
|
public Representation getInverseRepresentation(Language lang) {
|
146
|
if (lang == null){
|
147
|
return null;
|
148
|
}
|
149
|
Representation result = null;
|
150
|
if (this.isSymmetric()){
|
151
|
for (Representation repr : this.getRepresentations()){
|
152
|
if (repr != null && lang.equals(repr.getLanguage())){
|
153
|
result = repr;
|
154
|
}
|
155
|
}
|
156
|
}else{
|
157
|
for (Representation repr : this.getInverseRepresentations()){
|
158
|
if (repr != null && lang.equals(repr.getLanguage())){
|
159
|
result = repr;
|
160
|
}
|
161
|
}
|
162
|
}
|
163
|
return result;
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* Returns the InverseRepresentation in the preferred language. Preferred languages
|
168
|
* are specified by the parameter languages, which receives a list of
|
169
|
* Language instances in the order of preference. If no representation in
|
170
|
* any preferred languages is found the method falls back to return the
|
171
|
* Representation in Language.DEFAULT() and if necessary further falls back
|
172
|
* to return the first element found if any.
|
173
|
*
|
174
|
* TODO think about this fall-back strategy &
|
175
|
* see also {@link TextData#getPreferredLanguageString(List)}
|
176
|
* see also {@link TermBase#getPreferredRepresentation(List)}
|
177
|
*
|
178
|
* @param languages
|
179
|
* @return
|
180
|
*/
|
181
|
public Representation getPreferredInverseRepresentation(List<Language> languages) {
|
182
|
Representation repr = null;
|
183
|
if(languages != null){
|
184
|
for(Language language : languages) {
|
185
|
repr = getInverseRepresentation(language);
|
186
|
if(repr != null){
|
187
|
return repr;
|
188
|
}
|
189
|
}
|
190
|
}
|
191
|
if(repr == null){
|
192
|
repr = getInverseRepresentation(Language.DEFAULT());
|
193
|
}
|
194
|
if(repr == null){
|
195
|
Iterator<Representation> it = getInverseRepresentations().iterator();
|
196
|
if(it.hasNext()){
|
197
|
repr = getInverseRepresentations().iterator().next();
|
198
|
}
|
199
|
}
|
200
|
return repr;
|
201
|
}
|
202
|
|
203
|
/*
|
204
|
* Inverse representation convenience methods similar to TermBase.xxx
|
205
|
* @see eu.etaxonomy.cdm.model.common.TermBase#getLabel()
|
206
|
*/
|
207
|
@Transient
|
208
|
public String getInverseLabel() {
|
209
|
if(getInverseLabel(Language.DEFAULT())!=null){
|
210
|
return this.getInverseRepresentation(Language.DEFAULT()).getLabel();
|
211
|
}else{
|
212
|
for (Representation r : inverseRepresentations){
|
213
|
return r.getLabel();
|
214
|
}
|
215
|
}
|
216
|
return super.getUuid().toString();
|
217
|
}
|
218
|
|
219
|
public String getInverseLabel(Language lang) {
|
220
|
Representation r = this.getInverseRepresentation(lang);
|
221
|
if(r==null){
|
222
|
return null;
|
223
|
}else{
|
224
|
return r.getLabel();
|
225
|
}
|
226
|
}
|
227
|
|
228
|
@Transient
|
229
|
public String getInverseDescription() {
|
230
|
return this.getInverseRepresentation(Language.DEFAULT()).getDescription();
|
231
|
}
|
232
|
|
233
|
public String getInverseDescription(Language lang) {
|
234
|
return this.getInverseRepresentation(lang).getDescription();
|
235
|
}
|
236
|
|
237
|
public String getInverseSymbol() {
|
238
|
return inverseSymbol;
|
239
|
}
|
240
|
public void setInverseSymbol(String inverseSymbol) {
|
241
|
this.inverseSymbol = inverseSymbol;
|
242
|
}
|
243
|
|
244
|
//**************** CSV *************************/
|
245
|
|
246
|
@Override
|
247
|
public T readCsvLine(Class<T> termClass, List<String> csvLine, TermType termType, Map<UUID,DefinedTermBase> terms, boolean abbrevAsId) {
|
248
|
T newInstance = super.readCsvLine(termClass, csvLine, termType, terms, abbrevAsId);
|
249
|
|
250
|
String inverseText = CdmUtils.Ne(csvLine.get(6).trim());
|
251
|
String inverseLabel = csvLine.get(5).trim();
|
252
|
String inverseLabelAbbrev = CdmUtils.Ne(csvLine.get(7).trim());
|
253
|
newInstance.addInverseRepresentation(new Representation(inverseText, inverseLabel, inverseLabelAbbrev, Language.CSV_LANGUAGE()) );
|
254
|
newInstance.setSymmetric(Boolean.parseBoolean(csvLine.get(8)));
|
255
|
newInstance.setTransitive(Boolean.parseBoolean(csvLine.get(9)));
|
256
|
return newInstance;
|
257
|
}
|
258
|
|
259
|
@Override
|
260
|
public void writeCsvLine(CSVWriter writer,T term) {
|
261
|
String [] line = new String[8];
|
262
|
line[0] = term.getUuid().toString();
|
263
|
line[1] = term.getUri().toString();
|
264
|
line[2] = term.getLabel();
|
265
|
line[3] = term.getDescription();
|
266
|
line[4] = term.getDescription();
|
267
|
line[5] = term.getInverseLabel();
|
268
|
line[6] = term.getInverseDescription();
|
269
|
line[7] = String.valueOf(term.isSymmetric());
|
270
|
line[8] = String.valueOf(term.isTransitive());
|
271
|
writer.writeNext(line);
|
272
|
}
|
273
|
//*********************************** CLONE *********************************************************/
|
274
|
|
275
|
/* (non-Javadoc)
|
276
|
* @see eu.etaxonomy.cdm.model.common.DefinedTermBase#clone()
|
277
|
* @see java.lang.Object#clone()
|
278
|
*/
|
279
|
@Override
|
280
|
public Object clone() {
|
281
|
RelationshipTermBase<?> result = (RelationshipTermBase<?>)super.clone();
|
282
|
|
283
|
result.inverseRepresentations = new HashSet<Representation>();
|
284
|
for (Representation rep: this.inverseRepresentations){
|
285
|
result.addInverseRepresentation((Representation)rep.clone());
|
286
|
}
|
287
|
|
288
|
//no changes to: symmetric, transitiv
|
289
|
return result;
|
290
|
}
|
291
|
|
292
|
|
293
|
}
|