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