1
|
/**
|
2
|
* Copyright (C) 2009 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
|
package eu.etaxonomy.cdm.io.pesi.out.old;
|
10
|
|
11
|
import java.sql.Connection;
|
12
|
import java.sql.PreparedStatement;
|
13
|
import java.sql.ResultSet;
|
14
|
import java.sql.SQLException;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.List;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.springframework.stereotype.Component;
|
21
|
import org.springframework.transaction.TransactionStatus;
|
22
|
|
23
|
import eu.etaxonomy.cdm.io.common.IExportConfigurator.DO_REFERENCES;
|
24
|
import eu.etaxonomy.cdm.io.common.Source;
|
25
|
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
|
26
|
import eu.etaxonomy.cdm.io.pesi.out.PesiExportBase;
|
27
|
import eu.etaxonomy.cdm.io.pesi.out.PesiExportConfigurator;
|
28
|
import eu.etaxonomy.cdm.io.pesi.out.PesiExportMapping;
|
29
|
import eu.etaxonomy.cdm.io.pesi.out.PesiExportState;
|
30
|
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
|
31
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
32
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
33
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
34
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
35
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
36
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
37
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
38
|
|
39
|
/**
|
40
|
* The export class for additional information linked to {@link eu.etaxonomy.cdm.model.description.Distribution Distributions} and {@link eu.etaxonomy.cdm.model.common.DescriptionElementSource DescriptionElements}.<p>
|
41
|
* Inserts into DataWarehouse database table <code>OccurrenceSource</code>.
|
42
|
* @author e.-m.lee
|
43
|
* @date 15.03.2010
|
44
|
*
|
45
|
*/
|
46
|
@Component
|
47
|
@SuppressWarnings("unchecked")
|
48
|
public class PesiOccurrenceSourceExport extends PesiExportBase {
|
49
|
private static final Logger logger = Logger.getLogger(PesiOccurrenceSourceExport.class);
|
50
|
private static final Class<? extends CdmBase> standardMethodParameter = AnnotatableEntity.class;
|
51
|
|
52
|
private static int modCount = 1000;
|
53
|
private static final String dbTableName = "OccurrenceSource";
|
54
|
private static final String pluralString = "OccurrenceSources";
|
55
|
private static final String parentPluralString = "Taxa";
|
56
|
private static Taxon taxon = null;
|
57
|
|
58
|
//*************** CONSTRUCTOR ******************************/
|
59
|
|
60
|
public PesiOccurrenceSourceExport() {
|
61
|
super();
|
62
|
}
|
63
|
|
64
|
//************************************************************/
|
65
|
|
66
|
@Override
|
67
|
public Class<? extends CdmBase> getStandardMethodParameter() {
|
68
|
return standardMethodParameter;
|
69
|
}
|
70
|
|
71
|
@Override
|
72
|
protected boolean doCheck(PesiExportState state) {
|
73
|
boolean result = true;
|
74
|
return result;
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
protected void doInvoke(PesiExportState state) {
|
79
|
try {
|
80
|
logger.error("*** Started Making " + pluralString + " ...");
|
81
|
|
82
|
String occurrenceSql = "Insert into OccurrenceSource (OccurrenceFk, SourceFk, SourceNameCache, OldTaxonName) " +
|
83
|
"values (?, ?, ?, ?)";
|
84
|
Connection con = state.getConfig().getDestination().getConnection();
|
85
|
PreparedStatement stmt = con.prepareStatement(occurrenceSql);
|
86
|
|
87
|
// Get the limit for objects to save within a single transaction.
|
88
|
int limit = state.getConfig().getLimitSave();
|
89
|
|
90
|
// Stores whether this invoke was successful or not.
|
91
|
boolean success = true;
|
92
|
|
93
|
// PESI: Clear the database table OccurrenceSource.
|
94
|
doDelete(state);
|
95
|
|
96
|
// Get specific mappings: (CDM) ? -> (PESI) OccurrenceSource
|
97
|
PesiExportMapping mapping = getMapping();
|
98
|
|
99
|
// Initialize the db mapper
|
100
|
mapping.initialize(state);
|
101
|
|
102
|
// PESI: Create the OccurrenceSource
|
103
|
int count = 0;
|
104
|
int taxonCount = 0;
|
105
|
int pastCount = 0;
|
106
|
TransactionStatus txStatus = null;
|
107
|
List<TaxonBase> list = null;
|
108
|
|
109
|
// Start transaction
|
110
|
txStatus = startTransaction(true);
|
111
|
logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
|
112
|
while ((list = getTaxonService().list(null, limit, taxonCount, null, null)).size() > 0) {
|
113
|
|
114
|
logger.error("Fetched " + list.size() + " " + parentPluralString + ".");
|
115
|
taxonCount += list.size();
|
116
|
for (TaxonBase taxonBase : list) {
|
117
|
if (taxonBase.isInstanceOf(Taxon.class)) {
|
118
|
|
119
|
// Set the current Taxon
|
120
|
taxon = CdmBase.deproxy(taxonBase, Taxon.class);
|
121
|
|
122
|
// Determine the TaxonDescriptions
|
123
|
Set<TaxonDescription> taxonDescriptions = taxon.getDescriptions();
|
124
|
|
125
|
// Determine the DescriptionElements (Citations) for the current Taxon
|
126
|
for (TaxonDescription taxonDescription : taxonDescriptions) {
|
127
|
Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
|
128
|
for (DescriptionElementBase descriptionElement : descriptionElements) {
|
129
|
Set<DescriptionElementSource> elementSources = descriptionElement.getSources();
|
130
|
|
131
|
// Focus on descriptionElements with sources.
|
132
|
if (elementSources.size() > 0) {
|
133
|
for (DescriptionElementSource elementSource : elementSources) {
|
134
|
Reference reference = elementSource.getCitation();
|
135
|
|
136
|
// Citations can be empty (null): Is it wrong data or just a normal case?
|
137
|
if (reference != null) {
|
138
|
|
139
|
// Lookup sourceFk
|
140
|
Integer sourceFk = getSourceFk(reference, state);
|
141
|
|
142
|
if (sourceFk != null && ! state.alreadyProcessedSource(sourceFk)) {
|
143
|
doCount(count++, modCount, pluralString);
|
144
|
|
145
|
// Add to processed sourceFk's since sourceFk's can be scanned more than once.
|
146
|
state.addToProcessedSources(sourceFk);
|
147
|
|
148
|
// Query the database for all entries in table 'Occurrence' with the sourceFk just determined.
|
149
|
Set<Integer> occurrenceIds = getOccurrenceIds(sourceFk, state);
|
150
|
|
151
|
// Insert as many entries in database table 'OccurrenceSource' as occurrenceId's were determined.
|
152
|
insertColumns(stmt, reference, sourceFk, occurrenceIds);
|
153
|
}
|
154
|
|
155
|
}
|
156
|
}
|
157
|
}
|
158
|
|
159
|
}
|
160
|
}
|
161
|
}
|
162
|
}
|
163
|
|
164
|
state.clearAlreadyProcessedSources();
|
165
|
|
166
|
// Commit transaction
|
167
|
commitTransaction(txStatus);
|
168
|
logger.error("Committed transaction.");
|
169
|
logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
|
170
|
pastCount = count;
|
171
|
|
172
|
// Start transaction
|
173
|
txStatus = startTransaction(true);
|
174
|
logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
|
175
|
}
|
176
|
if (list.size() == 0) {
|
177
|
logger.error("No " + pluralString + " left to fetch.");
|
178
|
}
|
179
|
|
180
|
list = null;
|
181
|
// Commit transaction
|
182
|
commitTransaction(txStatus);
|
183
|
logger.error("Committed transaction.");
|
184
|
|
185
|
logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
|
186
|
if (!success){
|
187
|
state.setUnsuccessfull();
|
188
|
}
|
189
|
return;
|
190
|
} catch (SQLException e) {
|
191
|
e.printStackTrace();
|
192
|
logger.error(e.getMessage());
|
193
|
state.setUnsuccessfull();
|
194
|
return;
|
195
|
}
|
196
|
}
|
197
|
|
198
|
/**
|
199
|
* Inserts columns in the database table OccurrenceSource.
|
200
|
* @param stmt The prepared statement.
|
201
|
* @param reference {@link Reference Reference}.
|
202
|
* @param sourceFk The SourceFk.
|
203
|
* @param occurrenceIds A {@link java.util.Set Set} of OccurrenceId's.
|
204
|
*/
|
205
|
private void insertColumns(PreparedStatement stmt, Reference reference,
|
206
|
Integer sourceFk, Set<Integer> occurrenceIds) {
|
207
|
for (Integer occurrenceId : occurrenceIds) {
|
208
|
try {
|
209
|
stmt.setInt(1, occurrenceId);
|
210
|
stmt.setInt(2, sourceFk);
|
211
|
stmt.setString(3, getSourceNameCache(reference));
|
212
|
stmt.setString(4, null); // TODO: This is the name of the former taxon (accepted taxon as well as synonym) the source was associated to. How can we get a hand on it?
|
213
|
stmt.execute();
|
214
|
} catch (SQLException e) {
|
215
|
logger.error("SQLException during getOccurrenceId invoke.");
|
216
|
e.printStackTrace();
|
217
|
}
|
218
|
}
|
219
|
}
|
220
|
|
221
|
/**
|
222
|
* Returns a Set of OccurrenceId's associated to a given SourceFk.
|
223
|
* @param state The {@link PesiExportState PesiExportState}.
|
224
|
* @return Existing OccurrenceId's for a given SourceFk.
|
225
|
*/
|
226
|
private static Set<Integer> getOccurrenceIds(Integer sourceFk, PesiExportState state) {
|
227
|
String occurrenceSql = "Select OccurrenceId From Occurrence where SourceFk = ?";
|
228
|
Connection con = state.getConfig().getDestination().getConnection();
|
229
|
PreparedStatement stmt = null;
|
230
|
Set<Integer> occurrenceIds = new HashSet();
|
231
|
|
232
|
try {
|
233
|
stmt = con.prepareStatement(occurrenceSql);
|
234
|
stmt.setInt(1, sourceFk);
|
235
|
ResultSet resultSet = stmt.executeQuery();
|
236
|
while (resultSet.next()) {
|
237
|
occurrenceIds.add(resultSet.getInt(1));
|
238
|
}
|
239
|
} catch (SQLException e) {
|
240
|
logger.error("SQLException during getOccurrenceId invoke. (2)");
|
241
|
e.printStackTrace();
|
242
|
}
|
243
|
|
244
|
return occurrenceIds;
|
245
|
}
|
246
|
|
247
|
/**
|
248
|
* Deletes all entries of database tables related to <code>OccurrenceSource</code>.
|
249
|
* @param state The {@link PesiExportState PesiExportState}.
|
250
|
* @return Whether the delete operation was successful or not.
|
251
|
*/
|
252
|
protected boolean doDelete(PesiExportState state) {
|
253
|
PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
|
254
|
|
255
|
String sql;
|
256
|
Source destination = pesiConfig.getDestination();
|
257
|
|
258
|
// Clear OccurrenceSource
|
259
|
sql = "DELETE FROM " + dbTableName;
|
260
|
destination.setQuery(sql);
|
261
|
destination.update(sql);
|
262
|
return true;
|
263
|
}
|
264
|
|
265
|
@Override
|
266
|
protected boolean isIgnore(PesiExportState state) {
|
267
|
return ! ( state.getConfig().isDoOccurrenceSource() && state.getConfig().isDoOccurrence() && state.getConfig().getDoReferences().equals(DO_REFERENCES.ALL));
|
268
|
}
|
269
|
|
270
|
/**
|
271
|
* Returns the <code>OccurrenceFk</code> attribute.
|
272
|
* @param entity An {@link AnnotatableEntity AnnotatableEntity}.
|
273
|
* @param state The {@link PesiExportState PesiExportState}.
|
274
|
* @return The <code>OccurrenceFk</code> attribute.
|
275
|
* @see MethodMapper
|
276
|
*/
|
277
|
@SuppressWarnings("unused")
|
278
|
private static Integer getOccurrenceFk(AnnotatableEntity entity, PesiExportState state) {
|
279
|
Integer result = null;
|
280
|
return result;
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* Returns the <code>SourceFk</code> attribute.
|
285
|
* @param entity An {@link AnnotatableEntity AnnotatableEntity}.
|
286
|
* @param state The {@link PesiExportState PesiExportState}.
|
287
|
* @return The <code>SourceFk</code> attribute.
|
288
|
* @see MethodMapper
|
289
|
*/
|
290
|
private static Integer getSourceFk(AnnotatableEntity entity, PesiExportState state) {
|
291
|
Integer result = null;
|
292
|
if (state != null && entity != null && entity.isInstanceOf(Reference.class)) {
|
293
|
Reference reference = CdmBase.deproxy(entity, Reference.class);
|
294
|
result = state.getDbId(reference);
|
295
|
}
|
296
|
return result;
|
297
|
}
|
298
|
|
299
|
/**
|
300
|
* Returns the <code>SourceNameCache</code> attribute.
|
301
|
* @param entity An {@link AnnotatableEntity AnnotatableEntity}.
|
302
|
* @return The <code>SourceNameCache</code> attribute.
|
303
|
* @see MethodMapper
|
304
|
*/
|
305
|
private static String getSourceNameCache(AnnotatableEntity entity) {
|
306
|
String result = null;
|
307
|
if (entity != null && entity.isInstanceOf(Reference.class)) {
|
308
|
Reference reference = CdmBase.deproxy(entity, Reference.class);
|
309
|
result = reference.getTitle();
|
310
|
}
|
311
|
return result;
|
312
|
}
|
313
|
|
314
|
/**
|
315
|
* Returns the <code>OldTaxonName</code> attribute.
|
316
|
* @param entity An {@link AnnotatableEntity AnnotatableEntity}.
|
317
|
* @return The <code>OldTaxonName</code> attribute.
|
318
|
* @see MethodMapper
|
319
|
*/
|
320
|
@SuppressWarnings("unused")
|
321
|
private static String getOldTaxonName(AnnotatableEntity entity) {
|
322
|
// TODO: This is the name of the former taxon (accepted taxon as well as synonym) the source was associated to.
|
323
|
return null;
|
324
|
}
|
325
|
|
326
|
/**
|
327
|
* Returns the CDM to PESI specific export mappings.
|
328
|
* @return The {@link PesiExportMapping PesiExportMapping}.
|
329
|
*/
|
330
|
private PesiExportMapping getMapping() {
|
331
|
PesiExportMapping mapping = new PesiExportMapping(dbTableName);
|
332
|
|
333
|
// These mapping are not used.
|
334
|
mapping.addMapper(MethodMapper.NewInstance("OccurrenceFk", this.getClass(), "getOccurrenceFk", standardMethodParameter, PesiExportState.class));
|
335
|
mapping.addMapper(MethodMapper.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter, PesiExportState.class));
|
336
|
mapping.addMapper(MethodMapper.NewInstance("SourceNameCache", this));
|
337
|
mapping.addMapper(MethodMapper.NewInstance("OldTaxonName", this));
|
338
|
|
339
|
return mapping;
|
340
|
}
|
341
|
|
342
|
}
|