commit 734218f995535b1920b9feb5eab9e91141e0c536
Author: Katja Luther <k.luther@bgbm.org>
Date:   Thu Nov 3 14:19:39 2016 +0100

    fix #6175: transient objects are not marked as deleted in bulk editor only removed from model

diff --git a/eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/handler/DeleteHandler.java b/eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/handler/DeleteHandler.java
index a1b8e461c..0246e62e9 100644
--- a/eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/handler/DeleteHandler.java
+++ b/eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/handler/DeleteHandler.java
@@ -28,15 +28,16 @@ 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.IMediaService;
 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
 import eu.etaxonomy.cdm.api.service.IReferenceService;
+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;
@@ -87,300 +88,154 @@ public class DeleteHandler extends AbstractHandler {
 
 
 			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);
+
+                                }
 
-				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.isDeletable(((SpecimenOrObservationBase) object), null);
-							errorMessage = "The specimen or observation ";
-							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(null);
-										}
-
-									}
-								}
-							}
-						}
-					} else if (object instanceof Reference){
-						IReferenceService service = controller.getReferenceService();
-						if (object != null){
-							result = service.isDeletable((Reference)object, null);
-							errorMessage = "The reference ";
-						}
-						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(null);
-									}
-
-								}
-							}
-						}
-
-					} else if (object instanceof Group){
-						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(null);
-									}
-
-								}
-							}
-						}
-
-//						IGroupService service = controller.getGroupService();
-//						if (object != null){
-//							result = service.delete(((Group) object).getUuid());
-//							errorMessage = "The group ";
-//						}
-					}else if (object instanceof User){
-						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(null);
-									}
-
-								}
-							}
-						}
-
-//						IUserService service = controller.getUserService();
-//						if (object != null){
-//							result = service.delete(((User) object).getUuid());
-//							errorMessage = "The user ";
-//						}
-					} else if (object instanceof TaxonNameBase){
-						TaxonNameBase name = HibernateProxyHelper.deproxy(object, TaxonNameBase.class);
-
-
-						if (object != null){
-							NameDeletionConfigurator 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;
-							}
-
-							//operation = new DeleteTaxonNameOperation(commandName,((BulkEditor) editor).getUndoContext() , name, config, (BulkEditor)editor);
-							errorMessage = "The name ";
-
-							if (model != null) {
-								Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
-								while (iter.hasNext()) {
-									Object next = iter.next();
-									if (next instanceof LineAnnotation) {
-										result = controller.getNameService().isDeletable(name, config);
-										if (result.isOk()){
-											((LineAnnotation)next).markAsDeleted(config);
-										}
-
-									}
-								}
-							}
-						}
-					} else if (object instanceof TaxonBase){
-
-						// synonym
-						if(object instanceof Synonym){
-							Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
-							SynonymDeletionConfigurator 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;
                             }
-							//operation = new DeleteTaxonBaseOperation(commandName,((BulkEditor) editor).getUndoContext() , synonym, config, (BulkEditor)editor);
-							if (model != null) {
-								Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
-								while (iter.hasNext()) {
-									Object next = iter.next();
-									if (next instanceof LineAnnotation) {
-										result = controller.getTaxonService().isDeletable(synonym, config);
-										if (result.isOk()){
-											((LineAnnotation)next).markAsDeleted(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;
-							}
-							TaxonDeletionConfigurator config = new 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;
-                            }
-                            //operation = new DeleteTaxonBaseOperation(commandName, ((BulkEditor) editor).getUndoContext(), taxon, config, (BulkEditor)editor);
-                            if (model != null) {
-								Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
-								while (iter.hasNext()) {
-									Object next = iter.next();
-									if (next instanceof LineAnnotation) {
-										result = controller.getTaxonService().isDeletable(taxon, config);
-										if (result.isOk()){
-											((LineAnnotation)next).markAsDeleted(config);
-										}
-
-									}
-								}
-							}
-						}
-
-
-//						ITaxonService service = controller.getTaxonService();
-//						if (object != null){
-//							if (object instanceof Taxon){
-//							    TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
-//								config.setDeleteInAllClassifications(true);
-//								DeleteConfiguratorDialog dialog;
-//								if (((Taxon)object).getTaxonNodes().isEmpty()){
-//								    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 = service.deleteTaxon(((TaxonBase) object).getUuid(), config, null);
-//	                                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;
-//								}
-//
-//							}else{
-//								SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
-//								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 = service.deleteSynonym(((Synonym)object).getUuid(), config);
-//								errorMessage = "The synonym ";
-//							}
-//						}
-					} else if (object instanceof TeamOrPersonBase){
-
-						  if (model != null) {
-								Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
-								while (iter.hasNext()) {
-									Object next = iter.next();
-									if (next instanceof LineAnnotation) {
-
-										result = controller.getAgentService().isDeletable((TeamOrPersonBase) object, null);
-										if (result.isOk()){
-											((LineAnnotation)next).markAsDeleted(null);
-										}
-
-									}
-								}
-							}
-						//IAgentService service = controller.getAgentService();
-						//TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
-					//result = service.delete(((TeamOrPersonBase)object).getUuid());
-						errorMessage = "The team or person ";
-					} else if (object instanceof Media){
-						MediaDeletionConfigurator config = new MediaDeletionConfigurator();
-
-						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;
                         }
-                        IMediaService service = controller.getMediaService();
-                        //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
-
-                        errorMessage = "The media ";
-
-                        if (model != null) {
-							Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
-							while (iter.hasNext()) {
-								Object next = iter.next();
-								if (next instanceof LineAnnotation) {
-									 result = service.isDeletable(((Media)object), config);
-									if (result.isOk()){
-										((LineAnnotation)next).markAsDeleted(config);
-									}
-
-								}
-							}
-						}
                     }
 
-				} 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>();
+			    }
+		        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);
-					}
+                        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);
+                    }
+
+                }
 
-				}
+			    }
 
 			}
 		}
