Fauna Europaea import fixes and cleanup
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / faunaEuropaea / FaunaEuropaeaRelTaxonIncludeImport.java
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 static eu.etaxonomy.cdm.io.faunaEuropaea.FaunaEuropaeaTransformer.A_AUCT;
13
14 import java.sql.ResultSet;
15 import java.sql.SQLException;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.UUID;
22
23 import org.apache.log4j.Logger;
24 import org.springframework.stereotype.Component;
25 import org.springframework.transaction.TransactionStatus;
26
27 import eu.etaxonomy.cdm.io.common.ICdmIO;
28 import eu.etaxonomy.cdm.io.common.MapWrapper;
29 import eu.etaxonomy.cdm.io.common.Source;
30 import eu.etaxonomy.cdm.io.profiler.ProfilerController;
31 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
32 import eu.etaxonomy.cdm.model.common.CdmBase;
33 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
34 import eu.etaxonomy.cdm.model.taxon.Synonym;
35 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
36 import eu.etaxonomy.cdm.model.taxon.Taxon;
37 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38 import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
39
40
41
42 /**
43 * @author a.babadshanjan
44 * @created 12.05.2009
45 * @version 1.0
46 */
47 @Component
48 public class FaunaEuropaeaRelTaxonIncludeImport extends FaunaEuropaeaImportBase {
49
50 public static final String OS_NAMESPACE_TAXON = "Taxon";
51 private static final Logger logger = Logger.getLogger(FaunaEuropaeaRelTaxonIncludeImport.class);
52
53 private static String ALL_SYNONYM_FROM_CLAUSE = " FROM Taxon INNER JOIN Taxon AS Parent " +
54 " ON Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +
55 " WHERE (Taxon.TAX_VALID = 0) " +
56 " AND (Taxon.TAX_AUT_ID <> " + A_AUCT + " OR Taxon.TAX_AUT_ID IS NULL)";
57
58 // private static String PARENT_SYNONYM_FROM_CLAUSE = " FROM Taxon INNER JOIN Taxon AS Parent " +
59 // " ON Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +
60 // " WHERE (Taxon.TAX_TAX_IDGENUS is null) AND (Taxon.TAX_VALID = 0) " +
61 // " AND (Taxon.TAX_AUT_ID <> " + A_AUCT + " OR Taxon.TAX_AUT_ID IS NULL)";
62
63 // private static String ORIGINAL_GENUS_SYNONYM_FROM_CLAUSE = " FROM Taxon INNER JOIN Taxon AS Parent " +
64 // " ON Taxon.TAX_TAX_IDGENUS = Parent.TAX_ID " +
65 // " WHERE (Taxon.TAX_TAX_IDGENUS is not null) AND (Taxon.TAX_VALID = 0) " +
66 // " AND (Taxon.TAX_AUT_ID <> " + A_AUCT + " OR Taxon.TAX_AUT_ID IS NULL)";
67
68
69 /* (non-Javadoc)
70 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
71 */
72 @Override
73 protected boolean doCheck(FaunaEuropaeaImportState state) {
74 boolean result = true;
75 FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
76 logger.warn("Checking for Taxa not yet fully implemented");
77 result &= checkTaxonStatus(fauEuConfig);
78
79 return result;
80 }
81
82 /* (non-Javadoc)
83 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
84 */
85 protected boolean isIgnore(FaunaEuropaeaImportState state) {
86 return ! (state.getConfig().isDoTaxonomicallyIncluded() ||
87 state.getConfig().isDoMisappliedNames() || state.getConfig().isDoHeterotypicSynonyms());
88 }
89
90 private boolean checkTaxonStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
91 boolean result = true;
92 // try {
93 Source source = fauEuConfig.getSource();
94 String sqlStr = "";
95 ResultSet rs = source.getResultSet(sqlStr);
96 return result;
97 // } catch (SQLException e) {
98 // e.printStackTrace();
99 // return false;
100 // }
101 }
102
103 protected boolean doInvoke(FaunaEuropaeaImportState state) {
104
105 boolean success = true;
106
107 Map<String, MapWrapper<? extends CdmBase>> stores = state.getStores();
108 MapWrapper<TaxonBase> taxonStore = (MapWrapper<TaxonBase>)stores.get(ICdmIO.TAXON_STORE);
109 // taxonStore.makeEmpty();
110 taxonStore = null;
111 MapWrapper<TeamOrPersonBase> authorStore = (MapWrapper<TeamOrPersonBase>)stores.get(ICdmIO.TEAM_STORE);
112 authorStore.makeEmpty();
113
114 if(logger.isInfoEnabled()) { logger.info("Start making relationships..."); }
115
116 TransactionStatus txStatus = startTransaction();
117
118 TaxonBase taxon = getTaxonService().getTaxonByUuid(UUID.fromString("ac7b30dc-6207-4c71-9752-ee0fb838a271"));
119 ReferenceBase<?> sourceRef = taxon.getSec();
120
121 TaxonomicTree tree = getTaxonomicTreeFor(state, sourceRef);
122 commitTransaction(txStatus);
123
124 ProfilerController.memorySnapshot();
125 if (state.getConfig().isDoTaxonomicallyIncluded()) {
126 success = processParentsChildren(state);
127 }
128 ProfilerController.memorySnapshot();
129 if (state.getConfig().isDoMisappliedNames()) {
130 success = processMisappliedNames(state);
131 }
132 ProfilerController.memorySnapshot();
133 if (state.getConfig().isDoHeterotypicSynonyms()) {
134 if(logger.isInfoEnabled()) {
135 logger.info("Start making heterotypic synonym relationships...");
136 }
137 success = processHeterotypicSynonyms(state, ALL_SYNONYM_FROM_CLAUSE);
138 // success = processHeterotypicSynonyms(state, PARENT_SYNONYM_FROM_CLAUSE);
139 // success = processHeterotypicSynonyms(state, ORIGINAL_GENUS_SYNONYM_FROM_CLAUSE);
140 }
141 ProfilerController.memorySnapshot();
142
143 logger.info("End making taxa...");
144
145 return success;
146 }
147
148 /** Retrieve child-parent uuid map from CDM DB */
149 private boolean processParentsChildren(FaunaEuropaeaImportState state) {
150
151 int limit = state.getConfig().getLimitSave();
152
153 TransactionStatus txStatus = null;
154
155 Map<UUID, UUID> childParentMap = null;
156 FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
157 Source source = fauEuConfig.getSource();
158 int i = 0;
159 boolean success = true;
160
161 String selectCount =
162 " SELECT count(*) ";
163
164 String selectColumns = " SELECT Taxon.UUID AS ChildUuid, Parent.UUID AS ParentUuid ";
165
166 String fromClause = " FROM Taxon INNER JOIN Taxon AS Parent " +
167 " ON Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +
168 " WHERE (Taxon.TAX_VALID <> 0) AND (Taxon.TAX_AUT_ID <> " + A_AUCT + " OR Taxon.TAX_AUT_ID IS NULL )";
169
170 String orderClause = " ORDER BY Taxon.TAX_RNK_ID ASC";
171
172 String countQuery =
173 selectCount + fromClause;
174
175 String selectQuery =
176 selectColumns + fromClause + orderClause;
177
178 if(logger.isInfoEnabled()) { logger.info("Start making taxonomically included relationships..."); }
179
180 try {
181
182 ResultSet rs = source.getResultSet(countQuery);
183 rs.next();
184 int count = rs.getInt(1);
185
186 rs = source.getResultSet(selectQuery);
187
188 if (logger.isInfoEnabled()) {
189 logger.info("Number of rows: " + count);
190 logger.info("Count Query: " + countQuery);
191 logger.info("Select Query: " + selectQuery);
192 }
193
194 while (rs.next()) {
195
196 if ((i++ % limit) == 0) {
197
198 txStatus = startTransaction();
199 childParentMap = new HashMap<UUID, UUID>(limit);
200
201 if(logger.isInfoEnabled()) {
202 logger.info("Taxonomically included retrieved: " + (i-1));
203 }
204 }
205
206 String childUuidStr = rs.getString("ChildUuid");
207 String parentUuidStr = rs.getString("ParentUuid");
208 UUID childUuid = UUID.fromString(childUuidStr);
209 UUID parentUuid = UUID.fromString(parentUuidStr);
210
211 if (!childParentMap.containsKey(childUuid)) {
212
213 childParentMap.put(childUuid, parentUuid);
214
215 } else {
216 if(logger.isDebugEnabled()) {
217 logger.debug("Duplicated child UUID (" + childUuid + ")");
218 }
219 }
220 if (((i % limit) == 0 && i != 1 ) || i == count) {
221
222 success = createParentChildRelationships(state, childParentMap);
223
224 childParentMap = null;
225 commitTransaction(txStatus);
226
227 if(logger.isInfoEnabled()) {
228 logger.info("i = " + i + " - Transaction committed");
229 }
230 }
231 }
232
233 } catch (SQLException e) {
234 logger.error("SQLException:" + e);
235 success = false;
236 }
237 return success;
238 }
239
240
241 /** Retrieve misapplied name / accepted taxon uuid map from CDM DB */
242 private boolean processMisappliedNames(FaunaEuropaeaImportState state) {
243
244 int limit = state.getConfig().getLimitSave();
245
246 TransactionStatus txStatus = null;
247
248 Map<UUID, UUID> childParentMap = null;
249 FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
250 Source source = fauEuConfig.getSource();
251 int i = 0;
252 boolean success = true;
253
254 String selectCount =
255 " SELECT count(*) ";
256
257 String selectColumns = " SELECT Taxon.UUID AS MisappliedUuid, Parent.UUID AS AcceptedUuid ";
258
259 String fromClause = " FROM Taxon INNER JOIN Taxon AS Parent " +
260 " ON Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +
261 " WHERE (Taxon.TAX_VALID = 0) AND (Taxon.TAX_AUT_ID = " + A_AUCT + ")";
262
263 String orderClause = " ORDER BY dbo.Taxon.TAX_RNK_ID ASC ";
264
265 String countQuery =
266 selectCount + fromClause;
267
268 String selectQuery =
269 selectColumns + fromClause + orderClause;
270
271 if(logger.isInfoEnabled()) { logger.info("Start making misapplied name relationships..."); }
272
273 try {
274
275 ResultSet rs = source.getResultSet(countQuery);
276 rs.next();
277 int count = rs.getInt(1);
278
279 rs = source.getResultSet(selectQuery);
280
281 if (logger.isInfoEnabled()) {
282 logger.info("Number of rows: " + count);
283 logger.info("Count Query: " + countQuery);
284 logger.info("Select Query: " + selectQuery);
285 }
286
287 while (rs.next()) {
288
289 if ((i++ % limit) == 0) {
290
291 txStatus = startTransaction();
292 childParentMap = new HashMap<UUID, UUID>(limit);
293
294 if(logger.isInfoEnabled()) {
295 logger.info("Misapplied names retrieved: " + (i-1) );
296 }
297 }
298
299 String childUuidStr = rs.getString("MisappliedUuid");
300 String parentUuidStr = rs.getString("AcceptedUuid");
301 UUID childUuid = UUID.fromString(childUuidStr);
302 UUID parentUuid = UUID.fromString(parentUuidStr);
303
304 if (!childParentMap.containsKey(childUuid)) {
305
306 childParentMap.put(childUuid, parentUuid);
307
308 } else {
309 if(logger.isDebugEnabled()) {
310 logger.debug("Duplicated child UUID (" + childUuid + ")");
311 }
312 }
313
314 if (((i % limit) == 0 && i != 1 ) || i == count) {
315
316 success = createMisappliedNameRelationships(state, childParentMap);
317
318 childParentMap = null;
319 commitTransaction(txStatus);
320
321 if(logger.isInfoEnabled()) {
322 logger.info("i = " + i + " - Transaction committed");
323 }
324 }
325 }
326
327 } catch (SQLException e) {
328 logger.error("SQLException:" + e);
329 success = false;
330 }
331 return success;
332 }
333
334
335 /** Retrieve synonyms from FauEuDB DB */
336 private boolean processHeterotypicSynonyms(FaunaEuropaeaImportState state, String fromClause) {
337
338 FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
339 Source source = fauEuConfig.getSource();
340 boolean success = true;
341
342 String selectCount =
343 " SELECT count(*) ";
344
345 String selectColumns = " SELECT Taxon.UUID AS SynonymUuid, Parent.UUID AS AcceptedUuid ";
346
347 String orderClause = " ORDER BY dbo.Taxon.TAX_RNK_ID ASC ";
348
349 String countQuery =
350 selectCount + fromClause;
351
352 String selectQuery =
353 selectColumns + fromClause + orderClause;
354
355 try {
356
357 ResultSet rs = source.getResultSet(countQuery);
358 rs.next();
359 int count = rs.getInt(1);
360
361 rs = source.getResultSet(selectQuery);
362
363 if (logger.isInfoEnabled()) {
364 logger.info("Number of rows: " + count);
365 logger.info("Count Query: " + countQuery);
366 logger.info("Select Query: " + selectQuery);
367 }
368
369 success = storeSynonymRelationships(rs, count, state);
370
371 } catch (SQLException e) {
372 logger.error("SQLException:" + e);
373 success = false;
374 }
375 return success;
376 }
377
378
379 /** Retrieve synonyms from FauEuDB DB */
380 private boolean processHeterotypicSynonyms_(FaunaEuropaeaImportState state) {
381
382 int limit = state.getConfig().getLimitSave();
383
384 TransactionStatus txStatus = null;
385
386 Map<UUID, UUID> synonymAcceptedMap = null;
387 FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
388 Source source = fauEuConfig.getSource();
389 int i = 0;
390 boolean success = true;
391
392 String selectCount =
393 " SELECT count(*) ";
394
395 String selectColumns = " SELECT Taxon.UUID AS SynonymUuid, Parent.UUID AS AcceptedUuid ";
396
397 String fromClause = " FROM Taxon INNER JOIN Taxon AS Parent " +
398 " ON Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +
399 " WHERE (Taxon.TAX_VALID = 0) AND (Taxon.TAX_AUT_ID <> " + A_AUCT + " OR Taxon.TAX_AUT_ID IS NULL)";
400
401 String orderClause = " ORDER BY dbo.Taxon.TAX_RNK_ID ASC ";
402
403 String countQuery =
404 selectCount + fromClause;
405
406 String selectQuery =
407 selectColumns + fromClause + orderClause;
408
409 try {
410
411 ResultSet rs = source.getResultSet(countQuery);
412 rs.next();
413 int count = rs.getInt(1);
414
415 rs = source.getResultSet(selectQuery);
416
417 if (logger.isInfoEnabled()) {
418 logger.info("Number of rows: " + count);
419 logger.info("Count Query: " + countQuery);
420 logger.info("Select Query: " + selectQuery);
421 }
422
423 while (rs.next()) {
424
425 if ((i++ % limit) == 0) {
426
427 txStatus = startTransaction();
428 synonymAcceptedMap = new HashMap<UUID, UUID>(limit);
429
430 if(logger.isInfoEnabled()) {
431 logger.info("Synonyms retrieved: " + (i-1));
432 }
433 }
434
435 String synonymUuidStr = rs.getString("SynonymUuid");
436 String acceptedUuidStr = rs.getString("AcceptedUuid");
437 UUID synonymUuid = UUID.fromString(synonymUuidStr);
438 UUID acceptedUuid = UUID.fromString(acceptedUuidStr);
439
440 if (!synonymAcceptedMap.containsKey(synonymUuid)) {
441
442 synonymAcceptedMap.put(synonymUuid, acceptedUuid);
443
444 } else {
445 if(logger.isDebugEnabled()) {
446 logger.debug("Duplicated child UUID (" + synonymUuid + ")");
447 }
448 }
449
450 if (((i % limit) == 0 && i != 1 ) || i == count) {
451
452 success = createHeterotypicSynonyms(state, synonymAcceptedMap);
453
454 synonymAcceptedMap = null;
455 commitTransaction(txStatus);
456
457 if(logger.isInfoEnabled()) {
458 logger.info("i = " + i + " - Transaction committed");
459 }
460 }
461 }
462
463 } catch (SQLException e) {
464 logger.error("SQLException:" + e);
465 success = false;
466 }
467 return success;
468 }
469
470
471 private boolean storeSynonymRelationships(ResultSet rs, int count, FaunaEuropaeaImportState state)
472 throws SQLException {
473
474 TransactionStatus txStatus = null;
475 Map<UUID, UUID> synonymAcceptedMap = null;
476 int i = 0;
477 boolean success = true;
478 int limit = state.getConfig().getLimitSave();
479
480 while (rs.next()) {
481
482 if ((i++ % limit) == 0) {
483
484 txStatus = startTransaction();
485 synonymAcceptedMap = new HashMap<UUID, UUID>(limit);
486
487 if(logger.isInfoEnabled()) {
488 logger.info("Synonyms retrieved: " + (i-1));
489 }
490 }
491
492 String synonymUuidStr = rs.getString("SynonymUuid");
493 String acceptedUuidStr = rs.getString("AcceptedUuid");
494 UUID synonymUuid = UUID.fromString(synonymUuidStr);
495 UUID acceptedUuid = UUID.fromString(acceptedUuidStr);
496
497 if (!synonymAcceptedMap.containsKey(synonymUuid)) {
498
499 synonymAcceptedMap.put(synonymUuid, acceptedUuid);
500
501 } else {
502 if(logger.isDebugEnabled()) {
503 logger.debug("Duplicated synonym UUID (" + synonymUuid + ")");
504 }
505 }
506
507 if (((i % limit) == 0 && i != 1 ) || i == count) {
508
509 success = createHeterotypicSynonyms(state, synonymAcceptedMap);
510
511 synonymAcceptedMap = null;
512 commitTransaction(txStatus);
513
514 if(logger.isInfoEnabled()) {
515 logger.info("i = " + i + " - Transaction committed");
516 }
517 }
518 }
519 return success;
520 }
521
522
523 /* Creates parent-child relationships.
524 * Parent-child pairs are retrieved in blocks via findByUUID(Set<UUID>) from CDM DB.
525 */
526 private boolean createParentChildRelationships(FaunaEuropaeaImportState state, Map<UUID, UUID> childParentMap) {
527
528 TaxonBase taxon = getTaxonService().getTaxonByUuid(UUID.fromString("ac7b30dc-6207-4c71-9752-ee0fb838a271"));
529 ReferenceBase<?> sourceRef = taxon.getSec();
530 boolean success = true;
531 int limit = state.getConfig().getLimitSave();
532
533 TaxonomicTree tree = getTaxonomicTreeFor(state, sourceRef);
534
535 Set<TaxonBase> childSet = new HashSet<TaxonBase>(limit);
536
537 Set<UUID> childKeysSet = childParentMap.keySet();
538 Set<UUID> parentValuesSet = new HashSet<UUID>(childParentMap.values());
539
540 if (logger.isTraceEnabled()) {
541 logger.trace("Start reading children and parents");
542 }
543 List<TaxonBase> children = getTaxonService().findByUuid(childKeysSet);
544 List<TaxonBase> parents = getTaxonService().findByUuid(parentValuesSet);
545 Map<UUID, TaxonBase> parentsMap = new HashMap<UUID, TaxonBase>(parents.size());
546 for (TaxonBase taxonBase : parents){
547 parentsMap.put(taxonBase.getUuid(), taxonBase);
548 }
549
550 if (logger.isTraceEnabled()) {
551 logger.debug("End reading children and parents");
552 for (UUID uuid : childKeysSet) {
553 logger.trace("child uuid query: " + uuid);
554 }
555 for (UUID uuid : parentValuesSet) {
556 logger.trace("parent uuid query: " + uuid);
557 }
558 for (TaxonBase tb : children) {
559 logger.trace("child uuid result: " + tb.getUuid());
560 }
561 for (TaxonBase tb : parents) {
562 logger.trace("parent uuid result: " + tb.getUuid());
563 }
564 }
565
566 UUID mappedParentUuid = null;
567 UUID childUuid = null;
568
569 for (TaxonBase child : children) {
570
571 try {
572 Taxon childTaxon = child.deproxy(child, Taxon.class);
573 childUuid = childTaxon.getUuid();
574 mappedParentUuid = childParentMap.get(childUuid);
575 TaxonBase parent = null;
576
577 TaxonBase potentialParent = parentsMap.get(mappedParentUuid);
578 // for (TaxonBase potentialParent : parents ) {
579 // parentUuid = potentialParent.getUuid();
580 // if(parentUuid.equals(mappedParentUuid)) {
581 parent = potentialParent;
582 if (logger.isDebugEnabled()) {
583 logger.debug("Parent (" + mappedParentUuid + ") found for child (" + childUuid + ")");
584 }
585 // break;
586 // }
587 // }
588
589 Taxon parentTaxon = parent.deproxy(parent, Taxon.class);
590
591 if (childTaxon != null && parentTaxon != null) {
592
593 tree.addParentChild(parentTaxon, childTaxon, sourceRef, null);
594
595 if (logger.isDebugEnabled()) {
596 logger.debug("Parent-child (" + mappedParentUuid + "-" + childUuid +
597 ") relationship created");
598 }
599 if (childTaxon != null && !childSet.contains(childTaxon)) {
600
601 childSet.add(childTaxon);
602
603 if (logger.isTraceEnabled()) {
604 logger.trace("Child taxon (" + childUuid + ") added to Set");
605 }
606
607 } else {
608 if (logger.isDebugEnabled()) {
609 logger.debug("Duplicated child taxon (" + childUuid + ")");
610 }
611 }
612 } else {
613 if (logger.isDebugEnabled()) {
614 logger.debug("Parent(" + mappedParentUuid + ") or child (" + childUuid + " is null");
615 }
616 }
617
618 } catch (Exception e) {
619 logger.error("Error creating taxonomically included relationship parent-child (" +
620 mappedParentUuid + "-" + childUuid + ")", e);
621 }
622
623 }
624 if (logger.isTraceEnabled()) {
625 logger.trace("Start saving childSet");
626 }
627 getTaxonService().saveTaxonAll(childSet);
628 if (logger.isTraceEnabled()) {
629 logger.trace("End saving childSet");
630 }
631
632 parentValuesSet = null;
633 childSet = null;
634 children = null;
635 parents = null;
636 tree = null;
637
638 return success;
639 }
640
641 /* Creates misapplied name relationships.
642 * Misapplied name-accepted taxon pairs are retrieved in blocks via findByUUID(Set<UUID>) from CDM DB.
643 */
644 private boolean createMisappliedNameRelationships(FaunaEuropaeaImportState state, Map<UUID, UUID> fromToMap) {
645
646 TaxonBase taxon = getTaxonService().getTaxonByUuid(UUID.fromString("ac7b30dc-6207-4c71-9752-ee0fb838a271"));
647 ReferenceBase<?> sourceRef = taxon.getSec();
648 boolean success = true;
649 int limit = state.getConfig().getLimitSave();
650
651 Set<TaxonBase> misappliedNameSet = new HashSet<TaxonBase>(limit);
652
653 Set<UUID> misappliedNamesSet = fromToMap.keySet();
654 Set<UUID> acceptedTaxaSet = new HashSet<UUID>(fromToMap.values());
655
656 if (logger.isTraceEnabled()) {
657 logger.trace("Start reading misapplied names and accepted taxa");
658 }
659 List<TaxonBase> misappliedNames = getTaxonService().findByUuid(misappliedNamesSet);
660 List<TaxonBase> acceptedTaxa = getTaxonService().findByUuid(acceptedTaxaSet);
661 Map<UUID, TaxonBase> acceptedTaxaMap = new HashMap<UUID, TaxonBase>(acceptedTaxa.size());
662 for (TaxonBase taxonBase : acceptedTaxa){
663 acceptedTaxaMap.put(taxonBase.getUuid(), taxonBase);
664 }
665
666 if (logger.isTraceEnabled()) {
667 logger.info("End reading misapplied names and accepted taxa");
668 for (UUID uuid : misappliedNamesSet) {
669 logger.trace("misapplied name uuid query: " + uuid);
670 }
671 for (UUID uuid : acceptedTaxaSet) {
672 logger.trace("accepted taxon uuid query: " + uuid);
673 }
674 for (TaxonBase tb : misappliedNames) {
675 logger.trace("misapplied name uuid result: " + tb.getUuid());
676 }
677 for (TaxonBase tb : acceptedTaxa) {
678 logger.trace("accepted taxon uuid result: " + tb.getUuid());
679 }
680 }
681
682 UUID mappedAcceptedTaxonUuid = null;
683 UUID misappliedNameUuid = null;
684 Taxon misappliedNameTaxon = null;
685 TaxonBase acceptedTaxonBase = null;
686 Taxon acceptedTaxon = null;
687
688 for (TaxonBase misappliedName : misappliedNames) {
689
690 try {
691 misappliedNameTaxon = misappliedName.deproxy(misappliedName, Taxon.class);
692 misappliedNameUuid = misappliedNameTaxon.getUuid();
693 mappedAcceptedTaxonUuid = fromToMap.get(misappliedNameUuid);
694 acceptedTaxonBase = null;
695
696 acceptedTaxonBase = acceptedTaxaMap.get(mappedAcceptedTaxonUuid);
697 if (logger.isDebugEnabled()) {
698 logger.debug("Parent (" + mappedAcceptedTaxonUuid + ") found for child (" + misappliedNameUuid + ")");
699 }
700
701 acceptedTaxon = acceptedTaxonBase.deproxy(acceptedTaxonBase, Taxon.class);
702
703 if (misappliedNameTaxon != null && acceptedTaxon != null) {
704
705 acceptedTaxon.addMisappliedName(misappliedNameTaxon, sourceRef, null);
706
707 if (logger.isDebugEnabled()) {
708 logger.debug("Accepted taxon / misapplied name (" + mappedAcceptedTaxonUuid + "-" + misappliedNameUuid +
709 ") relationship created");
710 }
711 if (!misappliedNameSet.contains(misappliedNameTaxon)) {
712
713 misappliedNameSet.add(misappliedNameTaxon);
714
715 if (logger.isTraceEnabled()) {
716 logger.trace("Misapplied name taxon (" + misappliedNameUuid + ") added to Set");
717 }
718
719 } else {
720 if (logger.isDebugEnabled()) {
721 logger.debug("Duplicated misapplied name taxon (" + misappliedNameUuid + ")");
722 }
723 }
724 } else {
725 if (logger.isDebugEnabled()) {
726 logger.debug("Accepted taxon (" + mappedAcceptedTaxonUuid + ") or misapplied name (" + misappliedNameUuid + " is null");
727 }
728 }
729
730 if (misappliedNameTaxon != null && !misappliedNameSet.contains(misappliedNameTaxon)) {
731 misappliedNameSet.add(misappliedNameTaxon);
732 if (logger.isTraceEnabled()) {
733 logger.trace("Misapplied name taxon (" + misappliedNameUuid + ") added to Set");
734 }
735 } else {
736 if (logger.isDebugEnabled()) {
737 logger.debug("Duplicated misapplied name taxon (" + misappliedNameUuid + ")");
738 }
739 }
740
741 } catch (Exception e) {
742 logger.error("Error creating misapplied name relationship accepted taxon-misapplied name (" +
743 mappedAcceptedTaxonUuid + "-" + misappliedNameUuid + ")", e);
744 }
745
746 }
747 if (logger.isTraceEnabled()) {
748 logger.trace("Start saving misappliedNameSet");
749 }
750 getTaxonService().saveTaxonAll(misappliedNameSet);
751 if (logger.isTraceEnabled()) {
752 logger.trace("End saving misappliedNameSet");
753 }
754
755 acceptedTaxaSet = null;
756 misappliedNameSet = null;
757 misappliedNames = null;
758 acceptedTaxa = null;
759
760 return success;
761 }
762
763
764 /* Creates heterotypic synonym relationships.
765 * Synonym-accepted taxon pairs are retrieved in blocks via findByUUID(Set<UUID>) from CDM DB.
766 */
767 private boolean createHeterotypicSynonyms(FaunaEuropaeaImportState state, Map<UUID, UUID> fromToMap) {
768
769 int limit = state.getConfig().getLimitSave();
770 boolean success = true;
771
772 Set<TaxonBase> synonymSet = new HashSet<TaxonBase>(limit);
773
774 Set<UUID> synonymUuidSet = fromToMap.keySet();
775 Set<UUID> acceptedTaxaUuidSet = new HashSet<UUID>(fromToMap.values());
776
777 if (logger.isTraceEnabled()) {
778 logger.trace("Reading synonym names and accepted taxa...");
779 }
780 List<TaxonBase> synonyms = getTaxonService().findByUuid(synonymUuidSet);
781 List<TaxonBase> acceptedTaxa = getTaxonService().findByUuid(acceptedTaxaUuidSet);
782 Map<UUID, TaxonBase> acceptedTaxaMap = new HashMap<UUID, TaxonBase>(acceptedTaxa.size());
783 for (TaxonBase taxonBase : acceptedTaxa){
784 acceptedTaxaMap.put(taxonBase.getUuid(), taxonBase);
785 }
786
787 if (logger.isTraceEnabled()) {
788 logger.trace("End reading synonyms names and accepted taxa");
789 for (UUID uuid : synonymUuidSet) {
790 logger.trace("synonym uuid query: " + uuid);
791 }
792 for (UUID uuid : acceptedTaxaUuidSet) {
793 logger.trace("accepted taxon uuid query: " + uuid);
794 }
795 for (TaxonBase tb : synonyms) {
796 logger.trace("synonym uuid result: " + tb.getUuid());
797 }
798 for (TaxonBase tb : acceptedTaxa) {
799 logger.trace("accepted taxon uuid result: " + tb.getUuid());
800 }
801 }
802
803 UUID mappedAcceptedTaxonUuid = null;
804 UUID synonymUuid = null;
805 Synonym synonym = null;
806 TaxonBase acceptedTaxonBase = null;
807 Taxon acceptedTaxon = null;
808
809 for (TaxonBase synonymTaxonBase : synonyms) {
810
811 try {
812 synonym = synonymTaxonBase.deproxy(synonymTaxonBase, Synonym.class);
813 synonymUuid = synonym.getUuid();
814 mappedAcceptedTaxonUuid = fromToMap.get(synonymUuid);
815 acceptedTaxonBase = null;
816
817 acceptedTaxonBase = acceptedTaxaMap.get(mappedAcceptedTaxonUuid);
818 if (logger.isDebugEnabled()) {
819 logger.debug("Parent (" + mappedAcceptedTaxonUuid + ") found for child (" + synonymUuid + ")");
820 }
821 acceptedTaxon = acceptedTaxonBase.deproxy(acceptedTaxonBase, Taxon.class);
822
823 if (synonym != null && acceptedTaxon != null) {
824
825 //TODO: in case original genus exists must add synonym to original genus instead of to accepted taxon
826 acceptedTaxon.addSynonym(synonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
827
828 if (logger.isDebugEnabled()) {
829 logger.debug("Accepted taxon - synonym (" + mappedAcceptedTaxonUuid + " - " + synonymUuid +
830 ") relationship created");
831 }
832 if (synonym != null && !synonymSet.contains(synonym)) {
833
834 synonymSet.add(synonym);
835
836 if (logger.isTraceEnabled()) {
837 logger.trace("Synonym (" + synonymUuid + ") added to Set");
838 }
839
840 } else {
841 if (logger.isDebugEnabled()) {
842 logger.debug("Duplicated synonym (" + synonymUuid + ")");
843 }
844 }
845 } else {
846 if (logger.isDebugEnabled()) {
847 logger.debug("Accepted taxon (" + mappedAcceptedTaxonUuid + ") or misapplied name (" + synonymUuid + " is null");
848 }
849 }
850 } catch (Exception e) {
851 logger.error("Error creating synonym relationship: accepted taxon-synonym (" +
852 mappedAcceptedTaxonUuid + "-" + synonymUuid + ")", e);
853 }
854 }
855 if (logger.isTraceEnabled()) {
856 logger.trace("Start saving synonymSet");
857 }
858 getTaxonService().saveTaxonAll(synonymSet);
859 if (logger.isTraceEnabled()) {
860 logger.trace("End saving synonymSet");
861 }
862
863 acceptedTaxaUuidSet = null;
864 synonymSet = null;
865 synonyms = null;
866 acceptedTaxa = null;
867
868 return success;
869 }
870
871 }