c467a943e007f923330bb62fdee50b8681b1a0c4
[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.model.common.CdmBase;
25 import eu.etaxonomy.cdm.model.molecular.Sequence;
26 import eu.etaxonomy.cdm.model.molecular.SingleRead;
27 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
28 import eu.etaxonomy.taxeditor.model.AbstractUtility;
29 import eu.etaxonomy.taxeditor.model.MessagingUtils;
30 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
31 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
32 import eu.etaxonomy.taxeditor.store.CdmStore;
33 import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
34
35 /**
36 *
37 * @author pplitzner
38 * @date Oct 21, 2014
39 *
40 */
41 public class DeleteDerivateOperation extends AbstractPostOperation<CdmBase> {
42
43 private final SpecimenDeleteConfigurator deleteConfigurator;
44 private final TreeNode treeNode;
45
46 public DeleteDerivateOperation(String label, IUndoContext undoContext, CdmBase element, TreeNode treeNode,
47 IPostOperationEnabled postOperationEnabled) {
48 this(label, undoContext, element, treeNode, postOperationEnabled, new SpecimenDeleteConfigurator());
49 }
50
51 public DeleteDerivateOperation(String label, IUndoContext undoContext, CdmBase element, TreeNode treeNode,
52 IPostOperationEnabled postOperationEnabled, SpecimenDeleteConfigurator config) {
53 super(label, undoContext, element, postOperationEnabled);
54 this.deleteConfigurator = config;
55 this.treeNode = treeNode;
56 }
57
58
59 /** {@inheritDoc} */
60 @Override
61 public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
62 if(getPostOperationEnabled() instanceof ISaveablePart){
63 String confirmationQuestion = "Do you really want to delete the selected element";
64 if(deleteConfigurator.isDeleteChildren()){
65 confirmationQuestion += " and its children";
66 }
67 confirmationQuestion += "?";
68 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfigurator, null, "Confirm Deletion", confirmationQuestion)){
69 return Status.CANCEL_STATUS;
70 }
71 if(((ISaveablePart) getPostOperationEnabled()).isDirty()){
72 MessagingUtils.warningDialog("View has unsaved changes", this, "You need to save before performing this action");
73 return Status.CANCEL_STATUS;
74 }
75 }
76 DeleteResult deleteResult;
77 if(element instanceof SingleRead && treeNode!=null
78 && treeNode.getValue().equals(element)
79 && treeNode.getParent()!=null
80 && treeNode.getParent().getValue() instanceof Sequence){
81 deleteResult = CdmStore.getService(IOccurrenceService.class).deleteSingleRead((SingleRead)element, (Sequence) treeNode.getParent().getValue());
82 }
83 else{
84 deleteResult = CdmStore.getService(IOccurrenceService.class).deleteDerivateHierarchy(element, deleteConfigurator);
85 }
86 if(deleteResult.isOk()){
87 if(getPostOperationEnabled() instanceof DerivateView){
88 DerivateView derivateView = (DerivateView) getPostOperationEnabled();
89 //update DerivateView
90 derivateView.getConversationHolder().commit();
91 IStatus returnStatus = postExecute(null);
92 //close if no more items left
93 if(derivateView.getViewer().getTree().getItemCount()<1){
94 AbstractUtility.close(derivateView);
95 }
96 return returnStatus;
97 }
98 }
99 else{
100 MessagingUtils.warningDialog("Deletion 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 }