Project

General

Profile

Download (6.3 KB) Statistics
| Branch: | Tag: | Revision:
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.event.EventUtility;
38
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
39
import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
40
import eu.etaxonomy.taxeditor.model.MessagingUtils;
41
import eu.etaxonomy.taxeditor.store.CdmStore;
42
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
43

    
44
/**
45
 *
46
 * @author pplitzner
47
 * @date Oct 21, 2014
48
 *
49
 */
50
public class DeleteDerivateHandler {
51

    
52

    
53
    private SpecimenDeleteConfigurator deleteConfigurator;
54

    
55
    @Execute
56
    public Object execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode treeNode, ParameterizedCommand command) {
57
        deleteConfigurator = new SpecimenDeleteConfigurator();
58
        if(command.getId().equals(AppModelId.COMMAND_EU_ETAXONOMY_TAXEDITOR_EDITOR_DERIVATE_DEEPDELETE)){
59
            deleteConfigurator.setDeleteChildren(true);
60
        }
61

    
62
        DerivateView derivateView = (DerivateView) part.getObject();
63
        Object value = treeNode.getValue();
64
        IStatus allowStatus = allowOperations(derivateView, treeNode);
65
        if(allowStatus.isOK()) {
66
            DeleteResult deleteResult;
67
            if(value instanceof SingleRead
68
                    && treeNode.getParent()!=null
69
                    && treeNode.getParent().getValue() instanceof Sequence){
70
                deleteResult = CdmStore.getService(ISequenceService.class).deleteSingleRead(((SingleRead)value).getUuid(),
71
                        ((Sequence) treeNode.getParent().getValue()).getUuid());
72
            } else if(value instanceof Sequence){
73
                deleteResult = CdmStore.getService(ISequenceService.class).delete(((Sequence) value).getUuid());
74
            } else {
75
                deleteResult = CdmStore.getService(IOccurrenceService.class).delete(((CdmBase) value).getUuid(), deleteConfigurator);
76
            }
77

    
78
            if (!deleteResult.isOk()) {
79
                MessagingUtils.warningDialog(Messages.DeleteDerivateOperation_DELETE_FAILED, this, deleteResult.toString());
80

    
81
                return Status.CANCEL_STATUS;
82
            }
83
            //broadcast delete result
84
            EventUtility.postEvent(WorkbenchEventConstants.DELETE_DERIVATIVE, deleteResult);
85

    
86
            if(derivateView.postOperation(null)){
87
                derivateView.remove(treeNode);
88

    
89
            }
90
            return Status.OK_STATUS;
91
        }
92
        return null;
93
    }
94

    
95
    public IStatus allowOperations(DerivateView derivateView, TreeNode treeNode) {
96
        String confirmationQuestion = Messages.DeleteDerivateOperation_REALLY_DELETE;
97
        if(deleteConfigurator.isDeleteChildren()){
98
            confirmationQuestion += Messages.DeleteDerivateOperation_AND_CHILDREN;
99
        }
100

    
101
        if(derivateView.isDirty()){
102
            MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
103
            return Status.CANCEL_STATUS;
104
        }
105
        confirmationQuestion += "?"; //$NON-NLS-1$
106
        if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfigurator, null, Messages.DeleteDerivateOperation_CONFIRM, confirmationQuestion)){
107
            return Status.CANCEL_STATUS;
108
        }
109
        DeleteResult deleteResult;
110
        Object value = treeNode.getValue();
111
        if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){
112
            if (value instanceof Sequence || value instanceof SingleRead){
113
                deleteResult = CdmStore.getService(ISequenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator);
114
            } else{
115
                deleteResult = CdmStore.getService(IOccurrenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator);
116
            }
117
            if (deleteResult.isOk() || deleteResult.getExceptions().isEmpty()){ return Status.OK_STATUS;}
118
            else{
119
                if (!deleteResult.isOk()){
120
                    DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID);
121
                } else {
122
                    if (!deleteResult.getExceptions().isEmpty()){
123
                        DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateHandler_SUCCESSFULL_BUT_EXCEPTIONS, TaxeditorEditorPlugin.PLUGIN_ID);
124
                    }
125
                }
126

    
127
                return Status.CANCEL_STATUS;
128
            }
129
        }
130
        return Status.CANCEL_STATUS;
131
    }
132

    
133
    @CanExecute
134
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode node,
135
            MHandledMenuItem menuItem){
136
        boolean canExecute = node !=null;
137
        menuItem.setVisible(canExecute);
138
        return canExecute;
139
    }
140

    
141
}
(10-10/18)