Project

General

Profile

Download (11.4 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.redlist;
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.redlist.validation.RoteListeDbTaxonImportValidator;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.description.Distribution;
36
import eu.etaxonomy.cdm.model.description.PresenceTerm;
37
import eu.etaxonomy.cdm.model.description.TaxonDescription;
38
import eu.etaxonomy.cdm.model.location.NamedArea;
39
import eu.etaxonomy.cdm.model.location.TdwgArea;
40
import eu.etaxonomy.cdm.model.name.BotanicalName;
41
import eu.etaxonomy.cdm.model.name.Rank;
42
import eu.etaxonomy.cdm.model.reference.Reference;
43
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
44
import eu.etaxonomy.cdm.model.taxon.Classification;
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.strategy.parser.NonViralNameParserImpl;
49

    
50

    
51
/**
52
 * @author a.mueller
53
 * @created 27.08.2012
54
 * @version 1.0
55
 */
56
@Component
57
public class RoteListeDbTaxonImport  extends RoteListeDbImportBase<TaxonBase> implements IMappingImport<TaxonBase, RoteListeDbImportState>{
58
	private static final Logger logger = Logger.getLogger(RoteListeDbTaxonImport.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 RoteListeDbTaxonImport(){
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.erms.ErmsImportBase#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 = RoteListeDbTransformer.uuidAcceptedKew;
101
//			mapping.addMapper(DbImportMarkerMapper.NewInstance("accepted kew", uuidKew, "Accepted Kew", "Accepted Kew", "Kew", null));
102
			
103
		}
104
		
105
		return mapping;
106
	}
107

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

    
120
	
121
	@Override
122
	public boolean doPartition(ResultSetPartitioner partitioner, RoteListeDbImportState state) {
123
//		higherTaxonMap = new HashMap<UUID, Taxon>();
124
//		Reference genevaReference = getReferenceService().find(state.getConfig().getUuidGenevaReference());
125
//		if (genevaReference == null){
126
//			genevaReference = makeGenevaReference(state);
127
//			getReferenceService().save(genevaReference);
128
//		}
129
//		state.setGenevaReference(genevaReference);
130
		boolean success = super.doPartition(partitioner, state);
131
//		higherTaxonMap = new HashMap<UUID, Taxon>();
132
//		state.setGenevaReference(null);
133
		return success;
134
	}
135

    
136

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

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

    
160
		} catch (SQLException e) {
161
			throw new RuntimeException(e);
162
		}
163
		return result;
164
	}
165
	
166

    
167
	/* (non-Javadoc)
168
	 * @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet)
169
	 */
170
	public TaxonBase createObject(ResultSet rs, RoteListeDbImportState 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(sourceId, REFERENCE_NAMESPACE, sourceRef, null);
223
//		
224
//		
225
//		//geneva id
226
//		Reference genevaReference = state.getGenevaReference();
227
//		Object genevaId = rs.getObject("geneva_ID");
228
//		speciesTaxon.addSource(String.valueOf(genevaId), null, genevaReference, null);
229
//		
230
//		//distribution
231
//		handleDistribution(rs, speciesTaxon);
232
//		
233
//		return speciesTaxon;
234
		
235
		return null;
236
	}
237
	
238
	private void handleDistribution(ResultSet rs, Taxon speciesTaxon) throws SQLException {
239
		TaxonDescription description = TaxonDescription.NewInstance(speciesTaxon);
240
		
241
		Boolean isCongo = rs.getBoolean("drc");
242
		Boolean isBurundi = rs.getBoolean("burundi");
243
		Boolean isRwanda = rs.getBoolean("rwanda");
244

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

    
249
	}
250

    
251

    
252

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

    
265

    
266
	
267
	//TODO use Mapper
268
	private boolean makeTaxonomicallyIncluded(RoteListeDbImportState state, Integer treeRefFk, Taxon child, Taxon parent, Reference citation, String microCitation){
269
		String treeKey;
270
		UUID treeUuid;
271
		if (treeRefFk == null){
272
			treeKey = "1";  // there is only one tree and it gets the map key '1'
273
			treeUuid = state.getConfig().getClassificationUuid();
274
		}else{
275
			treeKey =String.valueOf(treeRefFk);
276
			treeUuid = state.getTreeUuidByTreeKey(treeKey);
277
		}
278
		Classification tree = (Classification)state.getRelatedObject(DbImportTaxIncludedInMapper.TAXONOMIC_TREE_NAMESPACE, treeKey);
279
		if (tree == null){
280
			IClassificationService service = state.getCurrentIO().getClassificationService();
281
			tree = service.find(treeUuid);
282
			if (tree == null){
283
				String treeName = state.getConfig().getClassificationName();
284
				tree = Classification.NewInstance(treeName);
285
				tree.setUuid(treeUuid);
286
				//FIXME tree reference
287
				//tree.setReference(ref);
288
				service.save(tree);
289
			}
290
			state.addRelatedObject(DbImportTaxIncludedInMapper.TAXONOMIC_TREE_NAMESPACE, treeKey, tree);
291
		}
292
		
293
		TaxonNode childNode = tree.addParentChild(parent, child, citation, microCitation);
294
		return (childNode != null);
295
	}
296

    
297

    
298
//	private Reference makeGenevaReference(RoteListeDbImportState state) {
299
//		Reference result = ReferenceFactory.newDatabase();
300
//		result.setTitleCache(state.getConfig().getGenevaReferenceTitle(), true);
301
//		result.setUuid(state.getConfig().getUuidGenevaReference());
302
//		return result;
303
//	}
304

    
305
	/* (non-Javadoc)
306
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
307
	 */
308
	@Override
309
	protected boolean doCheck(RoteListeDbImportState state){
310
		IOValidator<RoteListeDbImportState> validator = new RoteListeDbTaxonImportValidator();
311
		return validator.validate(state);
312
	}
313
	
314
	
315
	/* (non-Javadoc)
316
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
317
	 */
318
	protected boolean isIgnore(RoteListeDbImportState state){
319
		return ! state.getConfig().isDoTaxa();
320
	}
321

    
322

    
323

    
324
}
(4-4/5)