Project

General

Profile

« Previous | Next » 

Revision f08f9f6d

Added by Andreas Müller over 4 years ago

ref #8508, ref #8509, ref #1444 fix source use for ERMS

View differences:

cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/erms/ErmsSourceUsesImport.java
9 9

  
10 10
package eu.etaxonomy.cdm.io.pesi.erms;
11 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 12
import java.sql.ResultSet;
22 13
import java.sql.SQLException;
23 14
import java.util.HashMap;
......
36 27
import eu.etaxonomy.cdm.model.common.CdmBase;
37 28
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
38 29
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 30
import eu.etaxonomy.cdm.model.name.TaxonName;
45 31
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
46 32
import eu.etaxonomy.cdm.model.reference.Reference;
47
import eu.etaxonomy.cdm.model.taxon.Synonym;
48
import eu.etaxonomy.cdm.model.taxon.Taxon;
49 33
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
50 34

  
51 35
/**
......
71 55

  
72 56
	@Override
73 57
	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  ";
58
		String strQuery = " SELECT tu_id, source_id, sourceuse_id " +
59
						  " FROM tu_sources " +
60
						  " ORDER BY tu_id, source_id, sourceuse_id ";
77 61
		return strQuery;
78 62
	}
79 63

  
......
82 66
		String strRecordQuery =
83 67
			" SELECT * " +
84 68
			" 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 " +
69
			" WHERE ( tu_sources.tu_id IN (" + ID_LIST_TOKEN + ") AND " +
86 70
			" 		tu_sources.source_id IN (" + ID_LIST_TOKEN + ") AND " +
87
			"		tu_sources.tu_id IN (" + ID_LIST_TOKEN + ")  )";
71
			"		tu_sources.sourceuse_id IN (" + ID_LIST_TOKEN + ")   )";
88 72
		return strRecordQuery;
89 73
	}
90 74

  
......
94 78
		boolean success = true ;
95 79
		Set<CdmBase> objectsToSave = new HashSet<>();
96 80

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

  
81
		int i = 0;
100 82
		ResultSet rs = partitioner.getResultSet();
101 83
		try{
102 84
			while (rs.next()){
103
				//success &= mapping.invoke(rs,referencesToSave);
104

  
85
			    i++;
105 86
				//read and normalize values
106
				int sourceUseId = rs.getInt("sourceuse_id");
87
//				int sourceUseId = rs.getInt("sourceuse_id");
107 88
				int sourceId = rs.getInt("source_id");
89
				String strSourceUse = rs.getString("sourceuse_name");
108 90
				String strSourceId = String.valueOf(sourceId);
109 91
				int taxonId = rs.getInt("tu_id");
110 92
				String strTaxonId = String.valueOf(taxonId);
......
117 99
				try {
118 100
    				IdentifiableEntity<?> objectToSave = null;
119 101
    				//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
    				}
102
    				String srcUseStr = normalizeSourceUse(strSourceUse);
103
    				objectToSave = makeSourceUse(state, srcUseStr, ref, strTaxonId, strPageNr, strSourceId);
104

  
137 105
    				if(objectToSave != null){
138 106
    					objectsToSave.add(objectToSave);
139 107
    				}
......
147 115
			return false;
148 116
		}
149 117

  
118
		logger.debug("real partition size: " + i);
150 119
		partitioner.startDoSave();
151 120
		getCommonService().save(objectsToSave);
152 121
		return success;
153 122
	}
154 123

  
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
	}
124
    private String normalizeSourceUse(String sourceUse) {
125
        return sourceUse;
126
    }
247 127

  
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
	}
128
    private IdentifiableEntity<?> makeSourceUse(ErmsImportState state, String srcUseString,
129
            Reference ref, String strTaxonId, String strPageNr, String strSourceId) {
258 130

  
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
	}
131
        TaxonBase<?> taxonBase = (TaxonBase<?>)state.getRelatedObject(ErmsImportBase.TAXON_NAMESPACE, strTaxonId);
132
        if (taxonBase == null){
133
            logger.warn("TaxonBase " + strTaxonId + " could not be found for source ID " + strSourceId + " and source use " + srcUseString);
134
        }else{
135
            taxonBase.addSource(OriginalSourceType.Other, ref, strPageNr, srcUseString);
136
        }
268 137

  
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
	}
138
        return taxonBase;
139
    }
279 140

  
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
	}
141
//    private TaxonName makeOriginalDescription(ResultSetPartitioner<?> partitioner,
142
//	        ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
143
//	    TaxonName taxonName = (TaxonName)state.getRelatedObject(ErmsImportBase.NAME_NAMESPACE, strTaxonId);
144
//		taxonName.setNomenclaturalReference(ref);
145
//		taxonName.setNomenclaturalMicroReference(strPageNr);
146
//		return taxonName;
147
//	}
148

  
149

  
150
//	private IdentifiableEntity<?> makeAdditionalSource(ResultSetPartitioner<?> partitioner,
151
//	        ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
152
//
153
//		Feature citationFeature = Feature.CITATION();
154
//		DescriptionElementBase element = TextData.NewInstance(citationFeature);
155
//		DescriptionElementSource source = element.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, ref, strPageNr);
156
//		if (source == null){
157
//			logger.warn("Source is null");
158
//			return null;
159
//		}
160
//		TaxonBase<?> taxonBase = (TaxonBase<?>)state.getRelatedObject(ErmsImportBase.TAXON_NAMESPACE, strTaxonId);
161
//		Taxon taxon;
162
//
163
//		//if taxon base is a synonym, add the description to the accepted taxon
164
//		if (taxonBase.isInstanceOf(Synonym.class)){
165
//			Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
166
//			taxon = synonym.getAcceptedTaxon();
167
//			if (taxon == null){
168
//				String warning = "Synonym "+ strTaxonId + " has no accepted taxon";
169
//				logger.warn(warning);
170
//				return null;
171
//				//throw new IllegalStateException(warning);
172
//			}
173
//			//add synonym name as name used in source
174
//			source.setNameUsedInSource(synonym.getName());
175
//		}else{
176
//			taxon = (Taxon)taxonBase;
177
//		}
178
//
179
//		//get or create description and add the element
180
//		TaxonDescription description;
181
//		if (taxon.getDescriptions().size() > 0){
182
//			description = taxon.getDescriptions().iterator().next();
183
//		}else{
184
//			description = TaxonDescription.NewInstance(taxon);
185
//		}
186
//		description.addElement(element);
187
//		return taxon;
188
//	}
189

  
190
//	private IdentifiableEntity<?> makeSourceOfSynonymy(@SuppressWarnings("unused") ResultSetPartitioner<?> partitioner,
191
//	        ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
192
//
193
//		TaxonBase<?> taxonBase = (TaxonBase<?>)state.getRelatedObject(ErmsImportBase.TAXON_NAMESPACE, strTaxonId);
194
//		if (taxonBase == null){
195
//			String warning = "taxonBase (id = " + strTaxonId + ") could not be found ";
196
//			logger.warn(warning);
197
//			return null;
198
//		}else if (taxonBase.isInstanceOf(Taxon.class)){
199
//			Taxon taxon =CdmBase.deproxy(taxonBase, Taxon.class);
200
//			Feature sourceOfSynonymyFeature = getFeature(state, ErmsTransformer.uuidSourceOfSynonymy, "Source of synonymy", "Source of synonymy", null, null);
201
//			TextData element = TextData.NewInstance(sourceOfSynonymyFeature);
202
//			getTaxonDescription(taxon, false, true).addElement(element);
203
//			element.addPrimaryTaxonomicSource(ref, strPageNr);
204
//		}else if (taxonBase.isInstanceOf(Synonym.class)){
205
//			Synonym synonym =CdmBase.deproxy(taxonBase, Synonym.class);
206
//			if (synonym.getSec()!= null){
207
//			    logger.warn("Synonym has sec reference already. Source of synonym can not be set: " + strTaxonId);
208
//			}else{
209
//			    synonym.setSec(ref);
210
//			}
211
//			if (synonym.getSecMicroReference()!= null){
212
//                logger.warn("Synonym has sec micro reference already. Source of synonym detail can not be set: " + strTaxonId);
213
//            }else{
214
//                synonym.setSecMicroReference(strPageNr);
215
//            }
216
//		}else{
217
//		    throw new RuntimeException("Unsupported taxonbase class");
218
//		}
219
//
220
//		return taxonBase;
221
//	}
222

  
223
//	private boolean isFirstRediscription = true;
224
//	@SuppressWarnings("unused")
225
//    private IdentifiableEntity<?> makeRedescription(ResultSetPartitioner<?> partitioner, ErmsImportState state,
226
//            Reference ref, String strTaxonId, String strPageNr) {
227
//		if (isFirstRediscription){
228
//			logger.warn("Rediscription not yet implemented");
229
//			isFirstRediscription = false;
230
//		}
231
//		return null;
232
//	}
233

  
234
//	private IdentifiableEntity<?> makeCombinationReference(ResultSetPartitioner<?> partitioner,
235
//	        ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
236
//		// Kopie von Orig. Comb.
237
//		//TODO ist das wirklich der richtige Name, oder muss ein verknüpfter Name verwendet werden
238
//	    TaxonName taxonName = (TaxonName)state.getRelatedObject(ErmsImportBase.NAME_NAMESPACE, strTaxonId);
239
//		taxonName.setNomenclaturalReference(ref);
240
//		taxonName.setNomenclaturalMicroReference(strPageNr);
241
//		return taxonName;
242
//	}
243

  
244
//	private boolean isFirstStatusSource = true;
245
//	@SuppressWarnings("unused")
246
//    private IdentifiableEntity<?> makeStatusSource(ResultSetPartitioner<?> partitioner,
247
//            ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
248
//		if (isFirstStatusSource){
249
//			logger.warn("StatusSource not yet implemented");
250
//			isFirstStatusSource = false;
251
//		}
252
//		return null;
253
//	}
254

  
255
//	private boolean isFirstEmendation = true;
256
//	@SuppressWarnings("unused")
257
//    private IdentifiableEntity<?> makeEmendation(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
258
//		if (isFirstEmendation){
259
//			logger.warn("Emmendation not yet implemented");
260
//			isFirstEmendation = false;
261
//		}
262
//		return null;
263
//	}
289 264

  
290 265
	@SuppressWarnings("unused")
291 266
    public CommonTaxonName createObject(ResultSet rs, ErmsImportState state)
......
361 336

  
362 337
	@Override
363 338
    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;
339
		return ! state.getConfig().isDoSourceUse();
369 340
	}
370 341
}
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/erms/ErmsTransformer.java
44 44
	public static final int SOURCE_USE_STATUS_SOURCE = 7;
45 45
	public static final int SOURCE_USE_EMENDATION = 8;
46 46

  
47
	public static final String SOURCE_USE_STR_BASIS_OF_RECORD = "basis of record";
48

  
47 49
	//taxon relationship type uuids
48 50
	public static final UUID uuidTaxRelTypeIsTaxonSynonymOf = UUID.fromString("cc648276-0823-47b1-9deb-fa7c046e4afd");
49 51

  
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiTransformer.java
2451 2451
		}
2452 2452
	}
2453 2453

  
2454
	//TODO create a reverse map
2455
	public Integer getSourceUseKeyCacheByCache(String sourceUseCache) {
2456
        if (sourceUseCache == null){
2457
            return null;
2458
        }else{
2459
            for (Integer key: sourceUseCacheMap.keySet()){
2460
                if (sourceUseCacheMap.get(key).equalsIgnoreCase(sourceUseCache)){
2461
                    return key;
2462
                }
2463
            }
2464
        }
2465
        return null;
2466
    }
2467

  
2454 2468
	@Override
2455 2469
	public String getTaxonStatusCacheByKey(Integer taxonStatusId) throws UndefinedTransformerMethodException {
2456 2470
		if (taxonStatusId == null){

Also available in: Unified diff