4 package eu
.etaxonomy
.taxeditor
.editor
.name
.handler
;
6 import java
.util
.ArrayList
;
7 import java
.util
.HashSet
;
10 import java
.util
.UUID
;
12 import org
.apache
.log4j
.Logger
;
13 import org
.eclipse
.core
.commands
.AbstractHandler
;
14 import org
.eclipse
.core
.commands
.ExecutionEvent
;
15 import org
.eclipse
.core
.commands
.ExecutionException
;
16 import org
.eclipse
.core
.commands
.IHandler
;
17 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
18 import org
.eclipse
.jface
.viewers
.ISelection
;
19 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
20 import org
.eclipse
.ui
.IEditorPart
;
21 import org
.eclipse
.ui
.IWorkbenchPage
;
22 import org
.eclipse
.ui
.PartInitException
;
23 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
26 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
27 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
28 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationship
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
35 import eu
.etaxonomy
.taxeditor
.dialogs
.filteredSelection
.FilteredTaxonNodeSelectionDialog
;
36 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
37 import eu
.etaxonomy
.taxeditor
.propertysheet
.name
.TaxonBasePropertySource
;
38 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
44 public class ChangeAcceptedTaxonToSynonymHandler
extends AbstractHandler
46 private static final Logger logger
= Logger
47 .getLogger(ChangeAcceptedTaxonToSynonymHandler
.class);
50 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
52 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
53 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
56 ISelection menuSelection
= HandlerUtil
.getActiveMenuSelection(event
);
57 TaxonBasePropertySource taxonBasePropertySource
=
58 (TaxonBasePropertySource
) ((StructuredSelection
) menuSelection
).getFirstElement();
59 Taxon oldAcceptedTaxon
= (Taxon
) taxonBasePropertySource
.getTaxonBase();
61 // Retrieve active editor and active page for later use
62 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
63 IWorkbenchPage activePage
= HandlerUtil
.getActiveWorkbenchWindowChecked(event
).getActivePage();
65 // Choose new accepted taxon
66 List
<UUID
> excludeTaxa
= new ArrayList
<UUID
>();
67 excludeTaxa
.add(oldAcceptedTaxon
.getUuid());
68 Taxon newAcceptedTaxon
= FilteredTaxonNodeSelectionDialog
.selectTaxonNode(HandlerUtil
.getActiveShell(event
), "Choose the accepted taxon", excludeTaxa
, null).getTaxon();
71 if (newAcceptedTaxon
== null) {
77 Taxon newTaxon
= (Taxon
) CdmStore
.getTaxonService().load(newAcceptedTaxon
.getUuid());
78 Taxon oldTaxon
= (Taxon
) CdmStore
.getTaxonService().load(oldAcceptedTaxon
.getUuid());
80 // Check for multiple nodes
81 if (newTaxon
.getTaxonNodes().size() > 1 || oldTaxon
.getTaxonNodes().size() > 1) {
82 MessageDialog
.openInformation(HandlerUtil
.getActiveShell(event
),
83 "Taxon implemented in multiple trees",
84 "One or both of these taxa is implemented in multiple taxonomic trees.");
89 boolean proceed
= activePage
.closeEditor(editor
, true);
95 makeTaxonSynonym(oldTaxon
, newTaxon
, null, null, null);
97 conversation
.commit();
98 Set
<TaxonNode
> nodes
= newTaxon
.getTaxonNodes();
99 UUID uuid
= nodes
.iterator().next().getUuid();
101 EditorUtil
.open(uuid
);
103 } catch (PartInitException e
) {
105 throw new RuntimeException(e
);
110 conversation
.close();
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
);
140 //Move Taxon RelationShips to new Taxon
141 Set
<TaxonRelationship
> removableTaxonRels
= new HashSet
<TaxonRelationship
>();
142 for(TaxonRelationship taxonRelation
: oldTaxon
.getTaxonRelations()){
144 if (taxonRelation
.getType().equals(TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN())){
145 if (taxonRelation
.getFromTaxon() == oldTaxon
){
146 taxonRelation
.setFromTaxon(newAcceptedTaxon
);
147 }else if(taxonRelation
.getToTaxon() == oldTaxon
){
148 taxonRelation
.setToTaxon(newAcceptedTaxon
);
150 logger
.warn("Taxon is not part of its own Taxonrelationship");
154 if (taxonRelation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
155 if (taxonRelation
.getFromTaxon() == oldTaxon
){
156 taxonRelation
.setFromTaxon(newAcceptedTaxon
);
157 }else if(taxonRelation
.getToTaxon() == oldTaxon
){
158 taxonRelation
.setToTaxon(newAcceptedTaxon
);
160 logger
.warn("Taxon is not part of its own Taxonrelationship");
163 //Concept Relationships
165 logger
.warn("Transfer of concept relationships not implemented yet");
169 //Move Descriptions to new Taxon
170 Set
<TaxonDescription
> descriptions
= new HashSet
<TaxonDescription
>();
171 for(TaxonDescription taxDescription
: oldTaxon
.getDescriptions()){
172 descriptions
.add(taxDescription
);
174 for(TaxonDescription taxDescription
: descriptions
){
175 newAcceptedTaxon
.addDescription(taxDescription
);
179 oldTaxon
.getTaxonNodes().iterator().next().delete();