Project

General

Profile

Download (6.63 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 java.util.Iterator;
13

    
14
import javax.inject.Named;
15

    
16
import org.eclipse.core.commands.ParameterizedCommand;
17
import org.eclipse.core.runtime.IStatus;
18
import org.eclipse.core.runtime.Status;
19
import org.eclipse.e4.core.di.annotations.CanExecute;
20
import org.eclipse.e4.core.di.annotations.Execute;
21
import org.eclipse.e4.core.di.annotations.Optional;
22
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
24
import org.eclipse.e4.ui.services.IServiceConstants;
25
import org.eclipse.jface.viewers.IStructuredSelection;
26
import org.eclipse.jface.viewers.TreeNode;
27

    
28
import eu.etaxonomy.cdm.api.service.DeleteResult;
29
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
30
import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
31
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
32
import eu.etaxonomy.cdm.model.common.CdmBase;
33
import eu.etaxonomy.cdm.model.molecular.Sequence;
34
import eu.etaxonomy.cdm.model.molecular.SingleRead;
35
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
36
import eu.etaxonomy.taxeditor.editor.AppModelId;
37
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
38
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
39
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
40
import eu.etaxonomy.taxeditor.event.EventUtility;
41
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
42
import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
43
import eu.etaxonomy.taxeditor.model.MessagingUtils;
44
import eu.etaxonomy.taxeditor.store.CdmStore;
45
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
46

    
47
/**
48
 *
49
 * @author pplitzner
50
 * @date Oct 21, 2014
51
 *
52
 */
53
public class DeleteDerivateHandler {
54

    
55

    
56
    private SpecimenDeleteConfigurator deleteConfigurator;
57

    
58
    @Execute
59
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part,
60
            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
61
            ParameterizedCommand command) {
62
        deleteConfigurator = new SpecimenDeleteConfigurator();
63
        if(command.getId().equals(AppModelId.COMMAND_EU_ETAXONOMY_TAXEDITOR_EDITOR_DERIVATE_DEEPDELETE)){
64
            deleteConfigurator.setDeleteChildren(true);
65
        }
66
        DerivateView derivateView = (DerivateView) part.getObject();
67

    
68
        String confirmationQuestion = Messages.DeleteDerivateOperation_REALLY_DELETE;
69
        if(deleteConfigurator.isDeleteChildren()){
70
            confirmationQuestion += Messages.DeleteDerivateOperation_AND_CHILDREN;
71
        }
72

    
73
        if(derivateView.isDirty()){
74
            MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
75
            return;
76
        }
77
        confirmationQuestion += "?"; //$NON-NLS-1$
78
        if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfigurator, null, Messages.DeleteDerivateOperation_CONFIRM, confirmationQuestion)){
79
            return;
80
        }
81

    
82
        for(Iterator iter = selection.iterator();iter.hasNext();){
83
            Object object = iter.next();
84
            if(object instanceof TreeNode){
85
                delete((TreeNode)object, derivateView);
86
            }
87
        }
88

    
89
    }
90

    
91
    private void delete(TreeNode treeNode, DerivateView derivateView){
92
        Object value = treeNode.getValue();
93
        IStatus allowStatus = allowOperations(treeNode);
94
        if(allowStatus.isOK()) {
95
            DeleteResult deleteResult;
96
            if(value instanceof SingleRead
97
                    && treeNode.getParent()!=null
98
                    && treeNode.getParent().getValue() instanceof Sequence){
99
                deleteResult = CdmStore.getService(ISequenceService.class).deleteSingleRead(((SingleRead)value).getUuid(),
100
                        ((Sequence) treeNode.getParent().getValue()).getUuid());
101
            } else if(value instanceof Sequence){
102
                deleteResult = CdmStore.getService(ISequenceService.class).delete(((Sequence) value).getUuid());
103
            } else {
104
                deleteResult = CdmStore.getService(IOccurrenceService.class).delete(((CdmBase) value).getUuid(), deleteConfigurator);
105
            }
106

    
107
            if (!deleteResult.isOk()) {
108
                MessagingUtils.warningDialog(Messages.DeleteDerivateOperation_DELETE_FAILED, this, deleteResult.toString());
109
                return ;
110
            }
111
            //broadcast delete result
112
            EventUtility.postEvent(WorkbenchEventConstants.DELETE_DERIVATIVE, deleteResult);
113

    
114
            if(derivateView.postOperation(null)){
115
                derivateView.remove(treeNode);
116

    
117
            }
118
        }
119

    
120
    }
121

    
122
    public IStatus allowOperations(TreeNode treeNode) {
123
        DeleteResult deleteResult;
124
        Object value = treeNode.getValue();
125
        if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){
126
            if (value instanceof Sequence || value instanceof SingleRead){
127
                deleteResult = CdmStore.getService(ISequenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator);
128
            } else{
129
                deleteResult = CdmStore.getService(IOccurrenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator);
130
            }
131
            if (deleteResult.isOk() || deleteResult.getExceptions().isEmpty()){ return Status.OK_STATUS;}
132
            else{
133
                if (!deleteResult.isOk()){
134
                    DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID);
135
                } else {
136
                    if (!deleteResult.getExceptions().isEmpty()){
137
                        DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateHandler_SUCCESSFULL_BUT_EXCEPTIONS, TaxeditorEditorPlugin.PLUGIN_ID);
138
                    }
139
                }
140

    
141
                return Status.CANCEL_STATUS;
142
            }
143
        }
144
        return Status.CANCEL_STATUS;
145
    }
146

    
147
    @CanExecute
148
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
149
            MHandledMenuItem menuItem){
150
        boolean canExecute = false;
151
        canExecute = selection!=null && !selection.isEmpty();
152
        menuItem.setVisible(canExecute);
153
        return canExecute;
154
    }
155

    
156
}
(10-10/18)