Project

General

Profile

Download (12.9 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.bulkeditor.e4.handler;
11

    
12

    
13
import java.util.ArrayList;
14
import java.util.List;
15

    
16
import javax.inject.Named;
17

    
18
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.NullProgressMonitor;
20
import org.eclipse.e4.core.di.annotations.CanExecute;
21
import org.eclipse.e4.core.di.annotations.Execute;
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.dialogs.MessageDialog;
26
import org.eclipse.swt.widgets.Shell;
27

    
28
import eu.etaxonomy.cdm.api.application.ICdmRepository;
29
import eu.etaxonomy.cdm.api.service.DeleteResult;
30
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
31
import eu.etaxonomy.cdm.api.service.IReferenceService;
32
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
33
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
34
import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
35
import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
36
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
37
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
38
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
39
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
40
import eu.etaxonomy.cdm.model.common.CdmBase;
41
import eu.etaxonomy.cdm.model.common.Group;
42
import eu.etaxonomy.cdm.model.common.User;
43
import eu.etaxonomy.cdm.model.media.Media;
44
import eu.etaxonomy.cdm.model.name.TaxonName;
45
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
46
import eu.etaxonomy.cdm.model.reference.Reference;
47
import eu.etaxonomy.cdm.model.taxon.Synonym;
48
import eu.etaxonomy.cdm.model.taxon.Taxon;
49
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
50
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
51
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
52
import eu.etaxonomy.taxeditor.l10n.Messages;
53
import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
54
import eu.etaxonomy.taxeditor.model.MessagingUtils;
55
import eu.etaxonomy.taxeditor.store.CdmStore;
56
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
57

    
58

    
59
/**
60
 *
61
 * @author pplitzner
62
 * @date 11.09.2017
63
 *
64
 */
65
public class DeleteHandlerE4 {
66

    
67
    private static final String COULD_NOT_BE_DELETED = eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_COULD_NOT_BE_DELETED;
68
    private static final String AN_EXCEPTION_OCCURED = Messages.DeleteHandler_EXCEPTION;
69
    private static final String DELETE_NOT_POSSIBLE = Messages.DeleteHandler_DELETE_NOT_POSSIBLE;
70
    private static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_S = Messages.DeleteHandler_REALLY_DELETE;
71
    private static final String MEDIA = Messages.DeleteHandler_MEDIA;
72
    private static final String TEAM_OR_PERSON = Messages.DeleteHandler_TEAM_OR_PERSON;
73
    private static final String TAXON = Messages.DeleteHandler_TAXON;
74
    private static final String SYNONYM = Messages.DeleteHandler_SYNONYM;
75
    private static final String NAME = Messages.DeleteHandler_NAME;
76
    private static final String USER = Messages.DeleteHandler_USER;
77
    private static final String GROUP = Messages.DeleteHandler_GROUP;
78
    private static final String REFERENCE = Messages.DeleteHandler_REFERENCE;
79
    private static final String SPECIMEN_OR_OBSERVATION = Messages.DeleteHandler_SPECIMEN;
80
    private static final String OBJECT = Messages.DeleteHandler_OBJECT;
81
    private static final String SKIP = Messages.DeleteHandler_SKIP;
82
    private static final String DELETE = eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_DELETE;
83
    private static final String CONFIRM_DELETION = Messages.DeleteHandler_CONFIRM;
84

    
85
    @Execute
86
    public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)CdmBase object,
87
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
88
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
89

    
90
        BulkEditorE4 editor= (BulkEditorE4) activePart.getObject();
91

    
92
        DeleteConfiguratorBase config = null;
93
        DeleteResult result = new DeleteResult();
94
        String errorMessage= OBJECT;
95
        if (editor.isDirty()){
96
            boolean proceed = MessageDialog.openQuestion(null,
97
                    "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
98
            if (proceed) {
99
                editor.save(new NullProgressMonitor());
100
            } else {
101
                return ;
102
            }
103
        }
104
        try {
105
            ICdmRepository controller;
106
            controller = CdmStore.getCurrentApplicationConfiguration();
107
            if (object instanceof SpecimenOrObservationBase){
108
                IOccurrenceService service = controller.getOccurrenceService();
109
                config = new SpecimenDeleteConfigurator();
110

    
111
                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
112
                        shell, CONFIRM_DELETION, null,
113
                        String.format(
114
                                DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
115
                                SPECIMEN_OR_OBSERVATION),
116
                        MessageDialog.WARNING,
117
                        new String[] { DELETE, SKIP }, 0, true);
118
                int result_dialog = dialog.open();
119
                if (result_dialog != IStatus.OK){
120
                    return;
121
                }
122
                result = service.isDeletable(((SpecimenOrObservationBase<?>) object).getUuid(), config);
123
                errorMessage = SPECIMEN_OR_OBSERVATION;
124

    
125
            } else if (object instanceof Reference){
126
                IReferenceService service = controller.getReferenceService();
127
                result = service.isDeletable(((Reference)object).getUuid(), null);
128
                errorMessage = REFERENCE;
129

    
130
            } else if (object instanceof Group){
131
                errorMessage = GROUP;
132
            }else if (object instanceof User){
133
                errorMessage = USER;
134
            } else if (object instanceof TaxonName){
135
                TaxonName name = HibernateProxyHelper.deproxy(object, TaxonName.class);
136
                config = new NameDeletionConfigurator();
137

    
138
                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
139
                        shell, CONFIRM_DELETION, null,
140
                        String.format(
141
                                DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
142
                                NAME),
143
                        MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
144
                int result_dialog = dialog.open();
145
                if (result_dialog != IStatus.OK){
146
                    return;
147
                }
148
                result = controller.getNameService().isDeletable(name.getUuid(), config);
149
                errorMessage = NAME;
150

    
151
            } else if (object instanceof TaxonBase){
152

    
153
                // synonym
154
                if(object instanceof Synonym){
155
                    Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
156
                    config = new SynonymDeletionConfigurator();
157
                    errorMessage = SYNONYM;
158
                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
159
                            shell, CONFIRM_DELETION, null,
160
                            String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, SYNONYM),
161
                            MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
162
                    int result_dialog = dialog.open();
163
                    if (result_dialog != IStatus.OK){
164
                        return;
165
                    }
166
                    result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
167

    
168
                }
169
                else if(object instanceof Taxon ){
170
                    Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
171
                    if (((Taxon)object).getTaxonNodes().isEmpty()){
172
                        errorMessage = TAXON;
173
                    } else {
174
                        MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(),
175
                                Messages.DeleteHandler_TAXON_CAN_NOT_BE_DELETED,
176
                                null);
177
                        return;
178
                    }
179
                    config = new TaxonDeletionConfigurator();
180
                    ((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
181
                    DeleteConfiguratorDialog dialog;
182
                    dialog = new DeleteConfiguratorDialog(config, shell,
183
                            CONFIRM_DELETION, null,
184
                            String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, TAXON),
185
                            MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
186
                    int result_dialog = dialog.open();
187
                    if (result_dialog != IStatus.OK){
188
                        return;
189
                    }
190
                    result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
191

    
192
                }
193
            } else if (object instanceof TeamOrPersonBase){
194
                result = controller.getAgentService().isDeletable(object.getUuid(), null);
195
                errorMessage = TEAM_OR_PERSON;
196
            } else if (object instanceof Media){
197
                config = new MediaDeletionConfigurator();
198

    
199
                Media media = HibernateProxyHelper.deproxy(object, Media.class);
200
                ((MediaDeletionConfigurator)config).setDeleteFrom(media);
201
                DeleteConfiguratorDialog dialog;
202
                dialog = new DeleteConfiguratorDialog(config, shell,
203
                        CONFIRM_DELETION, null,
204
                        String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, MEDIA),
205
                        MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
206
                int result_dialog = dialog.open();
207
                if (result_dialog != IStatus.OK){
208
                    return;
209
                }
210

    
211
                result = controller.getMediaService().isDeletable(media.getUuid(), config);
212
                errorMessage = MEDIA;
213

    
214
            }
215

    
216

    
217
        } catch (Exception e){
218
            MessagingUtils.errorDialog(AN_EXCEPTION_OCCURED+" "+DELETE_NOT_POSSIBLE, getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true); //$NON-NLS-1$
219
        }
220
        if (result.isError() || result.isAbort()){
221
            //convert first letter to upper case
222
            errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
223
            if (!result.getExceptions().isEmpty()) {
224
                List<String> messages = new ArrayList<String>();
225
                for (Exception e:result.getExceptions()){
226
                    messages.add(e.getMessage());
227
                }
228
                errorMessage += " " +COULD_NOT_BE_DELETED; //$NON-NLS-1$
229
                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
230
                DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
231
            }else{
232
                MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(), errorMessage+" "+COULD_NOT_BE_DELETED+" "+AN_EXCEPTION_OCCURED, null); //$NON-NLS-1$ //$NON-NLS-2$
233
            }
234
        }
235
        if (result.isOk() ){
236
            editor.getEditorInput().addToDelete(object, config);
237
            editor.getEditorInput().getModel().remove(object);
238
            editor.refresh(true);
239
            editor.setDirty();
240

    
241
            if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
242
                List<String> messages = new ArrayList<String>();
243
                for (Exception e:result.getExceptions()){
244
                    messages.add(e.getMessage());
245
                }
246
                //convert first letter to upper case
247
                errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
248
                errorMessage += eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_CAN_BE_DELETED;
249
                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
250
                DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
251
            }
252
        }
253
    }
254

    
255
    @CanExecute
256
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
257
            MHandledMenuItem menuItem){
258
        boolean canExecute = false;
259
        canExecute = selection!=null;
260
        menuItem.setVisible(canExecute);
261
        return canExecute;
262
    }
263

    
264

    
265
}
(3-3/9)