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.SQLException;
|
12
|
import java.util.List;
|
13
|
import java.util.Set;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.springframework.stereotype.Component;
|
17
|
import org.springframework.transaction.TransactionStatus;
|
18
|
|
19
|
import eu.etaxonomy.cdm.io.common.Source;
|
20
|
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
|
21
|
import eu.etaxonomy.cdm.io.pesi.out.PesiExportBase;
|
22
|
import eu.etaxonomy.cdm.io.pesi.out.PesiExportConfigurator;
|
23
|
import eu.etaxonomy.cdm.io.pesi.out.PesiExportMapping;
|
24
|
import eu.etaxonomy.cdm.io.pesi.out.PesiExportState;
|
25
|
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
28
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
29
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
30
|
import eu.etaxonomy.cdm.model.description.TextData;
|
31
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
32
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
33
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
34
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
35
|
|
36
|
/**
|
37
|
* The export class for additional information linked to {@link eu.etaxonomy.cdm.model.taxon.Taxon Taxa} and {@link eu.etaxonomy.cdm.model.reference.Reference References}.<p>
|
38
|
* Inserts into DataWarehouse database table <code>AdditionalTaxonSource</code>.
|
39
|
* @author e.-m.lee
|
40
|
* @date 23.02.2010
|
41
|
*
|
42
|
*/
|
43
|
@Component
|
44
|
@SuppressWarnings("unchecked")
|
45
|
public class PesiAdditionalTaxonSourceExport extends PesiExportBase {
|
46
|
private static final Logger logger = Logger.getLogger(PesiAdditionalTaxonSourceExport.class);
|
47
|
private static final Class<? extends CdmBase> standardMethodParameter = Reference.class;
|
48
|
|
49
|
private static int modCount = 1000;
|
50
|
private static final String dbTableName = "AdditionalTaxonSource";
|
51
|
private static final String pluralString = "DescriptionElements";
|
52
|
private static final String parentPluralString = "Taxa";
|
53
|
private static boolean sourceUse_AdditionalSource = false;
|
54
|
private static boolean sourceUse_NomenclaturalReference = false;
|
55
|
private static boolean sourceUse_SourceOfSynonymy = false;
|
56
|
private static TaxonNameBase currentTaxonName = null;
|
57
|
private static String citationMicroReference = null;
|
58
|
|
59
|
public PesiAdditionalTaxonSourceExport() {
|
60
|
super();
|
61
|
}
|
62
|
|
63
|
/* (non-Javadoc)
|
64
|
* @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
|
65
|
*/
|
66
|
@Override
|
67
|
public Class<? extends CdmBase> getStandardMethodParameter() {
|
68
|
return standardMethodParameter;
|
69
|
}
|
70
|
|
71
|
/* (non-Javadoc)
|
72
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
|
73
|
*/
|
74
|
@Override
|
75
|
protected boolean doCheck(PesiExportState state) {
|
76
|
boolean result = true;
|
77
|
return result;
|
78
|
}
|
79
|
|
80
|
/* (non-Javadoc)
|
81
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
|
82
|
*/
|
83
|
@Override
|
84
|
protected void doInvoke(PesiExportState state) {
|
85
|
try {
|
86
|
logger.error("*** Started Making " + pluralString + " ...");
|
87
|
|
88
|
// Get the limit for objects to save within a single transaction.
|
89
|
// int limit = state.getConfig().getLimitSave();
|
90
|
int limit = 1000;
|
91
|
|
92
|
// Stores whether this invoke was successful or not.
|
93
|
boolean success = true;
|
94
|
|
95
|
// PESI: Clear the database table Note.
|
96
|
doDelete(state);
|
97
|
|
98
|
// CDM: Get the number of all available description elements.
|
99
|
// int maxCount = getDescriptionService().count(null);
|
100
|
// logger.error("Total amount of " + maxCount + " " + pluralString + " will be exported.");
|
101
|
|
102
|
// Get specific mappings: (CDM) DescriptionElement -> (PESI) Note
|
103
|
PesiExportMapping mapping = getMapping();
|
104
|
|
105
|
// Initialize the db mapper
|
106
|
mapping.initialize(state);
|
107
|
|
108
|
// PESI: Create the Notes
|
109
|
int count = 0;
|
110
|
int taxonCount = 0;
|
111
|
int pastCount = 0;
|
112
|
TransactionStatus txStatus = null;
|
113
|
List<TaxonBase> list = null;
|
114
|
|
115
|
// Start transaction
|
116
|
txStatus = startTransaction(true);
|
117
|
logger.info("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
|
118
|
while ((list = getTaxonService().list(null, limit, taxonCount, null, null)).size() > 0) {
|
119
|
|
120
|
taxonCount += list.size();
|
121
|
logger.info("Fetched " + list.size() + " " + parentPluralString + ".");
|
122
|
|
123
|
logger.info("PHASE 2: Check for SourceUse 'Additional Source'");
|
124
|
sourceUse_AdditionalSource = true;
|
125
|
for (TaxonBase taxonBase : list) {
|
126
|
|
127
|
// Set the current Taxon
|
128
|
currentTaxonName = taxonBase.getName();
|
129
|
|
130
|
if (taxonBase.isInstanceOf(Taxon.class)) {
|
131
|
|
132
|
Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
|
133
|
|
134
|
// Determine the TaxonDescriptions
|
135
|
Set<TaxonDescription> taxonDescriptions = taxon.getDescriptions();
|
136
|
|
137
|
// Determine the DescriptionElements (Citations) for the current Taxon
|
138
|
for (TaxonDescription taxonDescription : taxonDescriptions) {
|
139
|
Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
|
140
|
|
141
|
for (DescriptionElementBase descriptionElement : descriptionElements) {
|
142
|
// According to FaEu Import those DescriptionElementBase elements are of instance TextData
|
143
|
// There are no other indicators
|
144
|
if (descriptionElement.isInstanceOf(TextData.class)) {
|
145
|
Set<DescriptionElementSource> elementSources = descriptionElement.getSources();
|
146
|
|
147
|
for (DescriptionElementSource elementSource : elementSources) {
|
148
|
|
149
|
// Set the CitationMicroReference so it is accessible later in getSourceDetail()
|
150
|
setCitationMicroReference(elementSource.getCitationMicroReference());
|
151
|
|
152
|
// Get the citation
|
153
|
Reference reference = elementSource.getCitation();
|
154
|
|
155
|
// Check whether it was a synonym originally
|
156
|
TaxonNameBase<?,?> nameUsedInSource = elementSource.getNameUsedInSource();
|
157
|
if (nameUsedInSource != null) {
|
158
|
// It was a synonym originally: Set currentTaxonName to synonym's taxonName
|
159
|
currentTaxonName = nameUsedInSource;
|
160
|
}
|
161
|
|
162
|
// Citations can be empty (null): Is it wrong data or just a normal case?
|
163
|
if (reference != null) {
|
164
|
if (neededValuesNotNull(reference, state)) {
|
165
|
doCount(count++, modCount, pluralString);
|
166
|
success &= mapping.invoke(reference);
|
167
|
}
|
168
|
}
|
169
|
}
|
170
|
}
|
171
|
}
|
172
|
}
|
173
|
}
|
174
|
|
175
|
}
|
176
|
sourceUse_AdditionalSource = false;
|
177
|
logger.error("Exported " + (count - pastCount) + " " + pluralString + ".");
|
178
|
|
179
|
// Commit transaction
|
180
|
commitTransaction(txStatus);
|
181
|
logger.error("Committed transaction.");
|
182
|
logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
|
183
|
pastCount = count;
|
184
|
|
185
|
// Start transaction
|
186
|
txStatus = startTransaction(true);
|
187
|
logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
|
188
|
}
|
189
|
if (list.size() == 0) {
|
190
|
logger.error("No " + pluralString + " left to fetch.");
|
191
|
}
|
192
|
|
193
|
list = null;
|
194
|
// Commit transaction
|
195
|
commitTransaction(txStatus);
|
196
|
logger.error("Committed transaction.");
|
197
|
|
198
|
logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
|
199
|
|
200
|
return;
|
201
|
} catch (SQLException e) {
|
202
|
e.printStackTrace();
|
203
|
logger.error(e.getMessage());
|
204
|
state.setUnsuccessfull();
|
205
|
return;
|
206
|
}
|
207
|
}
|
208
|
|
209
|
/**
|
210
|
* Checks whether needed values for an entity are NULL.
|
211
|
* @return
|
212
|
*/
|
213
|
private boolean neededValuesNotNull(Reference reference, PesiExportState state) {
|
214
|
boolean result = true;
|
215
|
if (getSourceFk(reference, state) == null) {
|
216
|
logger.error("SourceFk is NULL, but is not allowed to be. Therefore no record was written to export database for this reference: " + reference.getUuid());
|
217
|
result = false;
|
218
|
}
|
219
|
if (getSourceUseFk(reference) == null) {
|
220
|
logger.error("SourceUseFk is NULL, but is not allowed to be. Therefore no record was written to export database for this reference: " + reference.getUuid());
|
221
|
result = false;
|
222
|
}
|
223
|
return result;
|
224
|
}
|
225
|
|
226
|
/**
|
227
|
* Deletes all entries of database tables related to <code>AdditionalTaxonSource</code>.
|
228
|
* @param state The {@link PesiExportState PesiExportState}.
|
229
|
* @return Whether the delete operation was successful or not.
|
230
|
*/
|
231
|
protected boolean doDelete(PesiExportState state) {
|
232
|
PesiExportConfigurator pesiConfig = state.getConfig();
|
233
|
|
234
|
String sql;
|
235
|
Source destination = pesiConfig.getDestination();
|
236
|
|
237
|
// Clear AdditionalTaxonSource
|
238
|
sql = "DELETE FROM " + dbTableName;
|
239
|
destination.setQuery(sql);
|
240
|
destination.update(sql);
|
241
|
return true;
|
242
|
}
|
243
|
|
244
|
/* (non-Javadoc)
|
245
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
|
246
|
*/
|
247
|
@Override
|
248
|
protected boolean isIgnore(PesiExportState state) {
|
249
|
return ! ( state.getConfig().isDoAdditionalTaxonSource());
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* Returns the <code>TaxonFk</code> attribute.
|
254
|
* @param reference The {@link Reference Reference}.
|
255
|
* @see MethodMapper
|
256
|
*/
|
257
|
@SuppressWarnings("unused")
|
258
|
private static Integer getTaxonFk(Reference reference, PesiExportState state) {
|
259
|
// Reference parameter isn't needed, but the DbSingleAttributeExportMapperBase throws a type mismatch exception otherwise
|
260
|
// since it awaits two parameters if one of them is of instance DbExportStateBase.
|
261
|
Integer result = null;
|
262
|
if (state != null && currentTaxonName != null) {
|
263
|
result = state.getDbId(currentTaxonName);
|
264
|
}
|
265
|
return result;
|
266
|
}
|
267
|
|
268
|
/**
|
269
|
* Returns the <code>SourceFk</code> attribute.
|
270
|
* @param reference The {@link Reference Reference}.
|
271
|
* @return The <code>SourceFk</code> attribute.
|
272
|
* @see MethodMapper
|
273
|
*/
|
274
|
private static Integer getSourceFk(Reference reference, PesiExportState state) {
|
275
|
Integer result = null;
|
276
|
if (state != null && reference != null) {
|
277
|
result = state.getDbId(reference);
|
278
|
}
|
279
|
return result;
|
280
|
}
|
281
|
|
282
|
/**
|
283
|
* Returns the <code>SourceUseFk</code> attribute.
|
284
|
* @param reference The {@link Reference Reference}.
|
285
|
* @return The <code>SourceUseFk</code> attribute.
|
286
|
* @see MethodMapper
|
287
|
*/
|
288
|
private static Integer getSourceUseFk(Reference reference) {
|
289
|
// TODO
|
290
|
Integer result = null;
|
291
|
if (sourceUse_AdditionalSource) {
|
292
|
result = PesiTransformer.sourceUseIdSourceUseId(3);
|
293
|
} else if (sourceUse_SourceOfSynonymy) {
|
294
|
result = PesiTransformer.sourceUseIdSourceUseId(4);
|
295
|
} else if (sourceUse_NomenclaturalReference) {
|
296
|
result = PesiTransformer.sourceUseIdSourceUseId(8);
|
297
|
}
|
298
|
return result;
|
299
|
}
|
300
|
|
301
|
/**
|
302
|
* Returns the <code>SourceUseCache</code> attribute.
|
303
|
* @param reference The {@link Reference Reference}.
|
304
|
* @return The <code>SourceUseCache</code> attribute.
|
305
|
* @see MethodMapper
|
306
|
*/
|
307
|
@SuppressWarnings("unused")
|
308
|
private static String getSourceUseCache(Reference reference) {
|
309
|
// TODO
|
310
|
String result = null;
|
311
|
//CHANGED, use PesiTransformer.getAdditionalSourceCacheByKey instead
|
312
|
// if (sourceUse_AdditionalSource) {
|
313
|
// result = PesiTransformer.sourceUseId2SourceUseCache(3);
|
314
|
// } else if (sourceUse_SourceOfSynonymy) {
|
315
|
// result = PesiTransformer.sourceUseId2SourceUseCache(4);
|
316
|
// } else if (sourceUse_NomenclaturalReference) {
|
317
|
// result = PesiTransformer.sourceUseId2SourceUseCache(8);
|
318
|
// }
|
319
|
return result;
|
320
|
}
|
321
|
|
322
|
/**
|
323
|
* Returns the <code>SourceNameCache</code> attribute.
|
324
|
* @param reference The {@link Reference Reference}.
|
325
|
* @return The <code>SourceNameCache</code> attribute.
|
326
|
* @see MethodMapper
|
327
|
*/
|
328
|
@SuppressWarnings("unused")
|
329
|
private static String getSourceNameCache(Reference reference) {
|
330
|
String result = null;
|
331
|
if (reference != null) {
|
332
|
result = reference.getTitle();
|
333
|
}
|
334
|
return result;
|
335
|
}
|
336
|
|
337
|
/**
|
338
|
* Returns the <code>SourceDetail</code> attribute.
|
339
|
* @param reference The {@link Reference Reference}.
|
340
|
* @return The <code>SourceDetail</code> attribute.
|
341
|
* @see MethodMapper
|
342
|
*/
|
343
|
@SuppressWarnings("unused")
|
344
|
private static String getSourceDetail(Reference reference) {
|
345
|
return PesiAdditionalTaxonSourceExport.citationMicroReference;
|
346
|
}
|
347
|
|
348
|
/**
|
349
|
* @param citationMicroReference the citationMicroReference to set
|
350
|
*/
|
351
|
public static void setCitationMicroReference(String citationMicroReference) {
|
352
|
PesiAdditionalTaxonSourceExport.citationMicroReference = citationMicroReference;
|
353
|
}
|
354
|
|
355
|
/**
|
356
|
* Returns the CDM to PESI specific export mappings.
|
357
|
* @return The {@link PesiExportMapping PesiExportMapping}.
|
358
|
*/
|
359
|
private PesiExportMapping getMapping() {
|
360
|
PesiExportMapping mapping = new PesiExportMapping(dbTableName);
|
361
|
|
362
|
mapping.addMapper(MethodMapper.NewInstance("TaxonFk", this.getClass(), "getTaxonFk", standardMethodParameter, PesiExportState.class));
|
363
|
mapping.addMapper(MethodMapper.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter, PesiExportState.class));
|
364
|
mapping.addMapper(MethodMapper.NewInstance("SourceUseFk", this));
|
365
|
mapping.addMapper(MethodMapper.NewInstance("SourceUseCache", this));
|
366
|
mapping.addMapper(MethodMapper.NewInstance("SourceNameCache", this));
|
367
|
mapping.addMapper(MethodMapper.NewInstance("SourceDetail", this));
|
368
|
|
369
|
return mapping;
|
370
|
}
|
371
|
|
372
|
}
|