Add referencing objects view functionality to derivative editor #5403
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / handler / DeleteHandler.java
index 3ba11e54d3fc3b85430903f69d198fb35009ed9d..08d0355e8182c4d4e24aaea1bce282a77ba50b35 100644 (file)
@@ -1,18 +1,22 @@
 // $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.ArrayList;
+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.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.ui.IEditorInput;
@@ -20,13 +24,38 @@ import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.handlers.HandlerUtil;
 import org.eclipse.ui.texteditor.IDocumentProvider;
 
+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.NameDeletionConfigurator;
+import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
+import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
+import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
+import eu.etaxonomy.cdm.model.common.Group;
+import eu.etaxonomy.cdm.model.common.User;
+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.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.BulkEditorUtil;
-import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
-import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
+import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
+import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteTaxonConfiguratorComposite;
+
 
 /**
  * @author n.hoffmann
@@ -40,45 +69,136 @@ public class DeleteHandler extends AbstractHandler {
         */
        @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;
-                               
-                               for(Object object : structuredSelection.toList()){
-                                       
-                                       LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
+
+               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;
+
+
+                       for(Object object : structuredSelection.toList()){
+
+                               LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
+                               DeleteResult result = new DeleteResult();
+                               String errorMessage= "The object ";
+                               //result.setError();
+                               try {
+                                       ICdmApplicationConfiguration controller;
+                                       controller = CdmStore.getCurrentApplicationConfiguration();
+
+                                       if (object instanceof SpecimenOrObservationBase){
+                                               IOccurrenceService service = controller.getOccurrenceService();
+                                               if (object != null){
+                                                       result = service.delete(((SpecimenOrObservationBase) object).getUuid());
+                                                       errorMessage = "The specimen or observation ";
+                                               }
+                                       } else if (object instanceof Reference){
+                                               IReferenceService service = controller.getReferenceService();
+                                               if (object != null){
+                                                       result = service.delete(((Reference) object).getUuid());
+                                                       errorMessage = "The reference ";
+                                               }
+
+                                       } else if (object instanceof Group){
+                                               IGroupService service = controller.getGroupService();
+                                               if (object != null){
+                                                       result = service.delete(((Group) object).getUuid());
+                                                       errorMessage = "The group ";
+                                               }
+                                       }else if (object instanceof User){
+                                               IUserService service = controller.getUserService();
+                                               if (object != null){
+                                                       result = service.delete(((User) object).getUuid());
+                                                       errorMessage = "The user ";
+                                               }
+                                       } else if (object instanceof TaxonNameBase){
+                                               INameService service = controller.getNameService();
+                                               if (object != null){
+                                                       NameDeletionConfigurator config = new NameDeletionConfigurator();
                                                        
-                                       try {
-                                               persistenceService.delete(object);
-                                       } catch (Exception e){
-                                               BulkEditorUtil.errorDialog("Could not delete", getClass(), e.getMessage(), null);
+                                                       DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), "Confirm Deletion",  null,  "Do you really want to delete the name", MessageDialog.WARNING, new String[] { "Delete", "Skip" }, 0); 
+                                                       int result_dialog= dialog.open();
+                                                       if (result_dialog == 1){
+                                                               return null;
+                                                       }
+                                                       result = service.delete(((TaxonNameBase) object).getUuid(), config);
+                                                       errorMessage = "The name ";
+                                               }
+                                       } else if (object instanceof TaxonBase){
+                                               ITaxonService service = controller.getTaxonService();
+                                               if (object != null){
+                                                       if (object instanceof Taxon){
+                                                               TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
+                                                               config.setDeleteInAllClassifications(true);
+                                                               DeleteConfiguratorDialog 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 == 1){
+                                                                       return null;
+                                                               }
+                                                               result = service.deleteTaxon(((TaxonBase) object).getUuid(), config, null);
+                                                               errorMessage = "The taxon ";
+                                                       }else{
+                                                               SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
+                                                               
+                                                               result = service.deleteSynonym(((Synonym)object).getUuid(), config);
+                                                               errorMessage = "The synonym ";
+                                                       }
+                                               }
+                                       } else if (object instanceof TeamOrPersonBase){
+                                               IAgentService service = controller.getAgentService();
+                                               //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
+                                               result = service.delete(((TeamOrPersonBase)object).getUuid());
+                                               errorMessage = "The team or person ";
+                                       }
+                               } 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);
                                        }
+                               }
+                               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 += "was 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{
-                       BulkEditorUtil.warningDialog("Feature not enabled", getClass(), "Deletion is currently an experimental feature." +
-                                       "Enable it via Preferences at your own risk.");
                }
-               
-               
+
+
                return null;
        }