Project

General

Profile

Download (5.13 KB) Statistics
| Branch: | Tag: | Revision:
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 void doInvoke(STATE state){
50
		
51
		logger.debug("Importing excel data");
52
    	
53
    	configurator = state.getConfig();
54
    	
55
		NomenclaturalCode nc = getConfigurator().getNomenclaturalCode();
56
		if (nc == null && needsNomenclaturalCode()) {
57
			logger.error("Nomenclatural code could not be determined. Skip invoke.");
58
			state.setUnsuccessfull();
59
			return;
60
		}
61
		// read and save all rows of the excel worksheet
62
		URI source = state.getConfig().getSource();
63
		String sheetName = getWorksheetName();
64
		try {
65
			recordList = ExcelUtils.parseXLS(source, sheetName);
66
		} catch (FileNotFoundException e) {
67
			String message = "File not found: " + source;
68
			warnProgress(state, message, e);
69
			logger.error(message);
70
			state.setUnsuccessfull();
71
			return;
72
		}
73
    	
74
    	handleRecordList(state, source);
75
    	logger.debug("End excel data import"); 
76
    	return;
77
	}
78

    
79
	protected boolean needsNomenclaturalCode() {
80
		return true;
81
	}
82

    
83
	/**
84
	 * @param state
85
	 * @param success
86
	 * @param source
87
	 * @return
88
	 */
89
	private void handleRecordList(STATE state, URI source) {
90
		Integer startingLine = 2;
91
		if (recordList != null) {
92
    		HashMap<String,String> record = null;
93
    		
94
    		TransactionStatus txStatus = startTransaction();
95

    
96
    		//first pass
97
    		state.setCurrentLine(startingLine);
98
    		for (int i = 0; i < recordList.size(); i++) {
99
    			record = recordList.get(i);
100
    			analyzeRecord(record, state);
101
    			try {
102
					firstPass(state);
103
				} catch (Exception e) {
104
					e.printStackTrace();
105
				}finally{
106
					state.incCurrentLine();
107
				}
108
    		}
109
    		//second pass
110
    		state.setCurrentLine(startingLine);
111
    		for (int i = 0; i < recordList.size(); i++) {
112
    			record = recordList.get(i);
113
    			analyzeRecord(record, state);
114
    			secondPass(state);
115
    			state.incCurrentLine();
116
    	   	}
117
    		
118
    		commitTransaction(txStatus);
119
    	}else{
120
    		logger.warn("No records found in " + source);
121
    	}
122
		return;
123
	}
124

    
125
	/**
126
	 * To define a worksheet name override this method. Otherwise the first worksheet is taken.
127
	 * @return worksheet name. <code>null</null> if not worksheet is defined.
128
	 */
129
	protected String getWorksheetName() {
130
		return null;
131
	}
132

    
133
	@Override
134
	protected boolean doCheck(STATE state) {
135
		boolean result = true;
136
		logger.warn("No check implemented for Excel import");
137
		return result;
138
	}
139
	
140
	/** 
141
	 * 
142
	 * 
143
	 * @param record
144
	 * @return
145
	 */
146
	protected abstract void analyzeRecord(HashMap<String,String> record, STATE state);
147
	
148
	protected abstract void firstPass(STATE state);
149
	protected abstract void secondPass(STATE state);
150
	
151
	
152
	public ExcelImportConfiguratorBase getConfigurator() {
153
		return configurator;
154
	}
155
	
156
	
157
	public CdmApplicationController getApplicationController() {
158
		return appCtr;
159
	}
160
	
161
	
162
	protected int floatString2IntValue(String value) {
163
		int intValue = 0;
164
		try {
165
			Float fobj = new Float(Float.parseFloat(value));
166
			intValue = fobj.intValue();
167
			if (logger.isDebugEnabled()) { logger.debug("Value formatted: " + intValue); }
168
		} catch (NumberFormatException ex) {
169
			logger.error(value + " is not an integer");
170
		}
171
		return intValue;
172
	}
173
	
174
	protected String floatString2IntStringValue(String value) {
175
		int i = floatString2IntValue(value);
176
		return String.valueOf(i);
177
	}
178
	
179

    
180
	/**
181
	 * @param start
182
	 * @param end
183
	 * @return
184
	 */
185
	protected TimePeriod getTimePeriod(String start, String end) {
186
		String strPeriod = CdmUtils.concat(" - ", start, end);
187
		TimePeriod result = TimePeriod.parseString(strPeriod);
188
		return result;
189
	}
190

    
191

    
192
}
(3-3/5)