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