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;
|
10
|
|
11
|
import java.sql.Connection;
|
12
|
import java.sql.PreparedStatement;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.springframework.stereotype.Component;
|
18
|
import org.springframework.transaction.TransactionStatus;
|
19
|
|
20
|
import eu.etaxonomy.cdm.io.common.Source;
|
21
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbAnnotationMapper;
|
22
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbFixedIntegerMapper;
|
23
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbFixedStringMapper;
|
24
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbObjectMapper;
|
25
|
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
28
|
import eu.etaxonomy.cdm.model.common.RelationshipBase;
|
29
|
import eu.etaxonomy.cdm.model.name.HybridRelationship;
|
30
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
31
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
32
|
import eu.etaxonomy.cdm.model.name.NomenclaturalSource;
|
33
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
34
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
35
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
36
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
37
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
38
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
39
|
|
40
|
/**
|
41
|
* The export class for relations between {@link eu.etaxonomy.cdm.model.taxon.TaxonBase TaxonBases}.<p>
|
42
|
* Inserts into DataWarehouse database table <code>RelTaxon</code>.
|
43
|
* @author e.-m.lee
|
44
|
* @since 23.02.2010
|
45
|
*/
|
46
|
@Component
|
47
|
public class PesiRelTaxonExport extends PesiExportBase {
|
48
|
|
49
|
private static final long serialVersionUID = 67808745337549629L;
|
50
|
private static final Logger logger = Logger.getLogger(PesiRelTaxonExport.class);
|
51
|
|
52
|
private static final Class<? extends CdmBase> standardMethodParameter = RelationshipBase.class;
|
53
|
|
54
|
private static int modCount = 1000;
|
55
|
private static final String dbTableName = "RelTaxon";
|
56
|
private static final String pluralString = "Relationships";
|
57
|
private static PreparedStatement synonymsStmt;
|
58
|
|
59
|
private PesiExportMapping mapping;
|
60
|
private PesiExportMapping synonymMapping;
|
61
|
private PesiExportMapping taxonNodeMapping;
|
62
|
private PesiExportMapping originalSpellingMapping;
|
63
|
|
64
|
public PesiRelTaxonExport() {
|
65
|
super();
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
public Class<? extends CdmBase> getStandardMethodParameter() {
|
70
|
return standardMethodParameter;
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
protected void doInvoke(PesiExportState state) {
|
75
|
try {
|
76
|
logger.info("*** Started Making " + pluralString + " ...");
|
77
|
|
78
|
doDelete(state);
|
79
|
|
80
|
Connection connection = state.getConfig().getDestination().getConnection();
|
81
|
String synonymsSql = "UPDATE Taxon SET KingdomFk = ?, RankFk = ?, RankCache = ? WHERE TaxonId = ?";
|
82
|
synonymsStmt = connection.prepareStatement(synonymsSql);
|
83
|
|
84
|
// Stores whether this invoke was successful or not.
|
85
|
boolean success = true;
|
86
|
|
87
|
// PESI: Clear the database table RelTaxon.
|
88
|
//doDelete(state); -> done by stored procedure
|
89
|
|
90
|
// Get specific mappings: (CDM) Relationship -> (PESI) RelTaxon
|
91
|
mapping = getMapping();
|
92
|
mapping.initialize(state);
|
93
|
|
94
|
// Get specific mappings: (CDM) Synonym -> (PESI) RelTaxon
|
95
|
synonymMapping = getSynonymMapping();
|
96
|
synonymMapping.initialize(state);
|
97
|
|
98
|
// Get specific mappings: (CDM) Synonym -> (PESI) RelTaxon
|
99
|
taxonNodeMapping = getTaxonNodeMapping();
|
100
|
taxonNodeMapping.initialize(state);
|
101
|
|
102
|
//Export taxon relations
|
103
|
success &= doPhase01(state, mapping);
|
104
|
|
105
|
//Export taxon nodes
|
106
|
success &= doPhase01b(state, taxonNodeMapping);
|
107
|
|
108
|
// Export name relations
|
109
|
success &= doPhase02(state, mapping);
|
110
|
|
111
|
// Export synonym relations (directly attached to taxa)
|
112
|
success &= doPhase03(state, synonymMapping);
|
113
|
|
114
|
if (! success){
|
115
|
state.getResult().addError("An unknown error occurred in PesiRelTaxonExport");
|
116
|
}
|
117
|
} catch (Exception e) {
|
118
|
e.printStackTrace();
|
119
|
logger.error(e.getMessage());
|
120
|
state.getResult().addException(e);
|
121
|
return;
|
122
|
}
|
123
|
}
|
124
|
|
125
|
private boolean doPhase03(PesiExportState state, PesiExportMapping synonymMapping) {
|
126
|
logger.info("PHASE 3: Direct Synonym Relationships ...");
|
127
|
|
128
|
boolean success = true;
|
129
|
int limit = state.getConfig().getLimitSave();
|
130
|
// Start transaction
|
131
|
TransactionStatus txStatus = startTransaction(true);
|
132
|
logger.debug("Started new transaction. Fetching some synonyms (max: " + limit + ") ...");
|
133
|
|
134
|
List<Synonym> list;
|
135
|
|
136
|
//taxon relations
|
137
|
int partitionCount = 0;
|
138
|
int totalCount = 0;
|
139
|
while ((list = getNextTaxonPartition(Synonym.class, limit, partitionCount++, null))!= null) {
|
140
|
totalCount = totalCount + list.size();
|
141
|
logger.info("Read " + list.size() + " PESI synonyms. Limit: " + limit + ". Total: " + totalCount );
|
142
|
// if (list.size() > 0){
|
143
|
// logger.warn("First relation type is " + list.get(0).getType().getTitleCache());
|
144
|
// }
|
145
|
for (Synonym synonym : list){
|
146
|
try {
|
147
|
synonymMapping.invoke(synonym);
|
148
|
} catch (Exception e) {
|
149
|
logger.error(e.getMessage() + ". Synonym: " + synonym.getUuid());
|
150
|
e.printStackTrace();
|
151
|
}
|
152
|
}
|
153
|
|
154
|
commitTransaction(txStatus);
|
155
|
txStatus = startTransaction();
|
156
|
}
|
157
|
commitTransaction(txStatus);
|
158
|
return success;
|
159
|
}
|
160
|
|
161
|
private boolean doPhase01(PesiExportState state, PesiExportMapping mapping) {
|
162
|
logger.info("PHASE 1: Taxon Relationships ...");
|
163
|
boolean success = true;
|
164
|
|
165
|
int limit = state.getConfig().getLimitSave();
|
166
|
// Start transaction
|
167
|
TransactionStatus txStatus = startTransaction(true);
|
168
|
logger.debug("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
|
169
|
|
170
|
List<RelationshipBase<?,?,?>> list;
|
171
|
|
172
|
//taxon relations
|
173
|
int partitionCount = 0;
|
174
|
int totalCount = 0;
|
175
|
while ((list = getNextTaxonRelationshipPartition(limit, partitionCount++, null)) != null) {
|
176
|
totalCount = totalCount + list.size();
|
177
|
logger.info("Read " + list.size() + " PESI relations. Limit: " + limit + ". Total: " + totalCount );
|
178
|
// if (list.size() > 0){
|
179
|
// logger.warn("First relation type is " + list.get(0).getType().getTitleCache());
|
180
|
// }
|
181
|
for (RelationshipBase<?,?,?> rel : list){
|
182
|
try {
|
183
|
mapping.invoke(rel);
|
184
|
} catch (Exception e) {
|
185
|
logger.error(e.getMessage() + ". Relationship: " + rel.getUuid());
|
186
|
e.printStackTrace();
|
187
|
}
|
188
|
}
|
189
|
|
190
|
commitTransaction(txStatus);
|
191
|
txStatus = startTransaction();
|
192
|
}
|
193
|
commitTransaction(txStatus);
|
194
|
return success;
|
195
|
}
|
196
|
|
197
|
private boolean doPhase01b(PesiExportState state, PesiExportMapping taxonNodeMapping) {
|
198
|
logger.info("PHASE 1b: Taxonnodes ...");
|
199
|
boolean success = true;
|
200
|
|
201
|
int limit = state.getConfig().getLimitSave();
|
202
|
// Start transaction
|
203
|
TransactionStatus txStatus = startTransaction(true);
|
204
|
logger.debug("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
|
205
|
|
206
|
List<TaxonNode> list;
|
207
|
|
208
|
//taxon nodes
|
209
|
int partitionCount = 0;
|
210
|
int totalCount = 0;
|
211
|
while ((list = getNextTaxonNodePartition(limit, partitionCount++, null)) != null) {
|
212
|
totalCount = totalCount + list.size();
|
213
|
logger.info("Read " + list.size() + " PESI taxon nodes. Limit: " + limit + ". Total: " + totalCount );
|
214
|
for (TaxonNode tn : list){
|
215
|
try {
|
216
|
taxonNodeMapping.invoke(tn);
|
217
|
} catch (Exception e) {
|
218
|
logger.error(e.getMessage() + ". TaxonNode: " + tn.getUuid());
|
219
|
e.printStackTrace();
|
220
|
}
|
221
|
}
|
222
|
|
223
|
commitTransaction(txStatus);
|
224
|
txStatus = startTransaction();
|
225
|
}
|
226
|
commitTransaction(txStatus);
|
227
|
return success;
|
228
|
}
|
229
|
|
230
|
private boolean doPhase02(PesiExportState state, PesiExportMapping mapping2) {
|
231
|
logger.info("PHASE 2: Name Relationships ...");
|
232
|
boolean success = true;
|
233
|
|
234
|
int limit = state.getConfig().getLimitSave();
|
235
|
// Start transaction
|
236
|
TransactionStatus txStatus = startTransaction(true);
|
237
|
logger.debug("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
|
238
|
|
239
|
//name relations
|
240
|
List<NameRelationship> list;
|
241
|
int partitionCount = 0;
|
242
|
while ((list = getNextNameRelationshipPartition(NameRelationship.class, limit, partitionCount++, null)) != null ) {
|
243
|
txStatus = handleNameRelationList(state, txStatus, list);
|
244
|
}
|
245
|
//hybrid relations
|
246
|
List<HybridRelationship> hybridList;
|
247
|
partitionCount = 0;
|
248
|
while ((hybridList = getNextNameRelationshipPartition(HybridRelationship.class, limit, partitionCount++, null)) != null ) {
|
249
|
txStatus = handleNameRelationList(state, txStatus, hybridList);
|
250
|
}
|
251
|
//original spellings
|
252
|
List<NomenclaturalSource> originalSpellingList;
|
253
|
partitionCount = 0;
|
254
|
while ((originalSpellingList = getNextOriginalSpellingPartition(limit, partitionCount++, null)) != null ) {
|
255
|
txStatus = handleOriginalSpellingList(state, txStatus, originalSpellingList);
|
256
|
}
|
257
|
|
258
|
commitTransaction(txStatus);
|
259
|
logger.info("End PHASE 2: Name Relationships ...");
|
260
|
state.setCurrentFromObject(null);
|
261
|
state.setCurrentToObject(null);
|
262
|
return success;
|
263
|
}
|
264
|
|
265
|
private TransactionStatus handleNameRelationList(PesiExportState state, TransactionStatus txStatus,
|
266
|
List<? extends RelationshipBase<?,?,?>> list) {
|
267
|
for (RelationshipBase<?,?,?> rel : list){
|
268
|
try {
|
269
|
TaxonName name1;
|
270
|
TaxonName name2;
|
271
|
if (rel.isInstanceOf(HybridRelationship.class)){
|
272
|
HybridRelationship hybridRel = CdmBase.deproxy(rel, HybridRelationship.class);
|
273
|
name1 = hybridRel.getParentName();
|
274
|
name2 = hybridRel.getHybridName();
|
275
|
}else if (rel.isInstanceOf(NameRelationship.class)){
|
276
|
NameRelationship nameRel = CdmBase.deproxy(rel, NameRelationship.class);
|
277
|
name1 = nameRel.getFromName();
|
278
|
name2 = nameRel.getToName();
|
279
|
}else{
|
280
|
logger.warn ("Only hybrid- and name-relationships allowed here");
|
281
|
continue;
|
282
|
}
|
283
|
List<IdentifiableEntity> fromList = new ArrayList<>();
|
284
|
List<IdentifiableEntity> toList = new ArrayList<>();
|
285
|
makeList(name1, fromList);
|
286
|
makeList(name2, toList);
|
287
|
|
288
|
for (IdentifiableEntity<?> fromEntity : fromList){
|
289
|
for (IdentifiableEntity<?> toEntity : toList){
|
290
|
//TODO set entities to state
|
291
|
state.setCurrentFromObject(fromEntity);
|
292
|
state.setCurrentToObject(toEntity);
|
293
|
mapping.invoke(rel);
|
294
|
}
|
295
|
}
|
296
|
} catch (Exception e) {
|
297
|
logger.error(e.getMessage() + ". Relationship: " + rel.getUuid());
|
298
|
e.printStackTrace();
|
299
|
}
|
300
|
}
|
301
|
commitTransaction(txStatus);
|
302
|
txStatus = startTransaction();
|
303
|
return txStatus;
|
304
|
}
|
305
|
|
306
|
private TransactionStatus handleOriginalSpellingList(PesiExportState state, TransactionStatus txStatus,
|
307
|
List<NomenclaturalSource> list) {
|
308
|
for (NomenclaturalSource nomSource : list){
|
309
|
try {
|
310
|
TaxonName name1 = nomSource.getNameUsedInSource();
|
311
|
TaxonName name2 = nomSource.getSourcedName();
|
312
|
List<IdentifiableEntity> fromList = new ArrayList<>();
|
313
|
List<IdentifiableEntity> toList = new ArrayList<>();
|
314
|
makeList(name1, fromList);
|
315
|
makeList(name2, toList);
|
316
|
|
317
|
for (IdentifiableEntity<?> fromEntity : fromList){
|
318
|
for (IdentifiableEntity<?> toEntity : toList){
|
319
|
//TODO set entities to state
|
320
|
state.setCurrentFromObject(fromEntity);
|
321
|
state.setCurrentToObject(toEntity);
|
322
|
originalSpellingMapping.invoke(nomSource);
|
323
|
}
|
324
|
}
|
325
|
} catch (Exception e) {
|
326
|
logger.error(e.getMessage() + ". NomenclaturalSource: " + nomSource.getUuid());
|
327
|
e.printStackTrace();
|
328
|
}
|
329
|
}
|
330
|
commitTransaction(txStatus);
|
331
|
txStatus = startTransaction();
|
332
|
return txStatus;
|
333
|
}
|
334
|
|
335
|
|
336
|
private void makeList(TaxonName name, List<IdentifiableEntity> list) {
|
337
|
if (! hasPesiTaxon(name)){
|
338
|
list.add(name);
|
339
|
}else{
|
340
|
for (TaxonBase<?> taxon: getPesiTaxa(name)){
|
341
|
list.add(taxon);
|
342
|
}
|
343
|
}
|
344
|
}
|
345
|
|
346
|
/**
|
347
|
* Deletes all entries of database tables related to <code>RelTaxon</code>.
|
348
|
* @param state The {@link PesiExportState PesiExportState}.
|
349
|
* @return Whether the delete operation was successful or not.
|
350
|
*/
|
351
|
protected boolean doDelete(PesiExportState state) {
|
352
|
PesiExportConfigurator pesiConfig = state.getConfig();
|
353
|
|
354
|
Source destination = pesiConfig.getDestination();
|
355
|
|
356
|
// Clear RelTaxon
|
357
|
String sql = "DELETE FROM " + dbTableName;
|
358
|
destination.update(sql);
|
359
|
return true;
|
360
|
}
|
361
|
|
362
|
/**
|
363
|
* Returns the <code>TaxonFk1</code> attribute. It corresponds to a CDM <code>TaxonRelationship</code>.
|
364
|
* @param relationship The {@link RelationshipBase Relationship}.
|
365
|
* @param state The {@link PesiExportState PesiExportState}.
|
366
|
* @return The <code>TaxonFk1</code> attribute.
|
367
|
* @see MethodMapper
|
368
|
*/
|
369
|
@SuppressWarnings("unused")
|
370
|
private static Integer getTaxonFk1(RelationshipBase<?, ?, ?> relationship, PesiExportState state) {
|
371
|
return getObjectFk(relationship, state, true);
|
372
|
}
|
373
|
|
374
|
/**
|
375
|
* Returns the <code>TaxonFk1</code> attribute. It corresponds to a CDM <code>TaxonRelationship</code>.
|
376
|
* @param relationship The {@link RelationshipBase Relationship}.
|
377
|
* @param state The {@link PesiExportState PesiExportState}.
|
378
|
* @return The <code>TaxonFk1</code> attribute.
|
379
|
* @see MethodMapper
|
380
|
*/
|
381
|
@SuppressWarnings("unused")
|
382
|
private static Integer getTaxonFk1(Synonym synonym, PesiExportState state) {
|
383
|
return synonym.getAcceptedTaxon().getId();
|
384
|
}
|
385
|
/**
|
386
|
* Returns the <code>TaxonFk1</code> attribute. It corresponds to a CDM <code>Synonym</code>.
|
387
|
* @param synonym The {@link Synonym synonym}.
|
388
|
* @param state The {@link PesiExportState PesiExportState}.
|
389
|
* @return The <code>TaxonFk1</code> attribute.
|
390
|
* @see MethodMapper
|
391
|
*/
|
392
|
@SuppressWarnings("unused")
|
393
|
private static Integer getSynonym(Synonym synonym, PesiExportState state) {
|
394
|
return state.getDbId(synonym);
|
395
|
}
|
396
|
|
397
|
/**
|
398
|
* Returns the <code>TaxonFk1</code> attribute. It corresponds to a CDM <code>Synonym</code>.
|
399
|
* @param synonym The {@link Synonym synonym}.
|
400
|
* @param state The {@link PesiExportState PesiExportState}.
|
401
|
* @return The <code>TaxonFk1</code> attribute.
|
402
|
* @see MethodMapper
|
403
|
*/
|
404
|
@SuppressWarnings("unused")
|
405
|
private static Integer getParent(TaxonNode taxonNode, PesiExportState state) {
|
406
|
TaxonNode parent = taxonNode == null? null : taxonNode.getParent();
|
407
|
Taxon parentTaxon = parent == null? null: parent.getTaxon();
|
408
|
return state.getDbId(parentTaxon);
|
409
|
}
|
410
|
|
411
|
/**
|
412
|
* Returns the id of the previously defined fromObject.
|
413
|
* @see MethodMapper
|
414
|
*/
|
415
|
@SuppressWarnings("unused")
|
416
|
private static Integer getFromObject(PesiExportState state) {
|
417
|
return state.getDbId(state.getCurrentFromObject());
|
418
|
}
|
419
|
/**
|
420
|
* Returns the id of the previously defined fromObject.
|
421
|
* @see MethodMapper
|
422
|
*/
|
423
|
@SuppressWarnings("unused")
|
424
|
private static Integer getToObject(PesiExportState state) {
|
425
|
return state.getDbId(state.getCurrentToObject());
|
426
|
}
|
427
|
|
428
|
/**
|
429
|
* Returns the <code>TaxonFk2</code> attribute. It corresponds to a CDM <code>SynonymRelationship</code>.
|
430
|
* @param relationship The {@link RelationshipBase Relationship}.
|
431
|
* @param state The {@link PesiExportState PesiExportState}.
|
432
|
* @return The <code>TaxonFk2</code> attribute.
|
433
|
* @see MethodMapper
|
434
|
*/
|
435
|
@SuppressWarnings("unused")
|
436
|
private static Integer getTaxonFk2(RelationshipBase<?, ?, ?> relationship, PesiExportState state) {
|
437
|
return getObjectFk(relationship, state, false);
|
438
|
}
|
439
|
|
440
|
/**
|
441
|
* Returns the <code>RelTaxonQualifierFk</code> attribute.
|
442
|
* @param relationship The {@link RelationshipBase Relationship}.
|
443
|
* @return The <code>RelTaxonQualifierFk</code> attribute.
|
444
|
* @see MethodMapper
|
445
|
*/
|
446
|
@SuppressWarnings("unused")
|
447
|
private static Integer getRelTaxonQualifierFk(RelationshipBase<?, ?, ?> relationship) {
|
448
|
return PesiTransformer.taxonRelation2RelTaxonQualifierFk(relationship);
|
449
|
}
|
450
|
|
451
|
@SuppressWarnings("unused")
|
452
|
private static Integer getRelTaxonQualifierFk(Synonym synonym) {
|
453
|
return PesiTransformer.synonym2RelTaxonQualifierFk(synonym);
|
454
|
}
|
455
|
|
456
|
/**
|
457
|
* Returns the <code>RelQualifierCache</code> attribute.
|
458
|
* @param relationship The {@link RelationshipBase Relationship}.
|
459
|
* @return The <code>RelQualifierCache</code> attribute.
|
460
|
* @see MethodMapper
|
461
|
*/
|
462
|
@SuppressWarnings("unused")
|
463
|
private static String getRelQualifierCache(RelationshipBase<?, ?, ?> relationship, PesiExportState state) {
|
464
|
String result = null;
|
465
|
NomenclaturalCode code = null;
|
466
|
Taxon taxon = null;
|
467
|
TaxonName name= null;
|
468
|
if (relationship.isInstanceOf(TaxonRelationship.class)){
|
469
|
TaxonRelationship rel = CdmBase.deproxy(relationship, TaxonRelationship.class);
|
470
|
taxon = rel.getToTaxon();
|
471
|
name = taxon.getName();
|
472
|
code = name.getNameType();
|
473
|
}else if (relationship.isInstanceOf(NameRelationship.class)){
|
474
|
NameRelationship rel = CdmBase.deproxy(relationship, NameRelationship.class);
|
475
|
name = rel.getFromName();
|
476
|
code =name.getNameType();
|
477
|
}else if (relationship.isInstanceOf(HybridRelationship.class)){
|
478
|
HybridRelationship rel = CdmBase.deproxy(relationship, HybridRelationship.class);
|
479
|
name = rel.getParentName();
|
480
|
code = name.getNameType();
|
481
|
}
|
482
|
if (code != null) {
|
483
|
result = state.getConfig().getTransformer().getCacheByRelationshipType(relationship, code);
|
484
|
} else {
|
485
|
logger.error("NomenclaturalCode is NULL while creating the following relationship: " + relationship.getUuid());
|
486
|
}
|
487
|
return result;
|
488
|
}
|
489
|
|
490
|
|
491
|
/**
|
492
|
* Returns the database key of an object in the given relationship.
|
493
|
* @param relationship {@link RelationshipBase RelationshipBase}.
|
494
|
* @param state {@link PesiExportState PesiExportState}.
|
495
|
* @param isFrom A boolean value indicating whether the database key of the parent or child in this relationship is searched. <code>true</code> means the child is searched. <code>false</code> means the parent is searched.
|
496
|
* @return The database key of an object in the given relationship.
|
497
|
*/
|
498
|
private static Integer getObjectFk(RelationshipBase<?, ?, ?> relationship, PesiExportState state, boolean isFrom) {
|
499
|
TaxonBase<?> taxonBase = null;
|
500
|
if (relationship.isInstanceOf(TaxonRelationship.class)) {
|
501
|
TaxonRelationship tr = (TaxonRelationship)relationship;
|
502
|
taxonBase = (isFrom) ? tr.getFromTaxon(): tr.getToTaxon();
|
503
|
} else if (relationship.isInstanceOf(NameRelationship.class)
|
504
|
|| relationship.isInstanceOf(HybridRelationship.class)) {
|
505
|
if (isFrom){
|
506
|
return state.getDbId(state.getCurrentFromObject());
|
507
|
}else{
|
508
|
return state.getDbId(state.getCurrentToObject());
|
509
|
}
|
510
|
}
|
511
|
if (taxonBase != null) {
|
512
|
if (! isPesiTaxon(taxonBase)){
|
513
|
logger.warn("Related taxonBase is not a PESI taxon. Taxon: " + taxonBase.getId() + "/" + taxonBase.getUuid() + "; TaxonRel: " + relationship.getId() + "(" + relationship.getType().getTitleCache() + ")");
|
514
|
return null;
|
515
|
}else{
|
516
|
return state.getDbId(taxonBase);
|
517
|
}
|
518
|
|
519
|
}
|
520
|
logger.warn("No taxon found in state for relationship: " + relationship.toString());
|
521
|
return null;
|
522
|
}
|
523
|
|
524
|
private static Integer getObjectFk(NomenclaturalSource nomSource, PesiExportState state, boolean isFrom) {
|
525
|
if (isFrom){
|
526
|
return state.getDbId(state.getCurrentFromObject());
|
527
|
}else{
|
528
|
return state.getDbId(state.getCurrentToObject());
|
529
|
}
|
530
|
}
|
531
|
|
532
|
@SuppressWarnings("unused") //for synonym mapping
|
533
|
private static String getSynonymTypeCache(Synonym synonym, PesiExportState state) {
|
534
|
String result = null;
|
535
|
NomenclaturalCode code = null;
|
536
|
code = synonym.getAcceptedTaxon().getName().getNameType();
|
537
|
|
538
|
if (code != null) {
|
539
|
result = state.getConfig().getTransformer().getCacheBySynonymType(synonym, code);
|
540
|
} else {
|
541
|
logger.error("NomenclaturalCode is NULL while creating the following synonym: " + synonym.getUuid());
|
542
|
}
|
543
|
return result;
|
544
|
}
|
545
|
|
546
|
/**
|
547
|
* Returns the CDM to PESI specific export mappings.
|
548
|
* @return The {@link PesiExportMapping PesiExportMapping}.
|
549
|
*/
|
550
|
PesiExportMapping getMapping() {
|
551
|
PesiExportMapping mapping = new PesiExportMapping(dbTableName);
|
552
|
|
553
|
mapping.addMapper(MethodMapper.NewInstance("TaxonFk1", this.getClass(), "getTaxonFk1", standardMethodParameter, PesiExportState.class));
|
554
|
mapping.addMapper(MethodMapper.NewInstance("TaxonFk2", this.getClass(), "getTaxonFk2", standardMethodParameter, PesiExportState.class));
|
555
|
mapping.addMapper(MethodMapper.NewInstance("RelTaxonQualifierFk", this));
|
556
|
mapping.addMapper(MethodMapper.NewInstance("RelQualifierCache", this, RelationshipBase.class, PesiExportState.class));
|
557
|
mapping.addMapper(DbAnnotationMapper.NewExludedInstance(getLastActionAnnotationTypes(), "Notes"));
|
558
|
|
559
|
return mapping;
|
560
|
}
|
561
|
|
562
|
/**
|
563
|
* Returns the CDM to PESI specific export mappings.
|
564
|
* @return The {@link PesiExportMapping PesiExportMapping}.
|
565
|
*/
|
566
|
PesiExportMapping getSynonymMapping() {
|
567
|
PesiExportMapping mapping = new PesiExportMapping(dbTableName);
|
568
|
|
569
|
mapping.addMapper(MethodMapper.NewInstance("TaxonFk1", this.getClass(), "getSynonym", Synonym.class, PesiExportState.class));
|
570
|
mapping.addMapper(DbObjectMapper.NewInstance("acceptedTaxon", "TaxonFk2"));
|
571
|
mapping.addMapper(MethodMapper.NewInstance("RelTaxonQualifierFk", this, Synonym.class));
|
572
|
mapping.addMapper(MethodMapper.NewInstance("RelQualifierCache", this.getClass(), "getSynonymTypeCache", Synonym.class, PesiExportState.class));
|
573
|
mapping.addMapper(DbAnnotationMapper.NewExludedInstance(getLastActionAnnotationTypes(), "Notes"));
|
574
|
|
575
|
return mapping;
|
576
|
}
|
577
|
|
578
|
PesiExportMapping getTaxonNodeMapping() {
|
579
|
PesiExportMapping mapping = new PesiExportMapping(dbTableName);
|
580
|
|
581
|
mapping.addMapper(MethodMapper.NewInstance("TaxonFk2", this.getClass(), "getParent", TaxonNode.class, PesiExportState.class));
|
582
|
mapping.addMapper(DbObjectMapper.NewInstance("taxon", "TaxonFk1"));
|
583
|
mapping.addMapper(DbFixedIntegerMapper.NewInstance(PesiTransformer.IS_TAXONOMICALLY_INCLUDED_IN, "RelTaxonQualifierFk"));
|
584
|
mapping.addMapper(DbFixedStringMapper.NewInstance("is taxonomically included in", "RelQualifierCache"));
|
585
|
// mapping.addMapper(DbAnnotationMapper.NewExludedInstance(getLastActionAnnotationTypes(), "Notes"));
|
586
|
|
587
|
return mapping;
|
588
|
}
|
589
|
|
590
|
PesiExportMapping getOriginalSpellingMapping() {
|
591
|
PesiExportMapping mapping = new PesiExportMapping(dbTableName);
|
592
|
|
593
|
mapping.addMapper(MethodMapper.NewInstance("TaxonFk1", this.getClass(), "getFromObject", PesiExportState.class));
|
594
|
mapping.addMapper(MethodMapper.NewInstance("TaxonFk2", this.getClass(), "getToObject", PesiExportState.class));
|
595
|
mapping.addMapper(DbFixedIntegerMapper.NewInstance(PesiTransformer.IS_ORIGINAL_SPELLING_FOR, "RelTaxonQualifierFk"));
|
596
|
mapping.addMapper(DbFixedStringMapper.NewInstance("is original spelling for", "RelQualifierCache"));
|
597
|
// mapping.addMapper(DbAnnotationMapper.NewExludedInstance(getLastActionAnnotationTypes(), "Notes"));
|
598
|
|
599
|
return mapping;
|
600
|
}
|
601
|
|
602
|
@Override
|
603
|
protected boolean doCheck(PesiExportState state) {
|
604
|
return true;
|
605
|
}
|
606
|
|
607
|
@Override
|
608
|
protected boolean isIgnore(PesiExportState state) {
|
609
|
return ! state.getConfig().isDoRelTaxa();
|
610
|
}
|
611
|
}
|