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
.store
.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
.taxon
.Taxon
;
21 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
22 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
23 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
30 public class TaxonUtil
{
31 private static final Logger logger
= Logger
32 .getLogger(TaxonUtil
.class);
36 * TODO error handling. throw exceptions if something goes wrong
41 public static boolean deleteTaxonBaseIsolated(UUID taxonUuid
){
42 // get a new conversation
43 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
45 TaxonBase
<?
> taxonBase
= CdmStore
.getTaxonService().findByUuid(taxonUuid
);
46 // delete the taxonBase
47 CdmStore
.getTaxonService().delete(taxonBase
);
48 // commit the conversation and throw it away
49 conversation
.commit(true);
53 public static Taxon
addChildTaxonBaseIsolated(UUID taxonUuid
, TaxonNameBase
<?
, ?
> newTaxonName
){
54 // disallow saving of zero length names
55 if(newTaxonName
.getFullTitleCache().length() == 0){
59 // get a new conversation
60 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
62 Taxon parentTaxon
= (Taxon
) CdmStore
.getTaxonService().findByUuid(taxonUuid
);
63 // logic to add new taxon name
64 Taxon childTaxon
= Taxon
.NewInstance(newTaxonName
, parentTaxon
.getSec());
65 parentTaxon
.addTaxonomicChild(childTaxon
, null, null);
66 //not needed here: CdmStore.getTaxonService().save(parentTaxon);
67 // commit the conversation and throw it away
68 conversation
.commit(true);
72 public static boolean moveTaxonBaseIsolated(UUID taxonUuid
, UUID newParentTaxonUuid
){
74 // get a new conversation
75 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
76 // find the taxonBases
77 Taxon taxon
= (Taxon
) CdmStore
.getTaxonService().findByUuid(taxonUuid
);
78 Taxon newParentTaxon
= (Taxon
) CdmStore
.getTaxonService().findByUuid(newParentTaxonUuid
);
80 Taxon oldParent
= taxon
.getTaxonomicParent();
81 if (oldParent
!= null) {
82 oldParent
.removeTaxonomicChild(taxon
);
85 newParentTaxon
.addTaxonomicChild(taxon
, null, null);
86 conversation
.commit(true);
91 * TODO this method should be in cdm library
94 * TaxonA(fromTaxon) isOverlapping TaxonB(toTaxon)
99 * @deprecated TODO this method should be in the library
101 public static TaxonRelationship
getRelationshipBetweenTwoTaxa(Taxon fromTaxon
, Taxon toTaxon
){
102 Set
<TaxonRelationship
> relations
= new HashSet
<TaxonRelationship
>();
104 for(TaxonRelationship relation
: fromTaxon
.getRelationsFromThisTaxon()){
105 if(relation
.getToTaxon().equals(toTaxon
)){
106 relations
.add(relation
);
110 if(relations
.size() > 1){
111 logger
.warn("Found multiple relations between two taxa. Does this make sense?");
114 if(relations
.size() == 0){
115 logger
.warn("No relations between the two given taxa");
116 new Throwable("No relations between the two given taxa");
119 return relations
.toArray(new TaxonRelationship
[0])[0];
123 * Returns true if <code>child</code> belongs to the children, grandchildren,
124 * etc. of <code>parent</code>.
130 public static boolean isTaxonChildOfTaxon(Taxon child
, Taxon parent
) {
132 // Traverse all checkTaxon's children
133 for (Taxon childTaxon
: parent
.getTaxonomicChildren()) {
135 if (childTaxon
.equals(child
)) {
139 // Compare taxon with childTaxon's children
140 if (isTaxonChildOfTaxon(child
, childTaxon
)) {