.
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / handler / ChangeAcceptedTaxonToSynonymHandler.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.editor.name.handler;
5
6 import java.util.HashSet;
7 import java.util.Set;
8 import java.util.UUID;
9
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;
21
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;
35
36 /**
37 * @author p.ciardelli
38 *
39 */
40 public class ChangeAcceptedTaxonToSynonymHandler extends AbstractHandler
41 implements IHandler {
42 private static final Logger logger = Logger
43 .getLogger(ChangeAcceptedTaxonToSynonymHandler.class);
44
45 /* (non-Javadoc)
46 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
47 */
48 public Object execute(ExecutionEvent event) throws ExecutionException {
49
50 // MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
51 // "Not yet implemented",
52 // "'Change Accepted Taxon to Synonym' not yet implemented.");
53
54 ISelection menuSelection = HandlerUtil.getActiveMenuSelection(event);
55 TaxonBasePropertySource taxonBasePropertySource =
56 (TaxonBasePropertySource) ((StructuredSelection) menuSelection).getFirstElement();
57 Taxon oldAcceptedTaxon = (Taxon) taxonBasePropertySource.getTaxonBase();
58
59 // Choose new accepted taxon
60 Taxon newAcceptedTaxon = FilteredTaxonSelectionDialog.getTaxon(HandlerUtil.getActiveShell(event), oldAcceptedTaxon);
61
62 if (newAcceptedTaxon == null) {
63 return null;
64 }
65
66 try {
67 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
68 conversation.bind();
69 Taxon newTaxon = (Taxon) CdmStore.getTaxonService().getTaxonByUuid(newAcceptedTaxon.getUuid());
70 Taxon oldTaxon = (Taxon) CdmStore.getTaxonService().getTaxonByUuid(oldAcceptedTaxon.getUuid());
71
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.");
77 return null;
78 }
79
80 // Close editor
81 IEditorPart editor = HandlerUtil.getActiveEditor(event);
82 boolean proceed = HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage().
83 closeEditor(editor, true);
84 if (!proceed) {
85 return null;
86 }
87
88 makeTaxonSynonym(oldTaxon, newTaxon, null, null, null);
89
90 // TEMP
91 // Set<TaxonDescription> descriptions = new HashSet<TaxonDescription>();
92 // for(TaxonDescription taxDescription : oldTaxon.getDescriptions()){
93 // descriptions.add(taxDescription);
94 // }
95 // for(TaxonDescription taxDescription : descriptions){
96 // newTaxon.addDescription(taxDescription);
97 // }
98
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();
103
104 EditorUtil.open(uuid);
105
106 } catch (PartInitException e) {
107 // TODO Auto-generated catch block
108 e.printStackTrace();
109 }
110
111 return null;
112 }
113
114 /**
115 * @param oldTaxon
116 * @param newTaxon
117 */
118 private void makeTaxonSynonym(Taxon oldTaxon, Taxon newAcceptedTaxon, SynonymRelationshipType synonymType, ReferenceBase citation, String citationMicroReference) {
119 if (oldTaxon == null || newAcceptedTaxon == null || oldTaxon.getName() == null){
120 return;
121 }
122
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();
128 }else{
129 //TODO synonymType
130 synonymType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
131 }
132 }
133 SynonymRelationship synRel = newAcceptedTaxon.addSynonymName(synonymName, synonymType, citation, citationMicroReference);
134
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());
140 }
141
142 //Move Taxon RelationShips to new Taxon
143 Set<TaxonRelationship> removableTaxonRels = new HashSet<TaxonRelationship>();
144 for(TaxonRelationship taxonRelation : oldTaxon.getTaxonRelations()){
145 //CHILDREN
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);
154 }else{
155 logger.warn("Taxon is not part of its own Taxonrelationship");
156 }
157 }
158 //MISAPPLIED NAMES
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);
168 }else{
169 logger.warn("Taxon is not part of its own Taxonrelationship");
170 }
171 }
172 //Concept Relationships
173 //FIXME implement
174 }
175
176 // for(TaxonRelationship taxonRel : removableTaxonRels) {
177 // oldTaxon.removeTaxonRelation(taxonRel);
178 // }
179
180 //Move Descriptions to new Taxon
181 Set<TaxonDescription> descriptions = new HashSet<TaxonDescription>();
182 for(TaxonDescription taxDescription : oldTaxon.getDescriptions()){
183 descriptions.add(taxDescription);
184 }
185 for(TaxonDescription taxDescription : descriptions){
186 newAcceptedTaxon.addDescription(taxDescription);
187 }
188
189 CdmStore.getTaxonService().saveTaxon(newAcceptedTaxon);
190 oldTaxon.getTaxonNodes().iterator().next().remove();
191 CdmStore.getTaxonService().removeTaxon(oldTaxon);
192 }
193
194 // /**
195 // * @param oldTaxon
196 // * @param newTaxon
197 // */
198 // private void makeTaxonSynonym(Taxon oldTaxon, Taxon newAcceptedTaxon, SynonymRelationshipType synonymType, ReferenceBase citation, String citationMicroReference) {
199 // if (oldTaxon == null || newAcceptedTaxon == null || oldTaxon.getName() == null){
200 // return;
201 // }
202 //
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();
208 // }else{
209 // //TODO synonymType
210 // synonymType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
211 // }
212 // }
213 // SynonymRelationship synRel = newAcceptedTaxon.addSynonymName(synonymName, synonymType, citation, citationMicroReference);
214 //
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());
219 // }
220 //
221 // //Move Taxon RelationShips to new Taxon
222 // Set<TaxonRelationship> removableTaxonRels = new HashSet<TaxonRelationship>();
223 // for(TaxonRelationship taxonRelation : oldTaxon.getTaxonRelations()){
224 // //CHILDREN
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);
233 // }else{
234 // logger.warn("Taxon is not part of its own Taxonrelationship");
235 // }
236 // }
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);
247 // }else{
248 // logger.warn("Taxon is not part of its own Taxonrelationship");
249 // }
250 // }
251 // //Concept Relationships
252 // //FIXME implement
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);
260 //// }else{
261 //// logger.warn("Taxon is not part of its own Taxonrelationship");
262 //// }
263 //// }
264 // }
265 //
266 // for(TaxonRelationship taxonRel : removableTaxonRels) {
267 // oldTaxon.removeTaxonRelation(taxonRel);
268 // }
269 //
270 // //Move Descriptions to new Taxon
271 // Set<TaxonDescription> descriptions = new HashSet<TaxonDescription>();
272 // for(TaxonDescription taxDescription : oldTaxon.getDescriptions()){
273 // descriptions.add(taxDescription);
274 // }
275 // for(TaxonDescription taxDescription : descriptions){
276 // newAcceptedTaxon.addDescription(taxDescription);
277 // }
278 //
279 // CdmStore.getTaxonService().saveTaxon(newAcceptedTaxon);
280 //// CdmStore.getTaxonService().removeTaxon(oldTaxon);
281 // }
282 }