Project

General

Profile

Download (14.8 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.TaxonName;
47
import eu.etaxonomy.cdm.model.reference.Reference;
48
import eu.etaxonomy.cdm.model.taxon.Synonym;
49
import eu.etaxonomy.cdm.model.taxon.Taxon;
50
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
51
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
52

    
53
/**
54
 * @author a.mueller
55
 * @since 20.02.2010
56
 */
57
@Component
58
public class ErmsSourceUsesImport  extends ErmsImportBase<CommonTaxonName> {
59
    private static final long serialVersionUID = -5139234838768878653L;
60

    
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
	@Override
74
	protected String getIdQuery() {
75
		String strQuery = " SELECT sourceuse_id, source_id, tu_id " + " " +
76
						" FROM tu_sources " +
77
						" ORDER BY sourceuse_id, source_id, tu_id  ";
78
		return strQuery;
79
	}
80

    
81
	@Override
82
	protected String getRecordQuery(ErmsImportConfigurator config) {
83
		String strRecordQuery =
84
			" SELECT * " +
85
			" FROM tu_sources INNER JOIN sourceuses ON tu_sources.sourceuse_id = sourceuses.sourceuse_id" +
86
			" WHERE ( tu_sources.sourceuse_id IN (" + ID_LIST_TOKEN + ") AND " +
87
			" 		tu_sources.source_id IN (" + ID_LIST_TOKEN + ") AND " +
88
			"		tu_sources.tu_id IN (" + ID_LIST_TOKEN + ")  )";
89
		return strRecordQuery;
90
	}
91

    
92

    
93
	@Override
94
    public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, ErmsImportState state) {
95
		boolean success = true ;
96
		Set objectsToSave = new HashSet<>();
97

    
98
// 		DbImportMapping<?, ?> mapping = getMapping();
99
//		mapping.initialize(state, cdmTargetClass);
100

    
101
		ResultSet rs = partitioner.getResultSet();
102
		try{
103
			while (rs.next()){
104
				//success &= mapping.invoke(rs,referencesToSave);
105

    
106
				//read and normalize values
107
				int sourceUseId = rs.getInt("sourceuse_id");
108
				int sourceId = rs.getInt("source_id");
109
				String strSourceId = String.valueOf(sourceId);
110
				int taxonId = rs.getInt("tu_id");
111
				String strTaxonId = String.valueOf(taxonId);
112
				String strPageNr = rs.getString("pagenr");
113
				if (StringUtils.isBlank(strPageNr)){
114
					strPageNr = null;
115
				}
116
				Reference ref = (Reference)state.getRelatedObject(ErmsImportBase.REFERENCE_NAMESPACE, strSourceId);
117

    
118
				try {
119
				IdentifiableEntity<?> objectToSave = null;
120
				//invoke methods for each sourceUse type
121
				if (sourceUseId == SOURCE_USE_ORIGINAL_DESCRIPTION){
122
					objectToSave = makeOriginalDescription(partitioner, state, ref, strTaxonId, strPageNr);
123
				}else if (sourceUseId == SOURCE_USE_BASIS_OF_RECORD){
124
					objectToSave = makeBasisOfRecord(partitioner, state, ref, strTaxonId, strPageNr);
125
				}else if (sourceUseId == SOURCE_USE_ADDITIONAL_SOURCE){
126
					objectToSave = makeAdditionalSource(partitioner, state, ref, strTaxonId, strPageNr);
127
				}else if (sourceUseId == SOURCE_USE_SOURCE_OF_SYNONYMY){
128
					objectToSave = makeSourceOfSynonymy(partitioner, state, ref, strTaxonId, strPageNr);
129
				}else if (sourceUseId == SOURCE_USE_REDESCRIPTION){
130
					objectToSave = makeRedescription(partitioner, state, ref, strTaxonId, strPageNr);
131
				}else if (sourceUseId == SOURCE_USE_NEW_COMBINATION_REFERENCE){
132
					objectToSave = makeCombinationReference(partitioner, state, ref, strTaxonId, strPageNr);
133
				}else if (sourceUseId == SOURCE_USE_STATUS_SOURCE){
134
					objectToSave = makeStatusSource(partitioner, state, ref, strTaxonId, strPageNr);
135
				}else if (sourceUseId == SOURCE_USE_EMENDATION){
136
					objectToSave = makeEmendation(partitioner, state, ref, strTaxonId, strPageNr);
137
				}
138
				if(objectToSave != null){
139
					objectsToSave.add(objectToSave);
140
				}
141
				} catch (Exception e) {
142
					e.printStackTrace();
143
					success = false;
144
			}
145
			}
146
		} catch (SQLException e) {
147
			logger.error("SQLException:" +  e);
148
			return false;
149
		}
150

    
151
		partitioner.startDoSave();
152
		getCommonService().save(objectsToSave);
153
		return success;
154
	}
155

    
156

    
157
	/**
158
	 * @param strTaxonId
159
	 * @param ref
160
	 * @param state
161
	 * @param partitioner
162
	 * @param strPageNr
163
	 *
164
	 */
165
	private TaxonName makeOriginalDescription(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
166
	    TaxonName taxonName = (TaxonName)state.getRelatedObject(ErmsImportBase.NAME_NAMESPACE, strTaxonId);
167
		taxonName.setNomenclaturalReference(ref);
168
		taxonName.setNomenclaturalMicroReference(strPageNr);
169
		return taxonName;
170
	}
171

    
172
	/**
173
	 * @param partitioner
174
	 * @param state
175
	 * @param ref
176
	 * @param strTaxonId
177
	 * @param strPageNr
178
	 */
179
	private boolean isFirstBasisOfRecord = true;
180
	@SuppressWarnings("unused")
181
    private IdentifiableEntity<?> makeBasisOfRecord(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
182
		if (isFirstBasisOfRecord){
183
			logger.warn("Basis of record not yet implemented");
184
			isFirstBasisOfRecord = false;
185
		}
186
		return null;
187
	}
188

    
189
	/**
190
	 * @param partitioner
191
	 * @param state
192
	 * @param ref
193
	 * @param strTaxonId
194
	 * @param strPageNr
195
	 */
196
	private IdentifiableEntity<?> makeAdditionalSource(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
197
		Feature citationFeature = Feature.CITATION();
198
		DescriptionElementBase element = TextData.NewInstance(citationFeature);
199
		DescriptionElementSource source = element.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, ref, strPageNr);
200
		if (source == null){
201
			logger.warn("Source is null");
202
			return null;
203
		}
204
		TaxonBase<?> taxonBase = (TaxonBase<?>)state.getRelatedObject(ErmsImportBase.TAXON_NAMESPACE, strTaxonId);
205
		Taxon taxon;
206

    
207
		//if taxon base is a synonym, add the description to the accepted taxon
208
		if (taxonBase.isInstanceOf(Synonym.class)){
209
			Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
210
			taxon = synonym.getAcceptedTaxon();
211
			if (taxon == null){
212
				String warning = "Synonym "+ strTaxonId + " has no accepted taxon";
213
				logger.warn(warning);
214
				return null;
215
				//throw new IllegalStateException(warning);
216
			}
217
			//add synonym name as name used in source
218
			source.setNameUsedInSource(synonym.getName());
219
		}else{
220
			taxon = (Taxon)taxonBase;
221
		}
222

    
223
		//get or create description and add the element
224
		TaxonDescription description;
225
		if (taxon.getDescriptions().size() > 0){
226
			description = taxon.getDescriptions().iterator().next();
227
		}else{
228
			description = TaxonDescription.NewInstance(taxon);
229
		}
230
		description.addElement(element);
231
		return taxon;
232
	}
233

    
234
	/**
235
	 * @param partitioner
236
	 * @param state
237
	 * @param ref
238
	 * @param strTaxonId
239
	 * @param strPageNr
240
	 */
241
	private IdentifiableEntity<?> makeSourceOfSynonymy(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
242
		TaxonBase<?> taxonBase = (TaxonBase<?>)state.getRelatedObject(ErmsImportBase.TAXON_NAMESPACE, strTaxonId);
243
		if (taxonBase == null){
244
			String warning = "taxonBase (id = " + strTaxonId + ") could not be found ";
245
			logger.warn(warning);
246
			return null;
247
		}else if (! taxonBase.isInstanceOf(Synonym.class)){
248
			String message = "TaxonBase is not of class Synonym but " + taxonBase.getClass().getSimpleName();
249
			logger.info(message);
250
			Taxon taxon =CdmBase.deproxy(taxonBase, Taxon.class);
251
			Set<TaxonRelationship> synRels = taxon.getTaxonRelations();
252
			if (synRels.size() != 1){
253
				logger.warn("TaxonSynonym (" + strTaxonId + ") has not 1 but " + synRels.size() + " relations!");
254
			}else{
255
				TaxonRelationship synRel = synRels.iterator().next();
256
				synRel.setCitation(ref);
257
				synRel.setCitationMicroReference(strPageNr);
258
			}
259
		}else{
260
			Synonym synonym =CdmBase.deproxy(taxonBase, Synonym.class);
261
			synonym.setSec(ref);
262
			synonym.setSecMicroReference(strPageNr);
263
		}
264

    
265
		return taxonBase;
266
	}
267

    
268
	/**
269
	 * @param partitioner
270
	 * @param state
271
	 * @param ref
272
	 * @param strTaxonId
273
	 * @param strPageNr
274
	 */
275
	private boolean isFirstRediscription = true;
276
	@SuppressWarnings("unused")
277
    private IdentifiableEntity<?> makeRedescription(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
278
		if (isFirstRediscription){
279
			logger.warn("Rediscription not yet implemented");
280
			isFirstRediscription = false;
281
		}
282
		return null;
283
	}
284

    
285
	/**
286
	 * @param partitioner
287
	 * @param state
288
	 * @param ref
289
	 * @param strTaxonId
290
	 * @param strPageNr
291
	 */
292
	private IdentifiableEntity<?> makeCombinationReference(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
293
		// Kopie von Orig. Comb.
294
		//TODO ist das wirklich der richtige Name, oder muss ein verknüpfter Name verwendet werden
295
	    TaxonName taxonName = (TaxonName)state.getRelatedObject(ErmsImportBase.NAME_NAMESPACE, strTaxonId);
296
		taxonName.setNomenclaturalReference(ref);
297
		taxonName.setNomenclaturalMicroReference(strPageNr);
298
		return taxonName;
299
	}
300

    
301

    
302
	/**
303
	 * @param partitioner
304
	 * @param state
305
	 * @param ref
306
	 * @param strTaxonId
307
	 * @param strPageNr
308
	 */
309
	private boolean isFirstStatusSource = true;
310
	@SuppressWarnings("unused")
311
    private IdentifiableEntity<?> makeStatusSource(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
312
		if (isFirstStatusSource){
313
			logger.warn("StatusSource not yet implemented");
314
			isFirstStatusSource = false;
315
		}
316
		return null;
317
	}
318

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

    
336
	@SuppressWarnings("unused")
337
    public CommonTaxonName createObject(ResultSet rs, ErmsImportState state)
338
			throws SQLException {
339
		return null;  //not needed
340
	}
341

    
342

    
343
//************************************ RELATED OBJECTS *************************************************/
344

    
345
	@Override
346
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, ErmsImportState state) {
347
		String nameSpace;
348
		Class<?> cdmClass;
349
		Set<String> idSet;
350
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
351

    
352
		try{
353
			Set<String> taxonIdSet = new HashSet<>();
354
			Set<String> nameIdSet = new HashSet<>();
355
			Set<String> referenceIdSet = new HashSet<>();
356
			while (rs.next()){
357
				handleForeignKey(rs, taxonIdSet, "tu_id");
358
				handleForeignKey(rs, nameIdSet, "tu_id");
359
				handleForeignKey(rs, referenceIdSet, "source_id");
360
			}
361

    
362
			//name map
363
			nameSpace = ErmsImportBase.NAME_NAMESPACE;
364
			cdmClass = TaxonName.class;
365
			idSet = nameIdSet;
366
			@SuppressWarnings("unchecked")
367
            Map<String, TaxonName> nameMap = (Map<String, TaxonName>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
368
			result.put(nameSpace, nameMap);
369

    
370
			//taxon map
371
			nameSpace = ErmsImportBase.TAXON_NAMESPACE;
372
			cdmClass = TaxonBase.class;
373
			idSet = taxonIdSet;
374
			@SuppressWarnings("unchecked")
375
            Map<String, TaxonBase<?>> taxonMap = (Map<String, TaxonBase<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
376
			result.put(nameSpace, taxonMap);
377

    
378
			//reference map
379
			nameSpace = ErmsImportBase.REFERENCE_NAMESPACE;
380
			cdmClass = Reference.class;
381
			idSet = referenceIdSet;
382
			@SuppressWarnings("unchecked")
383
            Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(Reference.class, idSet, nameSpace);
384
			result.put(nameSpace, referenceMap);
385

    
386

    
387
		} catch (SQLException e) {
388
			throw new RuntimeException(e);
389
		}
390
		return result;
391

    
392
	}
393

    
394
	@Override
395
	protected boolean doCheck(ErmsImportState state){
396
		IOValidator<ErmsImportState> validator = new ErmsSourceUsesImportValidator();
397
		return validator.validate(state);
398
	}
399

    
400
	@Override
401
    protected boolean isIgnore(ErmsImportState state){
402
		boolean result = state.getConfig().getDoReferences() != IImportConfigurator.DO_REFERENCES.ALL;
403
		result |= ! state.getConfig().isDoTaxa();
404
		return result;
405
	}
406

    
407
	@Override
408
	protected DbImportMapping<?, ?> getMapping() {
409
		logger.info("getMapping not implemented for EmrsSourceUsesImport");
410
		return null;  // not needed because Mapping is not implemented in this class yet
411
	}
412
}
(12-12/17)