add language to feature for NEEI
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / excel / common / ExcelImporterBase.java
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
27 /**
28 * @author a.babadshanjan
29 * @created 17.12.2008
30 * @version 1.0
31 */
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);
34
35 protected static final String SCIENTIFIC_NAME_COLUMN = "ScientificName";
36
37 ArrayList<HashMap<String, String>> recordList = null;
38
39 private CdmApplicationController appCtr = null;
40 private ExcelImportConfiguratorBase configurator = null;
41
42
43 /** Reads data from an Excel file and stores them into a CDM DB.
44 *
45 * @param config
46 * @param stores (not used)
47 */
48 @Override
49 protected boolean doInvoke(STATE state){
50
51 boolean success = true;
52
53 logger.debug("Importing excel data");
54
55 configurator = state.getConfig();
56
57 NomenclaturalCode nc = getConfigurator().getNomenclaturalCode();
58 if (nc == null && needsNomenclaturalCode()) {
59 logger.error("Nomenclatural code could not be determined. Skip invoke.");
60 return false;
61 }
62 // read and save all rows of the excel worksheet
63 URI source = state.getConfig().getSource();
64 String sheetName = getWorksheetName();
65 try {
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);
71 return false;
72 }
73
74 success &= handleRecordList(state, source);
75
76 logger.debug("End excel data import");
77
78
79 return success;
80 }
81
82 protected boolean needsNomenclaturalCode() {
83 return true;
84 }
85
86 /**
87 * @param state
88 * @param success
89 * @param source
90 * @return
91 */
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;
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 success &= analyzeRecord(record, state);
105 try {
106 success &= firstPass(state);
107 } catch (Exception e) {
108 e.printStackTrace();
109 }finally{
110 state.incCurrentLine();
111 }
112 }
113 //second pass
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();
120 }
121
122 commitTransaction(txStatus);
123 }else{
124 logger.warn("No records found in " + source);
125 }
126 return success;
127 }
128
129 /**
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.
132 */
133 protected String getWorksheetName() {
134 return null;
135 }
136
137 @Override
138 protected boolean doCheck(STATE state) {
139 boolean result = true;
140 logger.warn("No check implemented for Excel import");
141 return result;
142 }
143
144 /**
145 *
146 *
147 * @param record
148 * @return
149 */
150 protected abstract boolean analyzeRecord(HashMap<String,String> record, STATE state);
151
152 protected abstract boolean firstPass(STATE state);
153 protected abstract boolean secondPass(STATE state);
154
155
156 public ExcelImportConfiguratorBase getConfigurator() {
157 return configurator;
158 }
159
160
161 public CdmApplicationController getApplicationController() {
162 return appCtr;
163 }
164
165
166 protected int floatString2IntValue(String value) {
167 int intValue = 0;
168 try {
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");
174 }
175 return intValue;
176 }
177
178 protected String floatString2IntStringValue(String value) {
179 int i = floatString2IntValue(value);
180 return String.valueOf(i);
181 }
182
183
184 /**
185 * @param start
186 * @param end
187 * @return
188 */
189 protected TimePeriod getTimePeriod(String start, String end) {
190 String strPeriod = CdmUtils.concat(" - ", start, end);
191 TimePeriod result = TimePeriod.parseString(strPeriod);
192 return result;
193 }
194
195
196 }