adaption for error handling of delete methods
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / operation / DeleteOperation.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.navigation.navigator.operation;
12
13 import java.util.List;
14 import java.util.Set;
15 import java.util.UUID;
16
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.commands.operations.IUndoContext;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.jface.dialogs.MessageDialog;
23
24 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
25 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
26 import eu.etaxonomy.cdm.api.service.DeleteResult;
27 import eu.etaxonomy.cdm.api.service.IClassificationService;
28 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
29 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
30 import eu.etaxonomy.cdm.api.service.exception.DataChangeNoRollbackException;
31 import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
32 import eu.etaxonomy.cdm.model.common.CdmBase;
33 import eu.etaxonomy.cdm.model.taxon.Classification;
34 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
35 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36 import eu.etaxonomy.taxeditor.operation.AbstractPersistentPostOperation;
37 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
38 import eu.etaxonomy.taxeditor.store.CdmStore;
39
40 /**
41 * <p>DeleteTreeNodeOperation class.</p>
42 *
43 * @author n.hoffmann
44 * @created Jan 20, 2010
45 * @version 1.0
46 */
47 public class DeleteOperation extends AbstractPersistentPostOperation{
48
49 private Set<ITaxonTreeNode> treeNodes;
50 protected final TaxonDeletionConfigurator config;
51
52
53 /**
54 * <p>Constructor for DeleteTreeNodeOperation.</p>
55 *
56 * @param label a {@link java.lang.String} object.
57 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
58 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
59 * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
60 * @param treeNodes a {@link java.util.Set} object.
61 */
62 public DeleteOperation(String label, IUndoContext undoContext,
63 ITaxonTreeNode taxonNode, TaxonDeletionConfigurator config,
64 IPostOperationEnabled postOperationEnabled,
65 IConversationEnabled conversationEnabled) {
66 super(label, undoContext, postOperationEnabled, conversationEnabled);
67 this.taxonNode = (ITaxonTreeNode)CdmBase.deproxy(taxonNode, CdmBase.class);
68 this.config = config;
69 }
70
71 /**
72 * <p>Constructor for DeleteTreeNodeOperation.</p>
73 *
74 * @param label a {@link java.lang.String} object.
75 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
76 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
77 * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
78 * @param treeNodes a {@link java.util.Set} object.
79 */
80 public DeleteOperation(String label, IUndoContext undoContext,
81 Set<ITaxonTreeNode> treeNodes, TaxonDeletionConfigurator config,
82 IPostOperationEnabled postOperationEnabled,
83 IConversationEnabled conversationEnabled) {
84 super(label, undoContext, postOperationEnabled, conversationEnabled);
85 this.treeNodes = treeNodes;
86 this.config = config;
87 }
88
89
90 /* (non-Javadoc)
91 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
92 */
93 /** {@inheritDoc} */
94 @Override
95 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
96 throws ExecutionException {
97
98 bind();
99 monitor.worked(20);
100 ICdmApplicationConfiguration controller = CdmStore.getCurrentApplicationConfiguration();
101 ITaxonNodeService service = controller.getTaxonNodeService();
102 if(taxonNode != null && taxonNode instanceof TaxonNode){
103 //((TaxonNode) treeNode).delete();
104
105 element = ((TaxonNode)taxonNode).getTaxon();
106
107 DeleteResult result = service.deleteTaxonNode((TaxonNode)taxonNode, config);
108 if (result.isError()){
109 //TODO:Error message!
110 MessageDialog.openError(null, "Delete failed", result.getExceptions().get(0).getMessage());
111 }
112
113
114 }else if(taxonNode != null && taxonNode instanceof Classification){
115 Classification taxonomicTree = (Classification) taxonNode;
116 /*if(taxonomicTree.hasChildNodes()){
117 if(! MessageDialog.openConfirm(null, "Confirm Deletion", "The selected tree has children, do yu realy want to delete the whole tree with its children?")){
118 return null;
119 }
120 }*/
121
122 DeleteResult result = CdmStore.getService(IClassificationService.class).delete(taxonomicTree);
123 if (result.isError()){
124 //TODO:Error message!
125 MessageDialog.openError(null, "Delete failed", result.getExceptions().get(0).getMessage());
126 }
127
128 /*}else{
129 try{
130 CdmStore.getService(IClassificationService.class).delete(taxonomicTree);
131 }catch(ReferencedObjectUndeletableException e){
132 throw new ExecutionException(e.getMessage());
133 }
134 }*/
135 } else {
136
137 List<UUID> result =service.deleteTaxonNodes(treeNodes, config);
138
139 }
140
141 monitor.worked(40);
142 return postExecute(null);
143 }
144
145 /* (non-Javadoc)
146 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
147 */
148 /** {@inheritDoc} */
149 @Override
150 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
151 throws ExecutionException {
152 return null;
153 }
154
155 /* (non-Javadoc)
156 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
157 */
158 /** {@inheritDoc} */
159 @Override
160 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
161 throws ExecutionException {
162 return null;
163 }
164 }