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.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.jface.viewers.StructuredSelection;
27
import org.eclipse.swt.widgets.Shell;
28

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

    
60

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

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

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

    
93
        BulkEditorE4 editor= (BulkEditorE4) activePart.getObject();
94

    
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
        if(selection instanceof Object[]){
105
            for (Object object: (Object[])selection) {
106
                if(object instanceof CdmBase){
107
                    delete((CdmBase) object, shell, editor);
108
                }
109
            }
110
        }
111
        else if(selection instanceof CdmBase){
112
            delete((CdmBase) selection, shell, editor);
113
        }
114

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

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

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

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

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

    
174
            } else if (object instanceof TaxonBase){
175

    
176
                // synonym
177
                if(object instanceof Synonym){
178
                    try{
179
                        Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
180
                        config = new SynonymDeletionConfigurator();
181
                        errorMessage = SYNONYM;
182
                        DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
183
                                shell, CONFIRM_DELETION, null,
184
                                String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, SYNONYM),
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(synonym.getUuid(), config);
191
                    } catch(CdmEagerLoadingException e){
192
                        MessagingUtils.messageDialog(((TaxonBase) object).getTitleCache() + " " +ALREADY_DELETED, getClass(),
193
                                Messages.DeleteHandler_ALREADY_DELETED,
194
                                null);
195
                        return;
196
                    }
197

    
198

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

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

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

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

    
250
                result = controller.getMediaService().isDeletable(media.getUuid(), config);
251
                errorMessage = MEDIA;
252

    
253
            }
254

    
255

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

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

    
294
    @CanExecute
295
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
296
            MHandledMenuItem menuItem){
297
        boolean canExecute = false;
298
        StructuredSelection selection = (StructuredSelection)((BulkEditorE4)activePart.getObject()).getSelection();
299
        canExecute = !selection.isEmpty();
300
        menuItem.setVisible(canExecute);
301
        return canExecute;
302
    }
303

    
304

    
305
}
(3-3/9)