4 package eu
.etaxonomy
.taxeditor
.editor
.name
.handler
;
6 import java
.util
.HashSet
;
10 import org
.apache
.log4j
.Logger
;
11 import org
.eclipse
.core
.commands
.AbstractHandler
;
12 import org
.eclipse
.core
.commands
.ExecutionEvent
;
13 import org
.eclipse
.core
.commands
.ExecutionException
;
14 import org
.eclipse
.core
.commands
.IHandler
;
15 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
16 import org
.eclipse
.jface
.viewers
.ISelection
;
17 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
18 import org
.eclipse
.ui
.IEditorPart
;
19 import org
.eclipse
.ui
.PartInitException
;
20 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
22 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
23 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
24 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
25 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationship
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
32 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
33 import eu
.etaxonomy
.taxeditor
.propertysheet
.name
.TaxonBasePropertySource
;
34 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
40 public class ChangeAcceptedTaxonToSynonymHandler
extends AbstractHandler
42 private static final Logger logger
= Logger
43 .getLogger(ChangeAcceptedTaxonToSynonymHandler
.class);
46 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
48 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
50 // MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
51 // "Not yet implemented",
52 // "'Change Accepted Taxon to Synonym' not yet implemented.");
54 ISelection menuSelection
= HandlerUtil
.getActiveMenuSelection(event
);
55 TaxonBasePropertySource taxonBasePropertySource
=
56 (TaxonBasePropertySource
) ((StructuredSelection
) menuSelection
).getFirstElement();
57 Taxon oldAcceptedTaxon
= (Taxon
) taxonBasePropertySource
.getTaxonBase();
59 // Choose new accepted taxon
60 Taxon newAcceptedTaxon
= FilteredTaxonSelectionDialog
.getTaxon(HandlerUtil
.getActiveShell(event
), oldAcceptedTaxon
);
62 if (newAcceptedTaxon
== null) {
67 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
69 Taxon newTaxon
= (Taxon
) CdmStore
.getTaxonService().getTaxonByUuid(newAcceptedTaxon
.getUuid());
70 Taxon oldTaxon
= (Taxon
) CdmStore
.getTaxonService().getTaxonByUuid(oldAcceptedTaxon
.getUuid());
72 // Check for multiple nodes
73 if (newTaxon
.getTaxonNodes().size() > 1 || oldTaxon
.getTaxonNodes().size() > 1) {
74 MessageDialog
.openInformation(HandlerUtil
.getActiveShell(event
),
75 "Taxon implemented in multiple trees",
76 "One or both of these taxa is implemented in multiple taxonomic trees.");
81 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
82 boolean proceed
= HandlerUtil
.getActiveWorkbenchWindowChecked(event
).getActivePage().
83 closeEditor(editor
, true);
88 makeTaxonSynonym(oldTaxon
, newTaxon
, null, null, null);
91 // Set<TaxonDescription> descriptions = new HashSet<TaxonDescription>();
92 // for(TaxonDescription taxDescription : oldTaxon.getDescriptions()){
93 // descriptions.add(taxDescription);
95 // for(TaxonDescription taxDescription : descriptions){
96 // newTaxon.addDescription(taxDescription);
99 // CdmStore.getTaxonService().makeTaxonSynonym(oldTaxon, newTaxon, null, null, null);
100 conversation
.commit();
101 Set
<TaxonNode
> nodes
= newTaxon
.getTaxonNodes();
102 UUID uuid
= nodes
.iterator().next().getUuid();
104 EditorUtil
.open(uuid
);
106 } catch (PartInitException e
) {
107 // TODO Auto-generated catch block
118 private void makeTaxonSynonym(Taxon oldTaxon
, Taxon newAcceptedTaxon
, SynonymRelationshipType synonymType
, ReferenceBase citation
, String citationMicroReference
) {
119 if (oldTaxon
== null || newAcceptedTaxon
== null || oldTaxon
.getName() == null){
123 // Move oldTaxon to newTaxon
124 TaxonNameBase
<?
,?
> synonymName
= oldTaxon
.getName();
125 if (synonymType
== null){
126 if (synonymName
.isHomotypic(newAcceptedTaxon
.getName())){
127 synonymType
= SynonymRelationshipType
.HOMOTYPIC_SYNONYM_OF();
130 synonymType
= SynonymRelationshipType
.HETEROTYPIC_SYNONYM_OF();
133 SynonymRelationship synRel
= newAcceptedTaxon
.addSynonymName(synonymName
, synonymType
, citation
, citationMicroReference
);
135 //Move Synonym Relations to new Taxon
136 for(SynonymRelationship synRelation
: oldTaxon
.getSynonymRelations()){
137 synRelation
.setAcceptedTaxon(newAcceptedTaxon
);
138 // newAcceptedTaxon.addSynonym(synRelation.getSynonym(), synRelation.getType(),
139 // synRelation.getCitation(), synRelation.getCitationMicroReference());
142 //Move Taxon RelationShips to new Taxon
143 Set
<TaxonRelationship
> removableTaxonRels
= new HashSet
<TaxonRelationship
>();
144 for(TaxonRelationship taxonRelation
: oldTaxon
.getTaxonRelations()){
146 if (taxonRelation
.getType().equals(TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN())){
147 if (taxonRelation
.getFromTaxon() == oldTaxon
){
148 taxonRelation
.setFromTaxon(newAcceptedTaxon
);
149 // removableTaxonRels.add(taxonRelation);
150 }else if(taxonRelation
.getToTaxon() == oldTaxon
){
151 taxonRelation
.setToTaxon(newAcceptedTaxon
);
152 // newAcceptedTaxon.addTaxonomicChild(taxonRelation.getFromTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
153 // removableTaxonRels.add(taxonRelation);
155 logger
.warn("Taxon is not part of its own Taxonrelationship");
159 if (taxonRelation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
160 if (taxonRelation
.getFromTaxon() == oldTaxon
){
161 taxonRelation
.setFromTaxon(newAcceptedTaxon
);
162 // newAcceptedTaxon.addMisappliedName(taxonRelation.getToTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
163 // removableTaxonRels.add(taxonRelation);
164 }else if(taxonRelation
.getToTaxon() == oldTaxon
){
165 taxonRelation
.setToTaxon(newAcceptedTaxon
);
166 // newAcceptedTaxon.addMisappliedName(taxonRelation.getFromTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
167 // removableTaxonRels.add(taxonRelation);
169 logger
.warn("Taxon is not part of its own Taxonrelationship");
172 //Concept Relationships
176 // for(TaxonRelationship taxonRel : removableTaxonRels) {
177 // oldTaxon.removeTaxonRelation(taxonRel);
180 //Move Descriptions to new Taxon
181 Set
<TaxonDescription
> descriptions
= new HashSet
<TaxonDescription
>();
182 for(TaxonDescription taxDescription
: oldTaxon
.getDescriptions()){
183 descriptions
.add(taxDescription
);
185 for(TaxonDescription taxDescription
: descriptions
){
186 newAcceptedTaxon
.addDescription(taxDescription
);
189 CdmStore
.getTaxonService().saveTaxon(newAcceptedTaxon
);
190 oldTaxon
.getTaxonNodes().iterator().next().remove();
191 CdmStore
.getTaxonService().removeTaxon(oldTaxon
);
198 // private void makeTaxonSynonym(Taxon oldTaxon, Taxon newAcceptedTaxon, SynonymRelationshipType synonymType, ReferenceBase citation, String citationMicroReference) {
199 // if (oldTaxon == null || newAcceptedTaxon == null || oldTaxon.getName() == null){
203 // // Move oldTaxon to newTaxon
204 // TaxonNameBase<?,?> synonymName = oldTaxon.getName();
205 // if (synonymType == null){
206 // if (synonymName.isHomotypic(newAcceptedTaxon.getName())){
207 // synonymType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
209 // //TODO synonymType
210 // synonymType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
213 // SynonymRelationship synRel = newAcceptedTaxon.addSynonymName(synonymName, synonymType, citation, citationMicroReference);
215 // //Move Synonym Relations to new Taxon
216 // for(SynonymRelationship synRelation : oldTaxon.getSynonymRelations()){
217 // newAcceptedTaxon.addSynonym(synRelation.getSynonym(), synRelation.getType(),
218 // synRelation.getCitation(), synRelation.getCitationMicroReference());
221 // //Move Taxon RelationShips to new Taxon
222 // Set<TaxonRelationship> removableTaxonRels = new HashSet<TaxonRelationship>();
223 // for(TaxonRelationship taxonRelation : oldTaxon.getTaxonRelations()){
225 // if (taxonRelation.getType().equals(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN())){
226 // if (taxonRelation.getFromTaxon() == oldTaxon){
227 // removableTaxonRels.add(taxonRelation);
228 //// oldTaxon.removeTaxonRelation(taxonRelation);
229 // }else if(taxonRelation.getToTaxon() == oldTaxon){
230 // newAcceptedTaxon.addTaxonomicChild(taxonRelation.getFromTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
231 // removableTaxonRels.add(taxonRelation);
232 //// oldTaxon.removeTaxonRelation(taxonRelation);
234 // logger.warn("Taxon is not part of its own Taxonrelationship");
237 // //MISAPPLIED NAMES
238 // if (taxonRelation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
239 // if (taxonRelation.getFromTaxon() == oldTaxon){
240 // newAcceptedTaxon.addMisappliedName(taxonRelation.getToTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
241 // removableTaxonRels.add(taxonRelation);
242 //// oldTaxon.removeTaxonRelation(taxonRelation);
243 // }else if(taxonRelation.getToTaxon() == oldTaxon){
244 // newAcceptedTaxon.addMisappliedName(taxonRelation.getFromTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
245 // removableTaxonRels.add(taxonRelation);
246 //// oldTaxon.removeTaxonRelation(taxonRelation);
248 // logger.warn("Taxon is not part of its own Taxonrelationship");
251 // //Concept Relationships
253 //// if (taxonRelation.getType().equals(TaxonRelationshipType.MISAPPLIEDNAMEFOR())){
254 //// if (taxonRelation.getFromTaxon() == oldTaxon){
255 //// newAcceptedTaxon.addMisappliedName(taxonRelation.getToTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
256 //// removableTaxonRels.add(taxonRelation);
257 //// }else if(taxonRelation.getToTaxon() == oldTaxon){
258 //// newAcceptedTaxon.addMisappliedName(taxonRelation.getFromTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
259 //// removableTaxonRels.add(taxonRelation);
261 //// logger.warn("Taxon is not part of its own Taxonrelationship");
266 // for(TaxonRelationship taxonRel : removableTaxonRels) {
267 // oldTaxon.removeTaxonRelation(taxonRel);
270 // //Move Descriptions to new Taxon
271 // Set<TaxonDescription> descriptions = new HashSet<TaxonDescription>();
272 // for(TaxonDescription taxDescription : oldTaxon.getDescriptions()){
273 // descriptions.add(taxDescription);
275 // for(TaxonDescription taxDescription : descriptions){
276 // newAcceptedTaxon.addDescription(taxDescription);
279 // CdmStore.getTaxonService().saveTaxon(newAcceptedTaxon);
280 //// CdmStore.getTaxonService().removeTaxon(oldTaxon);