Project

General

Profile

Download (15.4 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.handler;
11

    
12

    
13
import java.util.ArrayList;
14
import java.util.List;
15

    
16
import org.eclipse.core.commands.AbstractHandler;
17
import org.eclipse.core.commands.ExecutionEvent;
18
import org.eclipse.core.commands.ExecutionException;
19
import org.eclipse.core.runtime.IStatus;
20
import org.eclipse.jface.dialogs.MessageDialog;
21
import org.eclipse.jface.text.TextSelection;
22
import org.eclipse.jface.viewers.IStructuredSelection;
23
import org.eclipse.ui.IEditorInput;
24
import org.eclipse.ui.IEditorPart;
25
import org.eclipse.ui.handlers.HandlerUtil;
26
import org.eclipse.ui.texteditor.IDocumentProvider;
27

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

    
61

    
62
/**
63
 * @author n.hoffmann
64
 * @created Mar 11, 2011
65
 * @version 1.0
66
 */
67
public class DeleteHandler extends AbstractHandler {
68

    
69

    
70

    
71
    private static final String COULD_NOT_BE_DELETED = Messages.DeleteHandler_COULD_NOT_BE_DELETED;
72
    private static final String AN_EXCEPTION_OCCURED = Messages.DeleteHandler_EXCEPTION;
73
    private static final String DELETE_NOT_POSSIBLE = Messages.DeleteHandler_DELETE_NOT_POSSIBLE;
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 = Messages.DeleteHandler_DELETE;
87
    private static final String CONFIRM_DELETION = Messages.DeleteHandler_CONFIRM;
88

    
89
    @Override
90
    public Object execute(ExecutionEvent event) throws ExecutionException {
91

    
92

    
93
        TextSelection selection = (TextSelection) HandlerUtil.getCurrentSelection(event);
94
        IEditorPart editor = HandlerUtil.getActiveEditor(event);
95

    
96
        IEditorInput input = editor.getEditorInput();
97

    
98
        if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
99

    
100

    
101
            IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
102
            LineAnnotationModel model =
103
                    (LineAnnotationModel) provider.getAnnotationModel(input);
104

    
105

    
106
            IStructuredSelection structuredSelection = (IStructuredSelection) selection;
107
            DeleteConfiguratorBase config = null;
108
            DeleteResult result = new DeleteResult();
109
            String errorMessage= OBJECT;
110
            for(Object object : structuredSelection.toList()){
111
                if (object instanceof CdmBase){
112
                    CdmBase base = (CdmBase)object;
113
                    LineAnnotation<?> annotation = (LineAnnotation<?>) model.getAnnotation(object);
114
                    if (base.getId() != 0){
115

    
116

    
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
                                        HandlerUtil.getActiveShell(event), 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 null;
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
                                TaxonName name = HibernateProxyHelper.deproxy(object, TaxonName.class);
149
                                config = new NameDeletionConfigurator();
150

    
151
                                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
152
                                        HandlerUtil.getActiveShell(event), CONFIRM_DELETION, null,
153
                                        String.format(
154
                                                DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
155
                                                NAME),
156
                                        MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
157
                                int result_dialog = dialog.open();
158
                                if (result_dialog != IStatus.OK){
159
                                    return null;
160
                                }
161
                                result = controller.getNameService().isDeletable(name.getUuid(), config);
162
                                errorMessage = NAME;
163

    
164
                            } else if (object instanceof TaxonBase){
165

    
166
                                // synonym
167
                                if(object instanceof Synonym){
168
                                    Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
169
                                    config = new SynonymDeletionConfigurator();
170
                                    errorMessage = SYNONYM;
171
                                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
172
                                            HandlerUtil.getActiveShell(event), CONFIRM_DELETION, null,
173
                                            String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, SYNONYM),
174
                                            MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
175
                                    int result_dialog = dialog.open();
176
                                    if (result_dialog != IStatus.OK){
177
                                        return null;
178
                                    }
179
                                    result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
180

    
181
                                }
182
                                else if(object instanceof Taxon ){
183
                                    Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
184
                                    if (((Taxon)object).getTaxonNodes().isEmpty()){
185
                                        errorMessage = TAXON;
186
                                    } else {
187
                                        MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(),
188
                                                Messages.DeleteHandler_TAXON_CAN_NOT_BE_DELETED,
189
                                                null);
190
                                        return null;
191
                                    }
192
                                    config = new TaxonDeletionConfigurator();
193
                                    ((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
194
                                    DeleteConfiguratorDialog dialog;
195
                                    dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event),
196
                                            CONFIRM_DELETION, null,
197
                                            String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, TAXON),
198
                                            MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
199
                                    int result_dialog = dialog.open();
200
                                    if (result_dialog != IStatus.OK){
201
                                        return null;
202
                                    }
203
                                    result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
204

    
205
                                }
206
                            } else if (object instanceof TeamOrPersonBase){
207
                                result = controller.getAgentService().isDeletable(((CdmBase) object).getUuid(), null);
208
                                errorMessage = TEAM_OR_PERSON;
209
                            } else if (object instanceof Media){
210
                                config = new MediaDeletionConfigurator();
211

    
212
                                Media media = HibernateProxyHelper.deproxy(object, Media.class);
213
                                ((MediaDeletionConfigurator)config).setDeleteFrom(media);
214
                                DeleteConfiguratorDialog dialog;
215
                                dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event),
216
                                        CONFIRM_DELETION, null,
217
                                        String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, MEDIA),
218
                                        MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
219
                                int result_dialog = dialog.open();
220
                                if (result_dialog != IStatus.OK){
221
                                    return null;
222
                                }
223

    
224
                                result = controller.getMediaService().isDeletable(media.getUuid(), config);
225
                                errorMessage = MEDIA;
226

    
227
                            }
228

    
229

    
230
                        } catch (Exception e){
231
                            MessagingUtils.errorDialog(AN_EXCEPTION_OCCURED+" "+DELETE_NOT_POSSIBLE, getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true); //$NON-NLS-1$
232
                        }
233
                        if (result.isError() || result.isAbort()){
234
                            //convert first letter to upper case
235
                            errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
236
                            if (!result.getExceptions().isEmpty()) {
237
                                List<String> messages = new ArrayList<String>();
238
                                for (Exception e:result.getExceptions()){
239
                                    messages.add(e.getMessage());
240
                                }
241
                                errorMessage += " " +COULD_NOT_BE_DELETED; //$NON-NLS-1$
242
                                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
243
                                DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
244
                            }else{
245
                                MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(), errorMessage+" "+COULD_NOT_BE_DELETED+" "+AN_EXCEPTION_OCCURED, null); //$NON-NLS-1$ //$NON-NLS-2$
246
                            }
247
                        }
248
                        annotation.markAsDeleted(config);
249
                    }
250
                    if (result.isOk() ){
251

    
252
                        ((BulkEditor) editor).removeAnnotatedLine(annotation);
253

    
254
                        if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
255
                            List<String> messages = new ArrayList<String>();
256
                            for (Exception e:result.getExceptions()){
257
                                messages.add(e.getMessage());
258
                            }
259
                            //convert first letter to upper case
260
                            errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
261
                            errorMessage += Messages.DeleteHandler_CAN_BE_DELETED;
262
                            //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
263
                            DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
264
                        }
265
                    }
266
                }
267
            }
268
        }
269
        return null;
270
    }
271

    
272
}
(4-4/10)