Merge branch 'develop' into feature/cdm-4.7
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / handler / DeleteDerivateHandler.java
index 1542fb204a105f217549ea9b62585074240c008a..6ccb5a00ea2e8460ea39eb660395a5650079b7e0 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
  * Copyright (C) 2007 EDIT
  * European Distributed Institute of Taxonomy
 
 package eu.etaxonomy.taxeditor.editor.view.derivate.handler;
 
-import org.eclipse.core.commands.AbstractHandler;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.common.NotDefinedException;
-import org.eclipse.core.commands.operations.IUndoContext;
-import org.eclipse.jface.viewers.IStructuredSelection;
+import javax.inject.Named;
+
+import org.eclipse.core.commands.ParameterizedCommand;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.e4.core.di.annotations.CanExecute;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.core.di.annotations.Optional;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
+import org.eclipse.e4.ui.services.IServiceConstants;
 import org.eclipse.jface.viewers.TreeNode;
-import org.eclipse.ui.IWorkbenchPart;
-import org.eclipse.ui.handlers.HandlerUtil;
 
+import eu.etaxonomy.cdm.api.service.DeleteResult;
+import eu.etaxonomy.cdm.api.service.IOccurrenceService;
+import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
+import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.molecular.Sequence;
 import eu.etaxonomy.cdm.model.molecular.SingleRead;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
-import eu.etaxonomy.taxeditor.editor.EditorUtil;
-import eu.etaxonomy.taxeditor.editor.view.derivate.operation.DeleteDerivateOperation;
-import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.editor.AppModelId;
+import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
+import eu.etaxonomy.taxeditor.editor.l10n.Messages;
+import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
+import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
-import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
 
 /**
  *
@@ -36,43 +45,92 @@ import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
  * @date Oct 21, 2014
  *
  */
-public class DeleteDerivateHandler extends AbstractHandler {
+public class DeleteDerivateHandler {
 
-    /* (non-Javadoc)
-     * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
-     */
-    /** {@inheritDoc} */
-    @Override
-    public Object execute(ExecutionEvent event) throws ExecutionException {
-        IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveSite(event).getSelectionProvider().getSelection();
 
-        IWorkbenchPart part = HandlerUtil.getActivePart(event);
-        IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
+    private SpecimenDeleteConfigurator deleteConfigurator;
 
-        try {
-            String label = event.getCommand().getName();
+    @Execute
+    public Object execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode treeNode, ParameterizedCommand command) {
+        deleteConfigurator = new SpecimenDeleteConfigurator();
+        if(command.getId().equals(AppModelId.COMMAND_EU_ETAXONOMY_TAXEDITOR_EDITOR_DERIVATE_DEEPDELETE)){
+            deleteConfigurator.setDeleteChildren(true);
+        }
 
-            IUndoContext undoContext = EditorUtil.getUndoContext();
+        DerivateView derivateView = (DerivateView) part.getObject();
+        Object value = treeNode.getValue();
+        IStatus allowStatus = allowOperations(derivateView, treeNode);
+        if(allowStatus.isOK()) {
+            DeleteResult deleteResult;
+            if(value instanceof SingleRead
+                    && treeNode.getParent()!=null
+                    && treeNode.getParent().getValue() instanceof Sequence){
+                deleteResult = CdmStore.getService(ISequenceService.class).deleteSingleRead(((SingleRead)value).getUuid(),
+                        ((Sequence) treeNode.getParent().getValue()).getUuid());
+            } else if(value instanceof Sequence){
+                deleteResult = CdmStore.getService(ISequenceService.class).delete(((Sequence) value).getUuid());
+            } else {
+                deleteResult = CdmStore.getService(IOccurrenceService.class).delete(((CdmBase) value).getUuid(), deleteConfigurator);
+            }
 
-            if(selection.size()>0){
-                Object object = selection.iterator().next();
+            if (!deleteResult.isOk()) {
+                MessagingUtils.warningDialog(Messages.DeleteDerivateOperation_DELETE_FAILED, this, deleteResult.toString());
 
-                if(object instanceof TreeNode){
-                    TreeNode treeNode = (TreeNode)object;
-                    Object value = ((TreeNode) object).getValue();
-                    if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){
-                        DeleteDerivateOperation deleteDerivateOperation = new DeleteDerivateOperation(label, undoContext, (CdmBase) object, postOperationEnabled);
-                        AbstractUtility.executeOperation(deleteDerivateOperation);
-                    }
-                }
-                else{
-                    MessagingUtils.error(getClass(), "Selection is not valid for this delete handler", null);
-                }
+                return Status.CANCEL_STATUS;
             }
+            if(derivateView.postOperation(null)){
+                derivateView.remove(treeNode);
 
-        } catch (NotDefinedException e) {
-            MessagingUtils.warn(getClass(), "Command name not set.");
+            }
+            return Status.OK_STATUS;
         }
         return null;
     }
+
+    public IStatus allowOperations(DerivateView derivateView, TreeNode treeNode) {
+        String confirmationQuestion = Messages.DeleteDerivateOperation_REALLY_DELETE;
+        if(deleteConfigurator.isDeleteChildren()){
+            confirmationQuestion += Messages.DeleteDerivateOperation_AND_CHILDREN;
+        }
+
+        if(derivateView.isDirty()){
+            MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
+            return Status.CANCEL_STATUS;
+        }
+        confirmationQuestion += "?"; //$NON-NLS-1$
+        if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfigurator, null, Messages.DeleteDerivateOperation_CONFIRM, confirmationQuestion)){
+            return Status.CANCEL_STATUS;
+        }
+        DeleteResult deleteResult;
+        Object value = treeNode.getValue();
+        if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){
+            if (value instanceof Sequence || value instanceof SingleRead){
+                deleteResult = CdmStore.getService(ISequenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator);
+            } else{
+                deleteResult = CdmStore.getService(IOccurrenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator);
+            }
+            if (deleteResult.isOk() || deleteResult.getExceptions().isEmpty()){ return Status.OK_STATUS;}
+            else{
+                if (!deleteResult.isOk()){
+                    DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID);
+                } else {
+                    if (!deleteResult.getExceptions().isEmpty()){
+                        DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateHandler_SUCCESSFULL_BUT_EXCEPTIONS, TaxeditorEditorPlugin.PLUGIN_ID);
+                    }
+                }
+
+                return Status.CANCEL_STATUS;
+            }
+        }
+        return Status.CANCEL_STATUS;
+    }
+
+    @CanExecute
+    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode node,
+            MHandledMenuItem menuItem){
+        boolean canExecute = node !=null;
+        menuItem.setVisible(canExecute);
+        return canExecute;
+    }
+
 }