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.faunaEuropaea;
|
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.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.Set;
|
19
|
import java.util.UUID;
|
20
|
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
import org.springframework.transaction.TransactionStatus;
|
24
|
|
25
|
import eu.etaxonomy.cdm.io.common.Source;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
28
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
29
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
30
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
31
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
32
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
33
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
34
|
|
35
|
|
36
|
/**
|
37
|
* @author a.babadshanjan
|
38
|
* @created 12.05.2009
|
39
|
* @version 1.0
|
40
|
*/
|
41
|
@Component
|
42
|
public class FaunaEuropaeaDistributionImport extends FaunaEuropaeaImportBase {
|
43
|
private static final Logger logger = Logger.getLogger(FaunaEuropaeaDistributionImport.class);
|
44
|
|
45
|
|
46
|
@Override
|
47
|
protected boolean doCheck(FaunaEuropaeaImportState state) {
|
48
|
boolean result = true;
|
49
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
50
|
logger.warn("Checking for Distributions not yet fully implemented");
|
51
|
result &= checkReferenceStatus(fauEuConfig);
|
52
|
|
53
|
return result;
|
54
|
}
|
55
|
|
56
|
private boolean checkReferenceStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
|
57
|
boolean result = true;
|
58
|
// try {
|
59
|
// Source source = fauEuConfig.getSource();
|
60
|
// String sqlStr = "";
|
61
|
// ResultSet rs = source.getResultSet(sqlStr);
|
62
|
return result;
|
63
|
// } catch (SQLException e) {
|
64
|
// e.printStackTrace();
|
65
|
// return false;
|
66
|
// }
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
protected void doInvoke(FaunaEuropaeaImportState state) {
|
71
|
/*
|
72
|
logger.warn("Start distribution doInvoke");
|
73
|
ProfilerController.memorySnapshot();
|
74
|
*/
|
75
|
int limit = state.getConfig().getLimitSave();
|
76
|
UUID noDataUuid;
|
77
|
/* Taxon store for retrieving taxa from and saving taxa to CDM */
|
78
|
List<TaxonBase> taxonList = null;
|
79
|
/* UUID store as input for retrieving taxa from CDM */
|
80
|
Set<UUID> taxonUuids = null;
|
81
|
/* Store to hold helper objects */
|
82
|
Map<UUID, FaunaEuropaeaDistributionTaxon> fauEuTaxonMap = null;
|
83
|
|
84
|
|
85
|
TransactionStatus txStatus = null;
|
86
|
|
87
|
//txStatus = startTransaction();
|
88
|
PresenceAbsenceTerm noDataStatusTerm = PresenceAbsenceTerm.NewPresenceInstance("no data", "no data", "nod");
|
89
|
noDataUuid = noDataStatusTerm.getUuid();
|
90
|
TermVocabulary voc = getVocabularyService().find(30);
|
91
|
voc.addTerm(noDataStatusTerm);
|
92
|
getVocabularyService().saveOrUpdate(voc);
|
93
|
// getTermService().save(noDataStatusTerm);
|
94
|
//commitTransaction(txStatus);
|
95
|
|
96
|
FaunaEuropaeaTransformer.setUUIDs(noDataUuid);
|
97
|
|
98
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
99
|
Source source = fauEuConfig.getSource();
|
100
|
|
101
|
int i = 0;
|
102
|
|
103
|
String selectCount =
|
104
|
" SELECT count(*) ";
|
105
|
|
106
|
String selectColumns =
|
107
|
" SELECT distribution.*, Area.*, Taxon.UUID ";
|
108
|
|
109
|
String fromClause =
|
110
|
" FROM distribution INNER JOIN " +
|
111
|
" Area ON distribution.dis_ara_id = Area.ara_id INNER JOIN " +
|
112
|
" Taxon ON distribution.dis_tax_id = Taxon.TAX_ID ";
|
113
|
String orderBy = " ORDER BY distribution.dis_tax_id";
|
114
|
|
115
|
String countQuery =
|
116
|
selectCount + fromClause;
|
117
|
|
118
|
String selectQuery =
|
119
|
selectColumns + fromClause + orderBy;
|
120
|
|
121
|
|
122
|
|
123
|
if(logger.isInfoEnabled()) { logger.info("Start making distributions..."); }
|
124
|
|
125
|
try {
|
126
|
ResultSet rs = source.getResultSet(countQuery);
|
127
|
rs.next();
|
128
|
int count = rs.getInt(1);
|
129
|
|
130
|
rs = source.getResultSet(selectQuery);
|
131
|
|
132
|
if (logger.isInfoEnabled()) {
|
133
|
logger.info("Number of rows: " + count);
|
134
|
logger.info("Count Query: " + countQuery);
|
135
|
logger.info("Select Query: " + selectQuery);
|
136
|
}
|
137
|
|
138
|
//int taxonId;
|
139
|
|
140
|
while (rs.next()) {
|
141
|
if ((i++ % limit) == 0) {
|
142
|
|
143
|
txStatus = startTransaction();
|
144
|
taxonUuids = new HashSet<UUID>(limit);
|
145
|
fauEuTaxonMap = new HashMap<UUID, FaunaEuropaeaDistributionTaxon>(limit);
|
146
|
|
147
|
if(logger.isInfoEnabled()) {
|
148
|
logger.info("i = " + i + " - Distribution import transaction started");
|
149
|
}
|
150
|
}
|
151
|
|
152
|
//taxonId = rs.getInt("dis_tax_id");
|
153
|
int disId = rs.getInt("dis_id");
|
154
|
int occStatusId = rs.getInt("dis_present");
|
155
|
int areaId = rs.getInt("ara_id");
|
156
|
String areaName = rs.getString("ara_name");
|
157
|
String areaCode = rs.getString("ara_code");
|
158
|
int extraLimital = rs.getInt("ara_extralimital");
|
159
|
UUID currentTaxonUuid = null;
|
160
|
if (resultSetHasColumn(rs,"UUID")){
|
161
|
currentTaxonUuid = UUID.fromString(rs.getString("UUID"));
|
162
|
} else {
|
163
|
currentTaxonUuid = UUID.randomUUID();
|
164
|
}
|
165
|
|
166
|
FaunaEuropaeaDistribution fauEuDistribution = new FaunaEuropaeaDistribution();
|
167
|
fauEuDistribution.setDistributionId(disId);
|
168
|
fauEuDistribution.setOccurrenceStatusId(occStatusId);
|
169
|
fauEuDistribution.setAreaId(areaId);
|
170
|
fauEuDistribution.setAreaName(areaName);
|
171
|
fauEuDistribution.setAreaCode(areaCode);
|
172
|
fauEuDistribution.setExtraLimital(extraLimital);
|
173
|
|
174
|
if (!taxonUuids.contains(currentTaxonUuid)) {
|
175
|
taxonUuids.add(currentTaxonUuid);
|
176
|
FaunaEuropaeaDistributionTaxon fauEuDistributionTaxon =
|
177
|
new FaunaEuropaeaDistributionTaxon(currentTaxonUuid);
|
178
|
fauEuTaxonMap.put(currentTaxonUuid, fauEuDistributionTaxon);
|
179
|
fauEuDistributionTaxon = null;
|
180
|
} else {
|
181
|
if (logger.isTraceEnabled()) {
|
182
|
logger.trace("Taxon (" + currentTaxonUuid + ") already stored.");
|
183
|
continue;
|
184
|
}
|
185
|
}
|
186
|
|
187
|
fauEuTaxonMap.get(currentTaxonUuid).addDistribution(fauEuDistribution);
|
188
|
|
189
|
if (((i % limit) == 0 && i != 1 ) || i == count ) {
|
190
|
|
191
|
try {
|
192
|
commitTaxaAndDistribution(state, noDataUuid, taxonUuids, fauEuTaxonMap, txStatus);
|
193
|
taxonUuids = null;
|
194
|
taxonList = null;
|
195
|
fauEuTaxonMap = null;
|
196
|
|
197
|
} catch (Exception e) {
|
198
|
logger.error("Commit of taxa and distributions failed" + e.getMessage());
|
199
|
e.printStackTrace();
|
200
|
}
|
201
|
|
202
|
if(logger.isInfoEnabled()) { logger.info("i = " + i + " - Transaction committed");}
|
203
|
}
|
204
|
|
205
|
|
206
|
}
|
207
|
if (taxonUuids != null){
|
208
|
try {
|
209
|
commitTaxaAndDistribution(state, noDataUuid, taxonUuids, fauEuTaxonMap, txStatus);
|
210
|
taxonUuids = null;
|
211
|
taxonList = null;
|
212
|
fauEuTaxonMap = null;
|
213
|
} catch (Exception e) {
|
214
|
logger.error("Commit of taxa and distributions failed");
|
215
|
logger.error(e.getMessage());
|
216
|
e.printStackTrace();
|
217
|
}
|
218
|
}
|
219
|
rs = null;
|
220
|
} catch (SQLException e) {
|
221
|
logger.error("SQLException:" + e);
|
222
|
state.setUnsuccessfull();
|
223
|
}
|
224
|
|
225
|
if(logger.isInfoEnabled()) { logger.info("End making distributions..."); }
|
226
|
|
227
|
return;
|
228
|
}
|
229
|
|
230
|
private void commitTaxaAndDistribution(
|
231
|
FaunaEuropaeaImportState state, UUID noDataUuid,
|
232
|
Set<UUID> taxonUuids,
|
233
|
Map<UUID, FaunaEuropaeaDistributionTaxon> fauEuTaxonMap,
|
234
|
TransactionStatus txStatus) throws Exception {
|
235
|
List<TaxonBase> taxonList = prepareTaxaAndDistribution(getTaxonService().find(taxonUuids), fauEuTaxonMap, noDataUuid, state);
|
236
|
|
237
|
getTaxonService().save(taxonList);
|
238
|
taxonList = null;
|
239
|
taxonUuids = null;
|
240
|
fauEuTaxonMap = null;
|
241
|
commitTransaction(txStatus);
|
242
|
|
243
|
}
|
244
|
|
245
|
private List<TaxonBase> prepareTaxaAndDistribution(List<TaxonBase> taxonList, Map<UUID, FaunaEuropaeaDistributionTaxon> fauEuTaxonMap, UUID noData, FaunaEuropaeaImportState state) throws Exception{
|
246
|
|
247
|
Distribution newDistribution = null;
|
248
|
NamedArea namedArea;
|
249
|
PresenceAbsenceTerm presenceAbsenceStatus;
|
250
|
FaunaEuropaeaDistributionTaxon fauEuHelperTaxon;
|
251
|
UUID taxonUuid;
|
252
|
TaxonDescription taxonDescription;
|
253
|
Taxon taxon;
|
254
|
for (TaxonBase<?> taxonBase : taxonList) {
|
255
|
|
256
|
if (taxonBase != null) {
|
257
|
|
258
|
if (taxonBase instanceof Taxon) {
|
259
|
taxon = CdmBase.deproxy(taxonBase, Taxon.class);
|
260
|
} else {
|
261
|
logger.warn("TaxonBase (" + taxonBase.getId() + " is not of type Taxon but: "
|
262
|
+ taxonBase.getClass().getSimpleName());
|
263
|
continue;
|
264
|
}
|
265
|
|
266
|
|
267
|
Set<TaxonDescription> descriptionSet = taxon.getDescriptions();
|
268
|
if (descriptionSet.size() > 0) {
|
269
|
taxonDescription = descriptionSet.iterator().next();
|
270
|
} else {
|
271
|
taxonDescription = TaxonDescription.NewInstance();
|
272
|
taxon.addDescription(taxonDescription);
|
273
|
}
|
274
|
|
275
|
taxonUuid = taxonBase.getUuid();
|
276
|
fauEuHelperTaxon= fauEuTaxonMap.get(taxonUuid);
|
277
|
|
278
|
for (FaunaEuropaeaDistribution fauEuHelperDistribution : fauEuHelperTaxon.getDistributions()) {
|
279
|
namedArea = null;
|
280
|
newDistribution = null;
|
281
|
presenceAbsenceStatus = null;
|
282
|
|
283
|
if (fauEuHelperDistribution.getOccurrenceStatusId() != 0 && fauEuHelperDistribution.getOccurrenceStatusId() != 2 && fauEuHelperDistribution.getOccurrenceStatusId() != 1){
|
284
|
presenceAbsenceStatus = (PresenceAbsenceTerm)getTermService().find(noData);
|
285
|
}else{
|
286
|
presenceAbsenceStatus = FaunaEuropaeaTransformer.occStatus2PresenceAbsence(fauEuHelperDistribution.getOccurrenceStatusId());
|
287
|
}
|
288
|
|
289
|
namedArea = FaunaEuropaeaTransformer.areaId2TdwgArea(fauEuHelperDistribution);
|
290
|
|
291
|
if (namedArea == null){
|
292
|
UUID areaUuid= FaunaEuropaeaTransformer.getUUIDByAreaAbbr(fauEuHelperDistribution.getAreaCode());
|
293
|
if (areaUuid == null){
|
294
|
logger.warn("Area " + fauEuHelperDistribution.getAreaCode() + "not found in FE transformer");
|
295
|
}
|
296
|
namedArea = getNamedArea(state, areaUuid, fauEuHelperDistribution.getAreaName(), fauEuHelperDistribution.getAreaName(), fauEuHelperDistribution.getAreaCode(), null, null);
|
297
|
|
298
|
}
|
299
|
|
300
|
newDistribution = Distribution.NewInstance(namedArea, presenceAbsenceStatus);
|
301
|
newDistribution.setCreated(null);
|
302
|
|
303
|
taxonDescription.addElement(newDistribution);
|
304
|
}
|
305
|
}
|
306
|
}
|
307
|
return taxonList;
|
308
|
}
|
309
|
|
310
|
@Override
|
311
|
protected boolean isIgnore(FaunaEuropaeaImportState state){
|
312
|
return !state.getConfig().isDoOccurrence();
|
313
|
}
|
314
|
}
|