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.PresenceAbsenceTerm;
39
import eu.etaxonomy.cdm.model.description.TaxonDescription;
40
import eu.etaxonomy.cdm.model.location.NamedArea;
41
import eu.etaxonomy.cdm.model.name.IBotanicalName;
42
import eu.etaxonomy.cdm.model.name.Rank;
43
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
44
import eu.etaxonomy.cdm.model.reference.Reference;
45
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
46
import eu.etaxonomy.cdm.model.taxon.Classification;
47
import eu.etaxonomy.cdm.model.taxon.Taxon;
48
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
49
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
50
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
51

    
52

    
53
/**
54
 * @author a.mueller
55
 * @since 20.02.2010
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.eflora.centralAfrica.checklist.CentralAfricaChecklistImportBase#getMapping()
94
	 */
95
	@Override
96
    protected DbImportMapping getMapping() {
97
		if (mapping == null){
98
			mapping = new DbImportMapping();
99

    
100
 			mapping.addMapper(DbImportObjectCreationMapper.NewInstance(this, "pk", TAXON_NAMESPACE)); //id + tu_status
101

    
102
			UUID uuidKew = CentralAfricaChecklistTransformer.uuidAcceptedKew;
103
			mapping.addMapper(DbImportMarkerMapper.NewInstance("accepted kew", uuidKew, "Accepted Kew", "Accepted Kew", "Kew", null));
104

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

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

    
112
		return mapping;
113
	}
114

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

    
127

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

    
143

    
144
	@Override
145
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, CentralAfricaChecklistImportState state) {
146
		String nameSpace;
147
		Class<?> cdmClass;
148
		Set<String> idSet;
149
		Set<String> referenceIdSet = new HashSet<String>();
150

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

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

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

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

    
171
	@Override
172
	public TaxonBase<?> createObject(ResultSet rs, CentralAfricaChecklistImportState state) throws SQLException {
173
		IBotanicalName speciesName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
174

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

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

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

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

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

    
205

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

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

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

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

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

    
234
		return speciesTaxon;
235
	}
236

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

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

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

    
248
	}
249

    
250

    
251

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

    
264

    
265

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

    
273

    
274

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

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

    
289

    
290

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

    
299

    
300

    
301

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

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

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

    
350

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

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

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

    
369

    
370

    
371
}
(6-6/7)