1 |
9479da48
|
Andreas Müller
|
/**
|
2 |
|
|
* Copyright (C) 2007 EDIT
|
3 |
4af7bea2
|
Andreas Kohlbecker
|
* European Distributed Institute of Taxonomy
|
4 |
9479da48
|
Andreas Müller
|
* http://www.e-taxonomy.eu
|
5 |
4af7bea2
|
Andreas Kohlbecker
|
*
|
6 |
9479da48
|
Andreas Müller
|
* 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 |
|
|
|
13 |
f7f4a80d
|
Andreas Müller
|
import java.net.URI;
|
14 |
f6765014
|
ben.clark
|
import java.util.HashSet;
|
15 |
|
|
import java.util.Iterator;
|
16 |
|
|
import java.util.List;
|
17 |
|
|
import java.util.Set;
|
18 |
|
|
import java.util.SortedSet;
|
19 |
|
|
import java.util.TreeSet;
|
20 |
|
|
import java.util.UUID;
|
21 |
fbd23d1d
|
Andreas Müller
|
|
22 |
f6765014
|
ben.clark
|
import javax.persistence.Entity;
|
23 |
|
|
import javax.persistence.FetchType;
|
24 |
|
|
import javax.persistence.Inheritance;
|
25 |
|
|
import javax.persistence.InheritanceType;
|
26 |
|
|
import javax.persistence.OneToMany;
|
27 |
b70a7f94
|
Andreas Kohlbecker
|
import javax.persistence.Transient;
|
28 |
3e57e2a8
|
a.babadshanjan
|
import javax.xml.bind.annotation.XmlAccessType;
|
29 |
|
|
import javax.xml.bind.annotation.XmlAccessorType;
|
30 |
|
|
import javax.xml.bind.annotation.XmlElement;
|
31 |
|
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
32 |
fa6763d6
|
a.babadshanjan
|
import javax.xml.bind.annotation.XmlIDREF;
|
33 |
3e57e2a8
|
a.babadshanjan
|
import javax.xml.bind.annotation.XmlRootElement;
|
34 |
fa6763d6
|
a.babadshanjan
|
import javax.xml.bind.annotation.XmlSchemaType;
|
35 |
3e57e2a8
|
a.babadshanjan
|
import javax.xml.bind.annotation.XmlType;
|
36 |
9479da48
|
Andreas Müller
|
|
37 |
f6765014
|
ben.clark
|
import org.apache.log4j.Logger;
|
38 |
|
|
import org.hibernate.annotations.Cascade;
|
39 |
|
|
import org.hibernate.annotations.CascadeType;
|
40 |
|
|
import org.hibernate.annotations.Type;
|
41 |
ee91bcd9
|
ben.clark
|
import org.hibernate.envers.Audited;
|
42 |
a13c5f66
|
Andreas Müller
|
import org.hibernate.search.annotations.Analyze;
|
43 |
51db8d4a
|
ben.clark
|
import org.hibernate.search.annotations.Field;
|
44 |
|
|
import org.hibernate.search.annotations.Indexed;
|
45 |
|
|
import org.hibernate.search.annotations.IndexedEmbedded;
|
46 |
f6765014
|
ben.clark
|
|
47 |
9479da48
|
Andreas Müller
|
|
48 |
|
|
/**
|
49 |
|
|
* A single enumeration must only contain DefinedTerm instances of one kind
|
50 |
975192f9
|
m.geoffroy
|
* (this means a subclass of DefinedTerm).
|
51 |
9479da48
|
Andreas Müller
|
* @author m.doering
|
52 |
|
|
* @created 08-Nov-2007 13:06:23
|
53 |
|
|
*/
|
54 |
3e57e2a8
|
a.babadshanjan
|
@XmlAccessorType(XmlAccessType.FIELD)
|
55 |
|
|
@XmlType(name = "TermVocabulary", propOrder = {
|
56 |
24e6e7c0
|
a.babadshanjan
|
"termSourceUri",
|
57 |
|
|
"terms"
|
58 |
3e57e2a8
|
a.babadshanjan
|
})
|
59 |
|
|
@XmlRootElement(name = "TermVocabulary")
|
60 |
9479da48
|
Andreas Müller
|
@Entity
|
61 |
51db8d4a
|
ben.clark
|
@Indexed(index = "eu.etaxonomy.cdm.model.common.TermVocabulary")
|
62 |
ee91bcd9
|
ben.clark
|
@Audited
|
63 |
2c3fa591
|
Andreas Müller
|
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
|
64 |
9479da48
|
Andreas Müller
|
public class TermVocabulary<T extends DefinedTermBase> extends TermBase implements Iterable<T> {
|
65 |
0d575644
|
Andreas Müller
|
private static final long serialVersionUID = 1925052321596648672L;
|
66 |
|
|
private static final Logger logger = Logger.getLogger(TermVocabulary.class);
|
67 |
12c8358b
|
Andreas Müller
|
|
68 |
4af7bea2
|
Andreas Kohlbecker
|
//The vocabulary source (e.g. ontology) defining the terms to be loaded when a database is created for the first time.
|
69 |
|
|
// Software can go and grap these terms incl labels and description.
|
70 |
9479da48
|
Andreas Müller
|
// UUID needed? Further vocs can be setup through our own ontology.
|
71 |
3e57e2a8
|
a.babadshanjan
|
@XmlElement(name = "TermSourceURI")
|
72 |
f88ffe46
|
Andreas Müller
|
@Type(type="uriUserType")
|
73 |
e63facc0
|
Andreas Kohlbecker
|
@Field(analyze = Analyze.NO)
|
74 |
f88ffe46
|
Andreas Müller
|
private URI termSourceUri;
|
75 |
4af7bea2
|
Andreas Kohlbecker
|
|
76 |
9479da48
|
Andreas Müller
|
|
77 |
99491025
|
Andreas Müller
|
//TODO Changed
|
78 |
3e57e2a8
|
a.babadshanjan
|
@XmlElementWrapper(name = "Terms")
|
79 |
|
|
@XmlElement(name = "Term")
|
80 |
fa6763d6
|
a.babadshanjan
|
@XmlIDREF
|
81 |
|
|
@XmlSchemaType(name = "IDREF")
|
82 |
ee91bcd9
|
ben.clark
|
@OneToMany(mappedBy="vocabulary", fetch=FetchType.LAZY, targetEntity = DefinedTermBase.class)
|
83 |
|
|
@Type(type="DefinedTermBase")
|
84 |
1ea1c087
|
ben.clark
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
|
85 |
51db8d4a
|
ben.clark
|
@IndexedEmbedded(depth = 2)
|
86 |
f6765014
|
ben.clark
|
protected Set<T> terms = getNewTermSet();
|
87 |
4af7bea2
|
Andreas Kohlbecker
|
|
88 |
99d3fb37
|
Andreas Müller
|
// ********************************* FACTORY METHODS *****************************************/
|
89 |
2a179529
|
Andreas Müller
|
|
90 |
884b63a3
|
Andreas Müller
|
|
91 |
|
|
public static TermVocabulary NewInstance(TermType type){
|
92 |
|
|
return new TermVocabulary(type);
|
93 |
|
|
}
|
94 |
|
|
|
95 |
533cbb43
|
Andreas Müller
|
public static TermVocabulary NewInstance(TermType type, String description, String label, String abbrev, URI termSourceUri){
|
96 |
|
|
return new TermVocabulary(type, description, label, abbrev, termSourceUri);
|
97 |
99d3fb37
|
Andreas Müller
|
}
|
98 |
4af7bea2
|
Andreas Kohlbecker
|
|
99 |
|
|
// ************************* CONSTRUCTOR *************************************************
|
100 |
99d3fb37
|
Andreas Müller
|
|
101 |
9e3239f6
|
Andreas Müller
|
//for hibernate use only
|
102 |
|
|
@Deprecated
|
103 |
117547a0
|
Andreas Müller
|
protected TermVocabulary() {
|
104 |
9e3239f6
|
Andreas Müller
|
super(TermType.Unknown);
|
105 |
99d3fb37
|
Andreas Müller
|
}
|
106 |
4af7bea2
|
Andreas Kohlbecker
|
|
107 |
884b63a3
|
Andreas Müller
|
protected TermVocabulary(TermType type) {
|
108 |
|
|
super(type);
|
109 |
|
|
}
|
110 |
|
|
|
111 |
533cbb43
|
Andreas Müller
|
protected TermVocabulary(TermType type, String term, String label, String labelAbbrev, URI termSourceUri) {
|
112 |
|
|
super(type, term, label, labelAbbrev);
|
113 |
99d3fb37
|
Andreas Müller
|
setTermSourceUri(termSourceUri);
|
114 |
|
|
}
|
115 |
|
|
|
116 |
4af7bea2
|
Andreas Kohlbecker
|
|
117 |
|
|
// ******************* METHODS *************************************************/
|
118 |
|
|
|
119 |
ee91bcd9
|
ben.clark
|
public T findTermByUuid(UUID uuid){
|
120 |
|
|
for(T t : terms) {
|
121 |
|
|
if(t.getUuid().equals(uuid)) {
|
122 |
|
|
return t;
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
return null;
|
126 |
|
|
}
|
127 |
f6765014
|
ben.clark
|
|
128 |
b70a7f94
|
Andreas Kohlbecker
|
@Transient
|
129 |
f6765014
|
ben.clark
|
Set<T> getNewTermSet() {
|
130 |
|
|
return new HashSet<T>();
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
public Set<T> getTerms() {
|
134 |
95e756cb
|
Andreas Müller
|
return terms;
|
135 |
|
|
}
|
136 |
4af7bea2
|
Andreas Kohlbecker
|
|
137 |
f6765014
|
ben.clark
|
public void addTerm(T term) {
|
138 |
9479da48
|
Andreas Müller
|
term.setVocabulary(this);
|
139 |
f6765014
|
ben.clark
|
this.terms.add(term);
|
140 |
9479da48
|
Andreas Müller
|
}
|
141 |
0a5d50a0
|
Andreas Müller
|
public void removeTerm(T term) {
|
142 |
f6765014
|
ben.clark
|
this.terms.remove(term);
|
143 |
e2dedac4
|
Andreas Müller
|
term.setVocabulary(null);
|
144 |
9479da48
|
Andreas Müller
|
}
|
145 |
|
|
|
146 |
f88ffe46
|
Andreas Müller
|
public URI getTermSourceUri() {
|
147 |
9479da48
|
Andreas Müller
|
return termSourceUri;
|
148 |
|
|
}
|
149 |
f88ffe46
|
Andreas Müller
|
public void setTermSourceUri(URI vocabularyUri) {
|
150 |
4af7bea2
|
Andreas Kohlbecker
|
this.termSourceUri = vocabularyUri;
|
151 |
9479da48
|
Andreas Müller
|
}
|
152 |
4af7bea2
|
Andreas Kohlbecker
|
|
153 |
|
|
|
154 |
|
|
@Override
|
155 |
|
|
public Iterator<T> iterator() {
|
156 |
0a5d50a0
|
Andreas Müller
|
return terms.iterator(); // OLD: new TermIterator<T>(this.terms);
|
157 |
9479da48
|
Andreas Müller
|
}
|
158 |
4af7bea2
|
Andreas Kohlbecker
|
|
159 |
fb2451a1
|
Andreas Müller
|
public int size(){
|
160 |
|
|
return terms.size();
|
161 |
|
|
}
|
162 |
4af7bea2
|
Andreas Kohlbecker
|
|
163 |
|
|
|
164 |
5c19d7f0
|
Andreas Müller
|
/**
|
165 |
|
|
* Returns all terms of this vocabulary sorted by their representation defined by the given language.
|
166 |
|
|
* If such an representation does not exist, the representation of the default language is testing instead for ordering.
|
167 |
|
|
* @param language
|
168 |
|
|
* @return
|
169 |
|
|
*/
|
170 |
81c31b35
|
Andreas Müller
|
public SortedSet<T> getTermsOrderedByLabels(Language language){
|
171 |
5c19d7f0
|
Andreas Müller
|
TermLanguageComparator<T> comp = new TermLanguageComparator<T>();
|
172 |
|
|
comp.setCompareLanguage(language);
|
173 |
4af7bea2
|
Andreas Kohlbecker
|
|
174 |
5c19d7f0
|
Andreas Müller
|
SortedSet<T> result = new TreeSet<T>(comp);
|
175 |
|
|
result.addAll(getTerms());
|
176 |
|
|
return result;
|
177 |
|
|
}
|
178 |
4af7bea2
|
Andreas Kohlbecker
|
|
179 |
|
|
|
180 |
2be752ce
|
Andreas Müller
|
/* (non-Javadoc)
|
181 |
|
|
* @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
|
182 |
|
|
*/
|
183 |
f6765014
|
ben.clark
|
public TermVocabulary<T> readCsvLine(List<String> csvLine) {
|
184 |
9cc4226b
|
Katja Luther
|
return readCsvLine(csvLine, Language.CSV_LANGUAGE());
|
185 |
2be752ce
|
Andreas Müller
|
}
|
186 |
4af7bea2
|
Andreas Kohlbecker
|
|
187 |
f6765014
|
ben.clark
|
public TermVocabulary<T> readCsvLine(List<String> csvLine, Language lang) {
|
188 |
2be752ce
|
Andreas Müller
|
this.setUuid(UUID.fromString(csvLine.get(0)));
|
189 |
f7f4a80d
|
Andreas Müller
|
this.setUri(URI.create(csvLine.get(1)));
|
190 |
b88436a0
|
Andreas Müller
|
String label = csvLine.get(2).trim();
|
191 |
32ddfd3c
|
Andreas Müller
|
String description = csvLine.get(3);
|
192 |
|
|
|
193 |
b88436a0
|
Andreas Müller
|
//see http://dev.e-taxonomy.eu/trac/ticket/3550
|
194 |
884b63a3
|
Andreas Müller
|
this.addRepresentation(Representation.NewInstance(description, label, null, lang) );
|
195 |
b88436a0
|
Andreas Müller
|
//preliminary until above is solved
|
196 |
884b63a3
|
Andreas Müller
|
// this.setTitleCache(label, true);
|
197 |
b88436a0
|
Andreas Müller
|
|
198 |
32ddfd3c
|
Andreas Müller
|
TermType termType = TermType.getByKey(csvLine.get(4));
|
199 |
|
|
if (termType == null){
|
200 |
|
|
throw new IllegalArgumentException("TermType can not be mapped: " + csvLine.get(4));
|
201 |
|
|
}
|
202 |
|
|
this.setTermType(termType);
|
203 |
533cbb43
|
Andreas Müller
|
|
204 |
2be752ce
|
Andreas Müller
|
return this;
|
205 |
|
|
}
|
206 |
4af7bea2
|
Andreas Kohlbecker
|
|
207 |
7bbabd80
|
Katja Luther
|
//*********************** CLONE ********************************************************/
|
208 |
4af7bea2
|
Andreas Kohlbecker
|
|
209 |
|
|
/**
|
210 |
7bbabd80
|
Katja Luther
|
* Clones <i>this</i> TermVocabulary. This is a shortcut that enables to create
|
211 |
|
|
* a new instance that differs only slightly from <i>this</i> TermVocabulary.
|
212 |
|
|
* The terms of the original vocabulary are cloned
|
213 |
4af7bea2
|
Andreas Kohlbecker
|
*
|
214 |
7bbabd80
|
Katja Luther
|
* @see eu.etaxonomy.cdm.model.common.TermBase#clone()
|
215 |
|
|
* @see java.lang.Object#clone()
|
216 |
|
|
*/
|
217 |
|
|
@Override
|
218 |
|
|
public Object clone() {
|
219 |
b88436a0
|
Andreas Müller
|
TermVocabulary<T> result;
|
220 |
7bbabd80
|
Katja Luther
|
try {
|
221 |
b88436a0
|
Andreas Müller
|
result = (TermVocabulary<T>) super.clone();
|
222 |
4af7bea2
|
Andreas Kohlbecker
|
|
223 |
7bbabd80
|
Katja Luther
|
}catch (CloneNotSupportedException e) {
|
224 |
|
|
logger.warn("Object does not implement cloneable");
|
225 |
|
|
e.printStackTrace();
|
226 |
|
|
return null;
|
227 |
|
|
}
|
228 |
|
|
result.terms = new HashSet<T>();
|
229 |
1f105b30
|
Katja Luther
|
for (T term: this.terms){
|
230 |
|
|
result.addTerm((T)term.clone());
|
231 |
|
|
}
|
232 |
4af7bea2
|
Andreas Kohlbecker
|
|
233 |
7bbabd80
|
Katja Luther
|
return result;
|
234 |
|
|
}
|
235 |
4af7bea2
|
Andreas Kohlbecker
|
|
236 |
9479da48
|
Andreas Müller
|
}
|