Fix type designation filter for derivative search
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / operation / DeleteDerivateOperation.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.operation;
11
12 import org.eclipse.core.commands.ExecutionException;
13 import org.eclipse.core.commands.operations.IUndoContext;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.viewers.TreeNode;
19 import org.eclipse.ui.ISaveablePart;
20
21 import eu.etaxonomy.cdm.api.service.DeleteResult;
22 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
23 import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
24 import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.molecular.Sequence;
27 import eu.etaxonomy.cdm.model.molecular.SingleRead;
28 import eu.etaxonomy.taxeditor.editor.Messages;
29 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
30 import eu.etaxonomy.taxeditor.model.MessagingUtils;
31 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
32 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34 import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
35
36 /**
37 *
38 * @author pplitzner
39 * @date Oct 21, 2014
40 *
41 */
42 public class DeleteDerivateOperation extends AbstractPostOperation<CdmBase> {
43
44 private final SpecimenDeleteConfigurator deleteConfigurator;
45 private final TreeNode treeNode;
46
47 public DeleteDerivateOperation(String label, IUndoContext undoContext, CdmBase element, TreeNode treeNode,
48 IPostOperationEnabled postOperationEnabled) {
49 this(label, undoContext, element, treeNode, postOperationEnabled, new SpecimenDeleteConfigurator());
50 }
51
52 public DeleteDerivateOperation(String label, IUndoContext undoContext, CdmBase element, TreeNode treeNode,
53 IPostOperationEnabled postOperationEnabled, SpecimenDeleteConfigurator config) {
54 super(label, undoContext, element, postOperationEnabled);
55 this.deleteConfigurator = config;
56 this.treeNode = treeNode;
57 }
58
59
60 /** {@inheritDoc} */
61 @Override
62 public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
63 if(getPostOperationEnabled() instanceof ISaveablePart){
64 String confirmationQuestion = Messages.DeleteDerivateOperation_REALLY_DELETE;
65 if(deleteConfigurator.isDeleteChildren()){
66 confirmationQuestion += Messages.DeleteDerivateOperation_AND_CHILDREN;
67 }
68 confirmationQuestion += "?"; //$NON-NLS-1$
69 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfigurator, null, Messages.DeleteDerivateOperation_CONFIRM, confirmationQuestion)){
70 return Status.CANCEL_STATUS;
71 }
72 if(((ISaveablePart) getPostOperationEnabled()).isDirty()){
73 MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
74 return Status.CANCEL_STATUS;
75 }
76 }
77 DeleteResult deleteResult;
78 if(element instanceof SingleRead && treeNode!=null
79 && treeNode.getValue().equals(element)
80 && treeNode.getParent()!=null
81 && treeNode.getParent().getValue() instanceof Sequence){
82 deleteResult = CdmStore.getService(IOccurrenceService.class).deleteSingleRead(((SingleRead)element).getUuid(),
83 ((Sequence) treeNode.getParent().getValue()).getUuid());
84 } else if(element instanceof Sequence){
85 deleteResult = CdmStore.getService(ISequenceService.class).delete(element.getUuid(), deleteConfigurator);
86 } else {
87 deleteResult = CdmStore.getService(IOccurrenceService.class).deleteDerivateHierarchy(element.getUuid(), deleteConfigurator);
88 }
89 if(deleteResult.isOk()){
90 if(getPostOperationEnabled() instanceof DerivateView){
91 DerivateView derivateView = (DerivateView) getPostOperationEnabled();
92 derivateView.remove(element);
93 //update DerivateView
94 derivateView.getConversationHolder().commit();
95 IStatus returnStatus = postExecute(null);
96 return returnStatus;
97 }
98 }
99 else{
100 MessagingUtils.warningDialog(Messages.DeleteDerivateOperation_DELETE_FAILED, this, deleteResult.toString());
101 return Status.CANCEL_STATUS;
102 }
103 return Status.OK_STATUS;
104 }
105
106 /** {@inheritDoc} */
107 @Override
108 public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
109 //no redo possible
110 return Status.CANCEL_STATUS ;
111 }
112
113 /** {@inheritDoc} */
114 @Override
115 public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
116 //no undo possible
117 return Status.CANCEL_STATUS;
118 }
119 }