Project

General

Profile

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

    
19
import org.apache.commons.lang.StringUtils;
20
import org.apache.log4j.Logger;
21
import org.springframework.stereotype.Component;
22

    
23
import eu.etaxonomy.cdm.io.common.IOValidator;
24
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
25
import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
26
import eu.etaxonomy.cdm.io.pesi.erms.validation.ErmsSourceUsesImportValidator;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
29
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
30
import eu.etaxonomy.cdm.model.name.TaxonName;
31
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
32
import eu.etaxonomy.cdm.model.reference.Reference;
33
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
34

    
35
/**
36
 * @author a.mueller
37
 * @since 20.02.2010
38
 */
39
@Component
40
public class ErmsSourceUsesImport  extends ErmsImportBase<CommonTaxonName> {
41

    
42
    private static final long serialVersionUID = -5139234838768878653L;
43

    
44
    private static final Logger logger = Logger.getLogger(ErmsSourceUsesImport.class);
45

    
46
//	private DbImportMapping<ErmsImportState, ErmsImportConfigurator> mapping; //not needed
47

    
48
	private static final String pluralString = "source uses";
49
	private static final String dbTableName = "tu_sources";
50
	private static final Class<?> cdmTargetClass = null;
51

    
52
	public ErmsSourceUsesImport(){
53
		super(pluralString, dbTableName, cdmTargetClass);
54
	}
55

    
56
	@Override
57
	protected String getIdQuery() {
58
		String strQuery = " SELECT tu_id, source_id, sourceuse_id " +
59
						  " FROM tu_sources " +
60
						  " ORDER BY tu_id, source_id, sourceuse_id ";
61
		return strQuery;
62
	}
63

    
64
	@Override
65
	protected String getRecordQuery(ErmsImportConfigurator config) {
66
		String strRecordQuery =
67
			" SELECT * " +
68
			" FROM tu_sources INNER JOIN sourceuses ON tu_sources.sourceuse_id = sourceuses.sourceuse_id" +
69
			" WHERE ( tu_sources.tu_id IN (" + ID_LIST_TOKEN + ") AND " +
70
			" 		tu_sources.source_id IN (" + ID_LIST_TOKEN + ") AND " +
71
			"		tu_sources.sourceuse_id IN (" + ID_LIST_TOKEN + ")   )";
72
		return strRecordQuery;
73
	}
74

    
75

    
76
	@Override
77
    public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, ErmsImportState state) {
78
		boolean success = true ;
79
		Set<CdmBase> objectsToSave = new HashSet<>();
80

    
81
		int i = 0;
82
		ResultSet rs = partitioner.getResultSet();
83
		try{
84
			while (rs.next()){
85
			    i++;
86
				//read and normalize values
87
//				int sourceUseId = rs.getInt("sourceuse_id");
88
				int sourceId = rs.getInt("source_id");
89
				String strSourceUse = rs.getString("sourceuse_name");
90
				String strSourceId = String.valueOf(sourceId);
91
				int taxonId = rs.getInt("tu_id");
92
				String strTaxonId = String.valueOf(taxonId);
93
				String strPageNr = rs.getString("pagenr");
94
				if (StringUtils.isBlank(strPageNr)){
95
					strPageNr = null;
96
				}
97
				Reference ref = (Reference)state.getRelatedObject(ErmsImportBase.REFERENCE_NAMESPACE, strSourceId);
98

    
99
				try {
100
    				IdentifiableEntity<?> objectToSave = null;
101
    				//invoke methods for each sourceUse type
102
    				String srcUseStr = normalizeSourceUse(strSourceUse);
103
    				objectToSave = makeSourceUse(state, srcUseStr, ref, strTaxonId, strPageNr, strSourceId);
104

    
105
    				if(objectToSave != null){
106
    					objectsToSave.add(objectToSave);
107
    				}
108
				} catch (Exception e) {
109
					e.printStackTrace();
110
					success = false;
111
				}
112
			}
113
		} catch (SQLException e) {
114
			logger.error("SQLException:" +  e);
115
			return false;
116
		}
117

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

    
124
    private String normalizeSourceUse(String sourceUse) {
125
        return sourceUse;
126
    }
127

    
128
    private IdentifiableEntity<?> makeSourceUse(ErmsImportState state, String srcUseString,
129
            Reference ref, String strTaxonId, String strPageNr, String strSourceId) {
130

    
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
        }
137

    
138
        return taxonBase;
139
    }
140

    
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
//	}
264

    
265
	@SuppressWarnings("unused")
266
    public CommonTaxonName createObject(ResultSet rs, ErmsImportState state)
267
			throws SQLException {
268
		return null;  //not needed
269
	}
270

    
271

    
272
//************************************ RELATED OBJECTS *************************************************/
273

    
274
	@Override
275
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
276
	        ErmsImportState state) {
277

    
278
	    String nameSpace;
279
		Set<String> idSet;
280
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
281

    
282
		try{
283
			Set<String> taxonIdSet = new HashSet<>();
284
			Set<String> nameIdSet = new HashSet<>();
285
			Set<String> referenceIdSet = new HashSet<>();
286
			while (rs.next()){
287
				handleForeignKey(rs, taxonIdSet, "tu_id");
288
				handleForeignKey(rs, nameIdSet, "tu_id");
289
				handleForeignKey(rs, referenceIdSet, "source_id");
290
			}
291

    
292
			//name map
293
			nameSpace = ErmsImportBase.NAME_NAMESPACE;
294
			idSet = nameIdSet;
295
			Map<String, TaxonName> nameMap = getCommonService().getSourcedObjectsByIdInSourceC(TaxonName.class, idSet, nameSpace);
296
			result.put(nameSpace, nameMap);
297

    
298
			//taxon map
299
			nameSpace = ErmsImportBase.TAXON_NAMESPACE;
300
			idSet = taxonIdSet;
301
			@SuppressWarnings("rawtypes")
302
            Map<String, TaxonBase> taxonMap = getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, nameSpace);
303
			result.put(nameSpace, taxonMap);
304

    
305
			//reference map
306
			nameSpace = ErmsImportBase.REFERENCE_NAMESPACE;
307
			idSet = referenceIdSet;
308
			Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
309
			result.put(nameSpace, referenceMap);
310

    
311

    
312
		} catch (SQLException e) {
313
			throw new RuntimeException(e);
314
		}
315
		return result;
316

    
317
	}
318

    
319

    
320
    @Override
321
    protected DbImportMapping<?, ?> getMapping() {
322
        logger.info("getMapping not implemented for EmrsSourceUsesImport");
323
        return null;  // not needed because Mapping is not implemented in this class yet
324
    }
325

    
326
	@Override
327
	protected boolean doCheck(ErmsImportState state){
328
		IOValidator<ErmsImportState> validator = new ErmsSourceUsesImportValidator();
329
		return validator.validate(state);
330
	}
331

    
332
	@Override
333
    protected boolean isIgnore(ErmsImportState state){
334
		return ! state.getConfig().isDoSourceUse();
335
	}
336
}
(12-12/17)