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.io.excel.common;
|
11
|
|
12
|
import java.io.FileNotFoundException;
|
13
|
import java.net.URI;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.HashMap;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.springframework.transaction.TransactionStatus;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
21
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
22
|
import eu.etaxonomy.cdm.common.ExcelUtils;
|
23
|
import eu.etaxonomy.cdm.io.common.CdmImportBase;
|
24
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
25
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
26
|
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
|
27
|
|
28
|
/**
|
29
|
* @author a.babadshanjan
|
30
|
* @created 17.12.2008
|
31
|
*/
|
32
|
public abstract class ExcelImporterBase<STATE extends ExcelImportState<? extends ExcelImportConfiguratorBase, ? extends ExcelRowBase>>
|
33
|
extends CdmImportBase<ExcelImportConfiguratorBase, STATE> {
|
34
|
private static final long serialVersionUID = 2759164811664484732L;
|
35
|
|
36
|
private static final Logger logger = Logger.getLogger(ExcelImporterBase.class);
|
37
|
|
38
|
protected static final String SCIENTIFIC_NAME_COLUMN = "ScientificName";
|
39
|
|
40
|
|
41
|
ArrayList<HashMap<String, String>> recordList = null;
|
42
|
|
43
|
private final CdmApplicationController appCtr = null;
|
44
|
private ExcelImportConfiguratorBase configurator = null;
|
45
|
|
46
|
|
47
|
/** Reads data from an Excel file and stores them into a CDM DB.
|
48
|
*
|
49
|
* @param config
|
50
|
* @param stores (not used)
|
51
|
*/
|
52
|
@Override
|
53
|
protected void doInvoke(STATE state){
|
54
|
|
55
|
logger.debug("Importing excel data");
|
56
|
|
57
|
configurator = state.getConfig();
|
58
|
|
59
|
NomenclaturalCode nc = getConfigurator().getNomenclaturalCode();
|
60
|
if (nc == null && needsNomenclaturalCode()) {
|
61
|
logger.error("Nomenclatural code could not be determined. Skip invoke.");
|
62
|
state.setUnsuccessfull();
|
63
|
return;
|
64
|
}
|
65
|
// read and save all rows of the excel worksheet
|
66
|
URI source = state.getConfig().getSource();
|
67
|
String sheetName = getWorksheetName();
|
68
|
try {
|
69
|
recordList = ExcelUtils.parseXLS(source, sheetName);
|
70
|
} catch (FileNotFoundException e) {
|
71
|
String message = "File not found: " + source;
|
72
|
warnProgress(state, message, e);
|
73
|
logger.error(message);
|
74
|
state.setUnsuccessfull();
|
75
|
return;
|
76
|
}
|
77
|
|
78
|
handleRecordList(state, source);
|
79
|
logger.debug("End excel data import");
|
80
|
return;
|
81
|
}
|
82
|
|
83
|
protected boolean needsNomenclaturalCode() {
|
84
|
return true;
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* @param state
|
89
|
* @param success
|
90
|
* @param source
|
91
|
* @return
|
92
|
*/
|
93
|
private void handleRecordList(STATE state, URI source) {
|
94
|
Integer startingLine = 2;
|
95
|
if (recordList != null) {
|
96
|
HashMap<String,String> record = null;
|
97
|
|
98
|
TransactionStatus txStatus = startTransaction();
|
99
|
|
100
|
//first pass
|
101
|
state.setCurrentLine(startingLine);
|
102
|
for (int i = 0; i < recordList.size(); i++) {
|
103
|
record = recordList.get(i);
|
104
|
analyzeRecord(record, state);
|
105
|
state.setOriginalRecord(record);
|
106
|
try {
|
107
|
firstPass(state);
|
108
|
} catch (Exception e) {
|
109
|
e.printStackTrace();
|
110
|
}finally{
|
111
|
state.incCurrentLine();
|
112
|
}
|
113
|
}
|
114
|
//second pass
|
115
|
state.setCurrentLine(startingLine);
|
116
|
for (int i = 0; i < recordList.size(); i++) {
|
117
|
record = recordList.get(i);
|
118
|
analyzeRecord(record, state);
|
119
|
state.setOriginalRecord(record);
|
120
|
secondPass(state);
|
121
|
state.incCurrentLine();
|
122
|
}
|
123
|
|
124
|
commitTransaction(txStatus);
|
125
|
}else{
|
126
|
logger.warn("No records found in " + source);
|
127
|
}
|
128
|
return;
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* To define a worksheet name override this method. Otherwise the first worksheet is taken.
|
133
|
* @return worksheet name. <code>null</null> if not worksheet is defined.
|
134
|
*/
|
135
|
protected String getWorksheetName() {
|
136
|
return null;
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
protected boolean doCheck(STATE state) {
|
141
|
boolean result = true;
|
142
|
logger.warn("No check implemented for Excel import");
|
143
|
return result;
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
*
|
148
|
*
|
149
|
* @param record
|
150
|
* @return
|
151
|
*/
|
152
|
protected abstract void analyzeRecord(HashMap<String,String> record, STATE state);
|
153
|
|
154
|
protected abstract void firstPass(STATE state);
|
155
|
protected abstract void secondPass(STATE state);
|
156
|
|
157
|
|
158
|
public ExcelImportConfiguratorBase getConfigurator() {
|
159
|
return configurator;
|
160
|
}
|
161
|
|
162
|
|
163
|
public CdmApplicationController getApplicationController() {
|
164
|
return appCtr;
|
165
|
}
|
166
|
|
167
|
|
168
|
protected int floatString2IntValue(String value) {
|
169
|
int intValue = 0;
|
170
|
try {
|
171
|
Float fobj = new Float(Float.parseFloat(value));
|
172
|
intValue = fobj.intValue();
|
173
|
if (logger.isDebugEnabled()) { logger.debug("Value formatted: " + intValue); }
|
174
|
} catch (NumberFormatException ex) {
|
175
|
logger.error(value + " is not an integer");
|
176
|
}
|
177
|
return intValue;
|
178
|
}
|
179
|
|
180
|
protected String floatString2IntStringValue(String value) {
|
181
|
int i = floatString2IntValue(value);
|
182
|
return String.valueOf(i);
|
183
|
}
|
184
|
|
185
|
|
186
|
/**
|
187
|
* @param start
|
188
|
* @param end
|
189
|
* @return
|
190
|
*/
|
191
|
protected TimePeriod getTimePeriod(String start, String end) {
|
192
|
String strPeriod = CdmUtils.concat(" - ", start, end);
|
193
|
TimePeriod result = TimePeriodParser.parseString(strPeriod);
|
194
|
return result;
|
195
|
}
|
196
|
|
197
|
|
198
|
}
|