1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
13
|
import org.apache.log4j.Logger;
|
14
|
import org.hibernate.annotations.Cascade;
|
15
|
import org.hibernate.annotations.CascadeType;
|
16
|
import org.hibernate.annotations.Type;
|
17
|
import java.util.*;
|
18
|
|
19
|
import javax.persistence.*;
|
20
|
|
21
|
|
22
|
/**
|
23
|
* A single enumeration must only contain DefinedTerm instances of one kind
|
24
|
* (this means a subclass of DefinedTerm).
|
25
|
* @author m.doering
|
26
|
* @version 1.0
|
27
|
* @created 08-Nov-2007 13:06:23
|
28
|
*/
|
29
|
@Entity
|
30
|
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
|
31
|
public class TermVocabulary<T extends DefinedTermBase> extends TermBase implements Iterable<T> {
|
32
|
static Logger logger = Logger.getLogger(TermVocabulary.class);
|
33
|
|
34
|
private static final UUID uuidLanguage = UUID.fromString("17ba1c02-256d-47cf-bed0-2964ec1108ba");
|
35
|
private static final UUID uuidRank = UUID.fromString("b17451eb-4278-4179-af68-44f27aa3d151");
|
36
|
private static final UUID uuidContinent = UUID.fromString("ed4e5948-172a-424c-83d6-6fc7c7da70ed");
|
37
|
|
38
|
|
39
|
public static TermVocabulary findByUuid(UUID uuid){
|
40
|
//in tests tems may no be initialised by database access
|
41
|
// if (!isInitialized()){
|
42
|
// initTermList(null);
|
43
|
// }
|
44
|
// return termVocabularyMap.get(uuid);
|
45
|
//TODO
|
46
|
logger.error("Not yet implemented");
|
47
|
return null;
|
48
|
}
|
49
|
public static final TermVocabulary getUUID(UUID uuid){
|
50
|
return findByUuid(uuid);
|
51
|
}
|
52
|
public static final TermVocabulary LANGUAGE(){
|
53
|
return getUUID(uuidLanguage);
|
54
|
}
|
55
|
|
56
|
//The vocabulary source (e.g. ontology) defining the terms to be loaded when a database is created for the first time.
|
57
|
// Software can go and grap these terms incl labels and description.
|
58
|
// UUID needed? Further vocs can be setup through our own ontology.
|
59
|
private String termSourceUri;
|
60
|
protected Class termClass;
|
61
|
|
62
|
//TODO Changed
|
63
|
public Set<T> terms = getNewTermSet();
|
64
|
|
65
|
//to be overriden by subclasses, e.g. OrderedTermVocabulary
|
66
|
@Transient
|
67
|
protected Set<T> getNewTermSet(){
|
68
|
return new HashSet<T>();
|
69
|
}
|
70
|
|
71
|
public TermVocabulary() {
|
72
|
super();
|
73
|
}
|
74
|
public TermVocabulary(String term, String label, String labelAbbrev, String termSourceUri) {
|
75
|
super(term, label, labelAbbrev);
|
76
|
setTermSourceUri(termSourceUri);
|
77
|
}
|
78
|
|
79
|
@Transient
|
80
|
public Set<T> getTerms() {
|
81
|
//Set<T> result = getNewTermSet();
|
82
|
//result.addAll(terms);
|
83
|
//return result;
|
84
|
return terms;
|
85
|
}
|
86
|
protected void setTerms(Set<T> terms) {
|
87
|
this.terms = terms;
|
88
|
}
|
89
|
|
90
|
@OneToMany(mappedBy="persistentVocabulary", fetch=FetchType.LAZY)
|
91
|
@Type(type="DefinedTermBase")
|
92
|
@Cascade({CascadeType.SAVE_UPDATE})
|
93
|
public Set<T> getPersistentTerms() {
|
94
|
return terms;
|
95
|
}
|
96
|
protected void setPersistentTerms(Set<T> terms) {
|
97
|
this.terms = terms;
|
98
|
}
|
99
|
public void addTerm(T term) throws WrongTermTypeException {
|
100
|
if (terms.size()<1){
|
101
|
// no term yet in the list. First term defines the vocabulary kind
|
102
|
termClass=term.getClass();
|
103
|
}else if (term.getClass()!=termClass){
|
104
|
// check if new term in this vocabulary matches the previous ones
|
105
|
throw new WrongTermTypeException(term.getClass().getCanonicalName());
|
106
|
}
|
107
|
term.setVocabulary(this);
|
108
|
}
|
109
|
public void removeTerm(T term) {
|
110
|
term.setVocabulary(null);
|
111
|
}
|
112
|
|
113
|
public String getTermSourceUri() {
|
114
|
return termSourceUri;
|
115
|
}
|
116
|
public void setTermSourceUri(String vocabularyUri) {
|
117
|
this.termSourceUri = vocabularyUri;
|
118
|
}
|
119
|
|
120
|
|
121
|
public Class getTermClass() {
|
122
|
return termClass;
|
123
|
}
|
124
|
private void setTermClass(Class termClass) {
|
125
|
this.termClass = termClass;
|
126
|
}
|
127
|
|
128
|
|
129
|
// // inner iterator class for the iterable interface
|
130
|
// private class TermIterator<T> implements Iterator<T> {
|
131
|
// // FIXME: using a list here is probably not safe. Sth passed by value, an array, would be better
|
132
|
// // but arrays cause generics problems: http://forum.java.sun.com/thread.jspa?threadID=651276&messageID=3832182
|
133
|
// // hack for now ;(
|
134
|
// private Set<T> array;
|
135
|
// private int i= 0;
|
136
|
// // ctor
|
137
|
// public TermIterator(Set<T> array) {
|
138
|
// // check for null being passed in etc.
|
139
|
// this.array= array;
|
140
|
// }
|
141
|
// // interface implementation
|
142
|
// public boolean hasNext() { return i < array.size(); }
|
143
|
// public T next() { return array.get(i++); }
|
144
|
// public void remove() { throw new UnsupportedOperationException(); }
|
145
|
// }
|
146
|
|
147
|
public Iterator<T> iterator() {
|
148
|
return terms.iterator(); // OLD: new TermIterator<T>(this.terms);
|
149
|
}
|
150
|
|
151
|
public int size(){
|
152
|
return terms.size();
|
153
|
}
|
154
|
|
155
|
|
156
|
/**
|
157
|
* Returns all terms of this vocabulary sorted by their representation defined by the given language.
|
158
|
* If such an representation does not exist, the representation of the default language is testing instead for ordering.
|
159
|
* @param language
|
160
|
* @return
|
161
|
*/
|
162
|
@Transient
|
163
|
public SortedSet<T> getTermsOrderedByLabels(Language language){
|
164
|
TermLanguageComparator<T> comp = new TermLanguageComparator<T>();
|
165
|
comp.setCompareLanguage(language);
|
166
|
|
167
|
SortedSet<T> result = new TreeSet<T>(comp);
|
168
|
result.addAll(getTerms());
|
169
|
return result;
|
170
|
}
|
171
|
|
172
|
|
173
|
/* (non-Javadoc)
|
174
|
* @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
|
175
|
*/
|
176
|
public TermVocabulary readCsvLine(List<String> csvLine) {
|
177
|
return readCsvLine(csvLine, Language.ENGLISH());
|
178
|
}
|
179
|
public TermVocabulary readCsvLine(List<String> csvLine, Language lang) {
|
180
|
this.setUuid(UUID.fromString(csvLine.get(0)));
|
181
|
this.setUri(csvLine.get(1));
|
182
|
//this.addRepresentation(Representation.NewInstance(csvLine.get(3), csvLine.get(2).trim(), lang) );
|
183
|
return this;
|
184
|
}
|
185
|
|
186
|
}
|