Project

General

Profile

Download (13.5 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.checklist;
11

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

    
20
import org.apache.commons.lang.StringUtils;
21
import org.apache.log4j.Logger;
22
import org.springframework.stereotype.Component;
23

    
24
import eu.etaxonomy.cdm.api.service.IClassificationService;
25
import eu.etaxonomy.cdm.common.CdmUtils;
26
import eu.etaxonomy.cdm.io.common.IOValidator;
27
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
28
import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
29
import eu.etaxonomy.cdm.io.common.mapping.DbImportMarkerMapper;
30
import eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapper;
31
import eu.etaxonomy.cdm.io.common.mapping.DbImportTaxIncludedInMapper;
32
import eu.etaxonomy.cdm.io.common.mapping.IMappingImport;
33
import eu.etaxonomy.cdm.io.eflora.centralAfrica.checklist.validation.CentralAfricaChecklistTaxonImportValidator;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
36
import eu.etaxonomy.cdm.model.description.Distribution;
37
import eu.etaxonomy.cdm.model.description.PresenceTerm;
38
import eu.etaxonomy.cdm.model.description.TaxonDescription;
39
import eu.etaxonomy.cdm.model.location.NamedArea;
40
import eu.etaxonomy.cdm.model.location.TdwgArea;
41
import eu.etaxonomy.cdm.model.name.BotanicalName;
42
import eu.etaxonomy.cdm.model.name.Rank;
43
import eu.etaxonomy.cdm.model.reference.Reference;
44
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
45
import eu.etaxonomy.cdm.model.taxon.Taxon;
46
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
47
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
48
import eu.etaxonomy.cdm.model.taxon.Classification;
49
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
50

    
51

    
52
/**
53
 * @author a.mueller
54
 * @created 20.02.2010
55
 * @version 1.0
56
 */
57
@Component
58
public class CentralAfricaChecklistTaxonImport  extends CentralAfricaChecklistImportBase<TaxonBase> implements IMappingImport<TaxonBase, CentralAfricaChecklistImportState>{
59
	private static final Logger logger = Logger.getLogger(CentralAfricaChecklistTaxonImport.class);
60
	
61
	private NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
62
	
63
	private Map<UUID, Taxon> higherTaxonMap;
64
	
65
	private Integer TREE_ID = null;
66
	
67
	private DbImportMapping mapping;
68
	
69
	private int modCount = 10000;
70
	private static final String pluralString = "taxa";
71
	private static final String dbTableName = "checklist";
72
	private static final Class cdmTargetClass = TaxonBase.class;
73
	private static final String strOrderBy = " ORDER BY family, genus, species ";
74

    
75
	public CentralAfricaChecklistTaxonImport(){
76
		super(pluralString, dbTableName, cdmTargetClass);
77
	}
78
	
79
	
80

    
81
	/* (non-Javadoc)
82
	 * @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getIdQuery()
83
	 */
84
	@Override
85
	protected String getIdQuery() {
86
		String strQuery = " SELECT pk FROM " + dbTableName +
87
						strOrderBy;
88
		return strQuery;
89
	}
90

    
91

    
92
	/* (non-Javadoc)
93
	 * @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getMapping()
94
	 */
95
	protected DbImportMapping getMapping() {
96
		if (mapping == null){
97
			mapping = new DbImportMapping();
98
			
99
 			mapping.addMapper(DbImportObjectCreationMapper.NewInstance(this, "pk", TAXON_NAMESPACE)); //id + tu_status
100
		
101
			UUID uuidKew = CentralAfricaChecklistTransformer.uuidAcceptedKew;
102
			mapping.addMapper(DbImportMarkerMapper.NewInstance("accepted kew", uuidKew, "Accepted Kew", "Accepted Kew", "Kew"));
103
			
104
			UUID uuidGeneva = CentralAfricaChecklistTransformer.uuidAcceptedGeneva;
105
			mapping.addMapper(DbImportMarkerMapper.NewInstance("accepted geneva", uuidGeneva, "Accepted Geneva", "Accepted Geneva", "Geneva"));
106

    
107
			UUID uuidItis = CentralAfricaChecklistTransformer.uuidAcceptedItis;
108
			mapping.addMapper(DbImportMarkerMapper.NewInstance("accepted itis", uuidItis, "Accepted ITIS", "Accepted ITIS", "ITIS"));
109
		}
110
		
111
		return mapping;
112
	}
113

    
114
	/* (non-Javadoc)
115
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
116
	 */
117
	@Override
118
	protected String getRecordQuery(CentralAfricaChecklistImportConfigurator config) {
119
		String strSelect = " SELECT * ";
120
		String strFrom = " FROM checklist";
121
		String strWhere = " WHERE ( pk IN (" + ID_LIST_TOKEN + ") )";
122
		String strRecordQuery = strSelect + strFrom + strWhere + strOrderBy;
123
		return strRecordQuery;
124
	}
125

    
126
	
127
	@Override
128
	public boolean doPartition(ResultSetPartitioner partitioner, CentralAfricaChecklistImportState state) {
129
		higherTaxonMap = new HashMap<UUID, Taxon>();
130
		Reference genevaReference = getReferenceService().find(state.getConfig().getUuidGenevaReference());
131
		if (genevaReference == null){
132
			genevaReference = makeGenevaReference(state);
133
			getReferenceService().save(genevaReference);
134
		}
135
		state.setGenevaReference(genevaReference);
136
		boolean success = super.doPartition(partitioner, state);
137
		higherTaxonMap = new HashMap<UUID, Taxon>();
138
		state.setGenevaReference(null);
139
		return success;
140
	}
141

    
142

    
143
	/* (non-Javadoc)
144
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
145
	 */
146
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
147
		String nameSpace;
148
		Class cdmClass;
149
		Set<String> idSet;
150
		Set<String> referenceIdSet = new HashSet<String>();
151
		
152
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
153
		
154
		try{
155
			while (rs.next()){
156
				handleForeignKey(rs, referenceIdSet, "source");
157
			}
158

    
159
			//reference map
160
			nameSpace = REFERENCE_NAMESPACE;
161
			cdmClass = Reference.class;
162
			idSet = referenceIdSet;
163
			Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, referenceIdSet, nameSpace);
164
			result.put(REFERENCE_NAMESPACE, referenceMap);
165

    
166
		} catch (SQLException e) {
167
			throw new RuntimeException(e);
168
		}
169
		return result;
170
	}
171
	
172

    
173
	/* (non-Javadoc)
174
	 * @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet)
175
	 */
176
	public TaxonBase createObject(ResultSet rs, CentralAfricaChecklistImportState state) throws SQLException {
177
		BotanicalName speciesName = BotanicalName.NewInstance(Rank.SPECIES());
178
		
179
		Reference sec = state.getConfig().getSourceReference();
180
		getReferenceService().saveOrUpdate(sec);
181
		
182
		String familyString = rs.getString("family");
183
		String genusString = rs.getString("genus");
184
		String speciesString = rs.getString("species");
185
		String authorityString = rs.getString("authority");
186
		
187
		if (logger.isDebugEnabled()){
188
			System.out.println(familyString + " " + genusString + " " + speciesString);
189
		}
190
		
191
		Taxon speciesTaxon = Taxon.NewInstance(speciesName, sec);;
192
		speciesName.setGenusOrUninomial(genusString);
193
		speciesName.setSpecificEpithet(speciesString);
194
		parser.handleAuthors(speciesName, CdmUtils.concat(" ", new String[] {"", genusString, speciesString, authorityString}), authorityString);
195
		
196
		//family
197
		Taxon familyTaxon = null;
198
		if (StringUtils.isNotBlank(familyString)){
199
			familyTaxon = getHigherTaxon(state, familyString, null);
200
			if (familyTaxon == null){
201
				BotanicalName familyName = BotanicalName.NewInstance(Rank.FAMILY());
202
				familyName.setGenusOrUninomial(familyString);
203
				familyTaxon = Taxon.NewInstance(familyName, sec);
204
				saveHigherTaxon(state, familyTaxon, familyString, null);
205
			}
206
			getTaxonService().saveOrUpdate(familyTaxon);	
207
		}
208
		
209
		
210
		//genus
211
		Taxon genusTaxon = getHigherTaxon(state, familyString, genusString);
212
		if (genusTaxon == null){
213
			BotanicalName genusName = BotanicalName.NewInstance(Rank.GENUS());
214
			genusName.setGenusOrUninomial(genusString);
215
			genusTaxon = Taxon.NewInstance(genusName, sec);
216
			saveHigherTaxon(state, genusTaxon, familyString, genusString);
217
			if (familyTaxon != null){
218
				makeTaxonomicallyIncluded(state, TREE_ID, genusTaxon, familyTaxon, null, null);
219
			}
220
		}
221
		makeTaxonomicallyIncluded(state, TREE_ID, speciesTaxon, genusTaxon, null, null);
222
		getTaxonService().saveOrUpdate(genusTaxon);
223

    
224
		String sourceString = rs.getString("source");
225
		String sourceId = rs.getString("source_id");
226
		
227
		Reference sourceRef = state.getRelatedObject(REFERENCE_NAMESPACE, sourceString, Reference.class);
228
		speciesTaxon.addSource(sourceId, REFERENCE_NAMESPACE, sourceRef, null);
229
		
230
		
231
		//geneva id
232
		Reference genevaReference = state.getGenevaReference();
233
		Object genevaId = rs.getObject("geneva_ID");
234
		speciesTaxon.addSource(String.valueOf(genevaId), null, genevaReference, null);
235
		
236
		//distribution
237
		handleDistribution(rs, speciesTaxon);
238
		
239
		return speciesTaxon;
240
	}
241
	
242
	private void handleDistribution(ResultSet rs, Taxon speciesTaxon) throws SQLException {
243
		TaxonDescription description = TaxonDescription.NewInstance(speciesTaxon);
244
		
245
		Boolean isCongo = rs.getBoolean("drc");
246
		Boolean isBurundi = rs.getBoolean("burundi");
247
		Boolean isRwanda = rs.getBoolean("rwanda");
248

    
249
		addDistribution(description, isCongo, "ZAI");
250
		addDistribution(description, isBurundi, "BUR");
251
		addDistribution(description, isRwanda, "RWA");
252

    
253
	}
254

    
255

    
256

    
257
	/**
258
	 * @param description
259
	 * @param isCongo
260
	 */
261
	private void addDistribution(TaxonDescription description, Boolean exists, String label) {
262
		if (exists == true){
263
			NamedArea namedArea = TdwgArea.getAreaByTdwgAbbreviation(label);
264
			Distribution distribution = Distribution.NewInstance(namedArea, PresenceTerm.PRESENT());
265
			description.addElement(distribution);
266
		}
267
	}
268

    
269

    
270

    
271
	private void saveHigherTaxon(CentralAfricaChecklistImportState state, Taxon higherTaxon, String family, String genus) {
272
		String higherName = normalizeHigherTaxonName(family, genus);
273
		UUID uuid = higherTaxon.getUuid();
274
		state.putHigherTaxon(higherName, uuid);
275
		higherTaxonMap.put(uuid, higherTaxon);
276
	}
277

    
278

    
279

    
280
	private Taxon getHigherTaxon(CentralAfricaChecklistImportState state, String family, String genus) {
281
		String higherName = normalizeHigherTaxonName(family, genus);
282
		UUID uuid = state.getHigherTaxon(higherName);
283
		
284
		Taxon taxon = null;
285
		if (uuid != null){
286
			taxon = higherTaxonMap.get(uuid);
287
			if (taxon == null){
288
				taxon = CdmBase.deproxy(getTaxonService().find(uuid), Taxon.class);
289
			}
290
		}
291
		return taxon;
292
	}
293

    
294

    
295

    
296
	/**
297
	 * @param family
298
	 * @param genus
299
	 */
300
	private String normalizeHigherTaxonName(String family, String genus) {
301
		return (CdmUtils.Nz(family) + "-" + CdmUtils.Nz(genus)).trim();
302
	}
303

    
304

    
305

    
306

    
307
//	private boolean makeTaxonomicallyIncluded(CentralAfricaChecklistImportState state, Taxon parent, Taxon child, Reference citation, String microCitation){
308
//		Reference sec = child.getSec();
309
//		UUID uuid = state.getTreeUuid(sec);
310
//		Classification tree;
311
//		tree = state.getTree(sec);
312
//		
313
//		if (tree == null){
314
//			tree = makeTreeMemSave(state, sec);
315
//		}
316
//		TaxonNode childNode;
317
//		if (parent != null){
318
//			childNode = tree.addParentChild(parent, child, citation, microCitation);
319
//		}else{
320
//			childNode = tree.addChildTaxon(child, citation, microCitation, null);
321
//		}
322
//		return (childNode != null);
323
//	}
324
	
325
	//TODO use Mapper
326
	private boolean makeTaxonomicallyIncluded(CentralAfricaChecklistImportState state, Integer treeRefFk, Taxon child, Taxon parent, Reference citation, String microCitation){
327
		String treeKey;
328
		UUID treeUuid;
329
		if (treeRefFk == null){
330
			treeKey = "1";  // there is only one tree and it gets the map key '1'
331
			treeUuid = state.getConfig().getClassificationUuid();
332
		}else{
333
			treeKey =String.valueOf(treeRefFk);
334
			treeUuid = state.getTreeUuidByTreeKey(treeKey);
335
		}
336
		Classification tree = (Classification)state.getRelatedObject(DbImportTaxIncludedInMapper.TAXONOMIC_TREE_NAMESPACE, treeKey);
337
		if (tree == null){
338
			IClassificationService service = state.getCurrentIO().getClassificationService();
339
			tree = service.getClassificationByUuid(treeUuid);
340
			if (tree == null){
341
				String treeName = state.getConfig().getClassificationName();
342
				tree = Classification.NewInstance(treeName);
343
				tree.setUuid(treeUuid);
344
				//FIXME tree reference
345
				//tree.setReference(ref);
346
				service.save(tree);
347
			}
348
			state.addRelatedObject(DbImportTaxIncludedInMapper.TAXONOMIC_TREE_NAMESPACE, treeKey, tree);
349
		}
350
		
351
		TaxonNode childNode = tree.addParentChild(parent, child, citation, microCitation);
352
		return (childNode != null);
353
	}
354

    
355

    
356
	private Reference makeGenevaReference(CentralAfricaChecklistImportState state) {
357
		Reference result = ReferenceFactory.newDatabase();
358
		result.setTitleCache(state.getConfig().getGenevaReferenceTitle(), true);
359
		result.setUuid(state.getConfig().getUuidGenevaReference());
360
		return result;
361
	}
362

    
363
	/* (non-Javadoc)
364
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
365
	 */
366
	@Override
367
	protected boolean doCheck(CentralAfricaChecklistImportState state){
368
		IOValidator<CentralAfricaChecklistImportState> validator = new CentralAfricaChecklistTaxonImportValidator();
369
		return validator.validate(state);
370
	}
371
	
372
	
373
	/* (non-Javadoc)
374
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
375
	 */
376
	protected boolean isIgnore(CentralAfricaChecklistImportState state){
377
		return ! state.getConfig().isDoTaxa();
378
	}
379

    
380

    
381

    
382
}
(6-6/7)