Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / operation / DeleteTermBaseOperation.java
index c5bbd9c4ec4a1f9319e4fcc8b8fd0377eb325fe7..6ec4fc4c2e04a7fb1fb58a2cb2102cceafb5c1f0 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$\r
 /**\r
 * Copyright (C) 2009 EDIT\r
-* European Distributed Institute of Taxonomy \r
+* European Distributed Institute of Taxonomy\r
 * http://www.e-taxonomy.eu\r
-* \r
+*\r
 * The contents of this file are subject to the Mozilla Public License Version 1.1\r
 * See LICENSE.TXT at the top of this package for the full license terms.\r
 */\r
@@ -15,16 +15,18 @@ import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.IProgressMonitor;\r
 import org.eclipse.core.runtime.IStatus;\r
 import org.eclipse.core.runtime.Status;\r
+import org.eclipse.jface.dialogs.MessageDialog;\r
 \r
+import eu.etaxonomy.cdm.api.service.DeleteResult;\r
 import eu.etaxonomy.cdm.api.service.ITermService;\r
 import eu.etaxonomy.cdm.api.service.IVocabularyService;\r
-import eu.etaxonomy.cdm.model.common.DefinedTerm;\r
 import eu.etaxonomy.cdm.model.common.DefinedTermBase;\r
 import eu.etaxonomy.cdm.model.common.TermBase;\r
 import eu.etaxonomy.cdm.model.common.TermVocabulary;\r
 import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermEditor;\r
 import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;\r
-import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;\r
+import eu.etaxonomy.taxeditor.model.MessagingUtils;\r
+import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;\r
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;\r
 import eu.etaxonomy.taxeditor.store.CdmStore;\r
 import eu.etaxonomy.taxeditor.store.StoreUtil;\r
@@ -34,24 +36,24 @@ import eu.etaxonomy.taxeditor.store.StoreUtil;
  * @date 22 Dec 2011\r
  *\r
  */\r
-public class DeleteTermBaseOperation extends AbstractPostOperation {\r
+public class DeleteTermBaseOperation extends AbstractPostTaxonOperation {\r
 \r
-       private TermEditorInput definedEditorInput;\r
-       private TermBase termBase;\r
+       private final TermEditorInput definedEditorInput;\r
+       private final TermBase termBase;\r
        private DefinedTermEditor definedTermEditor;\r
        /**\r
         * @param label\r
         * @param undoContext\r
         * @param postOperationEnabled\r
         */\r
-       public DeleteTermBaseOperation(String label, \r
-                       IUndoContext undoContext, \r
+       public DeleteTermBaseOperation(String label,\r
+                       IUndoContext undoContext,\r
                        TermBase termBase,\r
                        TermEditorInput definedEditorInput,\r
                        IPostOperationEnabled postOperationEnabled) {\r
                super(label, undoContext, postOperationEnabled);\r
                this.termBase = termBase;\r
-               this.definedEditorInput = definedEditorInput;           \r
+               this.definedEditorInput = definedEditorInput;\r
        }\r
 \r
        /* (non-Javadoc)\r
@@ -60,61 +62,76 @@ public class DeleteTermBaseOperation extends AbstractPostOperation {
        @Override\r
        public IStatus execute(IProgressMonitor monitor, IAdaptable info)\r
                        throws ExecutionException {\r
-               \r
-               \r
-               \r
+\r
+\r
+\r
                if (termBase instanceof TermVocabulary) {\r
                        if (((TermVocabulary)termBase).getCreatedBy() == null) {\r
                                IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This is a CDM system vocabulary");\r
-                               StoreUtil.warningDialog("Cannot delete vocabulary", termBase, status);\r
+                               MessagingUtils.warningDialog("Cannot delete vocabulary", termBase, status);\r
                                return status;\r
                        }\r
-                       \r
+\r
                        if (!((TermVocabulary)termBase).getTerms().isEmpty()) {\r
                                IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "Delete all terms from this vocaulary before deleting the vocabulary.");\r
-                               StoreUtil.warningDialog("Vocabulary not empty", termBase, status);\r
+                               MessagingUtils.warningDialog("Vocabulary not empty", termBase, status);\r
                                return status;\r
-                       }                       \r
+                       }\r
+\r
+                       definedEditorInput.getVocabularies().remove(termBase);\r
+\r
+                       DeleteResult result =   CdmStore.getService(IVocabularyService.class).delete(termBase.getUuid());\r
+                       if (result.isError()){\r
+                               StringBuffer errorString = new StringBuffer();\r
+                               for (Exception e:result.getExceptions()){\r
+                                       errorString.append(e.getMessage() + "\\n");\r
+                               }\r
+                               MessageDialog.openError(null, "Delete failed", errorString.toString());\r
+                       }\r
 \r
-                       definedEditorInput.getVocabularies().remove((TermVocabulary)termBase); \r
-                       CdmStore.getService(IVocabularyService.class).delete((TermVocabulary)termBase);\r
-                       \r
-                       \r
                } else if (termBase instanceof DefinedTermBase) {\r
-                       \r
-                       \r
+\r
+\r
                        DefinedTermBase definedTermBase = (DefinedTermBase) termBase;\r
-                       \r
+\r
                        if (((DefinedTermBase)termBase).getCreatedBy() == null) {\r
                                IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This is a CDM system defined term");\r
-                               StoreUtil.warningDialog("Cannot delete defined term", termBase, status);\r
+                               MessagingUtils.warningDialog("Cannot delete defined term", termBase, status);\r
                                return status;\r
                        }\r
                        if(!definedTermBase.getIncludes().isEmpty()){\r
-                               IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This term includes other terms. Please delete the included terms before deleting this term.");                            \r
-                               StoreUtil.warningDialog("Term has included terms", termBase, status);\r
+                               IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This term includes other terms. Please delete the included terms before deleting this term.");\r
+                               MessagingUtils.warningDialog("Term has included terms", termBase, status);\r
                                return status;\r
-                       } \r
-                                               \r
+                       }\r
+\r
 \r
                        DefinedTermBase partOf = definedTermBase.getPartOf();\r
                        if(partOf != null){\r
                                partOf.removeIncludes(definedTermBase);\r
                        }\r
-                       \r
+\r
                        DefinedTermBase kindOf = definedTermBase.getKindOf();\r
                        if(kindOf != null){\r
                                definedTermBase.removeGeneralization(kindOf);\r
                        }\r
-                       \r
+\r
                        TermVocabulary vocabulary = definedTermBase.getVocabulary();\r
                        if(vocabulary != null){\r
                                vocabulary.removeTerm(definedTermBase);\r
                        }\r
-                       CdmStore.getService(ITermService.class).delete((DefinedTermBase)termBase);\r
-                       \r
+\r
+                       DeleteResult result =   CdmStore.getService(ITermService.class).delete(termBase.getUuid());\r
+\r
+                       if (result.isError()){\r
+                               StringBuffer errorString = new StringBuffer();\r
+                               for (Exception e:result.getExceptions()){\r
+                                       errorString.append(e.getMessage() + "\\n");\r
+                               }\r
+                               MessageDialog.openError(null, "Delete failed", errorString.toString());\r
+                       }\r
                }\r
-               \r
+\r
                return postExecute(termBase);\r
        }\r
 \r