fixes 777
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / model / TaxonNodeUtil.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.taxeditor.model;
11
12 import java.util.HashSet;
13 import java.util.Set;
14 import java.util.UUID;
15
16 import org.apache.log4j.Logger;
17
18 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
19 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
20 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
21 import eu.etaxonomy.cdm.model.taxon.Synonym;
22 import eu.etaxonomy.cdm.model.taxon.Taxon;
23 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
24 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
25 import eu.etaxonomy.taxeditor.store.CdmStore;
26
27 /**
28 * @author n.hoffmann
29 * @created 25.03.2009
30 * @version 1.0
31 */
32 public class TaxonNodeUtil {
33 private static final Logger logger = Logger
34 .getLogger(TaxonNodeUtil.class);
35
36 /**
37 *
38 * @param taxonNodeUuid
39 * @return
40 */
41 public static boolean deleteTaxonNodeIsolated(UUID taxonNodeUuid){
42 // get a new conversation
43 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
44
45 TaxonNode taxonNode = CdmStore.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid);
46
47 boolean result = taxonNode.remove();
48
49 // commit the conversation and throw it away
50 conversation.commit(true);
51 return result;
52 }
53
54 public static TaxonNode addChildTaxonBaseIsolated(UUID taxonNodeUuid, TaxonNameBase<?, ?> newTaxonName){
55 // disallow saving of zero length names
56 if(newTaxonName.getFullTitleCache().length() == 0){
57 return null;
58 }
59
60 // get a new conversation
61 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
62 // find the taxonBase
63 TaxonNode parentTaxonNode = CdmStore.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid);
64 // logic to add new taxon name
65 Taxon childTaxon = Taxon.NewInstance(newTaxonName, parentTaxonNode.getTaxon().getSec());
66 TaxonNode childTaxonNode = parentTaxonNode.addChild(childTaxon);
67 //not needed here: CdmStore.getTaxonService().save(parentTaxon);
68 // commit the conversation and throw it away
69 conversation.commit(true);
70 return childTaxonNode;
71 }
72
73 /**
74 *
75 * @param taxonNodeUuid
76 * @param newParentTaxonNodeUuid
77 * @return
78 */
79 public static TaxonNode moveTaxonBaseIsolated(UUID taxonNodeUuid, UUID newParentTaxonNodeUuid){
80
81 // get a new conversation
82 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
83 // find the taxon node
84 TaxonNode taxonNode = CdmStore.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid);
85 // store all relevant information of taxon node and delete the taxon node.
86 Taxon taxon = taxonNode.getTaxon();
87 ReferenceBase parentChildReference = taxonNode.getReferenceForParentChildRelation();
88 String parentChildMicroReference = taxonNode.getMicroReferenceForParentChildRelation();
89 Synonym synonymUsed = taxonNode.getSynonymToBeUsed();
90
91 taxonNode.remove();
92
93 TaxonNode newParentTaxonNode = CdmStore.getTaxonService().getTaxonNodeByUuid(newParentTaxonNodeUuid);
94 TaxonNode newChild = newParentTaxonNode.addChild(taxon, parentChildReference, parentChildMicroReference, synonymUsed);
95 // commit the conversation
96 conversation.commit(true);
97 return newChild;
98 }
99
100 /**
101 * TODO this method should be in cdm library
102 *
103 * Example:
104 * TaxonA(fromTaxon) isOverlapping TaxonB(toTaxon)
105 *
106 * @param fromTaxon
107 * @param toTaxon
108 * @return
109 * @deprecated TODO this method should be in the library
110 */
111 public static TaxonRelationship getRelationshipBetweenTwoTaxa(Taxon fromTaxon, Taxon toTaxon){
112 Set<TaxonRelationship> relations = new HashSet<TaxonRelationship>();
113
114 for(TaxonRelationship relation : fromTaxon.getRelationsFromThisTaxon()){
115 if(relation.getToTaxon().equals(toTaxon)){
116 relations.add(relation);
117 }
118 }
119
120 if(relations.size() > 1){
121 logger.warn("Found multiple relations between two taxa. Does this make sense?");
122 }
123
124 if(relations.size() == 0){
125 logger.warn("No relations between the two given taxa");
126 new Throwable("No relations between the two given taxa");
127 }
128
129 return relations.toArray(new TaxonRelationship[0])[0];
130 }
131 }