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