Merge branch 'develop' into feature/cdm-4.7
[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 javax.inject.Named;
13
14 import org.eclipse.core.commands.ParameterizedCommand;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.e4.core.di.annotations.CanExecute;
18 import org.eclipse.e4.core.di.annotations.Execute;
19 import org.eclipse.e4.core.di.annotations.Optional;
20 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
22 import org.eclipse.e4.ui.services.IServiceConstants;
23 import org.eclipse.jface.viewers.TreeNode;
24
25 import eu.etaxonomy.cdm.api.service.DeleteResult;
26 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
27 import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
28 import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
29 import eu.etaxonomy.cdm.model.common.CdmBase;
30 import eu.etaxonomy.cdm.model.molecular.Sequence;
31 import eu.etaxonomy.cdm.model.molecular.SingleRead;
32 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
33 import eu.etaxonomy.taxeditor.editor.AppModelId;
34 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
35 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
36 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
37 import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
38 import eu.etaxonomy.taxeditor.model.MessagingUtils;
39 import eu.etaxonomy.taxeditor.store.CdmStore;
40 import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
41
42 /**
43 *
44 * @author pplitzner
45 * @date Oct 21, 2014
46 *
47 */
48 public class DeleteDerivateHandler {
49
50
51 private SpecimenDeleteConfigurator deleteConfigurator;
52
53 @Execute
54 public Object execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode treeNode, ParameterizedCommand command) {
55 deleteConfigurator = new SpecimenDeleteConfigurator();
56 if(command.getId().equals(AppModelId.COMMAND_EU_ETAXONOMY_TAXEDITOR_EDITOR_DERIVATE_DEEPDELETE)){
57 deleteConfigurator.setDeleteChildren(true);
58 }
59
60 DerivateView derivateView = (DerivateView) part.getObject();
61 Object value = treeNode.getValue();
62 IStatus allowStatus = allowOperations(derivateView, treeNode);
63 if(allowStatus.isOK()) {
64 DeleteResult deleteResult;
65 if(value instanceof SingleRead
66 && treeNode.getParent()!=null
67 && treeNode.getParent().getValue() instanceof Sequence){
68 deleteResult = CdmStore.getService(ISequenceService.class).deleteSingleRead(((SingleRead)value).getUuid(),
69 ((Sequence) treeNode.getParent().getValue()).getUuid());
70 } else if(value instanceof Sequence){
71 deleteResult = CdmStore.getService(ISequenceService.class).delete(((Sequence) value).getUuid());
72 } else {
73 deleteResult = CdmStore.getService(IOccurrenceService.class).delete(((CdmBase) value).getUuid(), deleteConfigurator);
74 }
75
76 if (!deleteResult.isOk()) {
77 MessagingUtils.warningDialog(Messages.DeleteDerivateOperation_DELETE_FAILED, this, deleteResult.toString());
78
79 return Status.CANCEL_STATUS;
80 }
81 if(derivateView.postOperation(null)){
82 derivateView.remove(treeNode);
83
84 }
85 return Status.OK_STATUS;
86 }
87 return null;
88 }
89
90 public IStatus allowOperations(DerivateView derivateView, TreeNode treeNode) {
91 String confirmationQuestion = Messages.DeleteDerivateOperation_REALLY_DELETE;
92 if(deleteConfigurator.isDeleteChildren()){
93 confirmationQuestion += Messages.DeleteDerivateOperation_AND_CHILDREN;
94 }
95
96 if(derivateView.isDirty()){
97 MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
98 return Status.CANCEL_STATUS;
99 }
100 confirmationQuestion += "?"; //$NON-NLS-1$
101 if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfigurator, null, Messages.DeleteDerivateOperation_CONFIRM, confirmationQuestion)){
102 return Status.CANCEL_STATUS;
103 }
104 DeleteResult deleteResult;
105 Object value = treeNode.getValue();
106 if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){
107 if (value instanceof Sequence || value instanceof SingleRead){
108 deleteResult = CdmStore.getService(ISequenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator);
109 } else{
110 deleteResult = CdmStore.getService(IOccurrenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator);
111 }
112 if (deleteResult.isOk() || deleteResult.getExceptions().isEmpty()){ return Status.OK_STATUS;}
113 else{
114 if (!deleteResult.isOk()){
115 DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID);
116 } else {
117 if (!deleteResult.getExceptions().isEmpty()){
118 DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateHandler_SUCCESSFULL_BUT_EXCEPTIONS, TaxeditorEditorPlugin.PLUGIN_ID);
119 }
120 }
121
122 return Status.CANCEL_STATUS;
123 }
124 }
125 return Status.CANCEL_STATUS;
126 }
127
128 @CanExecute
129 public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode node,
130 MHandledMenuItem menuItem){
131 boolean canExecute = node !=null;
132 menuItem.setVisible(canExecute);
133 return canExecute;
134 }
135
136 }