Project

General

Profile

Download (15 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.Iterator;
15
import java.util.List;
16

    
17
import javax.inject.Named;
18

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

    
30
import eu.etaxonomy.cdm.api.application.ICdmRepository;
31
import eu.etaxonomy.cdm.api.service.DeleteResult;
32
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
33
import eu.etaxonomy.cdm.api.service.IReferenceService;
34
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
35
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
36
import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
37
import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
38
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
39
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
40
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
41
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
42
import eu.etaxonomy.cdm.model.common.CdmBase;
43
import eu.etaxonomy.cdm.model.common.Group;
44
import eu.etaxonomy.cdm.model.common.User;
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.reference.Reference;
49
import eu.etaxonomy.cdm.model.taxon.Synonym;
50
import eu.etaxonomy.cdm.model.taxon.Taxon;
51
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
52
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
53
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
54
import eu.etaxonomy.taxeditor.l10n.Messages;
55
import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
56
import eu.etaxonomy.taxeditor.model.MessagingUtils;
57
import eu.etaxonomy.taxeditor.remoting.CdmEagerLoadingException;
58
import eu.etaxonomy.taxeditor.store.CdmStore;
59
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
60

    
61

    
62
/**
63
 *
64
 * @author pplitzner
65
 * @date 11.09.2017
66
 *
67
 */
68
public class DeleteHandlerE4 {
69

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

    
89
    @Execute
90
    public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
91
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
92

    
93
        BulkEditorE4 editor= (BulkEditorE4) activePart.getObject();
94
        IStructuredSelection selection = editor.getSelection();
95

    
96
        if (editor.isDirty()){
97
            boolean proceed = MessageDialog.openQuestion(null,
98
                    "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
99
            if (proceed) {
100
                editor.save(new NullProgressMonitor());
101
            } else {
102
                return ;
103
            }
104
        }
105

    
106
        Iterator iterator = selection.iterator();
107
        for(selection.iterator();iterator.hasNext();){
108
            CdmBase cdmBase = (CdmBase) iterator.next();
109
            delete(cdmBase, shell, editor);
110
        }
111
    }
112

    
113
    private void delete(CdmBase object, Shell shell, BulkEditorE4 editor){
114
        String errorMessage= OBJECT;
115
        DeleteConfiguratorBase config = null;
116
        DeleteResult result = new DeleteResult();
117
        try {
118
            ICdmRepository controller;
119
            controller = CdmStore.getCurrentApplicationConfiguration();
120
            if (object instanceof SpecimenOrObservationBase){
121
                IOccurrenceService service = controller.getOccurrenceService();
122
                config = new SpecimenDeleteConfigurator();
123

    
124
                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
125
                        shell, CONFIRM_DELETION, null,
126
                        String.format(
127
                                DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
128
                                SPECIMEN_OR_OBSERVATION),
129
                        MessageDialog.WARNING,
130
                        new String[] { DELETE, SKIP }, 0, true);
131
                int result_dialog = dialog.open();
132
                if (result_dialog != IStatus.OK){
133
                    return;
134
                }
135
                result = service.isDeletable(((SpecimenOrObservationBase<?>) object).getUuid(), config);
136
                errorMessage = SPECIMEN_OR_OBSERVATION;
137

    
138
            } else if (object instanceof Reference){
139
                IReferenceService service = controller.getReferenceService();
140
                result = service.isDeletable(((Reference)object).getUuid(), null);
141
                errorMessage = REFERENCE;
142

    
143
            } else if (object instanceof Group){
144
                errorMessage = GROUP;
145
            }else if (object instanceof User){
146
                errorMessage = USER;
147
            } else if (object instanceof TaxonName){
148
                try{
149
                    TaxonName name = HibernateProxyHelper.deproxy(object, TaxonName.class);
150
                    config = new NameDeletionConfigurator();
151

    
152
                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
153
                            shell, CONFIRM_DELETION, null,
154
                            String.format(
155
                                    DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
156
                                    NAME),
157
                            MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
158
                    int result_dialog = dialog.open();
159
                    if (result_dialog != IStatus.OK){
160
                        return;
161
                    }
162
                    result = controller.getNameService().isDeletable(name.getUuid(), config);
163
                    errorMessage = NAME;
164
                } catch(Exception e){
165
                    MessagingUtils.messageDialog(((TaxonName) object).getTitleCache() + " " +ALREADY_DELETED, getClass(),
166
                            Messages.DeleteHandler_ALREADY_DELETED,
167
                            null);
168
                    return;
169
                }
170

    
171
            } else if (object instanceof TaxonBase){
172

    
173
                // synonym
174
                if(object instanceof Synonym){
175
                    try{
176
                        Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
177
                        config = new SynonymDeletionConfigurator();
178
                        errorMessage = SYNONYM;
179
                        DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
180
                                shell, CONFIRM_DELETION, null,
181
                                String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, SYNONYM),
182
                                MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
183
                        int result_dialog = dialog.open();
184
                        if (result_dialog != IStatus.OK){
185
                            return;
186
                        }
187
                        result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
188
                    } catch(CdmEagerLoadingException e){
189
                        MessagingUtils.messageDialog(((TaxonBase) object).getTitleCache() + " " +ALREADY_DELETED, getClass(),
190
                                Messages.DeleteHandler_ALREADY_DELETED,
191
                                null);
192
                        return;
193
                    }
194

    
195

    
196
                }
197
                else if(object instanceof Taxon ){
198
                    try{
199
                        Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
200
                        if (((Taxon)object).getTaxonNodes().isEmpty()){
201
                            errorMessage = TAXON;
202
                        } else {
203
                            MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(),
204
                                    Messages.DeleteHandler_TAXON_CAN_NOT_BE_DELETED,
205
                                    null);
206
                            return;
207
                        }
208
                        config = new TaxonDeletionConfigurator();
209
                        ((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
210
                        DeleteConfiguratorDialog dialog;
211
                        dialog = new DeleteConfiguratorDialog(config, shell,
212
                                CONFIRM_DELETION, null,
213
                                String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, TAXON),
214
                                MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
215
                        int result_dialog = dialog.open();
216
                        if (result_dialog != IStatus.OK){
217
                            return;
218
                        }
219
                        result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
220

    
221
                    } catch(CdmEagerLoadingException e){
222
                        MessagingUtils.messageDialog(((TaxonBase) object).getTitleCache() + " " +ALREADY_DELETED, getClass(),
223
                                Messages.DeleteHandler_ALREADY_DELETED,
224
                                null);
225
                        return;
226
                    }
227

    
228
                }
229
            } else if (object instanceof TeamOrPersonBase){
230
                result = controller.getAgentService().isDeletable(object.getUuid(), null);
231
                errorMessage = TEAM_OR_PERSON;
232
            } else if (object instanceof Media){
233
                config = new MediaDeletionConfigurator();
234

    
235
                Media media = HibernateProxyHelper.deproxy(object, Media.class);
236
                ((MediaDeletionConfigurator)config).setDeleteFrom(media);
237
                DeleteConfiguratorDialog dialog;
238
                dialog = new DeleteConfiguratorDialog(config, shell,
239
                        CONFIRM_DELETION, null,
240
                        String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, MEDIA),
241
                        MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
242
                int result_dialog = dialog.open();
243
                if (result_dialog != IStatus.OK){
244
                    return;
245
                }
246

    
247
                result = controller.getMediaService().isDeletable(media.getUuid(), config);
248
                errorMessage = MEDIA;
249

    
250
            }
251

    
252

    
253
        } catch (Exception e){
254
            MessagingUtils.errorDialog(AN_EXCEPTION_OCCURED+" "+DELETE_NOT_POSSIBLE, getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true); //$NON-NLS-1$
255
        }
256
        if (result.isError() || result.isAbort()){
257
            //convert first letter to upper case
258
            errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
259
            if (!result.getExceptions().isEmpty()) {
260
                List<String> messages = new ArrayList<String>();
261
                for (Exception e:result.getExceptions()){
262
                    messages.add(e.getMessage());
263
                }
264
                errorMessage += " " +COULD_NOT_BE_DELETED; //$NON-NLS-1$
265
                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
266
                DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
267
            }else{
268
                MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(), errorMessage+" "+COULD_NOT_BE_DELETED+" "+AN_EXCEPTION_OCCURED, null); //$NON-NLS-1$ //$NON-NLS-2$
269
            }
270
        }
271
        if (result.isOk() ){
272
            editor.getEditorInput().addToDelete(object, config);
273
            editor.getEditorInput().getModel().remove(object);
274
            editor.refresh();
275
            editor.setDirty();
276

    
277
            if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
278
                List<String> messages = new ArrayList<String>();
279
                for (Exception e:result.getExceptions()){
280
                    messages.add(e.getMessage());
281
                }
282
                //convert first letter to upper case
283
                errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
284
                errorMessage += eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_CAN_BE_DELETED;
285
                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
286
                DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
287
            }
288
        }
289
    }
290

    
291
    @CanExecute
292
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
293
            MHandledMenuItem menuItem){
294
        boolean canExecute = false;
295
        IStructuredSelection selection = ((BulkEditorE4)activePart.getObject()).getSelection();
296
        canExecute = !selection.isEmpty();
297
        Iterator iterator = selection.iterator();
298
        for(selection.iterator();iterator.hasNext();){
299
            if(!(iterator.next() instanceof CdmBase)){
300
                canExecute = false;
301
                break;
302
            }
303
        }
304
        menuItem.setVisible(canExecute);
305
        return canExecute;
306
    }
307

    
308

    
309
}
(4-4/10)