Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / operation / DeleteOperation.java
index c7b5a37a05725930adb6e478c907326de4ce3724..a2fea97595d44cac993dbb5498591d26fc726f81 100644 (file)
@@ -1,40 +1,40 @@
 // $Id$
 /**
-* Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy
-* http://www.e-taxonomy.eu
-*
-* The contents of this file are subject to the Mozilla Public License Version 1.1
-* See LICENSE.TXT at the top of this package for the full license terms.
-*/
+ * Copyright (C) 2007 EDIT
+ * European Distributed Institute of Taxonomy
+ * http://www.e-taxonomy.eu
+ *
+ * The contents of this file are subject to the Mozilla Public License Version 1.1
+ * See LICENSE.TXT at the top of this package for the full license terms.
+ */
 
 package eu.etaxonomy.taxeditor.navigation.navigator.operation;
 
+import java.util.HashSet;
 import java.util.Set;
+import java.util.UUID;
 
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.commands.operations.IUndoContext;
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.jface.dialogs.MessageDialog;
 
-import eu.etaxonomy.cdm.api.application.CdmApplicationController;
 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
+import eu.etaxonomy.cdm.api.service.DeleteResult;
 import eu.etaxonomy.cdm.api.service.IClassificationService;
 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
-import eu.etaxonomy.cdm.api.service.ITaxonService;
 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
-import eu.etaxonomy.cdm.api.service.exception.DataChangeNoRollbackException;
 import eu.etaxonomy.cdm.model.common.CdmBase;
-import eu.etaxonomy.cdm.model.common.ITreeNode;
 import eu.etaxonomy.cdm.model.taxon.Classification;
 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.taxeditor.operation.AbstractPersistentPostOperation;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
+import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
 import eu.etaxonomy.taxeditor.store.CdmStore;
-import eu.etaxonomy.taxeditor.store.StoreUtil;
 
 /**
  * <p>DeleteTreeNodeOperation class.</p>
@@ -45,46 +45,52 @@ import eu.etaxonomy.taxeditor.store.StoreUtil;
  */
 public class DeleteOperation extends AbstractPersistentPostOperation{
 
-       private Set<ITaxonTreeNode> treeNodes;
-       private TaxonDeletionConfigurator config;
+    private Set<ITaxonTreeNode> treeNodes;
+    protected final TaxonDeletionConfigurator config;
+    private final ICdmEntitySessionEnabled cdmEntitySessionEnabled;
+
+    /**
+     * <p>Constructor for DeleteTreeNodeOperation.</p>
+     *
+     * @param label a {@link java.lang.String} object.
+     * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
+     * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
+     * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
+     * @param treeNodes a {@link java.util.Set} object.
+     */
+    public DeleteOperation(String label, IUndoContext undoContext,
+            ITaxonTreeNode taxonNode, TaxonDeletionConfigurator config,
+            IPostOperationEnabled postOperationEnabled,
+            IConversationEnabled conversationEnabled,
+            ICdmEntitySessionEnabled cdmEntitySessionEnabled) {
+        super(label, undoContext, postOperationEnabled, conversationEnabled, cdmEntitySessionEnabled);
+        this.taxonNode = (ITaxonTreeNode)CdmBase.deproxy(taxonNode, CdmBase.class);
+        this.config = config;
+        this.cdmEntitySessionEnabled = cdmEntitySessionEnabled;
+    }
+
+    /**
+     * <p>Constructor for DeleteTreeNodeOperation.</p>
+     *
+     * @param label a {@link java.lang.String} object.
+     * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
+     * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
+     * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
+     * @param treeNodes a {@link java.util.Set} object.
+     */
+    public DeleteOperation(String label, IUndoContext undoContext,
+            Set<ITaxonTreeNode> treeNodes, TaxonDeletionConfigurator config,
+            IPostOperationEnabled postOperationEnabled,
+            IConversationEnabled conversationEnabled,
+            ICdmEntitySessionEnabled cdmEntitySessionEnabled) {
+        super(label, undoContext, postOperationEnabled, conversationEnabled, cdmEntitySessionEnabled);
+        this.treeNodes = treeNodes;
+        this.config = config;
+        this.cdmEntitySessionEnabled = cdmEntitySessionEnabled;
+    }
+
 
 
-       /**
-        * <p>Constructor for DeleteTreeNodeOperation.</p>
-        *
-        * @param label a {@link java.lang.String} object.
-        * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
-        * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
-        * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
-        * @param treeNodes a {@link java.util.Set} object.
-        */
-       public DeleteOperation(String label, IUndoContext undoContext,
-                       ITaxonTreeNode taxonNode, TaxonDeletionConfigurator config,
-                       IPostOperationEnabled postOperationEnabled,
-                       IConversationEnabled conversationEnabled) {
-               super(label, undoContext, postOperationEnabled, conversationEnabled);
-               this.taxonNode = (ITaxonTreeNode)CdmBase.deproxy(taxonNode, CdmBase.class);
-               this.config = config;
-       }
-
-       /**
-        * <p>Constructor for DeleteTreeNodeOperation.</p>
-        *
-        * @param label a {@link java.lang.String} object.
-        * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
-        * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
-        * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
-        * @param treeNodes a {@link java.util.Set} object.
-        */
-       public DeleteOperation(String label, IUndoContext undoContext,
-                       Set<ITaxonTreeNode> treeNodes, TaxonDeletionConfigurator config,
-                       IPostOperationEnabled postOperationEnabled,
-                       IConversationEnabled conversationEnabled) {
-               super(label, undoContext, postOperationEnabled, conversationEnabled);
-               this.treeNodes = treeNodes;
-               this.config = config;
-       }
-       
 
        /* (non-Javadoc)
         * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
@@ -101,52 +107,70 @@ public class DeleteOperation extends AbstractPersistentPostOperation{
                        if(taxonNode != null && taxonNode instanceof TaxonNode){
                                //((TaxonNode) treeNode).delete();
 
-                               taxon = ((TaxonNode)taxonNode).getTaxon();
-                               try {
-                                       service.deleteTaxonNode((TaxonNode)taxonNode, config);
-                               } catch (DataChangeNoRollbackException e) {
-                                       
-                                       throw new ExecutionException(e.getMessage());
-                                       
+                               element = ((TaxonNode)taxonNode).getTaxon();
+
+                               DeleteResult result = service.deleteTaxonNode((TaxonNode)taxonNode, config);
+                               if (result.isError() && !result.getExceptions().isEmpty()){
+                                       //TODO:Error message!
+                                       MessageDialog.openError(null, "Delete failed", result.getExceptions().iterator().next().getMessage());
+                               } else if (!result.getExceptions().isEmpty()){
+                                       String separator = ", ";
+                                   String exceptionString = "";
+                                   for (Exception exception : result.getExceptions()) {
+                                   exceptionString += exception.getLocalizedMessage()+separator;
+                               }
+                                       MessageDialog.openInformation(null, "Delete of the node was successful but the taxon could not be deleted.", exceptionString);
                                }
-                               
+
+
                        }else if(taxonNode != null && taxonNode instanceof Classification){
                                Classification taxonomicTree = (Classification) taxonNode;
-                               if(taxonomicTree.hasChildNodes()){
-                                       StoreUtil.warningDialog("Tree is not empty", this, "It is not possible to delete a Taxonomic Tree that " +
-                                                       "is not empty. Please delete included taxa first");
-                               }else{
-                                       CdmStore.getService(IClassificationService.class).delete(taxonomicTree);
-                               }
-                       } else {
-                               try {
-                                       service.deleteTaxonNodes(treeNodes, config);
-                               } catch (DataChangeNoRollbackException e) {
-                                       throw new ExecutionException(e.getMessage());
-                               }
+
+                               DeleteResult result = CdmStore.getService(IClassificationService.class).delete(taxonomicTree.getUuid());
+                if (result.isError() && !result.getExceptions().isEmpty()){
+                    //TODO:Error message!
+                    MessageDialog.openError(null, "Delete failed", result.getExceptions().iterator().next().getMessage());
+                }
+        } else {
+            Set<UUID> treeNodeUuids = new HashSet<UUID>();
+            ITaxonTreeNode entity = null;
+            for(ITaxonTreeNode treeNode : treeNodes) {
+                if(entity == null) {
+                    entity = treeNode;
+                }
+                treeNodeUuids.add(treeNode.getUuid());
+            }
+
+            DeleteResult result = service.deleteTaxonNodes(treeNodeUuids, config);
+            if (result.isError() && !result.getExceptions().isEmpty()){
+                               //TODO:Error message!
+                               MessageDialog.openError(null, "Delete failed", result.getExceptions().iterator().next().getMessage());
                        }
-               
-               monitor.worked(40);
-               return postExecute(null);
-       }
 
-       /* (non-Javadoc)
-        * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
-        */
-       /** {@inheritDoc} */
-       @Override
-       public IStatus redo(IProgressMonitor monitor, IAdaptable info)
-                       throws ExecutionException {
-               return null;
-       }
+        }
 
-       /* (non-Javadoc)
-        * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
-        */
-       /** {@inheritDoc} */
-       @Override
-       public IStatus undo(IProgressMonitor monitor, IAdaptable info)
-                       throws ExecutionException {
-               return null;
-       }
+        monitor.worked(40);
+
+        return postExecute(null);
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
+     */
+    /** {@inheritDoc} */
+    @Override
+    public IStatus redo(IProgressMonitor monitor, IAdaptable info)
+            throws ExecutionException {
+        return null;
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
+     */
+    /** {@inheritDoc} */
+    @Override
+    public IStatus undo(IProgressMonitor monitor, IAdaptable info)
+            throws ExecutionException {
+        return null;
+    }
 }