Project

General

Profile

Download (14.5 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.IOValidator;
33
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
34
import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
35
import eu.etaxonomy.cdm.io.pesi.erms.validation.ErmsSourceUsesImportValidator;
36
import eu.etaxonomy.cdm.model.common.CdmBase;
37
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
38
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
39
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
40
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
41
import eu.etaxonomy.cdm.model.description.Feature;
42
import eu.etaxonomy.cdm.model.description.TaxonDescription;
43
import eu.etaxonomy.cdm.model.description.TextData;
44
import eu.etaxonomy.cdm.model.name.TaxonName;
45
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
46
import eu.etaxonomy.cdm.model.reference.Reference;
47
import eu.etaxonomy.cdm.model.taxon.Synonym;
48
import eu.etaxonomy.cdm.model.taxon.Taxon;
49
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
50

    
51
/**
52
 * @author a.mueller
53
 * @since 20.02.2010
54
 */
55
@Component
56
public class ErmsSourceUsesImport  extends ErmsImportBase<CommonTaxonName> {
57

    
58
    private static final long serialVersionUID = -5139234838768878653L;
59

    
60
    private static final Logger logger = Logger.getLogger(ErmsSourceUsesImport.class);
61

    
62
//	private DbImportMapping<ErmsImportState, ErmsImportConfigurator> mapping; //not needed
63

    
64
	private static final String pluralString = "source uses";
65
	private static final String dbTableName = "tu_sources";
66
	private static final Class<?> cdmTargetClass = null;
67

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

    
72
	@Override
73
	protected String getIdQuery() {
74
		String strQuery = " SELECT sourceuse_id, source_id, tu_id " + " " +
75
						" FROM tu_sources " +
76
						" ORDER BY sourceuse_id, source_id, tu_id  ";
77
		return strQuery;
78
	}
79

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

    
91

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

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

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

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

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

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

    
155
	private TaxonName makeOriginalDescription(ResultSetPartitioner<?> partitioner,
156
	        ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
157
	    TaxonName taxonName = (TaxonName)state.getRelatedObject(ErmsImportBase.NAME_NAMESPACE, strTaxonId);
158
		taxonName.setNomenclaturalReference(ref);
159
		taxonName.setNomenclaturalMicroReference(strPageNr);
160
		return taxonName;
161
	}
162

    
163
	private boolean isFirstBasisOfRecord = true;
164

    
165
	@SuppressWarnings("unused")
166
    private IdentifiableEntity<?> makeBasisOfRecord(ResultSetPartitioner<?> partitioner,
167
            ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
168
		if (isFirstBasisOfRecord){
169
			logger.warn("Basis of record for all records not yet implemented");
170
			isFirstBasisOfRecord = false;
171
		}
172
		return null;
173
	}
174

    
175
	private IdentifiableEntity<?> makeAdditionalSource(ResultSetPartitioner<?> partitioner,
176
	        ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
177

    
178
		Feature citationFeature = Feature.CITATION();
179
		DescriptionElementBase element = TextData.NewInstance(citationFeature);
180
		DescriptionElementSource source = element.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, ref, strPageNr);
181
		if (source == null){
182
			logger.warn("Source is null");
183
			return null;
184
		}
185
		TaxonBase<?> taxonBase = (TaxonBase<?>)state.getRelatedObject(ErmsImportBase.TAXON_NAMESPACE, strTaxonId);
186
		Taxon taxon;
187

    
188
		//if taxon base is a synonym, add the description to the accepted taxon
189
		if (taxonBase.isInstanceOf(Synonym.class)){
190
			Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
191
			taxon = synonym.getAcceptedTaxon();
192
			if (taxon == null){
193
				String warning = "Synonym "+ strTaxonId + " has no accepted taxon";
194
				logger.warn(warning);
195
				return null;
196
				//throw new IllegalStateException(warning);
197
			}
198
			//add synonym name as name used in source
199
			source.setNameUsedInSource(synonym.getName());
200
		}else{
201
			taxon = (Taxon)taxonBase;
202
		}
203

    
204
		//get or create description and add the element
205
		TaxonDescription description;
206
		if (taxon.getDescriptions().size() > 0){
207
			description = taxon.getDescriptions().iterator().next();
208
		}else{
209
			description = TaxonDescription.NewInstance(taxon);
210
		}
211
		description.addElement(element);
212
		return taxon;
213
	}
214

    
215
	private IdentifiableEntity<?> makeSourceOfSynonymy(@SuppressWarnings("unused") ResultSetPartitioner<?> partitioner,
216
	        ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
217

    
218
		TaxonBase<?> taxonBase = (TaxonBase<?>)state.getRelatedObject(ErmsImportBase.TAXON_NAMESPACE, strTaxonId);
219
		if (taxonBase == null){
220
			String warning = "taxonBase (id = " + strTaxonId + ") could not be found ";
221
			logger.warn(warning);
222
			return null;
223
		}else if (taxonBase.isInstanceOf(Taxon.class)){
224
			Taxon taxon =CdmBase.deproxy(taxonBase, Taxon.class);
225
			Feature sourceOfSynonymyFeature = getFeature(state, ErmsTransformer.uuidSourceOfSynonymy, "Source of synonymy", "Source of synonymy", null, null);
226
			TextData element = TextData.NewInstance(sourceOfSynonymyFeature);
227
			getTaxonDescription(taxon, false, true).addElement(element);
228
			element.addPrimaryTaxonomicSource(ref, strPageNr);
229
		}else if (taxonBase.isInstanceOf(Synonym.class)){
230
			Synonym synonym =CdmBase.deproxy(taxonBase, Synonym.class);
231
			if (synonym.getSec()!= null){
232
			    logger.warn("Synonym has sec reference already. Source of synonym can not be set: " + strTaxonId);
233
			}else{
234
			    synonym.setSec(ref);
235
			}
236
			if (synonym.getSecMicroReference()!= null){
237
                logger.warn("Synonym has sec micro reference already. Source of synonym detail can not be set: " + strTaxonId);
238
            }else{
239
                synonym.setSecMicroReference(strPageNr);
240
            }
241
		}else{
242
		    throw new RuntimeException("Unsupported taxonbase class");
243
		}
244

    
245
		return taxonBase;
246
	}
247

    
248
	private boolean isFirstRediscription = true;
249
	@SuppressWarnings("unused")
250
    private IdentifiableEntity<?> makeRedescription(ResultSetPartitioner<?> partitioner, ErmsImportState state,
251
            Reference ref, String strTaxonId, String strPageNr) {
252
		if (isFirstRediscription){
253
			logger.warn("Rediscription not yet implemented");
254
			isFirstRediscription = false;
255
		}
256
		return null;
257
	}
258

    
259
	private IdentifiableEntity<?> makeCombinationReference(ResultSetPartitioner<?> partitioner,
260
	        ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
261
		// Kopie von Orig. Comb.
262
		//TODO ist das wirklich der richtige Name, oder muss ein verknüpfter Name verwendet werden
263
	    TaxonName taxonName = (TaxonName)state.getRelatedObject(ErmsImportBase.NAME_NAMESPACE, strTaxonId);
264
		taxonName.setNomenclaturalReference(ref);
265
		taxonName.setNomenclaturalMicroReference(strPageNr);
266
		return taxonName;
267
	}
268

    
269
	private boolean isFirstStatusSource = true;
270
	@SuppressWarnings("unused")
271
    private IdentifiableEntity<?> makeStatusSource(ResultSetPartitioner<?> partitioner,
272
            ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
273
		if (isFirstStatusSource){
274
			logger.warn("StatusSource not yet implemented");
275
			isFirstStatusSource = false;
276
		}
277
		return null;
278
	}
279

    
280
	private boolean isFirstEmendation = true;
281
	@SuppressWarnings("unused")
282
    private IdentifiableEntity<?> makeEmendation(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
283
		if (isFirstEmendation){
284
			logger.warn("Emmendation not yet implemented");
285
			isFirstEmendation = false;
286
		}
287
		return null;
288
	}
289

    
290
	@SuppressWarnings("unused")
291
    public CommonTaxonName createObject(ResultSet rs, ErmsImportState state)
292
			throws SQLException {
293
		return null;  //not needed
294
	}
295

    
296

    
297
//************************************ RELATED OBJECTS *************************************************/
298

    
299
	@Override
300
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
301
	        ErmsImportState state) {
302
		String nameSpace;
303
		Class<?> cdmClass;
304
		Set<String> idSet;
305
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
306

    
307
		try{
308
			Set<String> taxonIdSet = new HashSet<>();
309
			Set<String> nameIdSet = new HashSet<>();
310
			Set<String> referenceIdSet = new HashSet<>();
311
			while (rs.next()){
312
				handleForeignKey(rs, taxonIdSet, "tu_id");
313
				handleForeignKey(rs, nameIdSet, "tu_id");
314
				handleForeignKey(rs, referenceIdSet, "source_id");
315
			}
316

    
317
			//name map
318
			nameSpace = ErmsImportBase.NAME_NAMESPACE;
319
			cdmClass = TaxonName.class;
320
			idSet = nameIdSet;
321
			@SuppressWarnings("unchecked")
322
            Map<String, TaxonName> nameMap = (Map<String, TaxonName>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
323
			result.put(nameSpace, nameMap);
324

    
325
			//taxon map
326
			nameSpace = ErmsImportBase.TAXON_NAMESPACE;
327
			cdmClass = TaxonBase.class;
328
			idSet = taxonIdSet;
329
			@SuppressWarnings("unchecked")
330
            Map<String, TaxonBase<?>> taxonMap = (Map<String, TaxonBase<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
331
			result.put(nameSpace, taxonMap);
332

    
333
			//reference map
334
			nameSpace = ErmsImportBase.REFERENCE_NAMESPACE;
335
			cdmClass = Reference.class;
336
			idSet = referenceIdSet;
337
			@SuppressWarnings("unchecked")
338
            Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(Reference.class, idSet, nameSpace);
339
			result.put(nameSpace, referenceMap);
340

    
341

    
342
		} catch (SQLException e) {
343
			throw new RuntimeException(e);
344
		}
345
		return result;
346

    
347
	}
348

    
349

    
350
    @Override
351
    protected DbImportMapping<?, ?> getMapping() {
352
        logger.info("getMapping not implemented for EmrsSourceUsesImport");
353
        return null;  // not needed because Mapping is not implemented in this class yet
354
    }
355

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

    
362
	@Override
363
    protected boolean isIgnore(ErmsImportState state){
364
		boolean result;
365
//		result = state.getConfig().getDoReferences() != IImportConfigurator.DO_REFERENCES.ALL;
366
//		result |= ! state.getConfig().isDoTaxa();
367
		result = ! state.getConfig().isDoTaxa();
368
		return result;
369
	}
370
}
(12-12/17)