Project

General

Profile

Download (13.2 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.TdwgAreaProvider;
29
import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
30
import eu.etaxonomy.cdm.io.common.mapping.DbImportMarkerMapper;
31
import eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapper;
32
import eu.etaxonomy.cdm.io.common.mapping.DbImportTaxIncludedInMapper;
33
import eu.etaxonomy.cdm.io.common.mapping.IMappingImport;
34
import eu.etaxonomy.cdm.io.eflora.centralAfrica.checklist.validation.CentralAfricaChecklistTaxonImportValidator;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
37
import eu.etaxonomy.cdm.model.description.Distribution;
38
import eu.etaxonomy.cdm.model.description.PresenceTerm;
39
import eu.etaxonomy.cdm.model.description.TaxonDescription;
40
import eu.etaxonomy.cdm.model.location.NamedArea;
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.Classification;
46
import eu.etaxonomy.cdm.model.taxon.Taxon;
47
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
48
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
49
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
50

    
51

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

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

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

    
90

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

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

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

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

    
141

    
142
	@Override
143
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, CentralAfricaChecklistImportState state) {
144
		String nameSpace;
145
		Class<?> cdmClass;
146
		Set<String> idSet;
147
		Set<String> referenceIdSet = new HashSet<String>();
148
		
149
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
150
		
151
		try{
152
			while (rs.next()){
153
				handleForeignKey(rs, referenceIdSet, "source");
154
			}
155

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

    
163
		} catch (SQLException e) {
164
			throw new RuntimeException(e);
165
		}
166
		return result;
167
	}
168
	
169
	@Override
170
	public TaxonBase<?> createObject(ResultSet rs, CentralAfricaChecklistImportState state) throws SQLException {
171
		BotanicalName speciesName = BotanicalName.NewInstance(Rank.SPECIES());
172
		
173
		Reference<?> sec = state.getConfig().getSourceReference();
174
		getReferenceService().saveOrUpdate(sec);
175
		
176
		String familyString = rs.getString("family");
177
		String genusString = rs.getString("genus");
178
		String speciesString = rs.getString("species");
179
		String authorityString = rs.getString("authority");
180
		
181
		if (logger.isDebugEnabled()){
182
			System.out.println(familyString + " " + genusString + " " + speciesString);
183
		}
184
		
185
		Taxon speciesTaxon = Taxon.NewInstance(speciesName, sec);;
186
		speciesName.setGenusOrUninomial(genusString);
187
		speciesName.setSpecificEpithet(speciesString);
188
		parser.handleAuthors(speciesName, CdmUtils.concat(" ", new String[] {"", genusString, speciesString, authorityString}), authorityString);
189
		
190
		//family
191
		Taxon familyTaxon = null;
192
		if (StringUtils.isNotBlank(familyString)){
193
			familyTaxon = getHigherTaxon(state, familyString, null);
194
			if (familyTaxon == null){
195
				BotanicalName familyName = BotanicalName.NewInstance(Rank.FAMILY());
196
				familyName.setGenusOrUninomial(familyString);
197
				familyTaxon = Taxon.NewInstance(familyName, sec);
198
				saveHigherTaxon(state, familyTaxon, familyString, null);
199
			}
200
			getTaxonService().saveOrUpdate(familyTaxon);	
201
		}
202
		
203
		
204
		//genus
205
		Taxon genusTaxon = getHigherTaxon(state, familyString, genusString);
206
		if (genusTaxon == null){
207
			BotanicalName genusName = BotanicalName.NewInstance(Rank.GENUS());
208
			genusName.setGenusOrUninomial(genusString);
209
			genusTaxon = Taxon.NewInstance(genusName, sec);
210
			saveHigherTaxon(state, genusTaxon, familyString, genusString);
211
			if (familyTaxon != null){
212
				makeTaxonomicallyIncluded(state, TREE_ID, genusTaxon, familyTaxon, null, null);
213
			}
214
		}
215
		makeTaxonomicallyIncluded(state, TREE_ID, speciesTaxon, genusTaxon, null, null);
216
		getTaxonService().saveOrUpdate(genusTaxon);
217

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

    
242
		addDistribution(description, isCongo, "ZAI");
243
		addDistribution(description, isBurundi, "BUR");
244
		addDistribution(description, isRwanda, "RWA");
245

    
246
	}
247

    
248

    
249

    
250
	/**
251
	 * @param description
252
	 * @param isCongo
253
	 */
254
	private void addDistribution(TaxonDescription description, Boolean exists, String label) {
255
		if (exists == true){
256
			NamedArea namedArea = TdwgAreaProvider.getAreaByTdwgAbbreviation(label);
257
			Distribution distribution = Distribution.NewInstance(namedArea, PresenceTerm.PRESENT());
258
			description.addElement(distribution);
259
		}
260
	}
261

    
262

    
263

    
264
	private void saveHigherTaxon(CentralAfricaChecklistImportState state, Taxon higherTaxon, String family, String genus) {
265
		String higherName = normalizeHigherTaxonName(family, genus);
266
		UUID uuid = higherTaxon.getUuid();
267
		state.putHigherTaxon(higherName, uuid);
268
		higherTaxonMap.put(uuid, higherTaxon);
269
	}
270

    
271

    
272

    
273
	private Taxon getHigherTaxon(CentralAfricaChecklistImportState state, String family, String genus) {
274
		String higherName = normalizeHigherTaxonName(family, genus);
275
		UUID uuid = state.getHigherTaxon(higherName);
276
		
277
		Taxon taxon = null;
278
		if (uuid != null){
279
			taxon = higherTaxonMap.get(uuid);
280
			if (taxon == null){
281
				taxon = CdmBase.deproxy(getTaxonService().find(uuid), Taxon.class);
282
			}
283
		}
284
		return taxon;
285
	}
286

    
287

    
288

    
289
	/**
290
	 * @param family
291
	 * @param genus
292
	 */
293
	private String normalizeHigherTaxonName(String family, String genus) {
294
		return (CdmUtils.Nz(family) + "-" + CdmUtils.Nz(genus)).trim();
295
	}
296

    
297

    
298

    
299

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

    
348

    
349
	private Reference<?> makeGenevaReference(CentralAfricaChecklistImportState state) {
350
		Reference<?> result = ReferenceFactory.newDatabase();
351
		result.setTitleCache(state.getConfig().getGenevaReferenceTitle(), true);
352
		result.setUuid(state.getConfig().getUuidGenevaReference());
353
		return result;
354
	}
355

    
356
	@Override
357
	protected boolean doCheck(CentralAfricaChecklistImportState state){
358
		IOValidator<CentralAfricaChecklistImportState> validator = new CentralAfricaChecklistTaxonImportValidator();
359
		return validator.validate(state);
360
	}
361
	
362
	@Override
363
	protected boolean isIgnore(CentralAfricaChecklistImportState state){
364
		return ! state.getConfig().isDoTaxa();
365
	}
366

    
367

    
368

    
369
}
(6-6/7)