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.io.InputStream;
|
13
|
import java.io.InputStreamReader;
|
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.IDefinedTerm;
|
27
|
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
|
28
|
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
|
29
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
30
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
31
|
|
32
|
@Component
|
33
|
public class TermLoader implements ITermLoader {
|
34
|
private static final Logger logger = Logger.getLogger(TermLoader.class);
|
35
|
|
36
|
private Map<Class<? extends DefinedTermBase>,String> termFileNames = new HashMap<Class<? extends DefinedTermBase>,String>();
|
37
|
|
38
|
public TermLoader() {
|
39
|
this.termFileNames.put(NamedArea.class, "TdwgArea.csv");
|
40
|
}
|
41
|
|
42
|
public void setTermFileNames(Map<Class<? extends DefinedTermBase>,String> termFileNames) {
|
43
|
this.termFileNames = termFileNames;
|
44
|
}
|
45
|
|
46
|
public <T extends DefinedTermBase> TermVocabulary<T> loadTerms(Class<T> termClass, Map<UUID,DefinedTermBase> terms) {
|
47
|
|
48
|
String filename = termClass.getSimpleName()+".csv";
|
49
|
|
50
|
/**
|
51
|
* Check to see if a non-standard filename should be used
|
52
|
* ( The file should still reside in the same directory )
|
53
|
*/
|
54
|
if(termFileNames.containsKey(termClass)) {
|
55
|
filename = termFileNames.get(termClass);
|
56
|
}
|
57
|
|
58
|
String strResourceFileName = "terms" + CdmUtils.getFolderSeperator() + filename;
|
59
|
logger.debug("strResourceFileName is " + strResourceFileName);
|
60
|
|
61
|
try {
|
62
|
//Could we use resources?
|
63
|
InputStream inputStream = CdmUtils.getReadableResourceStream("terms" + CdmUtils.getFolderSeperator() + filename);
|
64
|
if (inputStream == null) {logger.info("inputStream is null");}
|
65
|
CSVReader reader = new CSVReader(new InputStreamReader(inputStream));
|
66
|
|
67
|
//vocabulary
|
68
|
TermVocabulary<T> voc = null;
|
69
|
String labelAbbrev = null;
|
70
|
|
71
|
if (OrderedTermBase.class.isAssignableFrom(termClass)){
|
72
|
voc = new OrderedTermVocabulary(termClass.getCanonicalName(), termClass.getSimpleName(), labelAbbrev, termClass.getCanonicalName());
|
73
|
}else{
|
74
|
voc = new TermVocabulary<T>(termClass.getCanonicalName(), termClass.getSimpleName(), labelAbbrev, termClass.getCanonicalName());
|
75
|
}
|
76
|
|
77
|
String [] nextLine = reader.readNext();
|
78
|
if (nextLine != null){
|
79
|
voc.readCsvLine(arrayedLine(nextLine));
|
80
|
}
|
81
|
|
82
|
// Ugly, I know, but I don't think we can use a static method here . .
|
83
|
T termInstance = termClass.newInstance();
|
84
|
|
85
|
while ((nextLine = reader.readNext()) != null) {
|
86
|
// nextLine[] is an array of values from the line
|
87
|
if (nextLine.length == 0){
|
88
|
continue;
|
89
|
}
|
90
|
|
91
|
T term = (T) termInstance.readCsvLine(termClass,arrayedLine(nextLine), terms);
|
92
|
terms.put(term.getUuid(), term);
|
93
|
voc.addTerm(term);
|
94
|
}
|
95
|
return voc;
|
96
|
} catch (Exception e) {
|
97
|
logger.error(e + " " + e.getCause() + " " + e.getMessage());
|
98
|
for(StackTraceElement ste : e.getStackTrace()) {
|
99
|
logger.error(ste);
|
100
|
}
|
101
|
throw new RuntimeException(e);
|
102
|
}
|
103
|
|
104
|
}
|
105
|
|
106
|
private List<String> arrayedLine(String [] nextLine){
|
107
|
ArrayList<String> csvTermAttributeList = new ArrayList<String>(10);
|
108
|
for (String col : nextLine){
|
109
|
csvTermAttributeList.add(col);
|
110
|
}
|
111
|
while (csvTermAttributeList.size()<10){
|
112
|
csvTermAttributeList.add("");
|
113
|
}
|
114
|
return csvTermAttributeList;
|
115
|
}
|
116
|
}
|