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.erms;
|
11
|
|
12
|
import static eu.etaxonomy.cdm.io.erms.ErmsTransformer.SOURCE_USE_ADDITIONAL_SOURCE;
|
13
|
import static eu.etaxonomy.cdm.io.erms.ErmsTransformer.SOURCE_USE_BASIS_OF_RECORD;
|
14
|
import static eu.etaxonomy.cdm.io.erms.ErmsTransformer.SOURCE_USE_EMENDATION;
|
15
|
import static eu.etaxonomy.cdm.io.erms.ErmsTransformer.SOURCE_USE_NEW_COMBINATION_REFERENCE;
|
16
|
import static eu.etaxonomy.cdm.io.erms.ErmsTransformer.SOURCE_USE_ORIGINAL_DESCRIPTION;
|
17
|
import static eu.etaxonomy.cdm.io.erms.ErmsTransformer.SOURCE_USE_REDESCRIPTION;
|
18
|
import static eu.etaxonomy.cdm.io.erms.ErmsTransformer.SOURCE_USE_SOURCE_OF_SYNONYMY;
|
19
|
import static eu.etaxonomy.cdm.io.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.log4j.Logger;
|
29
|
import org.springframework.stereotype.Component;
|
30
|
|
31
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
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.erms.validation.ErmsSourceUsesImportValidator;
|
37
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
38
|
import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
|
39
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
40
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
41
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
42
|
import eu.etaxonomy.cdm.model.description.Feature;
|
43
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
44
|
import eu.etaxonomy.cdm.model.description.TextData;
|
45
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
46
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
47
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
48
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
|
49
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
50
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
51
|
|
52
|
/**
|
53
|
* @author a.mueller
|
54
|
* @created 20.02.2010
|
55
|
* @version 1.0
|
56
|
*/
|
57
|
@Component
|
58
|
public class ErmsSourceUsesImport extends ErmsImportBase<CommonTaxonName> {
|
59
|
private static final Logger logger = Logger.getLogger(ErmsSourceUsesImport.class);
|
60
|
|
61
|
private DbImportMapping mapping; //not needed
|
62
|
|
63
|
|
64
|
private int modCount = 10000;
|
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
|
|
74
|
/* (non-Javadoc)
|
75
|
* @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getIdQuery()
|
76
|
*/
|
77
|
@Override
|
78
|
protected String getIdQuery() {
|
79
|
String strQuery = " SELECT sourceuse_id, source_id, tu_id " + " " +
|
80
|
" FROM tu_sources " +
|
81
|
" ORDER BY sourceuse_id, source_id, tu_id ";
|
82
|
return strQuery;
|
83
|
}
|
84
|
|
85
|
|
86
|
|
87
|
/* (non-Javadoc)
|
88
|
* @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getRecordQuery(eu.etaxonomy.cdm.io.erms.ErmsImportConfigurator)
|
89
|
*/
|
90
|
@Override
|
91
|
protected String getRecordQuery(ErmsImportConfigurator config) {
|
92
|
String strRecordQuery =
|
93
|
" SELECT * " +
|
94
|
" FROM tu_sources INNER JOIN sourceuses ON tu_sources.sourceuse_id = sourceuses.sourceuse_id" +
|
95
|
" WHERE ( tu_sources.sourceuse_id IN (" + ID_LIST_TOKEN + ") AND " +
|
96
|
" tu_sources.source_id IN (" + ID_LIST_TOKEN + ") AND " +
|
97
|
" tu_sources.tu_id IN (" + ID_LIST_TOKEN + ") )";
|
98
|
return strRecordQuery;
|
99
|
}
|
100
|
|
101
|
|
102
|
public boolean doPartition(ResultSetPartitioner partitioner, ErmsImportState state) {
|
103
|
boolean success = true ;
|
104
|
ErmsImportConfigurator config = state.getConfig();
|
105
|
Set objectsToSave = new HashSet<IdentifiableEntity>();
|
106
|
|
107
|
// DbImportMapping<?, ?> mapping = getMapping();
|
108
|
// mapping.initialize(state, cdmTargetClass);
|
109
|
|
110
|
ResultSet rs = partitioner.getResultSet();
|
111
|
try{
|
112
|
while (rs.next()){
|
113
|
//success &= mapping.invoke(rs,referencesToSave);
|
114
|
|
115
|
//read and normalize values
|
116
|
int sourceUseId = rs.getInt("sourceuse_id");
|
117
|
int sourceId = rs.getInt("source_id");
|
118
|
String strSourceId = String.valueOf(sourceId);
|
119
|
int taxonId = rs.getInt("tu_id");
|
120
|
String strTaxonId = String.valueOf(taxonId);
|
121
|
String strPageNr = rs.getString("pagenr");
|
122
|
if (CdmUtils.isEmpty(strPageNr)){
|
123
|
strPageNr = null;
|
124
|
}
|
125
|
ReferenceBase ref = (ReferenceBase)state.getRelatedObject(ErmsReferenceImport.REFERENCE_NAMESPACE, strSourceId);
|
126
|
|
127
|
try {
|
128
|
IdentifiableEntity objectToSave = null;
|
129
|
//invoke methods for each sourceUse type
|
130
|
if (sourceUseId == SOURCE_USE_ORIGINAL_DESCRIPTION){
|
131
|
objectToSave = makeOriginalDescription(partitioner, state, ref, strTaxonId, strPageNr);
|
132
|
}else if (sourceUseId == SOURCE_USE_BASIS_OF_RECORD){
|
133
|
objectToSave = makeBasisOfRecord(partitioner, state, ref, strTaxonId, strPageNr);
|
134
|
}else if (sourceUseId == SOURCE_USE_ADDITIONAL_SOURCE){
|
135
|
objectToSave = makeAdditionalSource(partitioner, state, ref, strTaxonId, strPageNr);
|
136
|
}else if (sourceUseId == SOURCE_USE_SOURCE_OF_SYNONYMY){
|
137
|
objectToSave = makeSourceOfSynonymy(partitioner, state, ref, strTaxonId, strPageNr);
|
138
|
}else if (sourceUseId == SOURCE_USE_REDESCRIPTION){
|
139
|
objectToSave = makeRedescription(partitioner, state, ref, strTaxonId, strPageNr);
|
140
|
}else if (sourceUseId == SOURCE_USE_NEW_COMBINATION_REFERENCE){
|
141
|
objectToSave = makeCombinationReference(partitioner, state, ref, strTaxonId, strPageNr);
|
142
|
}else if (sourceUseId == SOURCE_USE_STATUS_SOURCE){
|
143
|
objectToSave = makeStatusSource(partitioner, state, ref, strTaxonId, strPageNr);
|
144
|
}else if (sourceUseId == SOURCE_USE_EMENDATION){
|
145
|
objectToSave = makeEmendation(partitioner, state, ref, strTaxonId, strPageNr);
|
146
|
}
|
147
|
if(objectToSave != null){
|
148
|
objectsToSave.add(objectToSave);
|
149
|
}
|
150
|
} catch (Exception e) {
|
151
|
e.printStackTrace();
|
152
|
success = false;
|
153
|
}
|
154
|
}
|
155
|
} catch (SQLException e) {
|
156
|
logger.error("SQLException:" + e);
|
157
|
return false;
|
158
|
}
|
159
|
|
160
|
partitioner.startDoSave();
|
161
|
getCommonService().save(objectsToSave);
|
162
|
return success;
|
163
|
}
|
164
|
|
165
|
|
166
|
/**
|
167
|
* @param strTaxonId
|
168
|
* @param ref
|
169
|
* @param state
|
170
|
* @param partitioner
|
171
|
* @param strPageNr
|
172
|
*
|
173
|
*/
|
174
|
private TaxonNameBase makeOriginalDescription(ResultSetPartitioner partitioner, ErmsImportState state, ReferenceBase ref, String strTaxonId, String strPageNr) {
|
175
|
TaxonNameBase taxonName = (TaxonNameBase)state.getRelatedObject(ErmsTaxonImport.NAME_NAMESPACE, strTaxonId);
|
176
|
taxonName.setNomenclaturalReference(ref);
|
177
|
taxonName.setNomenclaturalMicroReference(strPageNr);
|
178
|
return taxonName;
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* @param partitioner
|
183
|
* @param state
|
184
|
* @param ref
|
185
|
* @param strTaxonId
|
186
|
* @param strPageNr
|
187
|
*/
|
188
|
private boolean isFirstBasisOfRecord = true;
|
189
|
private IdentifiableEntity makeBasisOfRecord(ResultSetPartitioner partitioner, ErmsImportState state, ReferenceBase ref, String strTaxonId, String strPageNr) {
|
190
|
if (isFirstBasisOfRecord){
|
191
|
logger.warn("Basis of record not yet implemented");
|
192
|
isFirstBasisOfRecord = false;
|
193
|
}
|
194
|
return null;
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* @param partitioner
|
199
|
* @param state
|
200
|
* @param ref
|
201
|
* @param strTaxonId
|
202
|
* @param strPageNr
|
203
|
*/
|
204
|
private IdentifiableEntity makeAdditionalSource(ResultSetPartitioner partitioner, ErmsImportState state, ReferenceBase ref, String strTaxonId, String strPageNr) {
|
205
|
Feature citationFeature = Feature.CITATION();
|
206
|
DescriptionElementBase element = TextData.NewInstance(citationFeature);
|
207
|
DescriptionElementSource source = element.addSource(null, null, ref, strPageNr);
|
208
|
if (source == null){
|
209
|
logger.warn("Source is null");
|
210
|
return null;
|
211
|
}
|
212
|
TaxonBase taxonBase = (TaxonBase)state.getRelatedObject(ErmsTaxonImport.TAXON_NAMESPACE, strTaxonId);
|
213
|
Taxon taxon;
|
214
|
|
215
|
//if taxon base is a synonym, add the description to the accepted taxon
|
216
|
if (taxonBase.isInstanceOf(Synonym.class)){
|
217
|
Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
|
218
|
Set<Taxon> taxa = synonym.getAcceptedTaxa();
|
219
|
if (taxa.size() < 1){
|
220
|
String warning = "Synonym "+ strTaxonId + " has no accepted taxon";
|
221
|
logger.warn(warning);
|
222
|
return null;
|
223
|
//throw new IllegalStateException(warning);
|
224
|
}else if (taxa.size() > 1){
|
225
|
String warning = "Synonym "+ strTaxonId + " has more than 1 accepted taxon";
|
226
|
logger.warn(warning);
|
227
|
return null;
|
228
|
//throw new IllegalStateException(warning);
|
229
|
}
|
230
|
taxon = taxa.iterator().next();
|
231
|
//add synonym name as name used in source
|
232
|
source.setNameUsedInSource(synonym.getName());
|
233
|
}else{
|
234
|
taxon = (Taxon)taxonBase;
|
235
|
}
|
236
|
|
237
|
//get or create description and add the element
|
238
|
TaxonDescription description;
|
239
|
if (taxon.getDescriptions().size() > 0){
|
240
|
description = taxon.getDescriptions().iterator().next();
|
241
|
}else{
|
242
|
description = TaxonDescription.NewInstance(taxon);
|
243
|
}
|
244
|
description.addElement(element);
|
245
|
return taxon;
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* @param partitioner
|
250
|
* @param state
|
251
|
* @param ref
|
252
|
* @param strTaxonId
|
253
|
* @param strPageNr
|
254
|
*/
|
255
|
private IdentifiableEntity makeSourceOfSynonymy(ResultSetPartitioner partitioner, ErmsImportState state, ReferenceBase ref, String strTaxonId, String strPageNr) {
|
256
|
TaxonBase taxonBase = (TaxonBase)state.getRelatedObject(ErmsTaxonImport.TAXON_NAMESPACE, strTaxonId);
|
257
|
if (taxonBase == null){
|
258
|
String warning = "taxonBase (id = " + strTaxonId + ") could not be found ";
|
259
|
logger.warn(warning);
|
260
|
return null;
|
261
|
}else if (! taxonBase.isInstanceOf(Synonym.class)){
|
262
|
String warning = "TaxonBase is not of class Synonym but " + taxonBase.getClass().getSimpleName();
|
263
|
logger.warn(warning);
|
264
|
return null;
|
265
|
}
|
266
|
Synonym synonym =CdmBase.deproxy(taxonBase, Synonym.class);
|
267
|
Set<SynonymRelationship> synRels = synonym.getSynonymRelations();
|
268
|
if (synRels.size() != 1){
|
269
|
logger.warn("Synonym has not 1 but " + synRels.size() + " relations!");
|
270
|
}else{
|
271
|
SynonymRelationship synRel = synRels.iterator().next();
|
272
|
synRel.setCitation(ref);
|
273
|
synRel.setCitationMicroReference(strPageNr);
|
274
|
}
|
275
|
return taxonBase;
|
276
|
}
|
277
|
|
278
|
/**
|
279
|
* @param partitioner
|
280
|
* @param state
|
281
|
* @param ref
|
282
|
* @param strTaxonId
|
283
|
* @param strPageNr
|
284
|
*/
|
285
|
private boolean isFirstRediscription = true;
|
286
|
private IdentifiableEntity makeRedescription(ResultSetPartitioner partitioner, ErmsImportState state, ReferenceBase ref, String strTaxonId, String strPageNr) {
|
287
|
if (isFirstRediscription){
|
288
|
logger.warn("Rediscription not yet implemented");
|
289
|
isFirstRediscription = false;
|
290
|
}
|
291
|
return null;
|
292
|
}
|
293
|
|
294
|
/**
|
295
|
* @param partitioner
|
296
|
* @param state
|
297
|
* @param ref
|
298
|
* @param strTaxonId
|
299
|
* @param strPageNr
|
300
|
*/
|
301
|
private IdentifiableEntity makeCombinationReference(ResultSetPartitioner partitioner, ErmsImportState state, ReferenceBase ref, String strTaxonId, String strPageNr) {
|
302
|
// Kopie von Orig. Comb.
|
303
|
//TODO ist das wirklich der richtige Name, oder muss ein verkn?pfter Name verwendet werden
|
304
|
TaxonNameBase taxonName = (TaxonNameBase)state.getRelatedObject(ErmsTaxonImport.NAME_NAMESPACE, strTaxonId);
|
305
|
taxonName.setNomenclaturalReference(ref);
|
306
|
taxonName.setNomenclaturalMicroReference(strPageNr);
|
307
|
return taxonName;
|
308
|
}
|
309
|
|
310
|
|
311
|
/**
|
312
|
* @param partitioner
|
313
|
* @param state
|
314
|
* @param ref
|
315
|
* @param strTaxonId
|
316
|
* @param strPageNr
|
317
|
*/
|
318
|
private boolean isFirstStatusSource = true;
|
319
|
private IdentifiableEntity makeStatusSource(ResultSetPartitioner partitioner, ErmsImportState state, ReferenceBase ref, String strTaxonId, String strPageNr) {
|
320
|
if (isFirstStatusSource){
|
321
|
logger.warn("StatusSource not yet implemented");
|
322
|
isFirstStatusSource = false;
|
323
|
}
|
324
|
return null;
|
325
|
}
|
326
|
|
327
|
/**
|
328
|
* @param partitioner
|
329
|
* @param state
|
330
|
* @param ref
|
331
|
* @param strTaxonId
|
332
|
* @param strPageNr
|
333
|
*/
|
334
|
private boolean isFirstEmendation = true;
|
335
|
private IdentifiableEntity makeEmendation(ResultSetPartitioner partitioner, ErmsImportState state, ReferenceBase ref, String strTaxonId, String strPageNr) {
|
336
|
if (isFirstEmendation){
|
337
|
logger.warn("Emmendation not yet implemented");
|
338
|
isFirstEmendation = false;
|
339
|
}
|
340
|
return null;
|
341
|
}
|
342
|
|
343
|
|
344
|
|
345
|
/* (non-Javadoc)
|
346
|
* @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet, eu.etaxonomy.cdm.io.common.ImportStateBase)
|
347
|
*/
|
348
|
public CommonTaxonName createObject(ResultSet rs, ErmsImportState state)
|
349
|
throws SQLException {
|
350
|
return null; //not needed
|
351
|
}
|
352
|
|
353
|
/* (non-Javadoc)
|
354
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
|
355
|
*/
|
356
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
|
357
|
String nameSpace;
|
358
|
Class cdmClass;
|
359
|
Set<String> idSet;
|
360
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
361
|
|
362
|
try{
|
363
|
Set<String> taxonIdSet = new HashSet<String>();
|
364
|
Set<String> nameIdSet = new HashSet<String>();
|
365
|
Set<String> referenceIdSet = new HashSet<String>();
|
366
|
while (rs.next()){
|
367
|
handleForeignKey(rs, taxonIdSet, "tu_id");
|
368
|
handleForeignKey(rs, nameIdSet, "tu_id");
|
369
|
handleForeignKey(rs, referenceIdSet, "source_id");
|
370
|
}
|
371
|
|
372
|
//name map
|
373
|
nameSpace = ErmsTaxonImport.NAME_NAMESPACE;
|
374
|
cdmClass = TaxonNameBase.class;
|
375
|
idSet = nameIdSet;
|
376
|
Map<String, TaxonNameBase> nameMap = (Map<String, TaxonNameBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
377
|
result.put(nameSpace, nameMap);
|
378
|
|
379
|
//taxon map
|
380
|
nameSpace = ErmsTaxonImport.TAXON_NAMESPACE;
|
381
|
cdmClass = TaxonBase.class;
|
382
|
idSet = taxonIdSet;
|
383
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
384
|
result.put(nameSpace, taxonMap);
|
385
|
|
386
|
//reference map
|
387
|
nameSpace = ErmsReferenceImport.REFERENCE_NAMESPACE;
|
388
|
cdmClass = ReferenceBase.class;
|
389
|
idSet = referenceIdSet;
|
390
|
Map<String, ReferenceBase> referenceMap = (Map<String, ReferenceBase>)getCommonService().getSourcedObjectsByIdInSource(ReferenceBase.class, idSet, nameSpace);
|
391
|
result.put(nameSpace, referenceMap);
|
392
|
|
393
|
|
394
|
} catch (SQLException e) {
|
395
|
throw new RuntimeException(e);
|
396
|
}
|
397
|
return result;
|
398
|
|
399
|
}
|
400
|
|
401
|
/* (non-Javadoc)
|
402
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
403
|
*/
|
404
|
@Override
|
405
|
protected boolean doCheck(ErmsImportState state){
|
406
|
IOValidator<ErmsImportState> validator = new ErmsSourceUsesImportValidator();
|
407
|
return validator.validate(state);
|
408
|
}
|
409
|
|
410
|
/* (non-Javadoc)
|
411
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
412
|
*/
|
413
|
protected boolean isIgnore(ErmsImportState state){
|
414
|
boolean result = state.getConfig().getDoReferences() != IImportConfigurator.DO_REFERENCES.ALL;
|
415
|
result &= state.getConfig().isDoTaxa();
|
416
|
return state.getConfig().getDoReferences() != IImportConfigurator.DO_REFERENCES.ALL;
|
417
|
}
|
418
|
|
419
|
|
420
|
/* (non-Javadoc)
|
421
|
* @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getMapping()
|
422
|
*/
|
423
|
@Override
|
424
|
protected DbImportMapping<?, ?> getMapping() {
|
425
|
logger.warn("getMapping not yet implemented for EmrsSourceUsesImport");
|
426
|
return null; // not needed because Mapping is not implemented in this class yet
|
427
|
}
|
428
|
|
429
|
}
|