2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.excel
.common
;
12 import java
.io
.FileNotFoundException
;
14 import java
.util
.ArrayList
;
15 import java
.util
.HashMap
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.springframework
.transaction
.TransactionStatus
;
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
;
28 * @author a.babadshanjan
32 public abstract class ExcelImporterBase
<STATE
extends ExcelImportState
<?
extends ExcelImportConfiguratorBase
, ?
extends ExcelRowBase
>> extends CdmImportBase
<ExcelImportConfiguratorBase
, STATE
> {
33 private static final Logger logger
= Logger
.getLogger(ExcelImporterBase
.class);
35 protected static final String SCIENTIFIC_NAME_COLUMN
= "ScientificName";
37 ArrayList
<HashMap
<String
, String
>> recordList
= null;
39 private CdmApplicationController appCtr
= null;
40 private ExcelImportConfiguratorBase configurator
= null;
43 /** Reads data from an Excel file and stores them into a CDM DB.
46 * @param stores (not used)
49 protected boolean doInvoke(STATE state
){
51 boolean success
= true;
53 logger
.debug("Importing excel data");
55 configurator
= state
.getConfig();
57 NomenclaturalCode nc
= getConfigurator().getNomenclaturalCode();
58 if (nc
== null && needsNomenclaturalCode()) {
59 logger
.error("Nomenclatural code could not be determined. Skip invoke.");
62 // read and save all rows of the excel worksheet
63 URI source
= state
.getConfig().getSource();
64 String sheetName
= getWorksheetName();
66 recordList
= ExcelUtils
.parseXLS(source
, sheetName
);
67 } catch (FileNotFoundException e
) {
68 String message
= "File not found: " + source
;
69 warnProgress(state
, message
, e
);
70 logger
.error(message
);
74 success
&= handleRecordList(state
, source
);
76 logger
.debug("End excel data import");
82 protected boolean needsNomenclaturalCode() {
92 private boolean handleRecordList(STATE state
, URI source
) {
93 boolean success
= true;
94 Integer startingLine
= 2;
95 if (recordList
!= null) {
96 HashMap
<String
,String
> record
= null;
98 TransactionStatus txStatus
= startTransaction();
101 state
.setCurrentLine(startingLine
);
102 for (int i
= 0; i
< recordList
.size(); i
++) {
103 record
= recordList
.get(i
);
104 success
&= analyzeRecord(record
, state
);
106 success
&= firstPass(state
);
107 } catch (Exception e
) {
110 state
.incCurrentLine();
114 state
.setCurrentLine(startingLine
);
115 for (int i
= 0; i
< recordList
.size(); i
++) {
116 record
= recordList
.get(i
);
117 success
&= analyzeRecord(record
, state
);
118 success
&= secondPass(state
);
119 state
.incCurrentLine();
122 commitTransaction(txStatus
);
124 logger
.warn("No records found in " + source
);
130 * To define a worksheet name override this method. Otherwise the first worksheet is taken.
131 * @return worksheet name. <code>null</null> if not worksheet is defined.
133 protected String
getWorksheetName() {
138 protected boolean doCheck(STATE state
) {
139 boolean result
= true;
140 logger
.warn("No check implemented for Excel import");
150 protected abstract boolean analyzeRecord(HashMap
<String
,String
> record
, STATE state
);
152 protected abstract boolean firstPass(STATE state
);
153 protected abstract boolean secondPass(STATE state
);
156 public ExcelImportConfiguratorBase
getConfigurator() {
161 public CdmApplicationController
getApplicationController() {
166 protected int floatString2IntValue(String value
) {
169 Float fobj
= new Float(Float
.parseFloat(value
));
170 intValue
= fobj
.intValue();
171 if (logger
.isDebugEnabled()) { logger
.debug("Value formatted: " + intValue
); }
172 } catch (NumberFormatException ex
) {
173 logger
.error(value
+ " is not an integer");
178 protected String
floatString2IntStringValue(String value
) {
179 int i
= floatString2IntValue(value
);
180 return String
.valueOf(i
);
189 protected TimePeriod
getTimePeriod(String start
, String end
) {
190 String strPeriod
= CdmUtils
.concat(" - ", start
, end
);
191 TimePeriod result
= TimePeriod
.parseString(strPeriod
);