Project

General

Profile

Download (16.8 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.HashSet;
15
import java.util.Iterator;
16
import java.util.List;
17
import java.util.Set;
18

    
19
import javax.inject.Named;
20

    
21
import org.eclipse.core.runtime.IStatus;
22
import org.eclipse.core.runtime.NullProgressMonitor;
23
import org.eclipse.e4.core.di.annotations.CanExecute;
24
import org.eclipse.e4.core.di.annotations.Execute;
25
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
26
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
27
import org.eclipse.e4.ui.services.IServiceConstants;
28
import org.eclipse.jface.dialogs.MessageDialog;
29
import org.eclipse.jface.viewers.IStructuredSelection;
30
import org.eclipse.swt.widgets.Shell;
31

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

    
64

    
65
/**
66
 *
67
 * @author pplitzner
68
 * @date 11.09.2017
69
 *
70
 */
71
public class DeleteHandlerE4 {
72

    
73
    private static final String COULD_NOT_BE_DELETED = eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_COULD_NOT_BE_DELETED;
74
    private static final String AN_EXCEPTION_OCCURED = Messages.DeleteHandler_EXCEPTION;
75
    private static final String DELETE_NOT_POSSIBLE = Messages.DeleteHandler_DELETE_NOT_POSSIBLE;
76
    private static final String ALREADY_DELETED = Messages.DeleteHandler_ALREADY_DELETED;
77
    private static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_S = Messages.DeleteHandler_REALLY_DELETE;
78
    private static final String MEDIA = Messages.DeleteHandler_MEDIA;
79
    private static final String TEAM_OR_PERSON = Messages.DeleteHandler_TEAM_OR_PERSON;
80
    private static final String TAXON = Messages.DeleteHandler_TAXON;
81
    private static final String SYNONYM = Messages.DeleteHandler_SYNONYM;
82
    private static final String NAME = Messages.DeleteHandler_NAME;
83
    private static final String OBJECT_MISSIN_RIGHTS = Messages.DeleteHandler_OBJECT_MISSING_RIGHTS;
84
    private static final String USER = Messages.DeleteHandler_USER;
85
    private static final String GROUP = Messages.DeleteHandler_GROUP;
86
    private static final String REFERENCE = Messages.DeleteHandler_REFERENCE;
87
    private static final String SPECIMEN_OR_OBSERVATION = Messages.DeleteHandler_SPECIMEN;
88
    private static final String OBJECT = Messages.DeleteHandler_OBJECT;
89
    private static final String SKIP = Messages.DeleteHandler_SKIP;
90
    private static final String DELETE = eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_DELETE;
91
    private static final String CONFIRM_DELETION = Messages.DeleteHandler_CONFIRM;
92

    
93
    @Execute
94
    public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
95
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
96

    
97
        BulkEditor editor= (BulkEditor) activePart.getObject();
98
        IStructuredSelection selection = editor.getSelection();
99

    
100
        Iterator iterator = selection.iterator();
101
        int selectionCount = selection.size();
102
        Set<CdmBase> removeFromSelection = new HashSet();
103
        for(selection.iterator();iterator.hasNext();){
104
            CdmBase cdmBase = (CdmBase) iterator.next();
105
            if (!cdmBase.isPersited()){
106
                editor.getEditorInput().getModel().remove(cdmBase);
107
                editor.getEditorInput().getSaveCandidates().remove(cdmBase);
108
                removeFromSelection.add(cdmBase);
109
                selectionCount--;
110
            }
111
        }
112

    
113

    
114
        if (selectionCount == 0){
115
            if (editor.getEditorInput().getMergeCandidates().size() == 0 && editor.getEditorInput().getSaveCandidates().size() == 0 && editor.getEditorInput().getToDelete().size() == 0){
116
                editor.setDirty(false);
117
            }
118
            return;
119
        }
120

    
121
        if (editor.isDirty()){
122
            boolean proceed = MessageDialog.openQuestion(null,
123
                    "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
124
            if (proceed) {
125
                editor.save(new NullProgressMonitor());
126
            } else {
127
                return ;
128
            }
129
        }
130

    
131
        iterator = selection.iterator();
132
        for(selection.iterator();iterator.hasNext();){
133
            CdmBase cdmBase = (CdmBase) iterator.next();
134
            if (cdmBase.isPersited()){
135
                delete(cdmBase, shell, editor);
136
            }
137
        }
138
    }
139

    
140
    private void delete(CdmBase object, Shell shell, BulkEditor editor){
141
        String errorMessage= OBJECT;
142
        DeleteConfiguratorBase config = null;
143
        DeleteResult result = new DeleteResult();
144
        boolean deleteAllowed = CdmStore.currentAuthentiationHasPermission(object,
145
                RequiredPermissions.NAME_DELETE);
146
        if (!deleteAllowed){
147
            MessagingUtils.messageDialog(OBJECT_MISSIN_RIGHTS, getClass(),
148
                    String.format(
149
                            Messages.DeleteHandler_OBJECT_MISSING_RIGHTS_MESSAGE+"\n",
150
                            object.getUserFriendlyDescription()),
151
                    null);
152
            return;
153
        }
154
        try {
155
            ICdmRepository controller;
156
            controller = CdmStore.getCurrentApplicationConfiguration();
157
            if (object instanceof SpecimenOrObservationBase){
158
                IOccurrenceService service = controller.getOccurrenceService();
159
                config = new SpecimenDeleteConfigurator();
160

    
161
                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
162
                        shell, CONFIRM_DELETION, null,
163
                        String.format(
164
                                DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
165
                                SPECIMEN_OR_OBSERVATION),
166
                        MessageDialog.WARNING,
167
                        new String[] { DELETE, SKIP }, 0, true);
168
                int result_dialog = dialog.open();
169
                if (result_dialog != IStatus.OK){
170
                    return;
171
                }
172

    
173
                result = service.isDeletable(((SpecimenOrObservationBase<?>) object).getUuid(), config);
174
                errorMessage = SPECIMEN_OR_OBSERVATION;
175

    
176
            } else if (object instanceof Reference){
177
                IReferenceService service = controller.getReferenceService();
178
                result = service.isDeletable(((Reference)object).getUuid(), null);
179
                errorMessage = REFERENCE;
180

    
181
            } else if (object instanceof Group){
182
                errorMessage = GROUP;
183
            }else if (object instanceof User){
184
                errorMessage = USER;
185
            } else if (object instanceof TaxonName){
186
                try{
187
                    TaxonName name = HibernateProxyHelper.deproxy(object, TaxonName.class);
188
                    config = new NameDeletionConfigurator();
189

    
190
                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
191
                            shell, CONFIRM_DELETION, null,
192
                            String.format(
193
                                    DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
194
                                    NAME),
195
                            MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
196
                    int result_dialog = dialog.open();
197
                    if (result_dialog != IStatus.OK){
198
                        return;
199
                    }
200

    
201
                    if (deleteAllowed){
202
                        result = controller.getNameService().isDeletable(name.getUuid(), config);
203
                        errorMessage = NAME;
204
                    }else{
205
                        errorMessage = NAME;
206
                        result.setError();
207
                    }
208
                } catch(Exception e){
209
                    MessagingUtils.messageDialog(((TaxonName) object).getTitleCache() + " " +ALREADY_DELETED, getClass(),
210
                            Messages.DeleteHandler_ALREADY_DELETED,
211
                            null);
212
                    return;
213
                }
214

    
215
            } else if (object instanceof TaxonBase){
216

    
217
                // synonym
218
                if(object instanceof Synonym){
219
                    try{
220
                        Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
221
                        config = new SynonymDeletionConfigurator();
222
                        errorMessage = SYNONYM;
223
                        DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
224
                                shell, CONFIRM_DELETION, null,
225
                                String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, SYNONYM),
226
                                MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
227
                        int result_dialog = dialog.open();
228
                        if (result_dialog != IStatus.OK){
229
                            return;
230
                        }
231
                        result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
232
                    } catch(CdmEagerLoadingException e){
233
                        MessagingUtils.messageDialog(((TaxonBase) object).getTitleCache() + " " +ALREADY_DELETED, getClass(),
234
                                Messages.DeleteHandler_ALREADY_DELETED,
235
                                null);
236
                        return;
237
                    }
238

    
239

    
240
                }
241
                else if(object instanceof Taxon ){
242
                    try{
243
                        Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
244
                        if (((Taxon)object).getTaxonNodes().isEmpty()){
245
                            errorMessage = TAXON;
246
                        } else {
247
                            MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(),
248
                                    Messages.DeleteHandler_TAXON_CAN_NOT_BE_DELETED,
249
                                    null);
250
                            return;
251
                        }
252
                        config = new TaxonDeletionConfigurator();
253
                        ((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
254
                        DeleteConfiguratorDialog dialog;
255
                        dialog = new DeleteConfiguratorDialog(config, shell,
256
                                CONFIRM_DELETION, null,
257
                                String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, TAXON),
258
                                MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
259
                        int result_dialog = dialog.open();
260
                        if (result_dialog != IStatus.OK){
261
                            return;
262
                        }
263
                        result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
264

    
265
                    } catch(CdmEagerLoadingException e){
266
                        MessagingUtils.messageDialog(((TaxonBase) object).getTitleCache() + " " +ALREADY_DELETED, getClass(),
267
                                Messages.DeleteHandler_ALREADY_DELETED,
268
                                null);
269
                        return;
270
                    }
271

    
272
                }
273
            } else if (object instanceof TeamOrPersonBase){
274
                result = controller.getAgentService().isDeletable(object.getUuid(), null);
275
                errorMessage = TEAM_OR_PERSON;
276
            } else if (object instanceof Media){
277
                config = new MediaDeletionConfigurator();
278

    
279
                Media media = HibernateProxyHelper.deproxy(object, Media.class);
280
                ((MediaDeletionConfigurator)config).setDeleteFrom(media);
281
                DeleteConfiguratorDialog dialog;
282
                dialog = new DeleteConfiguratorDialog(config, shell,
283
                        CONFIRM_DELETION, null,
284
                        String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, MEDIA),
285
                        MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
286
                int result_dialog = dialog.open();
287
                if (result_dialog != IStatus.OK){
288
                    return;
289
                }
290

    
291
                result = controller.getMediaService().isDeletable(media.getUuid(), config);
292
                errorMessage = MEDIA;
293

    
294
            }
295

    
296

    
297
        } catch (Exception e){
298
            MessagingUtils.errorDialog(AN_EXCEPTION_OCCURED+" "+DELETE_NOT_POSSIBLE, getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true); //$NON-NLS-1$
299
        }
300
        if (result.isError() || result.isAbort()){
301
            //convert first letter to upper case
302
            errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
303
            if (!result.getExceptions().isEmpty()) {
304
                List<String> messages = new ArrayList<String>();
305
                for (Exception e:result.getExceptions()){
306
                    messages.add(e.getMessage());
307
                }
308
                errorMessage += " " +COULD_NOT_BE_DELETED; //$NON-NLS-1$
309
                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
310
                DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
311
            }else{
312
                MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(), errorMessage+" "+COULD_NOT_BE_DELETED+" "+AN_EXCEPTION_OCCURED, null); //$NON-NLS-1$ //$NON-NLS-2$
313
            }
314
        }
315
        if (result.isOk() ){
316
            editor.getEditorInput().addToDelete(object, config);
317
            editor.getEditorInput().getModel().remove(object);
318
            editor.refresh();
319
            editor.setDirty();
320

    
321
            if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
322
                List<String> messages = new ArrayList<String>();
323
                for (Exception e:result.getExceptions()){
324
                    messages.add(e.getMessage());
325
                }
326
                //convert first letter to upper case
327
                errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
328
                errorMessage += eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_CAN_BE_DELETED;
329
                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
330
                DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
331
            }
332
        }
333
    }
334

    
335
    @CanExecute
336
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
337
            MHandledMenuItem menuItem){
338
        boolean canExecute = false;
339
        IStructuredSelection selection = ((BulkEditor)activePart.getObject()).getSelection();
340
        canExecute = !selection.isEmpty();
341
        Iterator iterator = selection.iterator();
342
        for(selection.iterator();iterator.hasNext();){
343
            if(!(iterator.next() instanceof CdmBase)){
344
                canExecute = false;
345
                break;
346
            }
347
        }
348
        menuItem.setVisible(canExecute);
349
        return canExecute;
350
    }
351

    
352

    
353
}
(4-4/10)