2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.model
;
12 import java
.util
.HashSet
;
14 import java
.util
.UUID
;
16 import org
.apache
.log4j
.Logger
;
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
;
32 public class TaxonNodeUtil
{
33 private static final Logger logger
= Logger
34 .getLogger(TaxonNodeUtil
.class);
38 * @param taxonNodeUuid
41 public static boolean deleteTaxonNodeIsolated(UUID taxonNodeUuid
){
42 // get a new conversation
43 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
45 TaxonNode taxonNode
= CdmStore
.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid
);
47 boolean result
= taxonNode
.remove();
49 // commit the conversation and throw it away
50 conversation
.commit(true);
54 public static TaxonNode
addChildTaxonBaseIsolated(UUID taxonNodeUuid
, TaxonNameBase
<?
, ?
> newTaxonName
){
55 // disallow saving of zero length names
56 if(newTaxonName
.getFullTitleCache().length() == 0){
60 // get a new conversation
61 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
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
;
75 * @param taxonNodeUuid
76 * @param newParentTaxonNodeUuid
79 public static TaxonNode
moveTaxonBaseIsolated(UUID taxonNodeUuid
, UUID newParentTaxonNodeUuid
){
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();
93 TaxonNode newParentTaxonNode
= CdmStore
.getTaxonService().getTaxonNodeByUuid(newParentTaxonNodeUuid
);
94 TaxonNode newChild
= newParentTaxonNode
.addChild(taxon
, parentChildReference
, parentChildMicroReference
, synonymUsed
);
95 // commit the conversation
96 conversation
.commit(true);
101 * TODO this method should be in cdm library
104 * TaxonA(fromTaxon) isOverlapping TaxonB(toTaxon)
109 * @deprecated TODO this method should be in the library
111 public static TaxonRelationship
getRelationshipBetweenTwoTaxa(Taxon fromTaxon
, Taxon toTaxon
){
112 Set
<TaxonRelationship
> relations
= new HashSet
<TaxonRelationship
>();
114 for(TaxonRelationship relation
: fromTaxon
.getRelationsFromThisTaxon()){
115 if(relation
.getToTaxon().equals(toTaxon
)){
116 relations
.add(relation
);
120 if(relations
.size() > 1){
121 logger
.warn("Found multiple relations between two taxa. Does this make sense?");
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");
129 return relations
.toArray(new TaxonRelationship
[0])[0];