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.store.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.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;
|
24
|
|
25
|
/**
|
26
|
* @author n.hoffmann
|
27
|
* @created 25.03.2009
|
28
|
* @version 1.0
|
29
|
*/
|
30
|
public class TaxonUtil {
|
31
|
private static final Logger logger = Logger
|
32
|
.getLogger(TaxonUtil.class);
|
33
|
|
34
|
|
35
|
/**
|
36
|
* TODO error handling. throw exceptions if something goes wrong
|
37
|
*
|
38
|
* @param taxonUuid
|
39
|
* @return
|
40
|
*/
|
41
|
public static boolean deleteTaxonBaseIsolated(UUID taxonUuid){
|
42
|
// get a new conversation
|
43
|
ConversationHolder conversation = CdmStore.NewTransactionalConversation();
|
44
|
// find the taxonBase
|
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);
|
50
|
return true;
|
51
|
}
|
52
|
|
53
|
public static Taxon addChildTaxonBaseIsolated(UUID taxonUuid, TaxonNameBase newTaxonName){
|
54
|
// disallow saving of zero length names
|
55
|
if(newTaxonName.getFullTitleCache().length() == 0){
|
56
|
return null;
|
57
|
}
|
58
|
|
59
|
// get a new conversation
|
60
|
ConversationHolder conversation = CdmStore.NewTransactionalConversation();
|
61
|
// find the taxonBase
|
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);
|
69
|
return childTaxon;
|
70
|
}
|
71
|
|
72
|
public static boolean moveTaxonBaseIsolated(UUID taxonUuid, UUID newParentTaxonUuid){
|
73
|
|
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);
|
79
|
|
80
|
Taxon oldParent = taxon.getTaxonomicParent();
|
81
|
if (oldParent != null) {
|
82
|
oldParent.removeTaxonomicChild(taxon);
|
83
|
}
|
84
|
|
85
|
newParentTaxon.addTaxonomicChild(taxon, null, null);
|
86
|
conversation.commit(true);
|
87
|
return true;
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* TODO this method should be in cdm library
|
92
|
*
|
93
|
* Example:
|
94
|
* TaxonA(fromTaxon) isOverlapping TaxonB(toTaxon)
|
95
|
*
|
96
|
* @param fromTaxon
|
97
|
* @param toTaxon
|
98
|
* @return
|
99
|
* @deprecated TODO this method should be in the library
|
100
|
*/
|
101
|
public static TaxonRelationship getRelationshipBetweenTwoTaxa(Taxon fromTaxon, Taxon toTaxon){
|
102
|
Set<TaxonRelationship> relations = new HashSet<TaxonRelationship>();
|
103
|
|
104
|
for(TaxonRelationship relation : fromTaxon.getRelationsFromThisTaxon()){
|
105
|
if(relation.getToTaxon().equals(toTaxon)){
|
106
|
relations.add(relation);
|
107
|
}
|
108
|
}
|
109
|
|
110
|
if(relations.size() > 1){
|
111
|
logger.warn("Found multiple relations between two taxa. Does this make sense?");
|
112
|
}
|
113
|
|
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");
|
117
|
}
|
118
|
|
119
|
return relations.toArray(new TaxonRelationship[0])[0];
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Returns true if <code>child</code> belongs to the children, grandchildren,
|
124
|
* etc. of <code>parent</code>.
|
125
|
*
|
126
|
* @param child
|
127
|
* @param parent
|
128
|
* @return
|
129
|
*/
|
130
|
public static boolean isTaxonChildOfTaxon(Taxon child, Taxon parent) {
|
131
|
|
132
|
// Traverse all checkTaxon's children
|
133
|
for (Taxon childTaxon : parent.getTaxonomicChildren()) {
|
134
|
|
135
|
if (childTaxon.equals(child)) {
|
136
|
return true;
|
137
|
} else {
|
138
|
|
139
|
// Compare taxon with childTaxon's children
|
140
|
if (isTaxonChildOfTaxon(child, childTaxon)) {
|
141
|
return true;
|
142
|
}
|
143
|
}
|
144
|
}
|
145
|
return false;
|
146
|
}
|
147
|
}
|