Project

General

Profile

Download (8.75 KB) Statistics
| Branch: | 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.eflora.centralAfrica.ferns;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.HashSet;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import org.apache.commons.lang.StringUtils;
19
import org.apache.log4j.Logger;
20

    
21
import eu.etaxonomy.cdm.io.common.CdmImportBase;
22
import eu.etaxonomy.cdm.io.common.ICdmIO;
23
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
24
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
25
import eu.etaxonomy.cdm.io.common.Source;
26
import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.name.IBotanicalName;
29
import eu.etaxonomy.cdm.model.name.Rank;
30
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
31

    
32
/**
33
 * @author a.mueller
34
 * @since 20.03.2008
35
 * @version 1.0
36
 */
37
public abstract class CentralAfricaFernsImportBase<CDM_BASE extends CdmBase> extends CdmImportBase<CentralAfricaFernsImportConfigurator, CentralAfricaFernsImportState> implements ICdmIO<CentralAfricaFernsImportState>, IPartitionedIO<CentralAfricaFernsImportState> {
38
	private static final Logger logger = Logger.getLogger(CentralAfricaFernsImportBase.class);
39

    
40
	public static final UUID ID_IN_SOURCE_EXT_UUID = UUID.fromString("23dac094-e793-40a4-bad9-649fc4fcfd44");
41

    
42
	protected static final String TAXON_NAMESPACE = "African_pteridophytes_Taxon";
43
	protected static final String NAME_NAMESPACE = "African_pteridophytes_Name";
44
	protected static final String HIGHER_TAXON_NAMESPACE = "African_pteridophytes_Higher_Taxon";
45

    
46
	private NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
47

    
48

    
49
	private String pluralString;
50
	private String dbTableName;
51
	//TODO needed?
52
	private Class cdmTargetClass;
53

    
54

    
55

    
56

    
57
	/**
58
	 * @param dbTableName
59
	 * @param dbTableName2
60
	 */
61
	public CentralAfricaFernsImportBase(String pluralString, String dbTableName, Class cdmTargetClass) {
62
		this.pluralString = pluralString;
63
		this.dbTableName = dbTableName;
64
		this.cdmTargetClass = cdmTargetClass;
65
	}
66

    
67
	@Override
68
    protected void doInvoke(CentralAfricaFernsImportState state){
69
		logger.info("start make " + getPluralString() + " ...");
70
		CentralAfricaFernsImportConfigurator config = state.getConfig();
71
		Source source = config.getSource();
72

    
73
		String strIdQuery = getIdQuery();
74
		String strRecordQuery = getRecordQuery(config);
75

    
76
		int recordsPerTransaction = config.getRecordsPerTransaction();
77
		try{
78
			ResultSetPartitioner partitioner = ResultSetPartitioner.NewInstance(source, strIdQuery, strRecordQuery, recordsPerTransaction);
79
			while (partitioner.nextPartition()){
80
				partitioner.doPartition(this, state);
81
			}
82
		} catch (SQLException e) {
83
			logger.error("SQLException:" +  e);
84
			state.setUnsuccessfull();
85
			return;
86
		}
87

    
88
		logger.info("end make " + getPluralString() + " ... " + getSuccessString(true));
89
		return;
90
	}
91

    
92
	@Override
93
    public boolean doPartition(ResultSetPartitioner partitioner, CentralAfricaFernsImportState state) {
94
		boolean success = true ;
95
		Set objectsToSave = new HashSet();
96

    
97
 		DbImportMapping<?, ?> mapping = getMapping();
98
		mapping.initialize(state, cdmTargetClass);
99

    
100
		ResultSet rs = partitioner.getResultSet();
101
		try{
102
			while (rs.next()){
103
				success &= mapping.invoke(rs,objectsToSave);
104
			}
105
		} catch (SQLException e) {
106
			logger.error("SQLException:" +  e);
107
			return false;
108
		}
109

    
110
		partitioner.startDoSave();
111
		getCommonService().save(objectsToSave);
112
		return success;
113
	}
114

    
115

    
116

    
117
	/**
118
	 * @return
119
	 */
120
	protected abstract DbImportMapping<?, ?> getMapping();
121

    
122
	/**
123
	 * @return
124
	 */
125
	protected abstract String getRecordQuery(CentralAfricaFernsImportConfigurator config);
126

    
127
	/**
128
	 * @return
129
	 */
130
	protected String getIdQuery(){
131
		String result = " SELECT id FROM " + getTableName();
132
		return result;
133
	}
134

    
135
	/* (non-Javadoc)
136
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getPluralString()
137
	 */
138
	@Override
139
    public String getPluralString(){
140
		return pluralString;
141
	}
142

    
143
	/**
144
	 * @return
145
	 */
146
	protected String getTableName(){
147
		return this.dbTableName;
148
	}
149

    
150

    
151
	/**
152
	 * Reads a foreign key field from the result set and adds its value to the idSet.
153
	 * @param rs
154
	 * @param teamIdSet
155
	 * @throws SQLException
156
	 */
157
	protected void handleForeignKey(ResultSet rs, Set<String> idSet, String attributeName)
158
			throws SQLException {
159
		Object idObj = rs.getObject(attributeName);
160
		if (idObj != null){
161
			String id  = String.valueOf(idObj);
162
			idSet.add(id);
163
		}
164
	}
165

    
166
	/**
167
	 * Returns true if i is a multiple of recordsPerTransaction
168
	 * @param i
169
	 * @param recordsPerTransaction
170
	 * @return
171
	 */
172
	protected boolean loopNeedsHandling(int i, int recordsPerLoop) {
173
		startTransaction();
174
		return (i % recordsPerLoop) == 0;
175
	}
176

    
177
	protected void doLogPerLoop(int count, int recordsPerLog, String pluralString){
178
		if ((count % recordsPerLog ) == 0 && count!= 0 ){ logger.info(pluralString + " handled: " + (count));}
179
	}
180

    
181

    
182

    
183
	protected void setAuthor(IBotanicalName taxonName, ResultSet rs, String taxonNumber, boolean isHigherTaxon) throws SQLException {
184

    
185
		String authorsFull = null;
186
		String authorsAbbrev = null;
187
		if (! isHigherTaxon){
188
			authorsFull = rs.getString("Author/s - full");
189
			authorsAbbrev = rs.getString("Author/s - abbreviated");
190
		}
191

    
192
		Rank rank = taxonName.getRank();
193
		String authorString = null;
194
		if (rank != null){
195
			if (rank.equals(Rank.ORDER())){
196
				authorString =  rs.getString("Order name author");
197
			}else if (rank.equals(Rank.SUBORDER())){
198
				authorString = rs.getString("Suborder name author");
199
			}else if (rank.equals(Rank.FAMILY())){
200
				authorString = rs.getString("Family name author");
201
			}else if (rank.equals(Rank.SUBFAMILY())){
202
				authorString = rs.getString("Subfamily name author");
203
			}else if (rank.equals(Rank.TRIBE())){
204
				authorString = rs.getString("Tribus author");
205
			}else if (rank.equals(Rank.SUBTRIBE())){
206
				authorString = rs.getString("Subtribus author");
207
			}else if (rank.equals(Rank.SECTION_BOTANY())){
208
				authorString = rs.getString("Section name author");
209
			}else if (rank.equals(Rank.SUBSECTION_BOTANY())){
210
				authorString = rs.getString("Subsection author");
211
			}else if (rank.equals(Rank.GENUS())){
212
				authorString = rs.getString("Genus name author");
213
			}else if (rank.equals(Rank.SUBGENUS())){
214
				authorString = rs.getString("Subgenus name author");
215
			}else if (rank.equals(Rank.SERIES())){
216
				authorString = rs.getString("Series name author");
217
			}else if (rank.equals(Rank.SPECIES())){
218
				authorString =  rs.getString("Specific epithet author");
219
			}else if (rank.equals(Rank.SUBSPECIES())){
220
				authorString = rs.getString("Subspecies author");
221
			}else if (rank.equals(Rank.VARIETY())){
222
				authorString =  rs.getString("Variety name author");
223
			}else if (rank.equals(Rank.SUBVARIETY())){
224
				authorString = rs.getString("Subvariety author");
225
			}else if (rank.equals(Rank.FORM())){
226
				authorString = rs.getString("Forma name author");
227
			}else if (rank.equals(Rank.SUBFORM())){
228
				authorString = rs.getString("Subforma author");
229
			}else{
230
				logger.warn("Author string could not be defined");
231
				if (! isHigherTaxon){
232
					authorString = authorsAbbrev;
233
					if (StringUtils.isBlank(authorString)){
234
						logger.warn("Authors abbrev string could not be defined");
235
						authorString = authorsFull;
236
					}
237
				}
238
			}
239
		}else{
240
			logger.warn(taxonNumber + ": Rank is null");
241
			authorString = authorsAbbrev;
242
			if (StringUtils.isBlank(authorString)){
243
				logger.warn(taxonNumber + ": Authors abbrev string could not be defined");
244
				authorString = authorsFull;
245
			}
246
		}
247

    
248
		if (StringUtils.isNotBlank(authorString)){
249
			parser.handleAuthors(taxonName, taxonName.getNameCache().trim() + " " + authorString, authorString);
250
		}
251
		if (! isHigherTaxon){
252
			String combinationAuthor = taxonName.getCombinationAuthorship()==null ? "" :taxonName.getCombinationAuthorship().getNomenclaturalTitle();
253
			if (StringUtils.isNotBlank(authorsAbbrev) && ! authorsAbbrev.equalsIgnoreCase(combinationAuthor)){
254
				//it is expected that the fullAuthor and the abbrevAuthor are the combination authors but very often it is not
255
				logger.warn(taxonNumber + ": Rank author and abbrev author are not equal: " + authorString + "\t<-> " + combinationAuthor + "\t<-> " + authorsAbbrev);
256
			}
257
	//		if (StringUtils.isNotBlank(authorsFull) && ! authorsFull.equalsIgnoreCase(authorString)){
258
	//			logger.warn("Rank author and full author are not equal Rankauthor: " + authorString + ", full author " + authorsFull);
259
	//		}
260
		}
261
	}
262

    
263

    
264

    
265

    
266

    
267
}
(1-1/7)