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.algaterra;
|
11
|
|
12
|
import java.sql.Date;
|
13
|
import java.sql.ResultSet;
|
14
|
import java.sql.SQLException;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.HashSet;
|
17
|
import java.util.Map;
|
18
|
import java.util.Set;
|
19
|
|
20
|
import org.apache.commons.lang.StringUtils;
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
|
25
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade.DerivedUnitType;
|
26
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy;
|
27
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
|
28
|
import eu.etaxonomy.cdm.io.algaterra.validation.AlgaTerraSpecimenImportValidator;
|
29
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator;
|
30
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
|
31
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelReferenceImport;
|
32
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelTaxonImport;
|
33
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
34
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
35
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
36
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
37
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
38
|
import eu.etaxonomy.cdm.model.description.Feature;
|
39
|
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
|
40
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
41
|
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
|
42
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
43
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
|
44
|
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
|
45
|
import eu.etaxonomy.cdm.model.occurrence.FieldObservation;
|
46
|
import eu.etaxonomy.cdm.model.occurrence.Fossil;
|
47
|
import eu.etaxonomy.cdm.model.occurrence.LivingBeing;
|
48
|
import eu.etaxonomy.cdm.model.occurrence.Observation;
|
49
|
import eu.etaxonomy.cdm.model.occurrence.Specimen;
|
50
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
51
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
52
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
53
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
54
|
|
55
|
|
56
|
/**
|
57
|
* Alga Terra Import für den Fact mit FactId =202 (Ecology)
|
58
|
* @author a.mueller
|
59
|
* @created 01.09.2012
|
60
|
*/
|
61
|
@Component
|
62
|
public class AlgaTerraFactEcologyImport extends AlgaTerraSpecimenImportBase {
|
63
|
private static final Logger logger = Logger.getLogger(AlgaTerraFactEcologyImport.class);
|
64
|
|
65
|
|
66
|
private static int modCount = 5000;
|
67
|
private static final String pluralString = "determinations";
|
68
|
private static final String dbTableName = "Fact";
|
69
|
|
70
|
|
71
|
public AlgaTerraFactEcologyImport(){
|
72
|
super(dbTableName, pluralString);
|
73
|
}
|
74
|
|
75
|
|
76
|
|
77
|
/* (non-Javadoc)
|
78
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getIdQuery()
|
79
|
*/
|
80
|
@Override
|
81
|
protected String getIdQuery(BerlinModelImportState state) {
|
82
|
String result = " SELECT f.factId " +
|
83
|
" FROM Fact f LEFT JOIN PTaxon pt ON f.PTNameFk = pt.PTNameFk AND f.PTRefFk = pt.PTRefFk "
|
84
|
+ " WHERE f.FactCategoryFk = 202 "
|
85
|
+ " ORDER BY pt.RIdentifier, f.FactId ";
|
86
|
return result;
|
87
|
}
|
88
|
|
89
|
/* (non-Javadoc)
|
90
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
91
|
*/
|
92
|
@Override
|
93
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
94
|
String strQuery =
|
95
|
" SELECT pt.RIdentifier as taxonId, f.* " +
|
96
|
" FROM Fact f " +
|
97
|
" LEFT JOIN PTaxon pt ON f.PTNameFk =pt.PTNameFk AND f.PTRefFk = pt.PTRefFk " +
|
98
|
" WHERE f.FactCategoryFk = 202 AND (f.FactId IN (" + ID_LIST_TOKEN + ") )"
|
99
|
+ " ORDER BY pt.RIdentifier, f.FactId "
|
100
|
;
|
101
|
return strQuery;
|
102
|
}
|
103
|
|
104
|
/* (non-Javadoc)
|
105
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
|
106
|
*/
|
107
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState bmState) {
|
108
|
boolean success = true;
|
109
|
|
110
|
AlgaTerraImportState state = (AlgaTerraImportState)bmState;
|
111
|
Set<TaxonBase> taxaToSave = new HashSet<TaxonBase>();
|
112
|
|
113
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>) partitioner.getObjectMap(BerlinModelTaxonImport.NAMESPACE);
|
114
|
Map<String, DerivedUnitBase> ecoFactDerivedUnitMap = (Map<String, DerivedUnitBase>) partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE);
|
115
|
|
116
|
ResultSet rs = partitioner.getResultSet();
|
117
|
|
118
|
try {
|
119
|
|
120
|
int i = 0;
|
121
|
|
122
|
//for each reference
|
123
|
while (rs.next()){
|
124
|
|
125
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
|
126
|
|
127
|
Integer taxonId = nullSafeInt(rs, "taxonId");
|
128
|
int factId = rs.getInt("FactId");
|
129
|
Integer ecoFactId = nullSafeInt(rs, "ExtensionFk");
|
130
|
String recordBasis = rs.getString("RecordBasis");
|
131
|
|
132
|
|
133
|
try {
|
134
|
|
135
|
//source ref
|
136
|
Reference<?> sourceRef = state.getTransactionalSourceReference();
|
137
|
|
138
|
DerivedUnitBase<?> ecoFact = ecoFactDerivedUnitMap.get(String.valueOf(ecoFactId));
|
139
|
|
140
|
|
141
|
//description element
|
142
|
if (taxonId != null){
|
143
|
Taxon taxon = getTaxon(state, taxonId, taxonMap, factId);
|
144
|
|
145
|
if(taxon != null){
|
146
|
DerivedUnitBase identifiedSpecimen = makeIdentifiedSpecimen(ecoFact, recordBasis);
|
147
|
|
148
|
makeDetermination(state, rs, taxon, identifiedSpecimen, factId, partitioner);
|
149
|
|
150
|
makeIndividualsAssociation(state, taxon, sourceRef, identifiedSpecimen);
|
151
|
|
152
|
this.doIdCreatedUpdatedNotes(state, identifiedSpecimen, rs, factId, getDerivedUnitNameSpace());
|
153
|
|
154
|
identifiedSpecimen.setCacheStrategy(new DerivedUnitFacadeCacheStrategy());
|
155
|
taxaToSave.add(taxon);
|
156
|
}
|
157
|
}else{
|
158
|
logger.warn("No taxon defined for ecology fact: " + factId);
|
159
|
}
|
160
|
|
161
|
|
162
|
} catch (Exception e) {
|
163
|
logger.warn("Exception in FactEcology: FactId " + factId + ". " + e.getMessage());
|
164
|
e.printStackTrace();
|
165
|
}
|
166
|
|
167
|
}
|
168
|
|
169
|
// logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
|
170
|
|
171
|
logger.warn("Taxa to save: " + taxaToSave.size());
|
172
|
getTaxonService().save(taxaToSave);
|
173
|
|
174
|
return success;
|
175
|
} catch (SQLException e) {
|
176
|
logger.error("SQLException:" + e);
|
177
|
return false;
|
178
|
}
|
179
|
}
|
180
|
|
181
|
private void makeIndividualsAssociation(AlgaTerraImportState state, Taxon taxon, Reference<?> sourceRef, DerivedUnitBase<?> identifiedSpecimen){
|
182
|
TaxonDescription taxonDescription = getTaxonDescription(state, taxon, sourceRef);
|
183
|
IndividualsAssociation indAssociation = IndividualsAssociation.NewInstance();
|
184
|
Feature feature = makeFeature(identifiedSpecimen);
|
185
|
indAssociation.setAssociatedSpecimenOrObservation(identifiedSpecimen);
|
186
|
indAssociation.setFeature(feature);
|
187
|
taxonDescription.addElement(indAssociation);
|
188
|
}
|
189
|
|
190
|
private void makeDetermination(AlgaTerraImportState state, ResultSet rs, Taxon taxon, DerivedUnitBase<?> identifiedSpecimen, int factId, ResultSetPartitioner partitioner) throws SQLException {
|
191
|
Date identifiedWhen = rs.getDate("IdentifiedWhen");
|
192
|
Date identifiedWhenEnd = rs.getDate("IdentiedWhenEnd");
|
193
|
boolean restrictedFlag = rs.getBoolean("RestrictedFlag");
|
194
|
//Team FK ist immer null
|
195
|
String identifiedBy = rs.getString("IdentifiedBy");
|
196
|
String identificationReference = rs.getString("IdentificationReference");
|
197
|
Integer refFk = nullSafeInt(rs, "IdentifidationRefFk");
|
198
|
|
199
|
|
200
|
DeterminationEvent determination = DeterminationEvent.NewInstance(taxon, identifiedSpecimen);
|
201
|
TimePeriod determinationPeriod = TimePeriod.NewInstance(identifiedWhen, identifiedWhenEnd);
|
202
|
determination.setTimeperiod(determinationPeriod);
|
203
|
determination.setPreferredFlag(! restrictedFlag);
|
204
|
//TODO
|
205
|
|
206
|
TeamOrPersonBase<?> author = getAuthor(identifiedBy);
|
207
|
determination.setDeterminer(author);
|
208
|
if (refFk != null){
|
209
|
Map<String, Reference> biblioRefMap = (Map<String, Reference>) partitioner.getObjectMap(BerlinModelReferenceImport.BIBLIO_REFERENCE_NAMESPACE);
|
210
|
Map<String, Reference> nomRefMap = (Map<String, Reference>) partitioner.getObjectMap(BerlinModelReferenceImport.NOM_REFERENCE_NAMESPACE);
|
211
|
|
212
|
Reference<?> ref = getReferenceOnlyFromMaps(biblioRefMap, nomRefMap, String.valueOf(refFk));
|
213
|
determination.addReference(ref);
|
214
|
}else{
|
215
|
//IdentificationReference is not to be handled according to Henning
|
216
|
if (StringUtils.isNotBlank(identificationReference)){
|
217
|
logger.warn("IdentificationReference exists without IdentificationRefFk. FactId: "+ factId);
|
218
|
}
|
219
|
}
|
220
|
|
221
|
|
222
|
|
223
|
//TODO
|
224
|
// kind of identification, IdentificationUncertainty, IdentificationMethod,
|
225
|
|
226
|
|
227
|
}
|
228
|
|
229
|
|
230
|
|
231
|
private DerivedUnitBase<?> makeIdentifiedSpecimen(DerivedUnitBase<?> ecoFact, String recordBasis) {
|
232
|
//TODO event type
|
233
|
DerivationEvent event = DerivationEvent.NewInstance();
|
234
|
DerivedUnitType derivedUnitType = makeDerivedUnitType(recordBasis);
|
235
|
if (derivedUnitType == null){
|
236
|
logger.warn("NULL");
|
237
|
}
|
238
|
|
239
|
DerivedUnitBase<?> result = derivedUnitType.getNewDerivedUnitInstance();
|
240
|
result.setDerivedFrom(event);
|
241
|
ecoFact.addDerivationEvent(event);
|
242
|
|
243
|
return result;
|
244
|
}
|
245
|
|
246
|
|
247
|
|
248
|
protected String getDerivedUnitNameSpace(){
|
249
|
return FACT_ECOLOGY_NAMESPACE;
|
250
|
}
|
251
|
|
252
|
protected String getFieldObservationNameSpace(){
|
253
|
return null;
|
254
|
}
|
255
|
|
256
|
|
257
|
|
258
|
|
259
|
/**
|
260
|
* @param state
|
261
|
* @param ecoFactId
|
262
|
* @param derivedUnitMap
|
263
|
* @param type
|
264
|
* @return
|
265
|
*/
|
266
|
private DerivedUnitFacade getDerivedUnit(AlgaTerraImportState state, int ecoFactId, Map<String, DerivedUnit> derivedUnitMap, DerivedUnitType type) {
|
267
|
String key = String.valueOf(ecoFactId);
|
268
|
DerivedUnit derivedUnit = derivedUnitMap.get(key);
|
269
|
DerivedUnitFacade facade;
|
270
|
if (derivedUnit == null){
|
271
|
facade = DerivedUnitFacade.NewInstance(type);
|
272
|
derivedUnitMap.put(key, derivedUnit);
|
273
|
}else{
|
274
|
try {
|
275
|
facade = DerivedUnitFacade.NewInstance(derivedUnit);
|
276
|
} catch (DerivedUnitFacadeNotSupportedException e) {
|
277
|
logger.error(e.getMessage());
|
278
|
facade = DerivedUnitFacade.NewInstance(type);
|
279
|
}
|
280
|
}
|
281
|
|
282
|
return facade;
|
283
|
}
|
284
|
|
285
|
private Feature makeFeature(SpecimenOrObservationBase unit) {
|
286
|
if (unit.isInstanceOf(DerivedUnit.class)){
|
287
|
return Feature.INDIVIDUALS_ASSOCIATION();
|
288
|
}else if (unit.isInstanceOf(FieldObservation.class) || unit.isInstanceOf(Observation.class) ){
|
289
|
return Feature.OBSERVATION();
|
290
|
}else if (unit.isInstanceOf(Fossil.class) || unit.isInstanceOf(LivingBeing.class) || unit.isInstanceOf(Specimen.class )){
|
291
|
return Feature.SPECIMEN();
|
292
|
}
|
293
|
logger.warn("No feature defined for derived unit class: " + unit.getClass().getSimpleName());
|
294
|
return null;
|
295
|
}
|
296
|
|
297
|
|
298
|
private DerivedUnitType makeDerivedUnitType(String recordBasis) {
|
299
|
DerivedUnitType result = null;
|
300
|
if (StringUtils.isBlank(recordBasis)){
|
301
|
result = DerivedUnitType.DerivedUnit;
|
302
|
} else if (recordBasis.equalsIgnoreCase("FossileSpecimen")){
|
303
|
result = DerivedUnitType.Fossil;
|
304
|
}else if (recordBasis.equalsIgnoreCase("Observation")){
|
305
|
result = DerivedUnitType.Observation;
|
306
|
}else if (recordBasis.equalsIgnoreCase("HumanObservation")){
|
307
|
result = DerivedUnitType.Observation;
|
308
|
}else if (recordBasis.equalsIgnoreCase("Literature")){
|
309
|
logger.warn("Literature record basis not yet supported");
|
310
|
result = DerivedUnitType.DerivedUnit;
|
311
|
}else if (recordBasis.equalsIgnoreCase("LivingSpecimen")){
|
312
|
result = DerivedUnitType.LivingBeing;
|
313
|
}else if (recordBasis.equalsIgnoreCase("LivingCulture")){
|
314
|
logger.warn("LivingCulture record basis not yet supported");
|
315
|
result = DerivedUnitType.DerivedUnit;
|
316
|
}else if (recordBasis.equalsIgnoreCase("MachineObservation")){
|
317
|
logger.warn("MachineObservation record basis not yet supported");
|
318
|
result = DerivedUnitType.Observation;
|
319
|
}else if (recordBasis.equalsIgnoreCase("PreservedSpecimen")){
|
320
|
result = DerivedUnitType.Specimen;
|
321
|
}
|
322
|
return result;
|
323
|
}
|
324
|
|
325
|
|
326
|
|
327
|
/* (non-Javadoc)
|
328
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
|
329
|
*/
|
330
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
|
331
|
String nameSpace;
|
332
|
Class cdmClass;
|
333
|
Set<String> idSet;
|
334
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
335
|
|
336
|
try{
|
337
|
Set<String> taxonIdSet = new HashSet<String>();
|
338
|
Set<String> extensionFkSet = new HashSet<String>();
|
339
|
Set<String> referenceIdSet = new HashSet<String>();
|
340
|
|
341
|
while (rs.next()){
|
342
|
handleForeignKey(rs, taxonIdSet, "taxonId");
|
343
|
handleForeignKey(rs, extensionFkSet, "extensionFk");
|
344
|
handleForeignKey(rs, referenceIdSet, "IdentifidationRefFk");
|
345
|
}
|
346
|
|
347
|
//taxon map
|
348
|
nameSpace = BerlinModelTaxonImport.NAMESPACE;
|
349
|
cdmClass = TaxonBase.class;
|
350
|
idSet = taxonIdSet;
|
351
|
Map<String, TaxonBase> objectMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
352
|
result.put(nameSpace, objectMap);
|
353
|
|
354
|
//derived unit map
|
355
|
nameSpace = AlgaTerraFactEcologyImport.ECO_FACT_DERIVED_UNIT_NAMESPACE;
|
356
|
cdmClass = DerivedUnitBase.class;
|
357
|
idSet = extensionFkSet;
|
358
|
Map<String, DerivedUnitBase> derivedUnitMap = (Map<String, DerivedUnitBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
359
|
result.put(nameSpace, derivedUnitMap);
|
360
|
|
361
|
//nom reference map
|
362
|
nameSpace = BerlinModelReferenceImport.NOM_REFERENCE_NAMESPACE;
|
363
|
cdmClass = Reference.class;
|
364
|
idSet = referenceIdSet;
|
365
|
Map<String, Reference> nomReferenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
366
|
result.put(nameSpace, nomReferenceMap);
|
367
|
|
368
|
//biblio reference map
|
369
|
nameSpace = BerlinModelReferenceImport.BIBLIO_REFERENCE_NAMESPACE;
|
370
|
cdmClass = Reference.class;
|
371
|
idSet = referenceIdSet;
|
372
|
Map<String, Reference> biblioReferenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
373
|
result.put(nameSpace, biblioReferenceMap);
|
374
|
|
375
|
|
376
|
} catch (SQLException e) {
|
377
|
throw new RuntimeException(e);
|
378
|
}
|
379
|
return result;
|
380
|
}
|
381
|
|
382
|
/* (non-Javadoc)
|
383
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
|
384
|
*/
|
385
|
@Override
|
386
|
protected boolean doCheck(BerlinModelImportState state){
|
387
|
IOValidator<BerlinModelImportState> validator = new AlgaTerraSpecimenImportValidator();
|
388
|
return validator.validate(state);
|
389
|
}
|
390
|
|
391
|
|
392
|
/* (non-Javadoc)
|
393
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
394
|
*/
|
395
|
protected boolean isIgnore(BerlinModelImportState state){
|
396
|
return ! ((AlgaTerraImportState)state).getAlgaTerraConfigurator().isDoEcoFacts();
|
397
|
}
|
398
|
|
399
|
}
|