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