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.init;
|
11
|
|
12
|
import java.lang.reflect.Constructor;
|
13
|
import java.net.URI;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import org.apache.log4j.Logger;
|
21
|
import org.springframework.stereotype.Component;
|
22
|
|
23
|
import au.com.bytecode.opencsv.CSVReader;
|
24
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
25
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
26
|
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
|
27
|
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
|
28
|
import eu.etaxonomy.cdm.model.common.TermType;
|
29
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
30
|
import eu.etaxonomy.cdm.model.common.VocabularyEnum;
|
31
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
32
|
|
33
|
@Component
|
34
|
public class TermLoader implements ITermLoader {
|
35
|
private static final Logger logger = Logger.getLogger(TermLoader.class);
|
36
|
|
37
|
private Map<Class<? extends DefinedTermBase>,String> termFileNames = new HashMap<Class<? extends DefinedTermBase>,String>();
|
38
|
|
39
|
public TermLoader() {
|
40
|
this.termFileNames.put(NamedArea.class, "TdwgArea.csv");
|
41
|
}
|
42
|
|
43
|
public void setTermFileNames(Map<Class<? extends DefinedTermBase>,String> termFileNames) {
|
44
|
this.termFileNames = termFileNames;
|
45
|
}
|
46
|
|
47
|
public void unloadAllTerms(){
|
48
|
for(VocabularyEnum vocabularyEnum : VocabularyEnum.values()) {
|
49
|
// Class<? extends DefinedTermBase<?>> clazz = vocabularyEnum.getClazz();
|
50
|
this.unloadVocabularyType(vocabularyEnum);
|
51
|
}
|
52
|
}
|
53
|
|
54
|
private <T extends DefinedTermBase> void unloadVocabularyType(VocabularyEnum vocType){
|
55
|
Class<? extends DefinedTermBase> termClass = vocType.getClazz();
|
56
|
getInstance(termClass).resetTerms();
|
57
|
return;
|
58
|
}
|
59
|
|
60
|
|
61
|
|
62
|
public <T extends DefinedTermBase> TermVocabulary<T> loadTerms(VocabularyEnum vocType, Map<UUID,DefinedTermBase> terms) {
|
63
|
|
64
|
String filename = vocType.name()+".csv";
|
65
|
// termClass.getSimpleName()+".csv";
|
66
|
|
67
|
// /**
|
68
|
// * Check to see if a non-standard filename should be used
|
69
|
// * ( The file should still reside in the same directory )
|
70
|
// */
|
71
|
// if(termFileNames.containsKey(termClass)) {
|
72
|
// filename = termFileNames.get(termClass);
|
73
|
// }
|
74
|
Class<? extends DefinedTermBase> termClass = vocType.getClazz();
|
75
|
|
76
|
String strResourceFileName = "terms" + CdmUtils.getFolderSeperator() + filename;
|
77
|
logger.debug("strResourceFileName is " + strResourceFileName);
|
78
|
|
79
|
try {
|
80
|
CSVReader reader = new CSVReader(CdmUtils.getUtf8ResourceReader("terms" + CdmUtils.getFolderSeperator() + filename));
|
81
|
String [] nextLine = reader.readNext();
|
82
|
|
83
|
//vocabulary
|
84
|
TermVocabulary<T> voc = null;
|
85
|
String labelAbbrev = null;
|
86
|
|
87
|
TermType termType = TermType.Unknown;
|
88
|
if (OrderedTermBase.class.isAssignableFrom(termClass)){
|
89
|
voc = OrderedTermVocabulary.NewInstance(termType, termClass.getCanonicalName(), termClass.getSimpleName(), labelAbbrev, URI.create(termClass.getCanonicalName()));
|
90
|
}else{
|
91
|
voc = TermVocabulary.NewInstance(termType, termClass.getCanonicalName(), vocType.name(), labelAbbrev, URI.create(termClass.getCanonicalName()));
|
92
|
}
|
93
|
|
94
|
if (nextLine != null){
|
95
|
voc.readCsvLine(arrayedLine(nextLine));
|
96
|
}
|
97
|
termType = voc.getTermType();
|
98
|
boolean abbrevAsId = (arrayedLine(nextLine).get(5).equals("1"));
|
99
|
|
100
|
// Ugly, I know, but I don't think we can use a static method here . .
|
101
|
|
102
|
T classDefiningTermInstance = getInstance(termClass);// ((Class<T>)termClass).newInstance();
|
103
|
|
104
|
while ((nextLine = reader.readNext()) != null) {
|
105
|
// nextLine[] is an array of values from the line
|
106
|
if (nextLine.length == 0){
|
107
|
continue;
|
108
|
}
|
109
|
|
110
|
T term = (T) classDefiningTermInstance.readCsvLine(termClass,arrayedLine(nextLine), terms, abbrevAsId);
|
111
|
term.setTermType(termType);
|
112
|
terms.put(term.getUuid(), term);
|
113
|
voc.addTerm(term);
|
114
|
}
|
115
|
return voc;
|
116
|
} catch (Exception e) {
|
117
|
logger.error(e + " " + e.getCause() + " " + e.getMessage());
|
118
|
for(StackTraceElement ste : e.getStackTrace()) {
|
119
|
logger.error(ste);
|
120
|
}
|
121
|
throw new RuntimeException(e);
|
122
|
}
|
123
|
|
124
|
}
|
125
|
|
126
|
private <T extends DefinedTermBase> T getInstance(Class<? extends DefinedTermBase> termClass) {
|
127
|
try {
|
128
|
Constructor<T> c = ((Class<T>)termClass).getDeclaredConstructor();
|
129
|
c.setAccessible(true);
|
130
|
T termInstance = c.newInstance();
|
131
|
return termInstance;
|
132
|
} catch (Exception e) {
|
133
|
throw new RuntimeException(e);
|
134
|
}
|
135
|
}
|
136
|
|
137
|
private List<String> arrayedLine(String [] nextLine){
|
138
|
ArrayList<String> csvTermAttributeList = new ArrayList<String>(15);
|
139
|
for (String col : nextLine){
|
140
|
csvTermAttributeList.add(col);
|
141
|
}
|
142
|
while (csvTermAttributeList.size()<15){
|
143
|
csvTermAttributeList.add("");
|
144
|
}
|
145
|
return csvTermAttributeList;
|
146
|
}
|
147
|
}
|