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.e4.core.di.annotations.CanExecute;
20
import org.eclipse.e4.core.di.annotations.Execute;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
23
import org.eclipse.e4.ui.services.IServiceConstants;
24
import org.eclipse.jface.dialogs.MessageDialog;
25
import org.eclipse.swt.widgets.Shell;
26

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

    
57

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

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

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

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

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

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

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

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

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

    
150
            } else if (object instanceof TaxonBase){
151

    
152
                // synonym
153
                if(object instanceof Synonym){
154
                    Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
155
                    config = new SynonymDeletionConfigurator();
156
                    errorMessage = SYNONYM;
157
                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
158
                            shell, CONFIRM_DELETION, null,
159
                            String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, SYNONYM),
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.getTaxonService().isDeletable(synonym.getUuid(), config);
166

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

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

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

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

    
213
            }
214

    
215

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

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

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

    
263

    
264
}
(3-3/9)