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.DerivedUnitFacadeCacheStrategy;
|
26
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
|
27
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
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.DeterminationEvent;
|
44
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
45
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
46
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
47
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
48
|
|
49
|
|
50
|
/**
|
51
|
* Alga Terra Import für den Fact mit FactId =202 (Ecology)
|
52
|
* @author a.mueller
|
53
|
* @since 01.09.2012
|
54
|
*/
|
55
|
@Component
|
56
|
public class AlgaTerraFactEcologyImport extends AlgaTerraSpecimenImportBase {
|
57
|
private static final Logger logger = Logger.getLogger(AlgaTerraFactEcologyImport.class);
|
58
|
|
59
|
|
60
|
private static int modCount = 5000;
|
61
|
private static final String pluralString = "determinations";
|
62
|
private static final String dbTableName = "Fact";
|
63
|
|
64
|
|
65
|
public AlgaTerraFactEcologyImport(){
|
66
|
super(dbTableName, pluralString);
|
67
|
}
|
68
|
|
69
|
|
70
|
@Override
|
71
|
protected String getIdQuery(BerlinModelImportState state) {
|
72
|
String result = " SELECT f.factId " +
|
73
|
" FROM Fact f LEFT JOIN PTaxon pt ON f.PTNameFk = pt.PTNameFk AND f.PTRefFk = pt.PTRefFk "
|
74
|
+ " WHERE f.FactCategoryFk = 202 "
|
75
|
+ " ORDER BY pt.RIdentifier, f.FactId ";
|
76
|
return result;
|
77
|
}
|
78
|
|
79
|
@Override
|
80
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
81
|
String strQuery =
|
82
|
" SELECT pt.RIdentifier as taxonId, f.* " +
|
83
|
" FROM Fact f " +
|
84
|
" LEFT JOIN PTaxon pt ON f.PTNameFk =pt.PTNameFk AND f.PTRefFk = pt.PTRefFk " +
|
85
|
" WHERE f.FactCategoryFk = 202 AND (f.FactId IN (" + ID_LIST_TOKEN + ") )"
|
86
|
+ " ORDER BY pt.RIdentifier, f.FactId "
|
87
|
;
|
88
|
return strQuery;
|
89
|
}
|
90
|
|
91
|
@Override
|
92
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState bmState) {
|
93
|
boolean success = true;
|
94
|
|
95
|
AlgaTerraImportState state = (AlgaTerraImportState)bmState;
|
96
|
Set<TaxonBase> taxaToSave = new HashSet<TaxonBase>();
|
97
|
|
98
|
Map<String, TaxonBase> taxonMap = partitioner.getObjectMap(BerlinModelTaxonImport.NAMESPACE);
|
99
|
Map<String, DerivedUnit> ecoFactDerivedUnitMap = partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE);
|
100
|
|
101
|
ResultSet rs = partitioner.getResultSet();
|
102
|
|
103
|
try {
|
104
|
|
105
|
int i = 0;
|
106
|
|
107
|
//for each reference
|
108
|
while (rs.next()){
|
109
|
|
110
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
|
111
|
|
112
|
Integer taxonId = nullSafeInt(rs, "taxonId");
|
113
|
int factId = rs.getInt("FactId");
|
114
|
Integer ecoFactId = nullSafeInt(rs, "ExtensionFk");
|
115
|
String recordBasis = rs.getString("RecordBasis");
|
116
|
|
117
|
|
118
|
try {
|
119
|
|
120
|
//source ref
|
121
|
Reference sourceRef = state.getTransactionalSourceReference();
|
122
|
|
123
|
DerivedUnit ecoFact = ecoFactDerivedUnitMap.get(String.valueOf(ecoFactId));
|
124
|
if (ecoFact == null){
|
125
|
logger.warn("EcoFact is null for EcoFact: " + CdmUtils.Nz(ecoFactId) + ", taxonId: " + CdmUtils.Nz(taxonId));
|
126
|
}
|
127
|
|
128
|
//description element
|
129
|
if (taxonId != null){
|
130
|
Taxon taxon = getTaxon(state, taxonId, taxonMap, factId);
|
131
|
|
132
|
if(taxon != null){
|
133
|
DerivedUnit identifiedSpecimen = makeIdentifiedSpecimen(ecoFact, recordBasis, taxonId, ecoFactId);
|
134
|
|
135
|
makeDetermination(state, rs, taxon, identifiedSpecimen, factId, partitioner);
|
136
|
|
137
|
makeIndividualsAssociation(state, taxon, sourceRef, identifiedSpecimen);
|
138
|
|
139
|
this.doIdCreatedUpdatedNotes(state, identifiedSpecimen, rs, factId, getDerivedUnitNameSpace());
|
140
|
|
141
|
identifiedSpecimen.setCacheStrategy(new DerivedUnitFacadeCacheStrategy());
|
142
|
taxaToSave.add(taxon);
|
143
|
}
|
144
|
}else{
|
145
|
logger.warn("No taxon defined for ecology fact: " + factId);
|
146
|
}
|
147
|
|
148
|
|
149
|
} catch (Exception e) {
|
150
|
logger.warn("Exception in FactEcology: FactId " + factId + ". " + e.getMessage());
|
151
|
e.printStackTrace();
|
152
|
}
|
153
|
|
154
|
}
|
155
|
|
156
|
// logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
|
157
|
|
158
|
logger.warn("Taxa to save: " + taxaToSave.size());
|
159
|
getTaxonService().save(taxaToSave);
|
160
|
|
161
|
return success;
|
162
|
} catch (SQLException e) {
|
163
|
logger.error("SQLException:" + e);
|
164
|
return false;
|
165
|
}
|
166
|
}
|
167
|
|
168
|
private void makeIndividualsAssociation(AlgaTerraImportState state, Taxon taxon, Reference sourceRef, DerivedUnit identifiedSpecimen){
|
169
|
TaxonDescription taxonDescription = getTaxonDescription(state, taxon, sourceRef);
|
170
|
IndividualsAssociation indAssociation = IndividualsAssociation.NewInstance();
|
171
|
Feature feature = makeFeature(identifiedSpecimen.getRecordBasis(), state);
|
172
|
indAssociation.setAssociatedSpecimenOrObservation(identifiedSpecimen);
|
173
|
indAssociation.setFeature(feature);
|
174
|
taxonDescription.addElement(indAssociation);
|
175
|
}
|
176
|
|
177
|
private void makeDetermination(AlgaTerraImportState state, ResultSet rs, Taxon taxon, DerivedUnit identifiedSpecimen, int factId, ResultSetPartitioner partitioner) throws SQLException {
|
178
|
Date identifiedWhen = rs.getDate("IdentifiedWhen");
|
179
|
Date identifiedWhenEnd = rs.getDate("IdentiedWhenEnd");
|
180
|
boolean restrictedFlag = rs.getBoolean("RestrictedFlag");
|
181
|
//Team FK ist immer null
|
182
|
String identifiedBy = rs.getString("IdentifiedBy");
|
183
|
String identificationReference = rs.getString("IdentificationReference");
|
184
|
Integer refFk = nullSafeInt(rs, "IdentifidationRefFk");
|
185
|
|
186
|
|
187
|
DeterminationEvent determination = DeterminationEvent.NewInstance(taxon, identifiedSpecimen);
|
188
|
TimePeriod determinationPeriod = TimePeriod.NewInstance(identifiedWhen, identifiedWhenEnd);
|
189
|
determination.setTimeperiod(determinationPeriod);
|
190
|
determination.setPreferredFlag(! restrictedFlag);
|
191
|
//TODO
|
192
|
|
193
|
TeamOrPersonBase<?> author = getAuthor(identifiedBy);
|
194
|
determination.setDeterminer(author);
|
195
|
if (refFk != null){
|
196
|
Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
|
197
|
|
198
|
Reference ref = refMap.get(String.valueOf(refFk));
|
199
|
if (ref != null){
|
200
|
determination.addReference(ref);
|
201
|
}else{
|
202
|
logger.warn("Ref not found for Determination Event");
|
203
|
}
|
204
|
}else{
|
205
|
//IdentificationReference is not to be handled according to Henning
|
206
|
if (StringUtils.isNotBlank(identificationReference)){
|
207
|
logger.warn("IdentificationReference exists without IdentificationRefFk. FactId: "+ factId);
|
208
|
}
|
209
|
}
|
210
|
|
211
|
|
212
|
|
213
|
//TODO
|
214
|
// kind of identification, IdentificationUncertainty, IdentificationMethod,
|
215
|
|
216
|
|
217
|
}
|
218
|
|
219
|
|
220
|
|
221
|
private DerivedUnit makeIdentifiedSpecimen(DerivedUnit ecoFact, String recordBasis, Integer taxonId, Integer ecoFactId) {
|
222
|
//TODO event type
|
223
|
DerivationEvent event = DerivationEvent.NewInstance();
|
224
|
SpecimenOrObservationType derivedUnitType = makeDerivedUnitType(recordBasis);
|
225
|
if (derivedUnitType == null){
|
226
|
String message = "derivedUnitType is NULL for recordBasis (%s). Use dummy type instead. (TaxonId = %s)";
|
227
|
logger.warn(String.format(message, recordBasis, taxonId));
|
228
|
derivedUnitType = SpecimenOrObservationType.PreservedSpecimen;
|
229
|
}
|
230
|
|
231
|
DerivedUnit result = DerivedUnit.NewInstance(derivedUnitType);
|
232
|
result.setDerivedFrom(event);
|
233
|
if (ecoFact == null){
|
234
|
String message = "EcoFact (%s) is null for taxonId (%s)";
|
235
|
logger.warn(String.format(message, ecoFactId, taxonId));
|
236
|
}else{
|
237
|
ecoFact.addDerivationEvent(event);
|
238
|
}
|
239
|
return result;
|
240
|
}
|
241
|
|
242
|
|
243
|
|
244
|
@Override
|
245
|
protected String getDerivedUnitNameSpace(){
|
246
|
return FACT_ECOLOGY_NAMESPACE;
|
247
|
}
|
248
|
|
249
|
@Override
|
250
|
protected String getFieldObservationNameSpace(){
|
251
|
return null;
|
252
|
}
|
253
|
|
254
|
|
255
|
|
256
|
|
257
|
/**
|
258
|
* @param state
|
259
|
* @param ecoFactId
|
260
|
* @param derivedUnitMap
|
261
|
* @param type
|
262
|
* @return
|
263
|
*/
|
264
|
private DerivedUnitFacade getDerivedUnit(AlgaTerraImportState state, int ecoFactId, Map<String, DerivedUnit> derivedUnitMap, SpecimenOrObservationType type) {
|
265
|
String key = String.valueOf(ecoFactId);
|
266
|
DerivedUnit derivedUnit = derivedUnitMap.get(key);
|
267
|
DerivedUnitFacade facade;
|
268
|
if (derivedUnit == null){
|
269
|
facade = DerivedUnitFacade.NewInstance(type);
|
270
|
derivedUnitMap.put(key, derivedUnit);
|
271
|
}else{
|
272
|
try {
|
273
|
facade = DerivedUnitFacade.NewInstance(derivedUnit);
|
274
|
} catch (DerivedUnitFacadeNotSupportedException e) {
|
275
|
logger.error(e.getMessage());
|
276
|
facade = DerivedUnitFacade.NewInstance(type);
|
277
|
}
|
278
|
}
|
279
|
|
280
|
return facade;
|
281
|
}
|
282
|
|
283
|
@Override
|
284
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
|
285
|
String nameSpace;
|
286
|
Set<String> idSet;
|
287
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
288
|
|
289
|
try{
|
290
|
Set<String> taxonIdSet = new HashSet<>();
|
291
|
Set<String> extensionFkSet = new HashSet<>();
|
292
|
Set<String> referenceIdSet = new HashSet<>();
|
293
|
|
294
|
while (rs.next()){
|
295
|
handleForeignKey(rs, taxonIdSet, "taxonId");
|
296
|
handleForeignKey(rs, extensionFkSet, "extensionFk");
|
297
|
handleForeignKey(rs, referenceIdSet, "IdentifidationRefFk");
|
298
|
}
|
299
|
|
300
|
//taxon map
|
301
|
nameSpace = BerlinModelTaxonImport.NAMESPACE;
|
302
|
idSet = taxonIdSet;
|
303
|
@SuppressWarnings("rawtypes")
|
304
|
Map<String, TaxonBase> objectMap = getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, nameSpace);
|
305
|
result.put(nameSpace, objectMap);
|
306
|
|
307
|
//derived unit map
|
308
|
nameSpace = AlgaTerraSpecimenImportBase.ECO_FACT_DERIVED_UNIT_NAMESPACE;
|
309
|
idSet = extensionFkSet;
|
310
|
Map<String, DerivedUnit> derivedUnitMap = getCommonService().getSourcedObjectsByIdInSourceC(DerivedUnit.class, idSet, nameSpace);
|
311
|
result.put(nameSpace, derivedUnitMap);
|
312
|
|
313
|
//reference map
|
314
|
nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
|
315
|
idSet = referenceIdSet;
|
316
|
Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
|
317
|
result.put(nameSpace, referenceMap);
|
318
|
|
319
|
|
320
|
} catch (SQLException e) {
|
321
|
throw new RuntimeException(e);
|
322
|
}
|
323
|
return result;
|
324
|
}
|
325
|
|
326
|
@Override
|
327
|
protected boolean doCheck(BerlinModelImportState state){
|
328
|
IOValidator<BerlinModelImportState> validator = new AlgaTerraSpecimenImportValidator();
|
329
|
return validator.validate(state);
|
330
|
}
|
331
|
|
332
|
@Override
|
333
|
protected boolean isIgnore(BerlinModelImportState state){
|
334
|
return ! ((AlgaTerraImportState)state).getAlgaTerraConfigurator().isDoFactEcology();
|
335
|
}
|
336
|
|
337
|
}
|