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
|
* @created 20.02.2010
|
56
|
* @version 1.0
|
57
|
*/
|
58
|
@Component
|
59
|
public class ErmsSourceUsesImport extends ErmsImportBase<CommonTaxonName> {
|
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
|
|
73
|
/* (non-Javadoc)
|
74
|
* @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getIdQuery()
|
75
|
*/
|
76
|
@Override
|
77
|
protected String getIdQuery() {
|
78
|
String strQuery = " SELECT sourceuse_id, source_id, tu_id " + " " +
|
79
|
" FROM tu_sources " +
|
80
|
" ORDER BY sourceuse_id, source_id, tu_id ";
|
81
|
return strQuery;
|
82
|
}
|
83
|
|
84
|
|
85
|
|
86
|
/* (non-Javadoc)
|
87
|
* @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getRecordQuery(eu.etaxonomy.cdm.io.erms.ErmsImportConfigurator)
|
88
|
*/
|
89
|
@Override
|
90
|
protected String getRecordQuery(ErmsImportConfigurator config) {
|
91
|
String strRecordQuery =
|
92
|
" SELECT * " +
|
93
|
" FROM tu_sources INNER JOIN sourceuses ON tu_sources.sourceuse_id = sourceuses.sourceuse_id" +
|
94
|
" WHERE ( tu_sources.sourceuse_id IN (" + ID_LIST_TOKEN + ") AND " +
|
95
|
" tu_sources.source_id IN (" + ID_LIST_TOKEN + ") AND " +
|
96
|
" tu_sources.tu_id IN (" + ID_LIST_TOKEN + ") )";
|
97
|
return strRecordQuery;
|
98
|
}
|
99
|
|
100
|
|
101
|
@Override
|
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 TaxonName makeOriginalDescription(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
|
174
|
TaxonName taxonName = (TaxonName)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
|
taxon = synonym.getAcceptedTaxon();
|
218
|
if (taxon == null){
|
219
|
String warning = "Synonym "+ strTaxonId + " has no accepted taxon";
|
220
|
logger.warn(warning);
|
221
|
return null;
|
222
|
//throw new IllegalStateException(warning);
|
223
|
}
|
224
|
//add synonym name as name used in source
|
225
|
source.setNameUsedInSource(synonym.getName());
|
226
|
}else{
|
227
|
taxon = (Taxon)taxonBase;
|
228
|
}
|
229
|
|
230
|
//get or create description and add the element
|
231
|
TaxonDescription description;
|
232
|
if (taxon.getDescriptions().size() > 0){
|
233
|
description = taxon.getDescriptions().iterator().next();
|
234
|
}else{
|
235
|
description = TaxonDescription.NewInstance(taxon);
|
236
|
}
|
237
|
description.addElement(element);
|
238
|
return taxon;
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* @param partitioner
|
243
|
* @param state
|
244
|
* @param ref
|
245
|
* @param strTaxonId
|
246
|
* @param strPageNr
|
247
|
*/
|
248
|
private IdentifiableEntity<?> makeSourceOfSynonymy(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
|
249
|
TaxonBase<?> taxonBase = (TaxonBase<?>)state.getRelatedObject(ErmsTaxonImport.TAXON_NAMESPACE, strTaxonId);
|
250
|
if (taxonBase == null){
|
251
|
String warning = "taxonBase (id = " + strTaxonId + ") could not be found ";
|
252
|
logger.warn(warning);
|
253
|
return null;
|
254
|
}else if (! taxonBase.isInstanceOf(Synonym.class)){
|
255
|
String message = "TaxonBase is not of class Synonym but " + taxonBase.getClass().getSimpleName();
|
256
|
logger.info(message);
|
257
|
Taxon taxon =CdmBase.deproxy(taxonBase, Taxon.class);
|
258
|
Set<TaxonRelationship> synRels = taxon.getTaxonRelations();
|
259
|
if (synRels.size() != 1){
|
260
|
logger.warn("TaxonSynonym (" + strTaxonId + ") has not 1 but " + synRels.size() + " relations!");
|
261
|
}else{
|
262
|
TaxonRelationship synRel = synRels.iterator().next();
|
263
|
synRel.setCitation(ref);
|
264
|
synRel.setCitationMicroReference(strPageNr);
|
265
|
}
|
266
|
}else{
|
267
|
Synonym synonym =CdmBase.deproxy(taxonBase, Synonym.class);
|
268
|
synonym.setSec(ref);
|
269
|
synonym.setSecMicroReference(strPageNr);
|
270
|
}
|
271
|
|
272
|
return taxonBase;
|
273
|
}
|
274
|
|
275
|
/**
|
276
|
* @param partitioner
|
277
|
* @param state
|
278
|
* @param ref
|
279
|
* @param strTaxonId
|
280
|
* @param strPageNr
|
281
|
*/
|
282
|
private boolean isFirstRediscription = true;
|
283
|
private IdentifiableEntity<?> makeRedescription(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
|
284
|
if (isFirstRediscription){
|
285
|
logger.warn("Rediscription not yet implemented");
|
286
|
isFirstRediscription = false;
|
287
|
}
|
288
|
return null;
|
289
|
}
|
290
|
|
291
|
/**
|
292
|
* @param partitioner
|
293
|
* @param state
|
294
|
* @param ref
|
295
|
* @param strTaxonId
|
296
|
* @param strPageNr
|
297
|
*/
|
298
|
private IdentifiableEntity<?> makeCombinationReference(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
|
299
|
// Kopie von Orig. Comb.
|
300
|
//TODO ist das wirklich der richtige Name, oder muss ein verknüpfter Name verwendet werden
|
301
|
TaxonName taxonName = (TaxonName)state.getRelatedObject(ErmsTaxonImport.NAME_NAMESPACE, strTaxonId);
|
302
|
taxonName.setNomenclaturalReference(ref);
|
303
|
taxonName.setNomenclaturalMicroReference(strPageNr);
|
304
|
return taxonName;
|
305
|
}
|
306
|
|
307
|
|
308
|
/**
|
309
|
* @param partitioner
|
310
|
* @param state
|
311
|
* @param ref
|
312
|
* @param strTaxonId
|
313
|
* @param strPageNr
|
314
|
*/
|
315
|
private boolean isFirstStatusSource = true;
|
316
|
private IdentifiableEntity<?> makeStatusSource(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
|
317
|
if (isFirstStatusSource){
|
318
|
logger.warn("StatusSource not yet implemented");
|
319
|
isFirstStatusSource = false;
|
320
|
}
|
321
|
return null;
|
322
|
}
|
323
|
|
324
|
/**
|
325
|
* @param partitioner
|
326
|
* @param state
|
327
|
* @param ref
|
328
|
* @param strTaxonId
|
329
|
* @param strPageNr
|
330
|
*/
|
331
|
private boolean isFirstEmendation = true;
|
332
|
private IdentifiableEntity<?> makeEmendation(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
|
333
|
if (isFirstEmendation){
|
334
|
logger.warn("Emmendation not yet implemented");
|
335
|
isFirstEmendation = false;
|
336
|
}
|
337
|
return null;
|
338
|
}
|
339
|
|
340
|
|
341
|
|
342
|
/* (non-Javadoc)
|
343
|
* @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet, eu.etaxonomy.cdm.io.common.ImportStateBase)
|
344
|
*/
|
345
|
public CommonTaxonName createObject(ResultSet rs, ErmsImportState state)
|
346
|
throws SQLException {
|
347
|
return null; //not needed
|
348
|
}
|
349
|
|
350
|
|
351
|
//************************************ RELATED OBJECTS *************************************************/
|
352
|
|
353
|
@Override
|
354
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, ErmsImportState state) {
|
355
|
String nameSpace;
|
356
|
Class<?> cdmClass;
|
357
|
Set<String> idSet;
|
358
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
359
|
|
360
|
try{
|
361
|
Set<String> taxonIdSet = new HashSet<String>();
|
362
|
Set<String> nameIdSet = new HashSet<String>();
|
363
|
Set<String> referenceIdSet = new HashSet<String>();
|
364
|
while (rs.next()){
|
365
|
handleForeignKey(rs, taxonIdSet, "tu_id");
|
366
|
handleForeignKey(rs, nameIdSet, "tu_id");
|
367
|
handleForeignKey(rs, referenceIdSet, "source_id");
|
368
|
}
|
369
|
|
370
|
//name map
|
371
|
nameSpace = ErmsTaxonImport.NAME_NAMESPACE;
|
372
|
cdmClass = TaxonName.class;
|
373
|
idSet = nameIdSet;
|
374
|
Map<String, TaxonName> nameMap = (Map<String, TaxonName>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
375
|
result.put(nameSpace, nameMap);
|
376
|
|
377
|
//taxon map
|
378
|
nameSpace = ErmsTaxonImport.TAXON_NAMESPACE;
|
379
|
cdmClass = TaxonBase.class;
|
380
|
idSet = taxonIdSet;
|
381
|
Map<String, TaxonBase<?>> taxonMap = (Map<String, TaxonBase<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
382
|
result.put(nameSpace, taxonMap);
|
383
|
|
384
|
//reference map
|
385
|
nameSpace = ErmsReferenceImport.REFERENCE_NAMESPACE;
|
386
|
cdmClass = Reference.class;
|
387
|
idSet = referenceIdSet;
|
388
|
Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(Reference.class, idSet, nameSpace);
|
389
|
result.put(nameSpace, referenceMap);
|
390
|
|
391
|
|
392
|
} catch (SQLException e) {
|
393
|
throw new RuntimeException(e);
|
394
|
}
|
395
|
return result;
|
396
|
|
397
|
}
|
398
|
|
399
|
/* (non-Javadoc)
|
400
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
401
|
*/
|
402
|
@Override
|
403
|
protected boolean doCheck(ErmsImportState state){
|
404
|
IOValidator<ErmsImportState> validator = new ErmsSourceUsesImportValidator();
|
405
|
return validator.validate(state);
|
406
|
}
|
407
|
|
408
|
/* (non-Javadoc)
|
409
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
410
|
*/
|
411
|
@Override
|
412
|
protected boolean isIgnore(ErmsImportState state){
|
413
|
boolean result = state.getConfig().getDoReferences() != IImportConfigurator.DO_REFERENCES.ALL;
|
414
|
result |= ! state.getConfig().isDoTaxa();
|
415
|
return result;
|
416
|
}
|
417
|
|
418
|
|
419
|
/* (non-Javadoc)
|
420
|
* @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getMapping()
|
421
|
*/
|
422
|
@Override
|
423
|
protected DbImportMapping<?, ?> getMapping() {
|
424
|
logger.info("getMapping not implemented for EmrsSourceUsesImport");
|
425
|
return null; // not needed because Mapping is not implemented in this class yet
|
426
|
}
|
427
|
|
428
|
}
|