Fix possible NPE #4850
[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.AbstractUtility;
31 import eu.etaxonomy.taxeditor.model.MessagingUtils;
32 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
33 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
34 import eu.etaxonomy.taxeditor.store.CdmStore;
35 import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
36
37 /**
38 *
39 * @author pplitzner
40 * @date Oct 21, 2014
41 *
42 */
43 public class DeleteDerivateOperation extends AbstractPostOperation<CdmBase> {
44
45 private final SpecimenDeleteConfigurator deleteConfigurator;
46 private final TreeNode treeNode;
47
48 public DeleteDerivateOperation(String label, IUndoContext undoContext, CdmBase element, TreeNode treeNode,
49 IPostOperationEnabled postOperationEnabled) {
50 this(label, undoContext, element, treeNode, postOperationEnabled, new SpecimenDeleteConfigurator());
51 }
52
53 public DeleteDerivateOperation(String label, IUndoContext undoContext, CdmBase element, TreeNode treeNode,
54 IPostOperationEnabled postOperationEnabled, SpecimenDeleteConfigurator config) {
55 super(label, undoContext, element, postOperationEnabled);
56 this.deleteConfigurator = config;
57 this.treeNode = treeNode;
58 }
59
60
61 /** {@inheritDoc} */
62 @Override
63 public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
64 if(getPostOperationEnabled() instanceof ISaveablePart){
65 String confirmationQuestion = Messages.DeleteDerivateOperation_REALLY_DELETE;
66 if(deleteConfigurator.isDeleteChildren()){
67 confirmationQuestion += Messages.DeleteDerivateOperation_AND_CHILDREN;
68 }
69 confirmationQuestion += "?"; //$NON-NLS-1$
70 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfigurator, null, Messages.DeleteDerivateOperation_CONFIRM, confirmationQuestion)){
71 return Status.CANCEL_STATUS;
72 }
73 if(((ISaveablePart) getPostOperationEnabled()).isDirty()){
74 MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
75 return Status.CANCEL_STATUS;
76 }
77 }
78 DeleteResult deleteResult;
79 if(element instanceof SingleRead && treeNode!=null
80 && treeNode.getValue().equals(element)
81 && treeNode.getParent()!=null
82 && treeNode.getParent().getValue() instanceof Sequence){
83 deleteResult = CdmStore.getService(ISequenceService.class).deleteSingleRead(((SingleRead)element).getUuid(),
84 ((Sequence) treeNode.getParent().getValue()).getUuid());
85 } else if(element instanceof Sequence){
86 deleteResult = CdmStore.getService(ISequenceService.class).delete(element.getUuid());
87 } else {
88 deleteResult = CdmStore.getService(IOccurrenceService.class).delete(element.getUuid(), deleteConfigurator);
89 }
90 if(deleteResult.isOk()){
91 if(getPostOperationEnabled() instanceof DerivateView){
92 DerivateView derivateView = (DerivateView) getPostOperationEnabled();
93 derivateView.remove(element);
94 //update DerivateView
95 derivateView.getConversationHolder().commit();
96 IStatus returnStatus = postExecute(null);
97 //close if no more items left
98 if(derivateView.getViewer().getTree().getItemCount()<1){
99 AbstractUtility.close(derivateView);
100 }
101 return returnStatus;
102 }
103 }
104 else{
105 MessagingUtils.warningDialog(Messages.DeleteDerivateOperation_DELETE_FAILED, this, deleteResult.toString());
106 return Status.CANCEL_STATUS;
107 }
108 return Status.OK_STATUS;
109 }
110
111 /** {@inheritDoc} */
112 @Override
113 public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
114 //no redo possible
115 return Status.CANCEL_STATUS ;
116 }
117
118 /** {@inheritDoc} */
119 @Override
120 public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
121 //no undo possible
122 return Status.CANCEL_STATUS;
123 }
124 }