Project

General

Profile

Download (16.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.pesi.erms;
11

    
12
import static eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer.SOURCE_USE_ADDITIONAL_SOURCE;
13
import static eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer.SOURCE_USE_BASIS_OF_RECORD;
14
import static eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer.SOURCE_USE_EMENDATION;
15
import static eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer.SOURCE_USE_NEW_COMBINATION_REFERENCE;
16
import static eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer.SOURCE_USE_ORIGINAL_DESCRIPTION;
17
import static eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer.SOURCE_USE_REDESCRIPTION;
18
import static eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer.SOURCE_USE_SOURCE_OF_SYNONYMY;
19
import static eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer.SOURCE_USE_STATUS_SOURCE;
20

    
21
import java.sql.ResultSet;
22
import java.sql.SQLException;
23
import java.util.HashMap;
24
import java.util.HashSet;
25
import java.util.Map;
26
import java.util.Set;
27

    
28
import org.apache.commons.lang.StringUtils;
29
import org.apache.log4j.Logger;
30
import org.springframework.stereotype.Component;
31

    
32
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
33
import eu.etaxonomy.cdm.io.common.IOValidator;
34
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
35
import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
36
import eu.etaxonomy.cdm.io.pesi.erms.validation.ErmsSourceUsesImportValidator;
37
import eu.etaxonomy.cdm.model.common.CdmBase;
38
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
39
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
40
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
41
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
42
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
43
import eu.etaxonomy.cdm.model.description.Feature;
44
import eu.etaxonomy.cdm.model.description.TaxonDescription;
45
import eu.etaxonomy.cdm.model.description.TextData;
46
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
47
import eu.etaxonomy.cdm.model.reference.Reference;
48
import eu.etaxonomy.cdm.model.taxon.Synonym;
49
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
50
import eu.etaxonomy.cdm.model.taxon.Taxon;
51
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
52
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
53

    
54
/**
55
 * @author a.mueller
56
 * @created 20.02.2010
57
 * @version 1.0
58
 */
59
@Component
60
public class ErmsSourceUsesImport  extends ErmsImportBase<CommonTaxonName> {
61
	private static final Logger logger = Logger.getLogger(ErmsSourceUsesImport.class);
62
	
63
	private DbImportMapping<ErmsImportState,ErmsImportConfigurator> mapping; //not needed
64
	
65
	private static final String pluralString = "source uses";
66
	private static final String dbTableName = "tu_sources";
67
	private static final Class<?> cdmTargetClass = null;
68

    
69
	public ErmsSourceUsesImport(){
70
		super(pluralString, dbTableName, cdmTargetClass);
71
	}
72

    
73

    
74
	/* (non-Javadoc)
75
	 * @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getIdQuery()
76
	 */
77
	@Override
78
	protected String getIdQuery() {
79
		String strQuery = " SELECT sourceuse_id, source_id, tu_id " + " " +
80
						" FROM tu_sources " + 
81
						" ORDER BY sourceuse_id, source_id, tu_id  ";
82
		return strQuery;	
83
	}
84

    
85

    
86

    
87
	/* (non-Javadoc)
88
	 * @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getRecordQuery(eu.etaxonomy.cdm.io.erms.ErmsImportConfigurator)
89
	 */
90
	@Override
91
	protected String getRecordQuery(ErmsImportConfigurator config) {
92
		String strRecordQuery = 
93
			" SELECT * " + 
94
			" FROM tu_sources INNER JOIN sourceuses ON tu_sources.sourceuse_id = sourceuses.sourceuse_id" +
95
			" WHERE ( tu_sources.sourceuse_id IN (" + ID_LIST_TOKEN + ") AND " +
96
			" 		tu_sources.source_id IN (" + ID_LIST_TOKEN + ") AND " + 
97
			"		tu_sources.tu_id IN (" + ID_LIST_TOKEN + ")  )";
98
		return strRecordQuery;
99
	}
100

    
101
	
102
	public boolean doPartition(ResultSetPartitioner partitioner, ErmsImportState state) {
103
		boolean success = true ;
104
		Set objectsToSave = new HashSet<IdentifiableEntity>();
105
		
106
// 		DbImportMapping<?, ?> mapping = getMapping();
107
//		mapping.initialize(state, cdmTargetClass);
108
		
109
		ResultSet rs = partitioner.getResultSet();
110
		try{
111
			while (rs.next()){
112
				//success &= mapping.invoke(rs,referencesToSave);
113
				
114
				//read and normalize values
115
				int sourceUseId = rs.getInt("sourceuse_id");
116
				int sourceId = rs.getInt("source_id");
117
				String strSourceId = String.valueOf(sourceId);
118
				int taxonId = rs.getInt("tu_id");
119
				String strTaxonId = String.valueOf(taxonId);
120
				String strPageNr = rs.getString("pagenr");
121
				if (StringUtils.isBlank(strPageNr)){
122
					strPageNr = null;
123
				}
124
				Reference<?> ref = (Reference<?>)state.getRelatedObject(ErmsReferenceImport.REFERENCE_NAMESPACE, strSourceId);
125
				
126
				try {
127
				IdentifiableEntity<?> objectToSave = null;
128
				//invoke methods for each sourceUse type
129
				if (sourceUseId == SOURCE_USE_ORIGINAL_DESCRIPTION){
130
					objectToSave = makeOriginalDescription(partitioner, state, ref, strTaxonId, strPageNr);
131
				}else if (sourceUseId == SOURCE_USE_BASIS_OF_RECORD){
132
					objectToSave = makeBasisOfRecord(partitioner, state, ref, strTaxonId, strPageNr);
133
				}else if (sourceUseId == SOURCE_USE_ADDITIONAL_SOURCE){
134
					objectToSave = makeAdditionalSource(partitioner, state, ref, strTaxonId, strPageNr);
135
				}else if (sourceUseId == SOURCE_USE_SOURCE_OF_SYNONYMY){
136
					objectToSave = makeSourceOfSynonymy(partitioner, state, ref, strTaxonId, strPageNr);
137
				}else if (sourceUseId == SOURCE_USE_REDESCRIPTION){
138
					objectToSave = makeRedescription(partitioner, state, ref, strTaxonId, strPageNr);
139
				}else if (sourceUseId == SOURCE_USE_NEW_COMBINATION_REFERENCE){
140
					objectToSave = makeCombinationReference(partitioner, state, ref, strTaxonId, strPageNr);
141
				}else if (sourceUseId == SOURCE_USE_STATUS_SOURCE){
142
					objectToSave = makeStatusSource(partitioner, state, ref, strTaxonId, strPageNr);
143
				}else if (sourceUseId == SOURCE_USE_EMENDATION){
144
					objectToSave = makeEmendation(partitioner, state, ref, strTaxonId, strPageNr);
145
				}
146
				if(objectToSave != null){
147
					objectsToSave.add(objectToSave);
148
				}
149
				} catch (Exception e) {
150
					e.printStackTrace();
151
					success = false;
152
			}
153
			}
154
		} catch (SQLException e) {
155
			logger.error("SQLException:" +  e);
156
			return false;
157
		}
158
	
159
		partitioner.startDoSave();
160
		getCommonService().save(objectsToSave);
161
		return success;
162
	}
163

    
164

    
165
	/**
166
	 * @param strTaxonId 
167
	 * @param ref 
168
	 * @param state 
169
	 * @param partitioner 
170
	 * @param strPageNr 
171
	 * 
172
	 */
173
	private TaxonNameBase<?,?> makeOriginalDescription(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference<?> ref, String strTaxonId, String strPageNr) {
174
		TaxonNameBase<?,?> taxonName = (TaxonNameBase<?,?>)state.getRelatedObject(ErmsTaxonImport.NAME_NAMESPACE, strTaxonId);
175
		taxonName.setNomenclaturalReference(ref);
176
		taxonName.setNomenclaturalMicroReference(strPageNr);
177
		return taxonName;
178
	}
179
	
180
	/**
181
	 * @param partitioner
182
	 * @param state
183
	 * @param ref
184
	 * @param strTaxonId
185
	 * @param strPageNr
186
	 */
187
	private boolean isFirstBasisOfRecord = true; 
188
	private IdentifiableEntity<?> makeBasisOfRecord(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference<?> ref, String strTaxonId, String strPageNr) {
189
		if (isFirstBasisOfRecord){
190
			logger.warn("Basis of record not yet implemented");
191
			isFirstBasisOfRecord = false;
192
		}
193
		return null;
194
	}
195
	
196
	/**
197
	 * @param partitioner
198
	 * @param state
199
	 * @param ref
200
	 * @param strTaxonId
201
	 * @param strPageNr
202
	 */
203
	private IdentifiableEntity<?> makeAdditionalSource(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference<?> ref, String strTaxonId, String strPageNr) {
204
		Feature citationFeature = Feature.CITATION();
205
		DescriptionElementBase element = TextData.NewInstance(citationFeature);
206
		DescriptionElementSource source = element.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, ref, strPageNr);
207
		if (source == null){
208
			logger.warn("Source is null");
209
			return null;
210
		}
211
		TaxonBase<?> taxonBase = (TaxonBase<?>)state.getRelatedObject(ErmsTaxonImport.TAXON_NAMESPACE, strTaxonId);
212
		Taxon taxon;
213
		
214
		//if taxon base is a synonym, add the description to the accepted taxon
215
		if (taxonBase.isInstanceOf(Synonym.class)){
216
			Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
217
			Set<Taxon> taxa = synonym.getAcceptedTaxa();
218
			if (taxa.size() < 1){
219
				String warning = "Synonym "+ strTaxonId + " has no accepted taxon";
220
				logger.warn(warning);
221
				return null;
222
				//throw new IllegalStateException(warning);
223
			}else if (taxa.size() > 1){
224
				String warning = "Synonym "+ strTaxonId + " has more than 1 accepted taxon";
225
				logger.warn(warning);
226
				return null;
227
				//throw new IllegalStateException(warning);
228
			}
229
			taxon = taxa.iterator().next();
230
			//add synonym name as name used in source
231
			source.setNameUsedInSource(synonym.getName());
232
		}else{
233
			taxon = (Taxon)taxonBase;
234
		}
235
		
236
		//get or create description and add the element
237
		TaxonDescription description;
238
		if (taxon.getDescriptions().size() > 0){
239
			description = taxon.getDescriptions().iterator().next();
240
		}else{
241
			description = TaxonDescription.NewInstance(taxon);
242
		}
243
		description.addElement(element);
244
		return taxon;
245
	}
246
	
247
	/**
248
	 * @param partitioner
249
	 * @param state
250
	 * @param ref
251
	 * @param strTaxonId
252
	 * @param strPageNr
253
	 */
254
	private IdentifiableEntity<?> makeSourceOfSynonymy(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference<?> ref, String strTaxonId, String strPageNr) {
255
		TaxonBase<?> taxonBase = (TaxonBase<?>)state.getRelatedObject(ErmsTaxonImport.TAXON_NAMESPACE, strTaxonId);
256
		if (taxonBase == null){
257
			String warning = "taxonBase (id = " + strTaxonId + ") could not be found ";
258
			logger.warn(warning);
259
			return null;
260
		}else if (! taxonBase.isInstanceOf(Synonym.class)){
261
			String message = "TaxonBase is not of class Synonym but " + taxonBase.getClass().getSimpleName();
262
			logger.info(message);
263
			Taxon taxon =CdmBase.deproxy(taxonBase, Taxon.class);
264
			Set<TaxonRelationship> synRels = taxon.getTaxonRelations();
265
			if (synRels.size() != 1){
266
				logger.warn("TaxonSynonym (" + strTaxonId + ") has not 1 but " + synRels.size() + " relations!");
267
			}else{
268
				TaxonRelationship synRel = synRels.iterator().next();
269
				synRel.setCitation(ref);
270
				synRel.setCitationMicroReference(strPageNr);
271
			}
272
		}else{
273
			Synonym synonym =CdmBase.deproxy(taxonBase, Synonym.class);
274
			Set<SynonymRelationship> synRels = synonym.getSynonymRelations();
275
			if (synRels.size() != 1){
276
				logger.warn("Synonym (" + strTaxonId + ") has not 1 but " + synRels.size() + " relations!");
277
			}else{
278
				SynonymRelationship synRel = synRels.iterator().next();
279
				synRel.setCitation(ref);
280
				synRel.setCitationMicroReference(strPageNr);
281
			}
282
		}
283
			
284
		return taxonBase;
285
	}
286
	
287
	/**
288
	 * @param partitioner
289
	 * @param state
290
	 * @param ref
291
	 * @param strTaxonId
292
	 * @param strPageNr
293
	 */
294
	private boolean isFirstRediscription = true; 
295
	private IdentifiableEntity<?> makeRedescription(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference<?> ref, String strTaxonId, String strPageNr) {
296
		if (isFirstRediscription){
297
			logger.warn("Rediscription not yet implemented");
298
			isFirstRediscription = false;
299
		}
300
		return null;
301
	}
302

    
303
	/**
304
	 * @param partitioner
305
	 * @param state
306
	 * @param ref
307
	 * @param strTaxonId
308
	 * @param strPageNr
309
	 */
310
	private IdentifiableEntity<?> makeCombinationReference(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference<?> ref, String strTaxonId, String strPageNr) {
311
		// Kopie von Orig. Comb.
312
		//TODO ist das wirklich der richtige Name, oder muss ein verknüpfter Name verwendet werden
313
		TaxonNameBase<?,?> taxonName = (TaxonNameBase<?,?>)state.getRelatedObject(ErmsTaxonImport.NAME_NAMESPACE, strTaxonId);
314
		taxonName.setNomenclaturalReference(ref);
315
		taxonName.setNomenclaturalMicroReference(strPageNr);
316
		return taxonName;
317
	}
318

    
319

    
320
	/**
321
	 * @param partitioner
322
	 * @param state
323
	 * @param ref
324
	 * @param strTaxonId
325
	 * @param strPageNr
326
	 */
327
	private boolean isFirstStatusSource = true; 
328
	private IdentifiableEntity<?> makeStatusSource(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference<?> ref, String strTaxonId, String strPageNr) {
329
		if (isFirstStatusSource){
330
			logger.warn("StatusSource not yet implemented");
331
			isFirstStatusSource = false;
332
		}
333
		return null;
334
	}
335

    
336
	/**
337
	 * @param partitioner
338
	 * @param state
339
	 * @param ref
340
	 * @param strTaxonId
341
	 * @param strPageNr
342
	 */
343
	private boolean isFirstEmendation = true; 
344
	private IdentifiableEntity<?> makeEmendation(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference<?> ref, String strTaxonId, String strPageNr) {
345
		if (isFirstEmendation){
346
			logger.warn("Emmendation not yet implemented");
347
			isFirstEmendation = false;
348
		}
349
		return null;
350
	}
351

    
352

    
353

    
354
	/* (non-Javadoc)
355
	 * @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet, eu.etaxonomy.cdm.io.common.ImportStateBase)
356
	 */
357
	public CommonTaxonName createObject(ResultSet rs, ErmsImportState state)
358
			throws SQLException {
359
		return null;  //not needed
360
	}
361

    
362
	
363
//************************************ RELATED OBJECTS *************************************************/	
364
	
365
	/* (non-Javadoc)
366
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
367
	 */
368
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
369
		String nameSpace;
370
		Class<?> cdmClass;
371
		Set<String> idSet;
372
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
373
		
374
		try{
375
			Set<String> taxonIdSet = new HashSet<String>();
376
			Set<String> nameIdSet = new HashSet<String>();
377
			Set<String> referenceIdSet = new HashSet<String>();
378
			while (rs.next()){
379
				handleForeignKey(rs, taxonIdSet, "tu_id");
380
				handleForeignKey(rs, nameIdSet, "tu_id");
381
				handleForeignKey(rs, referenceIdSet, "source_id");
382
			}
383
			
384
			//name map
385
			nameSpace = ErmsTaxonImport.NAME_NAMESPACE;
386
			cdmClass = TaxonNameBase.class;
387
			idSet = nameIdSet;
388
			Map<String, TaxonNameBase<?,?>> nameMap = (Map<String, TaxonNameBase<?,?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
389
			result.put(nameSpace, nameMap);
390
			
391
			//taxon map
392
			nameSpace = ErmsTaxonImport.TAXON_NAMESPACE;
393
			cdmClass = TaxonBase.class;
394
			idSet = taxonIdSet;
395
			Map<String, TaxonBase<?>> taxonMap = (Map<String, TaxonBase<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
396
			result.put(nameSpace, taxonMap);
397
			
398
			//reference map
399
			nameSpace = ErmsReferenceImport.REFERENCE_NAMESPACE;
400
			cdmClass = Reference.class;
401
			idSet = referenceIdSet;
402
			Map<String, Reference<?>> referenceMap = (Map<String, Reference<?>>)getCommonService().getSourcedObjectsByIdInSource(Reference.class, idSet, nameSpace);
403
			result.put(nameSpace, referenceMap);
404
	
405
				
406
		} catch (SQLException e) {
407
			throw new RuntimeException(e);
408
		}
409
		return result;
410

    
411
	}
412
	
413
	/* (non-Javadoc)
414
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
415
	 */
416
	@Override
417
	protected boolean doCheck(ErmsImportState state){
418
		IOValidator<ErmsImportState> validator = new ErmsSourceUsesImportValidator();
419
		return validator.validate(state);
420
	}
421
	
422
	/* (non-Javadoc)
423
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
424
	 */
425
	protected boolean isIgnore(ErmsImportState state){
426
		boolean result = state.getConfig().getDoReferences() != IImportConfigurator.DO_REFERENCES.ALL;
427
		result |= ! state.getConfig().isDoTaxa();
428
		return result;
429
	}
430

    
431

    
432
	/* (non-Javadoc)
433
	 * @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getMapping()
434
	 */
435
	@Override
436
	protected DbImportMapping<?, ?> getMapping() {
437
		logger.info("getMapping not implemented for EmrsSourceUsesImport");
438
		return null;  // not needed because Mapping is not implemented in this class yet
439
	}
440

    
441
}
(12-12/17)