Update operation for move taxon nodes
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / TreeNodeDropAdapterAssistant.java
index f29a03d6484796cbcf16d1589218abe286b6ccaa..842f9a8c6fb0b43ff0cf68a2cc98fac3f1957b10 100644 (file)
@@ -1,12 +1,12 @@
 // $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;
 
@@ -14,6 +14,7 @@ import java.util.EnumSet;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
+import java.util.UUID;
 
 import org.apache.log4j.Logger;
 import org.eclipse.core.commands.operations.AbstractOperation;
@@ -52,221 +53,228 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
  */
 public class TreeNodeDropAdapterAssistant extends CommonDropAdapterAssistant implements IPostOperationEnabled {
 
-    private static final Logger logger = Logger.getLogger(TreeNodeDropAdapterAssistant.class);
-
-    /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
-    public static final String ID = "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
-
-    private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
-
-    /* (non-Javadoc)
-     * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
-     */
-    /** {@inheritDoc} */
-    @Override
-    public IStatus handleDrop(CommonDropAdapter dropAdapter,
-            DropTargetEvent dropTargetEvent, Object target) {
-
-
-        if (target instanceof ITaxonTreeNode) {
-            Set<TaxonNode> taxonNodes = getSelectedTaxa();
-            ITaxonTreeNode targetTreeNode = (ITaxonTreeNode) target;
-            if (targetTreeNode instanceof Classification){
-                targetTreeNode = ((Classification)targetTreeNode).getRootNode();
-                targetTreeNode = HibernateProxyHelper.deproxy(targetTreeNode, TaxonNode.class);
+       private static final Logger logger = Logger.getLogger(TreeNodeDropAdapterAssistant.class);
+
+       /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
+       public static final String ID = "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
+
+       private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
+
+       /* (non-Javadoc)
+        * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
+        */
+       /** {@inheritDoc} */
+       @Override
+       public IStatus handleDrop(CommonDropAdapter dropAdapter,
+                       DropTargetEvent dropTargetEvent, Object target) {
+
+
+               if (target instanceof ITaxonTreeNode) {
+                       Set<TaxonNode> taxonNodes = getSelectedTaxa();
+                       ITaxonTreeNode targetTreeNode = (ITaxonTreeNode) target;
+                       if (targetTreeNode instanceof Classification){
+                               targetTreeNode = ((Classification)targetTreeNode).getRootNode();
+                               targetTreeNode = HibernateProxyHelper.deproxy(targetTreeNode, TaxonNode.class);
+                       }
+                       //if(taxonNodes != null) {
+                               if (taxonNodes.size() >= 1){
+                                       return moveTaxon(taxonNodes, targetTreeNode);
+                               /*} else{
+                                       if( MessageDialog.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
+                                               return null;
+                                       }
+                               }*/
             }
-            if(taxonNodes != null) {
-                if (taxonNodes.size() == 1){
-                    return moveTaxon(taxonNodes.iterator().next(), targetTreeNode);
-                } else{
-                    if( MessageDialog.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
-                        return null;
-                    }
-                }
-            }
-        }
-
-        return Status.CANCEL_STATUS;
-    }
-
-    private Set<TaxonNode> getSelectedTaxa(){
-        HashSet<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
-
-        ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
-        if (selection instanceof TreeSelection) {
-
-            Iterator selectionIterator = ((TreeSelection) selection).iterator();
-
-            while (selectionIterator.hasNext()){
-                Object object = selectionIterator.next();
-                if(object instanceof TaxonNode){
-                    TaxonNode taxonNode = (TaxonNode) object;
-                    taxonNodes.add(taxonNode);
-                }
-            }
-        }
-        return taxonNodes.size() > 0 ? taxonNodes : null;
-    }
-
-    /* (non-Javadoc)
-     * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
-     */
-    /** {@inheritDoc} */
-    @Override
-    public IStatus validateDrop(Object target, int operation,
-            TransferData transferType) {
-        if (target instanceof ITaxonTreeNode) {
-
-            // check users permissions with target taxonnode and taxon
-            if (target instanceof TaxonNode) {
-                TaxonNode targetNode = (TaxonNode)target;
-                Boolean hasTargetNodePermission = CdmStore.currentAuthentiationHasPermission(targetNode, UPDATE);
+               }
+
+               return Status.CANCEL_STATUS;
+       }
+
+       private Set<TaxonNode> getSelectedTaxa(){
+               HashSet<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
+
+               ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
+               if (selection instanceof TreeSelection) {
+
+                       Iterator selectionIterator = ((TreeSelection) selection).iterator();
+
+                       while (selectionIterator.hasNext()){
+                               Object object = selectionIterator.next();
+                               if(object instanceof TaxonNode){
+                                       TaxonNode taxonNode = (TaxonNode) object;
+                                       taxonNodes.add(taxonNode);
+                               }
+                       }
+               }
+               return taxonNodes.size() > 0 ? taxonNodes : null;
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
+        */
+       /** {@inheritDoc} */
+       @Override
+       public IStatus validateDrop(Object target, int operation,
+                       TransferData transferType) {
+               if (target instanceof ITaxonTreeNode) {
+
+                   // check users permissions with target taxonnode and taxon
+                   if (target instanceof TaxonNode) {
+                       TaxonNode targetNode = (TaxonNode)target;
+                       Boolean hasTargetNodePermission = CdmStore.currentAuthentiationHasPermission(targetNode, UPDATE);
                 Boolean hasTargetTaxonPermission = CdmStore.currentAuthentiationHasPermission(targetNode.getTaxon(), UPDATE);
 
                 if(logger.isDebugEnabled()){
                     logger.debug("target: " + targetNode.getTaxon().getTitleCache());
                 }
 
-                if(!hasTargetNodePermission || ! hasTargetNodePermission){
-                    if(logger.isDebugEnabled()){
-                        logger.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission.toString() + " " + hasTargetTaxonPermission.toString() + " ");
-                    }
-                    return Status.CANCEL_STATUS;
-                }
-            }
-
-            // do not allow to drop onto itself and
-            // check users permissions with all selected taxon nodes and taxa
-            for(TaxonNode taxonNode : getSelectedTaxa()){
-                logger.debug("selectedTaxa: " + taxonNode.getTaxon().getTitleCache());
-                Boolean isSameTaxonNode = taxonNode.equals(target);
-                Boolean hasTaxonNodePermission = CdmStore.currentAuthentiationHasPermission(taxonNode, UPDATE);
-                Boolean hasTaxonPermission = CdmStore.currentAuthentiationHasPermission(taxonNode.getTaxon(), UPDATE);
+                       if(!hasTargetNodePermission || ! hasTargetNodePermission){
+                           if(logger.isDebugEnabled()){
+                               logger.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission.toString() + " " + hasTargetTaxonPermission.toString() + " ");
+                           }
+                           return Status.CANCEL_STATUS;
+                       }
+                   }
+
+                   // do not allow to drop onto itself and
+                   // check users permissions with all selected taxon nodes and taxa
+                   for(TaxonNode taxonNode : getSelectedTaxa()){
+                           logger.debug("selectedTaxa: " + taxonNode.getTaxon().getTitleCache());
+                               Boolean isSameTaxonNode = taxonNode.equals(target);
+                               Boolean hasTaxonNodePermission = CdmStore.currentAuthentiationHasPermission(taxonNode, UPDATE);
+                               Boolean hasTaxonPermission = CdmStore.currentAuthentiationHasPermission(taxonNode.getTaxon(), UPDATE);
                 if (
-                        isSameTaxonNode
-                        || !hasTaxonNodePermission
-                        || !hasTaxonPermission
-                        ) {
+                               isSameTaxonNode
+                               || !hasTaxonNodePermission
+                   || !hasTaxonPermission
+                       ) {
                     if(logger.isDebugEnabled()){
                         logger.debug("CANCEL_STATUS for selected  " + isSameTaxonNode.toString() + " " + hasTaxonNodePermission.toString() + " " + hasTaxonPermission.toString() + " ");
                     }
-                    return Status.CANCEL_STATUS;
-                }
-            }
-            logger.debug("OK_STATUS");
-            return Status.OK_STATUS;
-        }
-        logger.debug("CANCEL_STATUS");
-        return Status.CANCEL_STATUS;
-    }
-
-
-    /**
-     * @param childTaxonNode
-     * @param parentTaxon
-     * @return
-     */
-    private IStatus moveTaxon(TaxonNode taxonNode, ITaxonTreeNode targetITaxonTreeNode) {
-
-        TaxonNavigator taxonNavigator;
-        taxonNavigator = (TaxonNavigator) NavigationUtil.showView(TaxonNavigator.ID);
-
-        if(targetITaxonTreeNode instanceof TaxonNode){
-
-            TaxonNode targetTaxonNode = (TaxonNode) targetITaxonTreeNode;
-            // Make sure parent taxon does not have unsaved changes
-            if (NavigationUtil.isDirty(targetTaxonNode)){
-                MessageDialog.openWarning(NavigationUtil.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
-                        "changes in the parent taxon. Pleas save first.");
-                return Status.CANCEL_STATUS;
-            }
-
+                                       return Status.CANCEL_STATUS;
+                               }
+                       }
+                       logger.debug("OK_STATUS");
+                       return Status.OK_STATUS;
+               }
+               logger.debug("CANCEL_STATUS");
+               return Status.CANCEL_STATUS;
+       }
+
+
+       /**
+        * @param childTaxonNode
+        * @param parentTaxon
+        * @return
+        */
+       private IStatus moveTaxon(Set<TaxonNode> taxonNodes, ITaxonTreeNode targetITaxonTreeNode) {
+
+               TaxonNavigator taxonNavigator;
+               taxonNavigator = (TaxonNavigator) NavigationUtil.showView(TaxonNavigator.ID);
+
+               if(targetITaxonTreeNode instanceof TaxonNode){
+
+                       TaxonNode targetTaxonNode = (TaxonNode) targetITaxonTreeNode;
+               // Make sure parent taxon does not have unsaved changes
+                       if (NavigationUtil.isDirty(targetTaxonNode)){
+                               MessageDialog.openWarning(NavigationUtil.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
+                               "changes in the parent taxon. Pleas save first.");
+                               return Status.CANCEL_STATUS;
+                       }
+
+               }
+               Iterator<TaxonNode> taxIterator = taxonNodes.iterator();
+        Set<UUID> uuids = new HashSet<UUID>();
+        TaxonNode node = null;
+        while(taxIterator.hasNext()){
+            node = taxIterator.next();
+            uuids.add(node.getUuid());
         }
-        if (!PreferencesUtil.getSortNodesNaturally()){
-            IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
-            if (workspaceUndoContext == null) {
-                logger.error("Workspace undo context is null. DND operation cancelled");
-                return Status.CANCEL_STATUS;
-            }
-
-            if(CdmStore.getCurrentSessionManager().isRemoting()) {
-                AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, taxonNode, (TaxonNode)targetITaxonTreeNode, true);
-                NavigationUtil.executeOperation(operation, null);
-            } else {
-                AbstractPostOperation operation = new MoveTaxonOperation
-                        ("Move Taxon", workspaceUndoContext, taxonNode, targetITaxonTreeNode, this, taxonNavigator, true);
-                NavigationUtil.executeOperation(operation);
-            }
-            logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
-            return Status.OK_STATUS;
-        }else{
-            String[] buttonLables = {"Parent", "Predecessor", "Cancel"};
-            MessageDialog dialog = new MessageDialog(null, "Target node", null, "Do you want to use the target node as parent or do you want to move the taxon below the target.", MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
-            dialog.open();
-            int returnCode = dialog.getReturnCode();
-            if (returnCode == 0){
-                IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
-                if (workspaceUndoContext == null) {
-                    logger.error("Workspace undo context is null. DND operation cancelled");
-                    return Status.CANCEL_STATUS;
-                }
-
-
-                if(CdmStore.getCurrentSessionManager().isRemoting()) {
-                    AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, taxonNode, (TaxonNode)targetITaxonTreeNode, true);
-                    NavigationUtil.executeOperation(operation, null);
-                } else {
-                    AbstractPostOperation operation  = new MoveTaxonOperation
-                            ("Move Taxon", workspaceUndoContext, taxonNode, targetITaxonTreeNode, this, taxonNavigator, true);
-                    NavigationUtil.executeOperation(operation);
-                }
-
-                logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
-                return Status.OK_STATUS;
-            }else if (returnCode == 1){
-                IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
-                if (workspaceUndoContext == null) {
-                    logger.error("Workspace undo context is null. DND operation cancelled");
-                    return Status.CANCEL_STATUS;
-                }
-
-                if(CdmStore.getCurrentSessionManager().isRemoting()) {
-                    AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, taxonNode, (TaxonNode)targetITaxonTreeNode, true);
-                    NavigationUtil.executeOperation(operation, null);
-                } else {
-                    AbstractPostOperation operation = new MoveTaxonOperation
-                            ("Move Taxon", workspaceUndoContext, taxonNode, targetITaxonTreeNode, this, taxonNavigator, false);
-                    NavigationUtil.executeOperation(operation);
-                }
-                logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
-                return Status.OK_STATUS;
-            } else{
-                return Status.CANCEL_STATUS;
+               if (!PreferencesUtil.getSortNodesNaturally()){
+                       IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
+                       if (workspaceUndoContext == null) {
+                               logger.error("Workspace undo context is null. DND operation cancelled");
+                               return Status.CANCEL_STATUS;
+                       }
+
+                       if(CdmStore.getCurrentSessionManager().isRemoting()) {
+                           AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, true);
+                           NavigationUtil.executeOperation(operation, null);
+                       } else {
+                           AbstractPostOperation operation = new MoveTaxonOperation
+                                   ("Move Taxon", workspaceUndoContext, uuids, targetITaxonTreeNode, this, taxonNavigator, true);
+                           NavigationUtil.executeOperation(operation);
             }
 
-
-        }
-
-    }
-
-    /* (non-Javadoc)
-     * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
-     */
-    /** {@inheritDoc} */
-    @Override
+                       logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
+                       return Status.OK_STATUS;
+               }else{
+                       String[] buttonLables = {"Parent", "Predecessor", "Cancel"};
+                       MessageDialog dialog = new MessageDialog(null, "Target node", null, "Do you want to use the target node as parent or do you want to move the taxon below the target.", MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
+                       dialog.open();
+                       int returnCode = dialog.getReturnCode();
+                       if (returnCode == 0){
+                               IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
+                               if (workspaceUndoContext == null) {
+                                       logger.error("Workspace undo context is null. DND operation cancelled");
+                                       return Status.CANCEL_STATUS;
+                               }
+
+                               if(CdmStore.getCurrentSessionManager().isRemoting()) {
+                                   AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, true);
+                                   NavigationUtil.executeOperation(operation, null);
+                               } else {
+                                   AbstractPostOperation operation = new MoveTaxonOperation
+                                           ("Move Taxon", workspaceUndoContext, uuids, targetITaxonTreeNode, this, taxonNavigator, true);
+                                   NavigationUtil.executeOperation(operation);
+                               }
+
+                               logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
+                               return Status.OK_STATUS;
+                       }else if (returnCode == 1){
+                               IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
+                               if (workspaceUndoContext == null) {
+                                       logger.error("Workspace undo context is null. DND operation cancelled");
+                                       return Status.CANCEL_STATUS;
+                               }
+                               TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
+
+                               if(CdmStore.getCurrentSessionManager().isRemoting()) {
+                                   AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode.getParent(), true);
+                                   NavigationUtil.executeOperation(operation, null);
+                               } else {
+                                   AbstractPostOperation operation = new MoveTaxonOperation
+                                           ("Move Taxon", workspaceUndoContext, uuids, targetNode.getParent(), this, taxonNavigator, false);
+                                   NavigationUtil.executeOperation(operation);
+                               }
+                               logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
+                               return Status.OK_STATUS;
+                       } else{
+                               return Status.CANCEL_STATUS;
+                       }
+
+
+               }
+       }
+
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
+        */
+       /** {@inheritDoc} */
+       @Override
     public boolean postOperation(CdmBase objectAffectedByOperation) {
-        return true;
-    }
-
-    /**
-     * <p>onComplete</p>
-     *
-     * @return a boolean.
-     */
-    @Override
+               return true;
+       }
+
+       /**
+        * <p>onComplete</p>
+        *
+        * @return a boolean.
+        */
+       @Override
     public boolean onComplete() {
-        // TODO Auto-generated method stub
-        return false;
-    }
+               // TODO Auto-generated method stub
+               return false;
+       }
 
 }