1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.io.pesi.out;
|
11
|
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.springframework.stereotype.Component;
|
19
|
import org.springframework.transaction.TransactionStatus;
|
20
|
|
21
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbExtensionMapper;
|
22
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbStringMapper;
|
23
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbTimePeriodMapper;
|
24
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.IdMapper;
|
25
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.MethodMapper;
|
26
|
import eu.etaxonomy.cdm.io.common.Source;
|
27
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator.DO_REFERENCES;
|
28
|
import eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer;
|
29
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
30
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
31
|
import eu.etaxonomy.cdm.model.common.ExtensionType;
|
32
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
33
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
34
|
|
35
|
/**
|
36
|
* The export class for {@link eu.etaxonomy.cdm.model.reference.Reference References}.<p>
|
37
|
* Inserts into DataWarehouse database table <code>Source</code>.
|
38
|
* @author e.-m.lee
|
39
|
* @date 11.02.2010
|
40
|
*
|
41
|
*/
|
42
|
@Component
|
43
|
@SuppressWarnings("unchecked")
|
44
|
public class PesiSourceExport extends PesiExportBase {
|
45
|
private static final Logger logger = Logger.getLogger(PesiSourceExport.class);
|
46
|
private static final Class<? extends CdmBase> standardMethodParameter = Reference.class;
|
47
|
|
48
|
private static int modCount = 1000;
|
49
|
private static final String dbTableName = "Source";
|
50
|
private static final String pluralString = "Sources";
|
51
|
List<Integer> storedSourceIds = new ArrayList<Integer>();
|
52
|
|
53
|
public PesiSourceExport() {
|
54
|
super();
|
55
|
}
|
56
|
|
57
|
/* (non-Javadoc)
|
58
|
* @see eu.etaxonomy.cdm.io.pesi.out.PesiExportBase#getStandardMethodParameter()
|
59
|
*/
|
60
|
@Override
|
61
|
public Class<? extends CdmBase> getStandardMethodParameter() {
|
62
|
return standardMethodParameter;
|
63
|
}
|
64
|
|
65
|
/* (non-Javadoc)
|
66
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
|
67
|
*/
|
68
|
@Override
|
69
|
protected boolean doCheck(PesiExportState state) {
|
70
|
boolean result = true;
|
71
|
return result;
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* Checks whether a sourceId was stored already.
|
76
|
* @param sourceId
|
77
|
* @return
|
78
|
*/
|
79
|
protected boolean isStoredSourceId(Integer sourceId) {
|
80
|
if (storedSourceIds.contains(sourceId)) {
|
81
|
return true;
|
82
|
} else {
|
83
|
return false;
|
84
|
}
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Adds a sourceId to the list of storedSourceIds.
|
89
|
* @param sourceId
|
90
|
*/
|
91
|
protected void addToStoredSourceIds(Integer sourceId) {
|
92
|
if (! storedSourceIds.contains(sourceId)) {
|
93
|
this.storedSourceIds.add(sourceId);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
/* (non-Javadoc)
|
98
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
|
99
|
*/
|
100
|
@Override
|
101
|
protected void doInvoke(PesiExportState state) {
|
102
|
try{
|
103
|
logger.error("*** Started Making " + pluralString + " ...");
|
104
|
|
105
|
PesiExportConfigurator pesiExportConfigurator = state.getConfig();
|
106
|
|
107
|
// Get the limit for objects to save within a single transaction.
|
108
|
int limit = pesiExportConfigurator.getLimitSave();
|
109
|
|
110
|
// Stores whether this invoke was successful or not.
|
111
|
boolean success = true ;
|
112
|
|
113
|
// PESI: Clear the database table Source.
|
114
|
doDelete(state);
|
115
|
|
116
|
// Get specific mappings: (CDM) Reference -> (PESI) Source
|
117
|
PesiExportMapping mapping = getMapping();
|
118
|
|
119
|
// Initialize the db mapper
|
120
|
mapping.initialize(state);
|
121
|
|
122
|
// Create the Sources
|
123
|
int count = 0;
|
124
|
int pastCount = 0;
|
125
|
TransactionStatus txStatus = null;
|
126
|
List<Reference> list = null;
|
127
|
|
128
|
// logger.error("PHASE 1...");
|
129
|
// Start transaction
|
130
|
txStatus = startTransaction(true);
|
131
|
logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
|
132
|
while ((list = getReferenceService().list(null, limit, count, null, null)).size() > 0) {
|
133
|
|
134
|
logger.error("Fetched " + list.size() + " " + pluralString + ". Exporting...");
|
135
|
for (Reference<?> reference : list) {
|
136
|
doCount(count++, modCount, pluralString);
|
137
|
success &= mapping.invoke(reference);
|
138
|
}
|
139
|
|
140
|
// Commit transaction
|
141
|
commitTransaction(txStatus);
|
142
|
logger.error("Committed transaction.");
|
143
|
logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
|
144
|
pastCount = count;
|
145
|
|
146
|
// Start transaction
|
147
|
txStatus = startTransaction(true);
|
148
|
logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
|
149
|
}
|
150
|
if (list.size() == 0) {
|
151
|
logger.error("No " + pluralString + " left to fetch.");
|
152
|
}
|
153
|
// Commit transaction
|
154
|
commitTransaction(txStatus);
|
155
|
logger.error("Committed transaction.");
|
156
|
|
157
|
logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
|
158
|
|
159
|
if (!success){
|
160
|
state.setUnsuccessfull();
|
161
|
}
|
162
|
return;
|
163
|
} catch (SQLException e) {
|
164
|
e.printStackTrace();
|
165
|
logger.error(e.getMessage());
|
166
|
state.setUnsuccessfull();
|
167
|
return;
|
168
|
}
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* Deletes all entries of database tables related to <code>Source</code>.
|
173
|
* @param state The {@link PesiExportState PesiExportState}.
|
174
|
* @return Whether the delete operation was successful or not.
|
175
|
*/
|
176
|
protected boolean doDelete(PesiExportState state) {
|
177
|
PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
|
178
|
|
179
|
String sql;
|
180
|
Source destination = pesiConfig.getDestination();
|
181
|
|
182
|
// Clear Occurrences
|
183
|
sql = "DELETE FROM Occurrence";
|
184
|
destination.setQuery(sql);
|
185
|
destination.update(sql);
|
186
|
|
187
|
// Clear Taxa
|
188
|
sql = "DELETE FROM Taxon";
|
189
|
destination.setQuery(sql);
|
190
|
destination.update(sql);
|
191
|
|
192
|
// Clear Sources
|
193
|
sql = "DELETE FROM " + dbTableName;
|
194
|
destination.setQuery(sql);
|
195
|
destination.update(sql);
|
196
|
|
197
|
return true;
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Returns the <code>IMIS_Id</code> attribute.
|
202
|
* @param reference The {@link Reference Reference}.
|
203
|
* @return The <code>IMIS_Id</code> attribute.
|
204
|
* @see MethodMapper
|
205
|
*/
|
206
|
@SuppressWarnings("unused")
|
207
|
private static Integer getIMIS_Id(Reference<?> reference) {
|
208
|
return null;
|
209
|
}
|
210
|
|
211
|
/**
|
212
|
* Returns the <code>SourceCategoryFK</code> attribute.
|
213
|
* @param reference The {@link Reference Reference}.
|
214
|
* @return The <code>SourceCategoryFK</code> attribute.
|
215
|
* @see MethodMapper
|
216
|
*/
|
217
|
@SuppressWarnings("unused")
|
218
|
private static Integer getSourceCategoryFK(Reference<?> reference) {
|
219
|
Integer result = null;
|
220
|
try {
|
221
|
result = PesiTransformer.reference2SourceCategoryFK(reference);
|
222
|
} catch (Exception e) {
|
223
|
e.printStackTrace();
|
224
|
}
|
225
|
return result;
|
226
|
}
|
227
|
|
228
|
/**
|
229
|
* Returns the <code>SourceCategoryCache</code> attribute.
|
230
|
* @param reference The {@link Reference Reference}.
|
231
|
* @return The <code>SourceCategoryCache</code> attribute.
|
232
|
* @see MethodMapper
|
233
|
*/
|
234
|
@SuppressWarnings("unused")
|
235
|
private static String getSourceCategoryCache(Reference<?> reference) {
|
236
|
String result = null;
|
237
|
try {
|
238
|
result = PesiTransformer.getSourceCategoryCache(reference);
|
239
|
} catch (Exception e) {
|
240
|
e.printStackTrace();
|
241
|
}
|
242
|
return result;
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* Returns the <code>Name</code> attribute. The corresponding CDM attribute is <code>title</code>.
|
247
|
* @param reference The {@link Reference Reference}.
|
248
|
* @return The <code>Name</code> attribute.
|
249
|
* @see MethodMapper
|
250
|
*/
|
251
|
@SuppressWarnings("unused")
|
252
|
private static String getName(Reference<?> reference) {
|
253
|
if (reference != null) {
|
254
|
return reference.getTitleCache(); // was getTitle()
|
255
|
} else {
|
256
|
return null;
|
257
|
}
|
258
|
}
|
259
|
|
260
|
/**
|
261
|
* Returns the <code>AuthorString</code> attribute. The corresponding CDM attribute is the <code>titleCache</code> of an <code>authorTeam</code>.
|
262
|
* @param reference The {@link Reference Reference}.
|
263
|
* @return The <code>AuthorString</code> attribute.
|
264
|
* @see MethodMapper
|
265
|
*/
|
266
|
@SuppressWarnings("unused")
|
267
|
private static String getAuthorString(Reference<?> reference) {
|
268
|
String result = null;
|
269
|
|
270
|
try {
|
271
|
if (reference != null) {
|
272
|
TeamOrPersonBase team = reference.getAuthorTeam();
|
273
|
if (team != null) {
|
274
|
result = team.getTitleCache();
|
275
|
// result = team.getNomenclaturalTitle();
|
276
|
} else {
|
277
|
result = null;
|
278
|
}
|
279
|
}
|
280
|
} catch (Exception e) {
|
281
|
e.printStackTrace();
|
282
|
}
|
283
|
|
284
|
return result;
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* Returns the <code>NomRefCache</code> attribute. The corresponding CDM attribute is <code>titleCache</code>.
|
289
|
* @param reference The {@link Reference Reference}.
|
290
|
* @return The <code>NomRefCache</code> attribute.
|
291
|
* @see MethodMapper
|
292
|
*/
|
293
|
@SuppressWarnings("unused")
|
294
|
private static String getNomRefCache(Reference<?> reference) {
|
295
|
return null;
|
296
|
// if (reference != null) {
|
297
|
// return reference.getTitleCache();
|
298
|
// } else {
|
299
|
// return null;
|
300
|
// }
|
301
|
}
|
302
|
|
303
|
/**
|
304
|
* Returns the <code>Notes</code> attribute.
|
305
|
* @param reference The {@link Reference Reference}.
|
306
|
* @return The <code>Notes</code> attribute.
|
307
|
* @see MethodMapper
|
308
|
*/
|
309
|
@SuppressWarnings("unused")
|
310
|
private static String getNotes(Reference<?> reference) {
|
311
|
// TODO
|
312
|
return null;
|
313
|
}
|
314
|
|
315
|
/**
|
316
|
* Returns the <code>RefIdInSource</code> attribute.
|
317
|
* @param reference The {@link Reference Reference}.
|
318
|
* @return The <code>RefIdInSource</code> attribute.
|
319
|
* @see MethodMapper
|
320
|
*/
|
321
|
@SuppressWarnings("unused")
|
322
|
private static String getRefIdInSource(Reference<?> reference) {
|
323
|
String result = null;
|
324
|
|
325
|
try {
|
326
|
if (reference != null) {
|
327
|
Set<IdentifiableSource> sources = reference.getSources();
|
328
|
if (sources.size() == 1) {
|
329
|
result = sources.iterator().next().getIdInSource();
|
330
|
} else if (sources.size() > 1) {
|
331
|
logger.warn("Reference has multiple IdentifiableSources: " + reference.getUuid() + " (" + reference.getTitleCache() + ")");
|
332
|
int count = 1;
|
333
|
for (IdentifiableSource source : sources) {
|
334
|
result += source.getIdInSource();
|
335
|
if (count < sources.size()) {
|
336
|
result += "; ";
|
337
|
}
|
338
|
count++;
|
339
|
}
|
340
|
}
|
341
|
}
|
342
|
} catch (Exception e) {
|
343
|
e.printStackTrace();
|
344
|
}
|
345
|
|
346
|
return result;
|
347
|
}
|
348
|
|
349
|
/**
|
350
|
* Returns the <code>OriginalDB</code> attribute. The corresponding CDM attribute is the <code>titleCache</code> of a <code>citation</code>.
|
351
|
* @param reference The {@link Reference Reference}.
|
352
|
* @return The <code>OriginalDB</code> attribute.
|
353
|
* @see MethodMapper
|
354
|
*/
|
355
|
@SuppressWarnings("unused")
|
356
|
private static String getOriginalDB(Reference<?> reference) {
|
357
|
String result = "";
|
358
|
|
359
|
try {
|
360
|
if (reference != null) {
|
361
|
Set<IdentifiableSource> sources = reference.getSources();
|
362
|
if (sources.size() == 1) {
|
363
|
Reference citation = sources.iterator().next().getCitation();
|
364
|
if (citation != null) {
|
365
|
result = PesiTransformer.databaseString2Abbreviation(citation.getTitleCache()); //or just title
|
366
|
} else {
|
367
|
logger.warn("OriginalDB can not be determined because the citation of this source is NULL: " + sources.iterator().next().getUuid());
|
368
|
}
|
369
|
} else if (sources.size() > 1) {
|
370
|
logger.warn("Taxon has multiple IdentifiableSources: " + reference.getUuid() + " (" + reference.getTitleCache() + ")");
|
371
|
int count = 1;
|
372
|
for (IdentifiableSource source : sources) {
|
373
|
Reference citation = source.getCitation();
|
374
|
if (citation != null) {
|
375
|
result += PesiTransformer.databaseString2Abbreviation(citation.getTitleCache());
|
376
|
if (count < sources.size()) {
|
377
|
result += "; ";
|
378
|
}
|
379
|
count++;
|
380
|
}
|
381
|
}
|
382
|
} else {
|
383
|
result = null;
|
384
|
}
|
385
|
}
|
386
|
} catch (Exception e) {
|
387
|
e.printStackTrace();
|
388
|
}
|
389
|
|
390
|
return result;
|
391
|
}
|
392
|
|
393
|
/* (non-Javadoc)
|
394
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
|
395
|
*/
|
396
|
@Override
|
397
|
protected boolean isIgnore(PesiExportState state) {
|
398
|
return ! (((PesiExportConfigurator) state.getConfig()).getDoReferences().equals(DO_REFERENCES.ALL));
|
399
|
}
|
400
|
|
401
|
/**
|
402
|
* Returns the CDM to PESI specific export mappings.
|
403
|
* @return The {@link PesiExportMapping PesiExportMapping}.
|
404
|
*/
|
405
|
private PesiExportMapping getMapping() {
|
406
|
PesiExportMapping mapping = new PesiExportMapping(dbTableName);
|
407
|
ExtensionType extensionType = null;
|
408
|
|
409
|
mapping.addMapper(IdMapper.NewInstance("SourceId"));
|
410
|
|
411
|
// IMIS_Id
|
412
|
extensionType = (ExtensionType)getTermService().find(ErmsTransformer.IMIS_UUID);
|
413
|
if (extensionType != null) {
|
414
|
mapping.addMapper(DbExtensionMapper.NewInstance(extensionType, "IMIS_Id"));
|
415
|
} else {
|
416
|
mapping.addMapper(MethodMapper.NewInstance("IMIS_Id", this));
|
417
|
}
|
418
|
|
419
|
mapping.addMapper(MethodMapper.NewInstance("SourceCategoryFK", this));
|
420
|
mapping.addMapper(MethodMapper.NewInstance("SourceCategoryCache", this));
|
421
|
mapping.addMapper(MethodMapper.NewInstance("Name", this));
|
422
|
mapping.addMapper(DbStringMapper.NewInstance("referenceAbstract", "Abstract"));
|
423
|
mapping.addMapper(DbStringMapper.NewInstance("title", "Title"));
|
424
|
mapping.addMapper(MethodMapper.NewInstance("AuthorString", this));
|
425
|
mapping.addMapper(DbTimePeriodMapper.NewInstance("datePublished", "RefYear"));
|
426
|
mapping.addMapper(MethodMapper.NewInstance("NomRefCache", this));
|
427
|
mapping.addMapper(DbStringMapper.NewInstance("uri", "Link"));
|
428
|
mapping.addMapper(MethodMapper.NewInstance("Notes", this));
|
429
|
mapping.addMapper(MethodMapper.NewInstance("RefIdInSource", this));
|
430
|
mapping.addMapper(MethodMapper.NewInstance("OriginalDB", this));
|
431
|
|
432
|
return mapping;
|
433
|
}
|
434
|
|
435
|
}
|