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.term.init;
|
11
|
|
12
|
import java.io.IOException;
|
13
|
import java.lang.reflect.Constructor;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.HashSet;
|
17
|
import java.util.List;
|
18
|
import java.util.Map;
|
19
|
import java.util.Set;
|
20
|
import java.util.UUID;
|
21
|
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.springframework.stereotype.Component;
|
24
|
|
25
|
import au.com.bytecode.opencsv.CSVReader;
|
26
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
27
|
import eu.etaxonomy.cdm.common.ConfigFileUtil;
|
28
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
29
|
import eu.etaxonomy.cdm.model.term.OrderedTermBase;
|
30
|
import eu.etaxonomy.cdm.model.term.OrderedTermVocabulary;
|
31
|
import eu.etaxonomy.cdm.model.term.TermType;
|
32
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
33
|
import eu.etaxonomy.cdm.model.term.VocabularyEnum;
|
34
|
|
35
|
@Component
|
36
|
public class TermLoader implements ITermLoader {
|
37
|
private static final Logger logger = Logger.getLogger(TermLoader.class);
|
38
|
|
39
|
@Override
|
40
|
public void unloadAllTerms(){
|
41
|
for(VocabularyEnum vocabularyEnum : VocabularyEnum.values()) {
|
42
|
// Class<? extends DefinedTermBase<?>> clazz = vocabularyEnum.getClazz();
|
43
|
this.unloadVocabularyType(vocabularyEnum);
|
44
|
}
|
45
|
}
|
46
|
|
47
|
private <T extends DefinedTermBase> void unloadVocabularyType(VocabularyEnum vocType){
|
48
|
Class<? extends DefinedTermBase> termClass = vocType.getClazz();
|
49
|
getInstance(termClass).resetTerms();
|
50
|
return;
|
51
|
}
|
52
|
|
53
|
@Override
|
54
|
public UUID loadUuids(VocabularyEnum vocType, Map<UUID, Set<UUID>> uuidMap) {
|
55
|
|
56
|
try {
|
57
|
CSVReader reader = getCsvReader(vocType);
|
58
|
String[] nextLine = reader.readNext();
|
59
|
UUID uuidVocabulary = UUID.fromString(nextLine[0]);
|
60
|
Set<UUID> termSet = new HashSet<>();
|
61
|
uuidMap.put(uuidVocabulary, termSet);
|
62
|
|
63
|
while ( (nextLine = reader.readNext()) != null) {
|
64
|
UUID uuidTerm = UUID.fromString(nextLine[0]);
|
65
|
termSet.add(uuidTerm);
|
66
|
}
|
67
|
return uuidVocabulary;
|
68
|
|
69
|
|
70
|
} catch (Exception e) {
|
71
|
logger.error(e + " " + e.getCause() + " " + e.getMessage());
|
72
|
for(StackTraceElement ste : e.getStackTrace()) {
|
73
|
logger.error(ste);
|
74
|
}
|
75
|
throw new RuntimeException(e);
|
76
|
}
|
77
|
|
78
|
}
|
79
|
|
80
|
@Override
|
81
|
public <T extends DefinedTermBase> TermVocabulary<T> loadTerms(VocabularyEnum vocType, Map<UUID,DefinedTermBase> terms) {
|
82
|
|
83
|
|
84
|
try {
|
85
|
CSVReader reader = getCsvReader(vocType);
|
86
|
|
87
|
String [] nextLine = reader.readNext();
|
88
|
|
89
|
|
90
|
Class<? extends DefinedTermBase> termClass = vocType.getClazz();
|
91
|
|
92
|
//vocabulary
|
93
|
TermVocabulary<T> voc;
|
94
|
TermType termType = TermType.Unknown;
|
95
|
if (OrderedTermBase.class.isAssignableFrom(termClass)){
|
96
|
voc = OrderedTermVocabulary.NewInstance(termType);
|
97
|
}else{
|
98
|
voc = TermVocabulary.NewInstance(termType);
|
99
|
}
|
100
|
|
101
|
if (nextLine != null){
|
102
|
voc.readCsvLine(arrayedLine(nextLine));
|
103
|
}
|
104
|
termType = voc.getTermType();
|
105
|
boolean abbrevAsId = (arrayedLine(nextLine).get(5).equals("1"));
|
106
|
|
107
|
// Ugly, I know, but I don't think we can use a static method here . .
|
108
|
|
109
|
T classDefiningTermInstance = getInstance(termClass);// ((Class<T>)termClass).newInstance();
|
110
|
|
111
|
while ((nextLine = reader.readNext()) != null) {
|
112
|
// nextLine[] is an array of values from the line
|
113
|
if (nextLine.length == 0){
|
114
|
continue;
|
115
|
}
|
116
|
|
117
|
handleSingleTerm(nextLine, terms, termClass, voc,
|
118
|
abbrevAsId, classDefiningTermInstance);
|
119
|
|
120
|
}
|
121
|
return voc;
|
122
|
} catch (Exception e) {
|
123
|
logger.error(e + " " + e.getCause() + " " + e.getMessage());
|
124
|
for(StackTraceElement ste : e.getStackTrace()) {
|
125
|
logger.error(ste);
|
126
|
}
|
127
|
throw new RuntimeException(e);
|
128
|
}
|
129
|
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* Handles a single csv line, creates the term and adds it to the vocabulary and to the terms map.
|
134
|
* @param csvLine csv line
|
135
|
* @param terms UUID-Term map this term should be added to
|
136
|
* @param termClass the class of the term to create
|
137
|
* @param voc the vocabulary the term should be added to
|
138
|
* @param abbrevAsId boolean value, if true the abbreviation should be taken as idInVocabulary
|
139
|
* @param classDefiningTermInstance instance for calling readCsvLine
|
140
|
* @return
|
141
|
*/
|
142
|
private <T extends DefinedTermBase> T handleSingleTerm(String[] csvLine, Map<UUID,DefinedTermBase> terms,
|
143
|
Class<? extends DefinedTermBase> termClass,
|
144
|
TermVocabulary<T> voc, boolean abbrevAsId,
|
145
|
T classDefiningTermInstance) {
|
146
|
T term = (T) classDefiningTermInstance.readCsvLine(termClass,arrayedLine(csvLine), voc.getTermType(), terms, abbrevAsId);
|
147
|
voc.addTerm(term);
|
148
|
terms.put(term.getUuid(), term);
|
149
|
return term;
|
150
|
}
|
151
|
|
152
|
|
153
|
@Override
|
154
|
public <T extends DefinedTermBase> Set<T> loadSingleTerms(VocabularyEnum vocType,
|
155
|
TermVocabulary<T> voc, Set<UUID> missingTerms) {
|
156
|
try {
|
157
|
Class<? extends DefinedTermBase> termClass = vocType.getClazz();
|
158
|
|
159
|
CSVReader reader = getCsvReader(vocType);
|
160
|
String [] nextLine = reader.readNext();
|
161
|
|
162
|
if (! UUID.fromString(nextLine[0]).equals(voc.getUuid())){
|
163
|
throw new IllegalStateException("Vocabularies in csv file and vocabulary must be equal");
|
164
|
}
|
165
|
|
166
|
|
167
|
|
168
|
boolean abbrevAsId = (arrayedLine(nextLine).get(5).equals("1"));
|
169
|
T classDefiningTermInstance = getInstance(termClass);// ((Class<T>)termClass).newInstance();
|
170
|
Map<UUID,DefinedTermBase> allVocTerms = new HashMap<UUID, DefinedTermBase>();
|
171
|
for (T term:voc.getTerms()){
|
172
|
allVocTerms.put(term.getUuid(), term);
|
173
|
}
|
174
|
|
175
|
while ((nextLine = reader.readNext()) != null) {
|
176
|
if (nextLine.length == 0){
|
177
|
continue;
|
178
|
}
|
179
|
UUID uuid = UUID.fromString(nextLine[0]);
|
180
|
if (missingTerms.contains(uuid)){
|
181
|
handleSingleTerm(nextLine, allVocTerms, termClass, voc, abbrevAsId, classDefiningTermInstance);
|
182
|
}
|
183
|
}
|
184
|
|
185
|
return null;
|
186
|
} catch (Exception e) {
|
187
|
e.printStackTrace();
|
188
|
throw new RuntimeException(e);
|
189
|
}
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* Returns the {@link CSVReader} for the given {@link VocabularyEnum}.
|
194
|
* @param vocType
|
195
|
* @return
|
196
|
* @throws IOException
|
197
|
*/
|
198
|
private CSVReader getCsvReader(VocabularyEnum vocType) throws IOException {
|
199
|
String filename = vocType.name()+".csv";
|
200
|
String strResourceFileName = "terms" + ConfigFileUtil.getFolderSeperator() + filename;
|
201
|
if (logger.isDebugEnabled()){logger.debug("strResourceFileName is " + strResourceFileName);}
|
202
|
CSVReader reader = new CSVReader(CdmUtils.getUtf8ResourceReader(strResourceFileName));
|
203
|
return reader;
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Returns a new instance for the given class by using the default constructor.
|
208
|
* The constructor must be declared but can be unaccessible (e.g. private)
|
209
|
* @param termClass
|
210
|
* @return
|
211
|
*/
|
212
|
private <T extends DefinedTermBase> T getInstance(Class<? extends DefinedTermBase> termClass) {
|
213
|
try {
|
214
|
Constructor<T> c = ((Class<T>)termClass).getDeclaredConstructor();
|
215
|
c.setAccessible(true);
|
216
|
T termInstance = c.newInstance();
|
217
|
return termInstance;
|
218
|
} catch (Exception e) {
|
219
|
throw new RuntimeException(e);
|
220
|
}
|
221
|
}
|
222
|
|
223
|
private List<String> arrayedLine(String [] nextLine){
|
224
|
ArrayList<String> csvTermAttributeList = new ArrayList<String>(15);
|
225
|
for (String col : nextLine){
|
226
|
csvTermAttributeList.add(col);
|
227
|
}
|
228
|
while (csvTermAttributeList.size()<15){
|
229
|
csvTermAttributeList.add("");
|
230
|
}
|
231
|
return csvTermAttributeList;
|
232
|
}
|
233
|
|
234
|
}
|