Merge branch 'develop' into nameEditorE4
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / handler / DeleteTaxonBaseHandler.java
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.editor.name.handler;
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.core.commands.common.NotDefinedException;
16 import org.eclipse.ui.handlers.HandlerUtil;
17
18 import eu.etaxonomy.cdm.api.service.DeleteResult;
19 import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
20 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
21 import eu.etaxonomy.cdm.model.common.CdmBase;
22 import eu.etaxonomy.cdm.model.taxon.Synonym;
23 import eu.etaxonomy.cdm.model.taxon.Taxon;
24 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
25 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
26 import eu.etaxonomy.taxeditor.editor.EditorUtil;
27 import eu.etaxonomy.taxeditor.editor.Page;
28 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
29 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
30 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
31 import eu.etaxonomy.taxeditor.editor.name.operation.DeleteMisapplicationOperation;
32 import eu.etaxonomy.taxeditor.editor.name.operation.DeleteSynonymOperation;
33 import eu.etaxonomy.taxeditor.editor.name.operation.DeleteTaxonBaseOperation;
34 import eu.etaxonomy.taxeditor.model.AbstractUtility;
35 import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
36 import eu.etaxonomy.taxeditor.model.MessagingUtils;
37 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
38 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
39 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
40 import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
41
42 /**
43 * @author n.hoffmann
44 * @created 21.04.2009
45 * @version 1.0
46 */
47 public class DeleteTaxonBaseHandler extends AbstractHandler implements IHandler, IPostOperationEnabled {
48
49 TaxonNameEditor editor;
50 /** {@inheritDoc} */
51 @Override
52 public Object execute(ExecutionEvent event) throws ExecutionException {
53 editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
54
55 Object selectedElement = EditorUtil.getSelection(event).getFirstElement();
56
57 doExecute(event, editor, selectedElement);
58
59 return null;
60 }
61
62 protected void doExecute(ExecutionEvent event, TaxonNameEditor editor, Object selectedElement) {
63 AbstractPostOperation operation = null;
64 String commandName = null;
65
66 try {
67 commandName = event.getCommand().getName();
68 } catch (NotDefinedException e) {
69 MessagingUtils.error(getClass(), e);
70 }
71 if (selectedElement instanceof TaxonBase){
72 if (((TaxonBase)selectedElement).getId() == 0){
73 if (selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()) {
74 editor.getTaxon().removeTaxon((Taxon)selectedElement, TaxonRelationshipType.MISAPPLIED_NAME_FOR());
75
76 } else if (selectedElement instanceof Synonym){
77 editor.getTaxon().removeSynonym((Synonym)selectedElement);
78 }
79 editor.redraw();
80 return;
81
82 }
83
84 }
85
86 // synonym
87 if(selectedElement instanceof Synonym){
88 SynonymDeletionConfigurator deleteConfig = new SynonymDeletionConfigurator();
89 if(! DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfig, HandlerUtil.getActiveShell(event), Messages.DeleteTaxonBaseHandler_CONFIRM_DELETION, Messages.DeleteTaxonBaseHandler_REALLY_DELETE_SYNONYM)){
90 return ;
91 }
92 operation = new DeleteSynonymOperation(commandName, editor.getUndoContext(), deleteConfig, editor.getTaxon(), (Synonym) selectedElement,this, editor, (ICdmEntitySessionEnabled)editor.getEditorInput());
93
94 }
95 // misapplication
96 else if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()){
97 TaxonDeletionConfigurator deleteConfig = new TaxonDeletionConfigurator();
98 if(! DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfig, HandlerUtil.getActiveShell(event), Messages.DeleteTaxonBaseHandler_CONFIRM_DELETION, Messages.DeleteTaxonBaseHandler_REALLY_DELETE_MISAPPLICATION)){
99 return ;
100 }
101 operation = new DeleteMisapplicationOperation(commandName, editor.getUndoContext(), deleteConfig, editor.getTaxon(), (Taxon) selectedElement,this, editor, (ICdmEntitySessionEnabled)editor.getEditorInput());
102 } else {
103 throw new IllegalArgumentException(Messages.DeleteTaxonBaseHandler_ELEMENT_MUST_BE_SYNONYM_MISAPP_CONCEPT);
104 }
105
106 AbstractUtility.executeOperation(operation);
107 DeleteResult result = ((DeleteTaxonBaseOperation)operation).getResult();
108 if (result != null){
109 if (result.isError()){
110 DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID);
111 } else if (selectedElement instanceof Synonym){
112 this.editor.redraw();
113 if (!result.getExceptions().isEmpty()){
114 DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteTaxonBaseHandler_DELETE_SYNONYM_SUCCESSFULL_BUT_REMAINING_RELATED_OBJECTS, TaxeditorEditorPlugin.PLUGIN_ID);
115 }
116 } else if (selectedElement instanceof Taxon && !result.getExceptions().isEmpty()){
117 this.editor.redraw();
118 DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteTaxonBaseHandler_DELETE_MISAPPLIEDNAME_SUCCESSFULL_BUT_REMAINING_RELATED_OBJECTS, TaxeditorEditorPlugin.PLUGIN_ID);
119 }
120 }
121
122
123 }
124
125 @Override
126 public boolean postOperation(CdmBase objectAffectedByOperation) {
127
128 return true;
129 }
130
131 @Override
132 public boolean onComplete() {
133
134 return false;
135 }
136
137 }