fix #6175: transient objects are not marked as deleted in bulk editor only removed...
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / handler / DeleteHandler.java
index 95c415566bf6b265b617ef22556c62642e832341..0246e62e996dea9ef89bf9ca0e734c827fd68130 100644 (file)
@@ -1,62 +1,61 @@
 // $Id$
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* 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.bulkeditor.handler;
 
-import java.util.UUID;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
 
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.text.TextSelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.IViewPart;
-import org.eclipse.ui.IViewReference;
-import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.handlers.HandlerUtil;
 import org.eclipse.ui.texteditor.IDocumentProvider;
-import org.eclipse.ui.IWorkbenchPage;
 
+import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
 import eu.etaxonomy.cdm.api.service.DeleteResult;
-import eu.etaxonomy.cdm.api.service.IAgentService;
-import eu.etaxonomy.cdm.api.service.IGroupService;
-import eu.etaxonomy.cdm.api.service.INameService;
 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
 import eu.etaxonomy.cdm.api.service.IReferenceService;
-import eu.etaxonomy.cdm.api.service.ITaxonService;
-import eu.etaxonomy.cdm.api.service.IUserService;
+import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
+import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
 import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
+import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
+import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.Group;
 import eu.etaxonomy.cdm.model.common.User;
-
+import eu.etaxonomy.cdm.model.media.Media;
 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 import eu.etaxonomy.cdm.model.reference.Reference;
 import eu.etaxonomy.cdm.model.taxon.Synonym;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
-import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
-import eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.ReferencingObjectsView;
-
-
+import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
+import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
-import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
-import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
 
 
 /**
@@ -66,114 +65,184 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
  */
 public class DeleteHandler extends AbstractHandler {
 
+
+
        /* (non-Javadoc)
         * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
         */
        @Override
        public Object execute(ExecutionEvent event) throws ExecutionException {
-               
-               if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_EXPERIMENTAL_FEATURES)){
-                       ISelection selection = HandlerUtil.getCurrentSelection(event);
-                       
-                       IEditorPart editor = HandlerUtil.getActiveEditor(event);
-                       
-                       IEditorInput input = editor.getEditorInput();
-                       
-                       if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
-                               
-                               
-                               IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
-                               LineAnnotationModel model = 
-                                               (LineAnnotationModel) provider.getAnnotationModel(input);
-                               
-                               
-                               IStructuredSelection structuredSelection = (IStructuredSelection) selection;
-                               
-                               IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
-                                IViewPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
-                                                   .findView("eu.etaxonomy.taxeditor.bulkeditor.view.referencingobjects");
-                                   if (part != null){
-                                               ((ReferencingObjectsView)part).dispose();
-                                       }
-                               
-                               for(Object object : structuredSelection.toList()){
-                                       
-                                       LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
-                                       DeleteResult result = new DeleteResult();       
-                                       //result.setError();
-                                       try {
-                                               ICdmApplicationConfiguration controller;
-                                               controller = (ICdmApplicationConfiguration) CdmStore.getCurrentApplicationConfiguration();
-                                               //persistenceService.delete(object);
-                                               if (object instanceof SpecimenOrObservationBase){
-                                                       
-                                                       IOccurrenceService service = controller.getOccurrenceService();
-                                                       service.load(((SpecimenOrObservationBase) object).getUuid());
-                                                       result = service.delete((SpecimenOrObservationBase)object);
-                                               } else if (object instanceof Reference){
-                                                       IReferenceService service = controller.getReferenceService();
-                                                       service.load(((Reference) object).getUuid());
-                                                       result = service.delete((Reference)object);
-                                               } else if (object instanceof Group){
-                                                       IGroupService service = controller.getGroupService();
-                                                       service.load(((Group) object).getUuid());
-                                                       result = service.delete((Group) object);
-                                               }else if (object instanceof User){
-                                                       IUserService service = controller.getUserService();
-                                                       service.load(((User) object).getUuid());
-                                                       result = service.delete((User) object);
-                                               } else if (object instanceof TaxonNameBase){
-                                                       INameService service = controller.getNameService();
-                                                       TaxonNameBase name = service.load(((TaxonNameBase) object).getUuid());
-                                                       NameDeletionConfigurator config = new NameDeletionConfigurator();
-                                                       
-                                                       result = service.delete(name, config);
-                                               } else if (object instanceof TaxonBase){
-                                                       ITaxonService service = controller.getTaxonService();
-                                                       service.load(((TaxonBase) object).getUuid());
-                                                       if (object instanceof Taxon){
-                                                               result = service.deleteTaxon((Taxon)object, null, null);
-                                                       }else{
-                                                               result = service.deleteSynonym((Synonym)object, null);
-                                                       }
-                                               } else if (object instanceof TeamOrPersonBase){
-                                                       IAgentService service = controller.getAgentService();
-                                                       object = service.load(((TeamOrPersonBase) object).getUuid());
-                                                       result = service.delete((TeamOrPersonBase)object);
-                                               }
-                                       } catch (Exception e){
-                                               MessagingUtils.messageDialog("Exception occured. Could not delete", getClass(), e.getMessage(), null);
-                                       }
-                                       if (result.isError() || result.isAbort()|| !result.getExceptions().isEmpty()){
-                                               if (!result.getExceptions().isEmpty()) {
-                                                       MessagingUtils.messageDialog("Could not delete", getClass(), result.getExceptions().get(0).getMessage(), null);
-                                               }else{
-                                                       MessagingUtils.messageDialog("Could not delete", getClass(), "The object could not be deleted. An exception occured.", null);
-                                                       
-                                               }
-                                       }
-                                       if (result.isOk() && result.getExceptions().isEmpty()){
-                                               ((BulkEditor) editor).removeAnnotatedLine(annotation);
-                                               
-
-                                       }
-                                       if (part != null){
-                                               ((ReferencingObjectsView)part).refresh();
-                                       }
-                                       part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
-                                                   .findView("eu.etaxonomy.taxeditor.editor.view.descriptive");
-                                   if (part != null){
-                                               //((DescriptiveViewPart)part).dispose();
-                                       }
-                               }                               
+
+
+               TextSelection selection = (TextSelection) HandlerUtil.getCurrentSelection(event);
+               IEditorPart editor = HandlerUtil.getActiveEditor(event);
+
+               IEditorInput input = editor.getEditorInput();
+
+               if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
+
+
+                       IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
+                       LineAnnotationModel model =
+                                       (LineAnnotationModel) provider.getAnnotationModel(input);
+
+
+                       IStructuredSelection structuredSelection = (IStructuredSelection) selection;
+                       DeleteConfiguratorBase config = null;
+                       IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
+                       DeleteResult result = new DeleteResult();
+                       String errorMessage= "The object ";
+                       for(Object object : structuredSelection.toList()){
+                           if (object instanceof CdmBase){
+                               CdmBase base = (CdmBase)object;
+                               LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
+                               if (base.getId() != 0){
+
+
+                                       try {
+                                           ICdmApplicationConfiguration controller;
+                                           controller = CdmStore.getCurrentApplicationConfiguration();
+                                           if (object instanceof SpecimenOrObservationBase){
+                                               IOccurrenceService service = controller.getOccurrenceService();
+                                                       if (object != null){
+                                                               result = service.isDeletable(((SpecimenOrObservationBase) object).getUuid(), null);
+                                                               errorMessage = "The specimen or observation ";
+
+                                                       }
+                                           } else if (object instanceof Reference){
+                                                       IReferenceService service = controller.getReferenceService();
+                                                       if (object != null){
+                                                               result = service.isDeletable(((Reference)object).getUuid(), null);
+                                                               errorMessage = "The reference ";
+                                                       }
+
+                                           } else if (object instanceof Group){
+                                               errorMessage = "The group ";
+                                           }else if (object instanceof User){
+                                               errorMessage = "The user ";
+                                           } else if (object instanceof TaxonNameBase){
+                                               TaxonNameBase name = HibernateProxyHelper.deproxy(object, TaxonNameBase.class);
+                                               if (object != null){
+                                                   config = new NameDeletionConfigurator();
+
+                                                   DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), "Confirm Deletion",  null,  "Do you really want to delete the name?\nThis operation is irreversible!", MessageDialog.WARNING, new String[] { "Delete", "Skip" }, 0);
+                                                   int result_dialog= dialog.open();
+                                                   if (result_dialog != Status.OK){
+                                                       return null;
+                                                   }
+                                                   result = controller.getNameService().isDeletable(name.getUuid(), config);
+                                                   errorMessage = "The name ";
+
+                                               }
+                                           } else if (object instanceof TaxonBase){
+
+                                                       // synonym
+                                                       if(object instanceof Synonym){
+                                                               Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
+                                                               config = new SynonymDeletionConfigurator();
+                                                               errorMessage = "The synonym ";
+                                                               DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), "Confirm Deletion",  null,  "Do you really want to delete the synonym?", MessageDialog.WARNING, new String[] { "Delete", "Skip" }, 0);
+                                    int result_dialog= dialog.open();
+                                    if (result_dialog != Status.OK){
+                                         return null;
+                                    }
+                                    result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
+
+                                                       }
+                                                       else if(object instanceof Taxon ){
+                                                               Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
+                                                               if (((Taxon)object).getTaxonNodes().isEmpty()){
+                                            errorMessage = "The taxon ";
+                                                               } else{
+                                                                   MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
+                                                                   return null;
+                                                               }
+                                                               config = new TaxonDeletionConfigurator();
+                                                               ((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
+                                                               DeleteConfiguratorDialog dialog;
+                                                           dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), "Confirm Deletion",  null,  "Do you really want to delete the taxon?", MessageDialog.WARNING, new String[] { "Delete", "Skip" }, 0);
+                                                               int result_dialog= dialog.open();
+                                    if (result_dialog != Status.OK){
+                                         return null;
+                                    }
+                                    result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
+
+                                                       }
+                                           } else if (object instanceof TeamOrPersonBase){
+
+                                                 errorMessage = "The team or person ";
+                                          } else if (object instanceof Media){
+                                              config = new MediaDeletionConfigurator();
+                                              Media media = HibernateProxyHelper.deproxy(object, Media.class);
+                                              DeleteConfiguratorDialog dialog;
+                                                  dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), "Confirm Deletion",  null,  "Do you really want to delete the media?", MessageDialog.WARNING, new String[] { "Delete", "Skip" }, 0);
+                                                  int result_dialog= dialog.open();
+                               if (result_dialog != Status.OK){
+                                   return null;
+                               }
+
+                               result = controller.getMediaService().isDeletable(media.getUuid(), config);
+                               errorMessage = "The media ";
+
+                           }
+
+
+                               } catch (Exception e){
+                                       MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
+                               }
+                               if (result.isError() || result.isAbort()){
+                                       if (!result.getExceptions().isEmpty()) {
+                                               List<String> messages = new ArrayList<String>();
+                                               int i = result.getExceptions().size();
+                                               for (Exception e:result.getExceptions()){
+                                                       messages.add(e.getMessage());
+                                               }
+                                               errorMessage += "could not be deleted.";
+                                               //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
+                                               DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
+                                       }else{
+                                               MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
+                                       }
+                               }else if (model != null) {
+                        Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
+                        while (iter.hasNext()) {
+                            Object next = iter.next();
+                            if (next instanceof LineAnnotation) {
+                                if (result.isOk()){
+                                    ((LineAnnotation)next).markAsDeleted(config);
+
+                                }
+
+                            }
+                        }
+                    }
+
+                           }
+                       if (result.isOk() ){
+
+                    ((BulkEditor) editor).removeAnnotatedLine(annotation);
+
+                    if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
+                        List<String> messages = new ArrayList<String>();
+                        int i = result.getExceptions().size();
+                        for (Exception e:result.getExceptions()){
+                            messages.add(e.getMessage());
+                        }
+                        errorMessage += "can be deleted but related object(s) could not be deleted. ";
+                        //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
+                        DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
+                    }
+
+                }
+
+                           }
+
                        }
-               }else{
-                       MessagingUtils.warningDialog("Feature not enabled", getClass(), "Deletion is currently an experimental feature." +
-                                       "Enable it via Preferences at your own risk.");
                }
-               
-               
+
+
                return null;
        }
 
+
 }