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
.ExcelUtils
;
22 import eu
.etaxonomy
.cdm
.io
.common
.CdmImportBase
;
23 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
26 * @author a.babadshanjan
30 public abstract class ExcelImporterBase
<STATE
extends ExcelImportState
<?
extends ExcelImportConfiguratorBase
>> extends CdmImportBase
<ExcelImportConfiguratorBase
, STATE
> {
31 private static final Logger logger
= Logger
.getLogger(ExcelImporterBase
.class);
33 protected static final String SCIENTIFIC_NAME_COLUMN
= "ScientificName";
35 ArrayList
<HashMap
<String
, String
>> recordList
= null;
37 private CdmApplicationController appCtr
= null;
38 private ExcelImportConfiguratorBase configurator
= null;
41 /** Reads data from an Excel file and stores them into a CDM DB.
44 * @param stores (not used)
47 protected boolean doInvoke(STATE state
){
49 boolean success
= false;
51 logger
.debug("Importing excel data");
53 configurator
= state
.getConfig();
55 NomenclaturalCode nc
= getConfigurator().getNomenclaturalCode();
57 logger
.error("Nomenclatural code could not be determined.");
60 // read and save all rows of the excel worksheet
61 URI source
= state
.getConfig().getSource();
63 recordList
= ExcelUtils
.parseXLS(source
);
64 } catch (FileNotFoundException e
) {
65 String message
= "File not found: " + source
;
66 warnProgress(state
, message
, e
);
67 logger
.error(message
);
71 if (recordList
!= null) {
72 HashMap
<String
,String
> record
= null;
74 TransactionStatus txStatus
= startTransaction();
77 for (int i
= 0; i
< recordList
.size(); i
++) {
78 record
= recordList
.get(i
);
79 success
= analyzeRecord(record
, state
);
81 success
= firstPass(state
);
82 } catch (Exception e
) {
87 for (int i
= 0; i
< recordList
.size(); i
++) {
88 record
= recordList
.get(i
);
89 success
= analyzeRecord(record
, state
);
90 success
= secondPass(state
);
93 commitTransaction(txStatus
);
95 logger
.warn("No records found in " + source
);
99 logger
.debug("End excel data import");
101 } catch (Exception e
) {
102 logger
.error("Error closing the application context");
110 protected boolean doCheck(STATE state
) {
111 boolean result
= true;
112 logger
.warn("No check implemented for Excel import");
122 protected abstract boolean analyzeRecord(HashMap
<String
,String
> record
, STATE state
);
124 protected abstract boolean firstPass(STATE state
);
125 protected abstract boolean secondPass(STATE state
);
128 public ExcelImportConfiguratorBase
getConfigurator() {
133 public CdmApplicationController
getApplicationController() {
138 protected int floatString2IntValue(String value
) {
141 Float fobj
= new Float(Float
.parseFloat(value
));
142 intValue
= fobj
.intValue();
143 if (logger
.isDebugEnabled()) { logger
.debug("Value formatted: " + intValue
); }
144 } catch (NumberFormatException ex
) {
145 logger
.error(value
+ " is not an integer");
150 protected String
floatString2IntStringValue(String value
) {
151 int i
= floatString2IntValue(value
);
152 return String
.valueOf(i
);