adapting to chages in CdmUtils
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / handler / DeleteDerivateHandler.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.view.derivate.handler;
11
12 import org.eclipse.core.commands.ExecutionEvent;
13 import org.eclipse.core.commands.common.NotDefinedException;
14 import org.eclipse.core.commands.operations.AbstractOperation;
15 import org.eclipse.core.commands.operations.IUndoContext;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.TreeNode;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.handlers.HandlerUtil;
23
24 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
25 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
26 import eu.etaxonomy.cdm.api.service.DeleteResult;
27 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
28 import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
29 import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.molecular.Sequence;
32 import eu.etaxonomy.cdm.model.molecular.SingleRead;
33 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
34 import eu.etaxonomy.taxeditor.editor.EditorUtil;
35 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
36 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
37 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
38 import eu.etaxonomy.taxeditor.editor.view.derivate.operation.DeleteDerivateOperation;
39 import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
40 import eu.etaxonomy.taxeditor.model.MessagingUtils;
41 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
42 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
43 import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
44 import eu.etaxonomy.taxeditor.store.CdmStore;
45 import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
46 import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
47
48 /**
49 *
50 * @author pplitzner
51 * @date Oct 21, 2014
52 *
53 */
54 public class DeleteDerivateHandler extends RemotingCdmHandler {
55
56 SpecimenDeleteConfigurator deleteConfigurator;
57 IEditorPart editor;
58 TreeNode node;
59
60 /**
61 * @param label
62 */
63 public DeleteDerivateHandler(String label) {
64 super(label);
65
66 }
67
68 /**
69 * @param label
70 */
71 public DeleteDerivateHandler() {
72 super(Messages.DeleteDerivateHandler_LABEL);
73
74 }
75
76 // /** {@inheritDoc} */
77 // @Override
78 // public Object execute(ExecutionEvent event) throws ExecutionException {
79 // IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveSite(event).getSelectionProvider().getSelection();
80 //
81 // IWorkbenchPart part = HandlerUtil.getActivePart(event);
82 // IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
83 //
84 // try {
85 // String label = event.getCommand().getName();
86 //
87 // IUndoContext undoContext = EditorUtil.getUndoContext();
88 //
89 // if(selection.size()>0){
90 // Object object = selection.iterator().next();
91 //
92 // if(object instanceof TreeNode){
93 // Object value = ((TreeNode) object).getValue();
94 // if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){
95 // if(postOperationEnabled instanceof IConversationEnabled){
96 // ConversationHolder conversationHolder = ((IConversationEnabled) postOperationEnabled).getConversationHolder();
97 // label += " "+DerivateLabelProvider.getDerivateText(value, conversationHolder); //$NON-NLS-1$
98 // }
99 // SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator();
100 // boolean deepDelete = event.getCommand().getId().equals("eu.etaxonomy.taxeditor.editor.derivate.deepDelete"); //$NON-NLS-1$
101 // config.setDeleteChildren(deepDelete);
102 // config.setDeleteMolecularData(deepDelete);
103 // DeleteDerivateOperation deleteDerivateOperation = new DeleteDerivateOperation(label, undoContext, (CdmBase) value, (TreeNode) object, postOperationEnabled, config);
104 // IStatus status = AbstractUtility.executeOperation(deleteDerivateOperation);
105 // DerivateView derivateView = (DerivateView)postOperationEnabled;
106 // if(derivateView.postOperation(null)){
107 // derivateView.remove(object);
108 //
109 // // derivateView.updateRootEntities();
110 // //update DerivateView
111 //
112 //
113 // }
114 // }
115 // }
116 // else{
117 // MessagingUtils.error(getClass(), Messages.DeleteDerivateHandler_INVALID_SELECTION, null);
118 // }
119 // }
120 //
121 // } catch (NotDefinedException e) {
122 // MessagingUtils.warn(getClass(), "Command name not set"); //$NON-NLS-1$
123 // }
124 // return null;
125 // }
126
127 /**
128 * {@inheritDoc}
129 */
130 @Override
131 public IStatus allowOperations(ExecutionEvent event) {
132 //check whether object can be deleted
133 editor = HandlerUtil.getActiveEditor(event);
134 IStructuredSelection selection = (IStructuredSelection) HandlerUtil
135 .getCurrentSelection(event);
136 AbstractPostOperation operation;
137 deleteConfigurator = new SpecimenDeleteConfigurator();
138
139 String confirmationQuestion = Messages.DeleteDerivateOperation_REALLY_DELETE;
140 if(deleteConfigurator.isDeleteChildren()){
141 confirmationQuestion += Messages.DeleteDerivateOperation_AND_CHILDREN;
142 }
143
144 if(editor.isDirty()){
145 MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
146 return Status.CANCEL_STATUS;
147 }
148
149 confirmationQuestion += "?"; //$NON-NLS-1$
150 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfigurator, null, Messages.DeleteDerivateOperation_CONFIRM, confirmationQuestion)){
151 return Status.CANCEL_STATUS;
152 }
153 Object object = selection.iterator().next();
154 DeleteResult deleteResult;
155 if(object instanceof TreeNode){
156 Object value = ((TreeNode) object).getValue();
157 if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){
158 if (value instanceof Sequence || value instanceof SingleRead){
159 deleteResult = CdmStore.getService(ISequenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator);
160 } else{
161 deleteResult = CdmStore.getService(IOccurrenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator);
162 }
163 if (deleteResult.isOk() || deleteResult.getExceptions().isEmpty()){ return Status.OK_STATUS;}
164 else{
165 if (!deleteResult.isOk()){
166 DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID);
167 } else {
168 if (!deleteResult.getExceptions().isEmpty()){
169 DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateHandler_SUCCESSFULL_BUT_EXCEPTIONS, TaxeditorEditorPlugin.PLUGIN_ID);
170 }
171 }
172
173 return Status.CANCEL_STATUS;
174 }
175 }
176
177 }
178
179 return Status.CANCEL_STATUS;
180
181 }
182
183 /**
184 * {@inheritDoc}
185 */
186 @Override
187 public AbstractOperation prepareOperation(ExecutionEvent event) {
188 IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveSite(event).getSelectionProvider().getSelection();
189
190 IWorkbenchPart part = HandlerUtil.getActivePart(event);
191 IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
192
193 try {
194 String label = event.getCommand().getName();
195
196 IUndoContext undoContext = EditorUtil.getUndoContext();
197
198 if(selection.size()>0){
199 Object object = selection.iterator().next();
200
201 if(object instanceof TreeNode){
202 node = (TreeNode)object;
203 Object value = ((TreeNode) object).getValue();
204 if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){
205 if(postOperationEnabled instanceof IConversationEnabled){
206 ConversationHolder conversationHolder = ((IConversationEnabled) postOperationEnabled).getConversationHolder();
207 label += " "+DerivateLabelProvider.getDerivateText(value, conversationHolder); //$NON-NLS-1$
208 }
209
210
211 DeleteDerivateOperation deleteDerivateOperation = new DeleteDerivateOperation(label, undoContext, (CdmBase) value, (TreeNode) object, postOperationEnabled, this.deleteConfigurator);
212 return deleteDerivateOperation;
213 }
214 }
215 }
216 } catch (NotDefinedException e) {
217 MessagingUtils.warn(getClass(), "Command name not set"); //$NON-NLS-1$
218 }
219 return null;
220
221 }
222
223 /**
224 * {@inheritDoc}
225 */
226 @Override
227 public void onComplete() {
228
229 IPostOperationEnabled postOperationEnabled = (editor instanceof IPostOperationEnabled) ? (IPostOperationEnabled) editor : null;
230 if (postOperationEnabled != null){
231 DerivateView derivateView = (DerivateView)postOperationEnabled;
232 if(derivateView.postOperation(null)){
233 derivateView.remove(node);
234
235 }
236 }
237 }
238 }