-/**
-* Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy
-* http://www.e-taxonomy.eu
-*
-* The contents of this file are subject to the Mozilla Public License Version 1.1
-* See LICENSE.TXT at the top of this package for the full license terms.
-*/
-
-package eu.etaxonomy.taxeditor.model;
-
-import java.util.HashSet;
-import java.util.Set;
-import java.util.UUID;
-
-import org.apache.log4j.Logger;
-
-import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
-import eu.etaxonomy.cdm.model.name.TaxonNameBase;
-import eu.etaxonomy.cdm.model.reference.ReferenceBase;
-import eu.etaxonomy.cdm.model.taxon.Synonym;
-import eu.etaxonomy.cdm.model.taxon.Taxon;
-import eu.etaxonomy.cdm.model.taxon.TaxonNode;
-import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
-import eu.etaxonomy.taxeditor.store.CdmStore;
-
-/**
- * @author n.hoffmann
- * @created 25.03.2009
- * @version 1.0
- */
-public class TaxonNodeUtil {
- private static final Logger logger = Logger
- .getLogger(TaxonNodeUtil.class);
-
- /**
- *
- * @param taxonNodeUuid
- * @return
- */
- public static boolean deleteTaxonNodeIsolated(UUID taxonNodeUuid){
- // get a new conversation
- ConversationHolder conversation = CdmStore.NewTransactionalConversation();
-
- TaxonNode taxonNode = CdmStore.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid);
-
- boolean result = taxonNode.remove();
-
- // commit the conversation and throw it away
- conversation.commit(true);
- return result;
- }
-
- public static TaxonNode addChildTaxonBaseIsolated(UUID taxonNodeUuid, TaxonNameBase<?, ?> newTaxonName){
- // disallow saving of zero length names
- if(newTaxonName.getFullTitleCache().length() == 0){
- return null;
- }
-
- // get a new conversation
- ConversationHolder conversation = CdmStore.NewTransactionalConversation();
- // find the taxonBase
- TaxonNode parentTaxonNode = CdmStore.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid);
- // logic to add new taxon name
- Taxon childTaxon = Taxon.NewInstance(newTaxonName, parentTaxonNode.getTaxon().getSec());
- TaxonNode childTaxonNode = parentTaxonNode.addChild(childTaxon);
- //not needed here: CdmStore.getTaxonService().save(parentTaxon);
- // commit the conversation and throw it away
- conversation.commit(true);
- return childTaxonNode;
- }
-
- /**
- *
- * @param taxonNodeUuid
- * @param newParentTaxonNodeUuid
- * @return
- */
- public static TaxonNode moveTaxonBaseIsolated(UUID taxonNodeUuid, UUID newParentTaxonNodeUuid){
-
- // get a new conversation
- ConversationHolder conversation = CdmStore.NewTransactionalConversation();
- // find the taxon node
- TaxonNode taxonNode = CdmStore.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid);
- // store all relevant information of taxon node and delete the taxon node.
- Taxon taxon = taxonNode.getTaxon();
- ReferenceBase parentChildReference = taxonNode.getReferenceForParentChildRelation();
- String parentChildMicroReference = taxonNode.getMicroReferenceForParentChildRelation();
- Synonym synonymUsed = taxonNode.getSynonymToBeUsed();
-
- taxonNode.remove();
-
- TaxonNode newParentTaxonNode = CdmStore.getTaxonService().getTaxonNodeByUuid(newParentTaxonNodeUuid);
- TaxonNode newChild = newParentTaxonNode.addChild(taxon, parentChildReference, parentChildMicroReference, synonymUsed);
- // commit the conversation
- conversation.commit(true);
- return newChild;
- }
-
- /**
- * TODO this method should be in cdm library
- *
- * Example:
- * TaxonA(fromTaxon) isOverlapping TaxonB(toTaxon)
- *
- * @param fromTaxon
- * @param toTaxon
- * @return
- * @deprecated TODO this method should be in the library
- */
- public static TaxonRelationship getRelationshipBetweenTwoTaxa(Taxon fromTaxon, Taxon toTaxon){
- Set<TaxonRelationship> relations = new HashSet<TaxonRelationship>();
-
- for(TaxonRelationship relation : fromTaxon.getRelationsFromThisTaxon()){
- if(relation.getToTaxon().equals(toTaxon)){
- relations.add(relation);
- }
- }
-
- if(relations.size() > 1){
- logger.warn("Found multiple relations between two taxa. Does this make sense?");
- }
-
- if(relations.size() == 0){
- logger.warn("No relations between the two given taxa");
- new Throwable("No relations between the two given taxa");
- }
-
- return relations.toArray(new TaxonRelationship[0])[0];
- }
-}