Project

General

Profile

Download (14.5 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)CdmBase object,
90
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
91
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
92

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

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

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

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

    
133
            } else if (object instanceof Group){
134
                errorMessage = GROUP;
135
            }else if (object instanceof User){
136
                errorMessage = USER;
137
            } else if (object instanceof TaxonName){
138
                try{
139
                    TaxonName name = HibernateProxyHelper.deproxy(object, TaxonName.class);
140
                    config = new NameDeletionConfigurator();
141

    
142
                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
143
                            shell, CONFIRM_DELETION, null,
144
                            String.format(
145
                                    DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
146
                                    NAME),
147
                            MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
148
                    int result_dialog = dialog.open();
149
                    if (result_dialog != IStatus.OK){
150
                        return;
151
                    }
152
                    result = controller.getNameService().isDeletable(name.getUuid(), config);
153
                    errorMessage = NAME;
154
                } catch(Exception e){
155
                    MessagingUtils.messageDialog(((TaxonName) object).getTitleCache() + " " +ALREADY_DELETED, getClass(),
156
                            Messages.DeleteHandler_ALREADY_DELETED,
157
                            null);
158
                    return;
159
                }
160

    
161
            } else if (object instanceof TaxonBase){
162

    
163
                // synonym
164
                if(object instanceof Synonym){
165
                    try{
166
                        Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
167
                        config = new SynonymDeletionConfigurator();
168
                        errorMessage = SYNONYM;
169
                        DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
170
                                shell, CONFIRM_DELETION, null,
171
                                String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, SYNONYM),
172
                                MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
173
                        int result_dialog = dialog.open();
174
                        if (result_dialog != IStatus.OK){
175
                            return;
176
                        }
177
                        result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
178
                    } catch(CdmEagerLoadingException e){
179
                        MessagingUtils.messageDialog(((TaxonBase) object).getTitleCache() + " " +ALREADY_DELETED, getClass(),
180
                                Messages.DeleteHandler_ALREADY_DELETED,
181
                                null);
182
                        return;
183
                    }
184

    
185

    
186
                }
187
                else if(object instanceof Taxon ){
188
                    try{
189
                        Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
190
                        if (((Taxon)object).getTaxonNodes().isEmpty()){
191
                            errorMessage = TAXON;
192
                        } else {
193
                            MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(),
194
                                    Messages.DeleteHandler_TAXON_CAN_NOT_BE_DELETED,
195
                                    null);
196
                            return;
197
                        }
198
                        config = new TaxonDeletionConfigurator();
199
                        ((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
200
                        DeleteConfiguratorDialog dialog;
201
                        dialog = new DeleteConfiguratorDialog(config, shell,
202
                                CONFIRM_DELETION, null,
203
                                String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, TAXON),
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
                        result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
210

    
211
                    } catch(CdmEagerLoadingException e){
212
                        MessagingUtils.messageDialog(((TaxonBase) object).getTitleCache() + " " +ALREADY_DELETED, getClass(),
213
                                Messages.DeleteHandler_ALREADY_DELETED,
214
                                null);
215
                        return;
216
                    }
217

    
218
                }
219
            } else if (object instanceof TeamOrPersonBase){
220
                result = controller.getAgentService().isDeletable(object.getUuid(), null);
221
                errorMessage = TEAM_OR_PERSON;
222
            } else if (object instanceof Media){
223
                config = new MediaDeletionConfigurator();
224

    
225
                Media media = HibernateProxyHelper.deproxy(object, Media.class);
226
                ((MediaDeletionConfigurator)config).setDeleteFrom(media);
227
                DeleteConfiguratorDialog dialog;
228
                dialog = new DeleteConfiguratorDialog(config, shell,
229
                        CONFIRM_DELETION, null,
230
                        String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, MEDIA),
231
                        MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
232
                int result_dialog = dialog.open();
233
                if (result_dialog != IStatus.OK){
234
                    return;
235
                }
236

    
237
                result = controller.getMediaService().isDeletable(media.getUuid(), config);
238
                errorMessage = MEDIA;
239

    
240
            }
241

    
242

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

    
267
            if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
268
                List<String> messages = new ArrayList<String>();
269
                for (Exception e:result.getExceptions()){
270
                    messages.add(e.getMessage());
271
                }
272
                //convert first letter to upper case
273
                errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
274
                errorMessage += eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_CAN_BE_DELETED;
275
                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
276
                DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
277
            }
278
        }
279
    }
280

    
281
    @CanExecute
282
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
283
            MHandledMenuItem menuItem){
284
        boolean canExecute = false;
285
        StructuredSelection selection = (StructuredSelection)((BulkEditorE4)activePart.getObject()).getSelection();
286
        canExecute = !selection.isEmpty();
287
        menuItem.setVisible(canExecute);
288
        return canExecute;
289
    }
290

    
291

    
292
}
(3-3/9)