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.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 SKIP = "Skip";
72
    private static final String DELETE = "Delete";
73
    private static final String CONFIRM_DELETION = "Confirm Deletion";
74

    
75
    @Override
76
    public Object execute(ExecutionEvent event) throws ExecutionException {
77

    
78

    
79
        TextSelection selection = (TextSelection) HandlerUtil.getCurrentSelection(event);
80
        IEditorPart editor = HandlerUtil.getActiveEditor(event);
81

    
82
        IEditorInput input = editor.getEditorInput();
83

    
84
        if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
85

    
86

    
87
            IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
88
            LineAnnotationModel model =
89
                    (LineAnnotationModel) provider.getAnnotationModel(input);
90

    
91

    
92
            IStructuredSelection structuredSelection = (IStructuredSelection) selection;
93
            DeleteConfiguratorBase config = null;
94
            DeleteResult result = new DeleteResult();
95
            String errorMessage= "The object ";
96
            for(Object object : structuredSelection.toList()){
97
                if (object instanceof CdmBase){
98
                    CdmBase base = (CdmBase)object;
99
                    LineAnnotation<?> annotation = (LineAnnotation<?>) model.getAnnotation(object);
100
                    if (base.getId() != 0){
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, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  Messages.BulkEditor_DeleteHandler_reallyDeleteSpecimen, MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
111
                                int result_dialog= dialog.open();
112
                                if (result_dialog != IStatus.OK){
113
                                    return null;
114
                                }
115
                                result = service.isDeletable(((SpecimenOrObservationBase<?>) object).getUuid(), config);
116
                                errorMessage = "The specimen or observation ";
117

    
118
                            } else if (object instanceof Reference){
119
                                IReferenceService service = controller.getReferenceService();
120
                                result = service.isDeletable(((Reference)object).getUuid(), null);
121
                                errorMessage = "The reference ";
122

    
123
                            } else if (object instanceof Group){
124
                                errorMessage = "The group ";
125
                            }else if (object instanceof User){
126
                                errorMessage = "The user ";
127
                            } else if (object instanceof TaxonName){
128
                                TaxonName name = HibernateProxyHelper.deproxy(object, TaxonName.class);
129
                                config = new NameDeletionConfigurator();
130

    
131
                                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  "Do you really want to delete the name?\nThis operation is irreversible!", MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
132
                                int result_dialog= dialog.open();
133
                                if (result_dialog != IStatus.OK){
134
                                    return null;
135
                                }
136
                                result = controller.getNameService().isDeletable(name.getUuid(), config);
137
                                errorMessage = "The name ";
138

    
139
                            } else if (object instanceof TaxonBase){
140

    
141
                                // synonym
142
                                if(object instanceof Synonym){
143
                                    Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
144
                                    config = new SynonymDeletionConfigurator();
145
                                    errorMessage = "The synonym ";
146
                                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  "Do you really want to delete the synonym?", MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
147
                                    int result_dialog= dialog.open();
148
                                    if (result_dialog != IStatus.OK){
149
                                        return null;
150
                                    }
151
                                    result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
152

    
153
                                }
154
                                else if(object instanceof Taxon ){
155
                                    Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
156
                                    if (((Taxon)object).getTaxonNodes().isEmpty()){
157
                                        errorMessage = "The taxon ";
158
                                    } else{
159
                                        MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
160
                                        return null;
161
                                    }
162
                                    config = new TaxonDeletionConfigurator();
163
                                    ((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
164
                                    DeleteConfiguratorDialog dialog;
165
                                    dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  "Do you really want to delete the taxon?", MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
166
                                    int result_dialog= dialog.open();
167
                                    if (result_dialog != IStatus.OK){
168
                                        return null;
169
                                    }
170
                                    result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
171

    
172
                                }
173
                            } else if (object instanceof TeamOrPersonBase){
174
                                result = controller.getAgentService().isDeletable(((CdmBase) object).getUuid(), null);
175
                                errorMessage = "The team or person ";
176
                            } else if (object instanceof Media){
177
                                config = new MediaDeletionConfigurator();
178

    
179
                                Media media = HibernateProxyHelper.deproxy(object, Media.class);
180
                                ((MediaDeletionConfigurator)config).setDeleteFrom(media);
181
                                DeleteConfiguratorDialog dialog;
182
                                dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  "Do you really want to delete the media?", MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
183
                                int result_dialog= dialog.open();
184
                                if (result_dialog != IStatus.OK){
185
                                    return null;
186
                                }
187

    
188
                                result = controller.getMediaService().isDeletable(media.getUuid(), config);
189
                                errorMessage = "The media ";
190

    
191
                            }
192

    
193

    
194
                        } catch (Exception e){
195
                            MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
196
                        }
197
                        if (result.isError() || result.isAbort()){
198
                            if (!result.getExceptions().isEmpty()) {
199
                                List<String> messages = new ArrayList<String>();
200
                                for (Exception e:result.getExceptions()){
201
                                    messages.add(e.getMessage());
202
                                }
203
                                errorMessage += "could not be deleted.";
204
                                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
205
                                DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
206
                            }else{
207
                                MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
208
                            }
209
                        }
210
                        annotation.markAsDeleted(config);
211
                    }
212
                    if (result.isOk() ){
213

    
214
                        ((BulkEditor) editor).removeAnnotatedLine(annotation);
215

    
216
                        if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
217
                            List<String> messages = new ArrayList<String>();
218
                            for (Exception e:result.getExceptions()){
219
                                messages.add(e.getMessage());
220
                            }
221
                            errorMessage += "can be deleted but related object(s) could not be deleted. ";
222
                            //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
223
                            DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
224
                        }
225
                    }
226
                }
227
            }
228
        }
229
        return null;
230
    }
231

    
232
}
(4-4/10)