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.util.ArrayList;
|
14
|
import java.util.HashMap;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.springframework.transaction.TransactionStatus;
|
18
|
|
19
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
20
|
import eu.etaxonomy.cdm.common.ExcelUtils;
|
21
|
import eu.etaxonomy.cdm.io.common.CdmImportBase;
|
22
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
23
|
|
24
|
/**
|
25
|
* @author a.babadshanjan
|
26
|
* @created 17.12.2008
|
27
|
* @version 1.0
|
28
|
*/
|
29
|
public abstract class ExcelImporterBase<STATE extends ExcelImportState> extends CdmImportBase<ExcelImportConfiguratorBase, STATE> {
|
30
|
private static final Logger logger = Logger.getLogger(ExcelImporterBase.class);
|
31
|
|
32
|
protected static final String SCIENTIFIC_NAME_COLUMN = "ScientificName";
|
33
|
|
34
|
ArrayList<HashMap<String, String>> recordList = null;
|
35
|
|
36
|
private CdmApplicationController appCtr = null;
|
37
|
private ExcelImportConfiguratorBase configurator = null;
|
38
|
|
39
|
|
40
|
/** Reads data from an Excel file and stores them into a CDM DB.
|
41
|
*
|
42
|
* @param config
|
43
|
* @param stores (not used)
|
44
|
*/
|
45
|
@Override
|
46
|
protected boolean doInvoke(STATE state){
|
47
|
|
48
|
boolean success = false;
|
49
|
|
50
|
logger.debug("Importing excel data");
|
51
|
|
52
|
configurator = state.getConfig();
|
53
|
|
54
|
NomenclaturalCode nc = getConfigurator().getNomenclaturalCode();
|
55
|
if (nc == null) {
|
56
|
logger.error("Nomenclatural code could not be determined.");
|
57
|
return false;
|
58
|
}
|
59
|
// read and save all rows of the excel worksheet
|
60
|
String source = (String)state.getConfig().getSource();
|
61
|
source = source.replace("file:/", "");
|
62
|
try {
|
63
|
recordList = ExcelUtils.parseXLS(source);
|
64
|
} catch (FileNotFoundException e1) {
|
65
|
logger.error("File not found: " + source);
|
66
|
return false;
|
67
|
}
|
68
|
|
69
|
if (recordList != null) {
|
70
|
HashMap<String,String> record = null;
|
71
|
|
72
|
TransactionStatus txStatus = startTransaction();
|
73
|
|
74
|
//first pass
|
75
|
for (int i = 0; i < recordList.size(); i++) {
|
76
|
record = recordList.get(i);
|
77
|
success = analyzeRecord(record, state);
|
78
|
success = firstPass(state);
|
79
|
}
|
80
|
//second pass
|
81
|
for (int i = 0; i < recordList.size(); i++) {
|
82
|
record = recordList.get(i);
|
83
|
success = analyzeRecord(record, state);
|
84
|
success = secondPass(state);
|
85
|
}
|
86
|
|
87
|
commitTransaction(txStatus);
|
88
|
}else{
|
89
|
logger.warn("No records found in " + source);
|
90
|
}
|
91
|
|
92
|
try {
|
93
|
logger.debug("End excel data import");
|
94
|
|
95
|
} catch (Exception e) {
|
96
|
logger.error("Error closing the application context");
|
97
|
e.printStackTrace();
|
98
|
}
|
99
|
|
100
|
return success;
|
101
|
}
|
102
|
|
103
|
@Override
|
104
|
protected boolean doCheck(STATE state) {
|
105
|
boolean result = true;
|
106
|
logger.warn("No check implemented for Excel import");
|
107
|
return result;
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
*
|
112
|
*
|
113
|
* @param record
|
114
|
* @return
|
115
|
*/
|
116
|
protected abstract boolean analyzeRecord(HashMap<String,String> record, STATE state);
|
117
|
|
118
|
protected abstract boolean firstPass(STATE state);
|
119
|
protected abstract boolean secondPass(STATE state);
|
120
|
|
121
|
|
122
|
public ExcelImportConfiguratorBase getConfigurator() {
|
123
|
return configurator;
|
124
|
}
|
125
|
|
126
|
|
127
|
public CdmApplicationController getApplicationController() {
|
128
|
return appCtr;
|
129
|
}
|
130
|
|
131
|
|
132
|
protected int floatString2IntValue(String value) {
|
133
|
int intValue = 0;
|
134
|
try {
|
135
|
Float fobj = new Float(Float.parseFloat(value));
|
136
|
intValue = fobj.intValue();
|
137
|
if (logger.isDebugEnabled()) { logger.debug("Value formatted: " + intValue); }
|
138
|
} catch (NumberFormatException ex) {
|
139
|
logger.error(value + " is not an integer");
|
140
|
}
|
141
|
return intValue;
|
142
|
}
|
143
|
|
144
|
|
145
|
}
|