Project

General

Profile

Download (13.1 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.PresenceAbsenceTerm;
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
	@Override
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", null));
103

    
104
			UUID uuidGeneva = CentralAfricaChecklistTransformer.uuidAcceptedGeneva;
105
			mapping.addMapper(DbImportMarkerMapper.NewInstance("accepted geneva", uuidGeneva, "Accepted Geneva", "Accepted Geneva", "Geneva", null));
106

    
107
			UUID uuidItis = CentralAfricaChecklistTransformer.uuidAcceptedItis;
108
			mapping.addMapper(DbImportMarkerMapper.NewInstance("accepted itis", uuidItis, "Accepted ITIS", "Accepted ITIS", "ITIS", null));
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
	@Override
144
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, CentralAfricaChecklistImportState state) {
145
		String nameSpace;
146
		Class<?> cdmClass;
147
		Set<String> idSet;
148
		Set<String> referenceIdSet = new HashSet<String>();
149

    
150
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
151

    
152
		try{
153
			while (rs.next()){
154
				handleForeignKey(rs, referenceIdSet, "source");
155
			}
156

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

    
164
		} catch (SQLException e) {
165
			throw new RuntimeException(e);
166
		}
167
		return result;
168
	}
169

    
170
	@Override
171
	public TaxonBase<?> createObject(ResultSet rs, CentralAfricaChecklistImportState state) throws SQLException {
172
		BotanicalName speciesName = BotanicalName.NewInstance(Rank.SPECIES());
173

    
174
		Reference sec = state.getConfig().getSourceReference();
175
		getReferenceService().saveOrUpdate(sec);
176

    
177
		String familyString = rs.getString("family");
178
		String genusString = rs.getString("genus");
179
		String speciesString = rs.getString("species");
180
		String authorityString = rs.getString("authority");
181

    
182
		if (logger.isDebugEnabled()){
183
			System.out.println(familyString + " " + genusString + " " + speciesString);
184
		}
185

    
186
		Taxon speciesTaxon = Taxon.NewInstance(speciesName, sec);;
187
		speciesName.setGenusOrUninomial(genusString);
188
		speciesName.setSpecificEpithet(speciesString);
189
		parser.handleAuthors(speciesName, CdmUtils.concat(" ", new String[] {"", genusString, speciesString, authorityString}), authorityString);
190

    
191
		//family
192
		Taxon familyTaxon = null;
193
		if (StringUtils.isNotBlank(familyString)){
194
			familyTaxon = getHigherTaxon(state, familyString, null);
195
			if (familyTaxon == null){
196
				BotanicalName familyName = BotanicalName.NewInstance(Rank.FAMILY());
197
				familyName.setGenusOrUninomial(familyString);
198
				familyTaxon = Taxon.NewInstance(familyName, sec);
199
				saveHigherTaxon(state, familyTaxon, familyString, null);
200
			}
201
			getTaxonService().saveOrUpdate(familyTaxon);
202
		}
203

    
204

    
205
		//genus
206
		Taxon genusTaxon = getHigherTaxon(state, familyString, genusString);
207
		if (genusTaxon == null){
208
			BotanicalName genusName = BotanicalName.NewInstance(Rank.GENUS());
209
			genusName.setGenusOrUninomial(genusString);
210
			genusTaxon = Taxon.NewInstance(genusName, sec);
211
			saveHigherTaxon(state, genusTaxon, familyString, genusString);
212
			if (familyTaxon != null){
213
				makeTaxonomicallyIncluded(state, TREE_ID, genusTaxon, familyTaxon, null, null);
214
			}
215
		}
216
		makeTaxonomicallyIncluded(state, TREE_ID, speciesTaxon, genusTaxon, null, null);
217
		getTaxonService().saveOrUpdate(genusTaxon);
218

    
219
		String sourceString = rs.getString("source");
220
		String sourceId = rs.getString("source_id");
221

    
222
		Reference sourceRef = state.getRelatedObject(REFERENCE_NAMESPACE, sourceString, Reference.class);
223
		speciesTaxon.addSource(OriginalSourceType.Import, sourceId, REFERENCE_NAMESPACE, sourceRef, null);
224

    
225
		//geneva id
226
		Reference genevaReference = state.getGenevaReference();
227
		Object genevaId = rs.getObject("geneva_ID");
228
		speciesTaxon.addSource(OriginalSourceType.Import, String.valueOf(genevaId), null, genevaReference, null);
229

    
230
		//distribution
231
		handleDistribution(rs, speciesTaxon);
232

    
233
		return speciesTaxon;
234
	}
235

    
236
	private void handleDistribution(ResultSet rs, Taxon speciesTaxon) throws SQLException {
237
		TaxonDescription description = TaxonDescription.NewInstance(speciesTaxon);
238

    
239
		Boolean isCongo = rs.getBoolean("drc");
240
		Boolean isBurundi = rs.getBoolean("burundi");
241
		Boolean isRwanda = rs.getBoolean("rwanda");
242

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

    
247
	}
248

    
249

    
250

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

    
263

    
264

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

    
272

    
273

    
274
	private Taxon getHigherTaxon(CentralAfricaChecklistImportState state, String family, String genus) {
275
		String higherName = normalizeHigherTaxonName(family, genus);
276
		UUID uuid = state.getHigherTaxon(higherName);
277

    
278
		Taxon taxon = null;
279
		if (uuid != null){
280
			taxon = higherTaxonMap.get(uuid);
281
			if (taxon == null){
282
				taxon = CdmBase.deproxy(getTaxonService().find(uuid), Taxon.class);
283
			}
284
		}
285
		return taxon;
286
	}
287

    
288

    
289

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

    
298

    
299

    
300

    
301
//	private boolean makeTaxonomicallyIncluded(CentralAfricaChecklistImportState state, Taxon parent, Taxon child, Reference citation, String microCitation){
302
//		Reference sec = child.getSec();
303
//		UUID uuid = state.getTreeUuid(sec);
304
//		Classification tree;
305
//		tree = state.getTree(sec);
306
//
307
//		if (tree == null){
308
//			tree = makeTreeMemSave(state, sec);
309
//		}
310
//		TaxonNode childNode;
311
//		if (parent != null){
312
//			childNode = tree.addParentChild(parent, child, citation, microCitation);
313
//		}else{
314
//			childNode = tree.addChildTaxon(child, citation, microCitation, null);
315
//		}
316
//		return (childNode != null);
317
//	}
318

    
319
	//TODO use Mapper
320
	private boolean makeTaxonomicallyIncluded(CentralAfricaChecklistImportState state, Integer treeRefFk, Taxon child, Taxon parent, Reference citation, String microCitation){
321
		String treeKey;
322
		UUID treeUuid;
323
		if (treeRefFk == null){
324
			treeKey = "1";  // there is only one tree and it gets the map key '1'
325
			treeUuid = state.getConfig().getClassificationUuid();
326
		}else{
327
			treeKey =String.valueOf(treeRefFk);
328
			treeUuid = state.getTreeUuidByTreeKey(treeKey);
329
		}
330
		Classification tree = (Classification)state.getRelatedObject(DbImportTaxIncludedInMapper.TAXONOMIC_TREE_NAMESPACE, treeKey);
331
		if (tree == null){
332
			IClassificationService service = state.getCurrentIO().getClassificationService();
333
			tree = service.find(treeUuid);
334
			if (tree == null){
335
				String treeName = state.getConfig().getClassificationName();
336
				tree = Classification.NewInstance(treeName);
337
				tree.setUuid(treeUuid);
338
				//FIXME tree reference
339
				//tree.setReference(ref);
340
				service.save(tree);
341
			}
342
			state.addRelatedObject(DbImportTaxIncludedInMapper.TAXONOMIC_TREE_NAMESPACE, treeKey, tree);
343
		}
344

    
345
		TaxonNode childNode = tree.addParentChild(parent, child, citation, microCitation);
346
		return (childNode != null);
347
	}
348

    
349

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

    
357
	@Override
358
	protected boolean doCheck(CentralAfricaChecklistImportState state){
359
		IOValidator<CentralAfricaChecklistImportState> validator = new CentralAfricaChecklistTaxonImportValidator();
360
		return validator.validate(state);
361
	}
362

    
363
	@Override
364
	protected boolean isIgnore(CentralAfricaChecklistImportState state){
365
		return ! state.getConfig().isDoTaxa();
366
	}
367

    
368

    
369

    
370
}
(6-6/7)