Project

General

Profile

Download (12.7 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.Iterator;
15
import java.util.List;
16

    
17
import org.eclipse.core.commands.AbstractHandler;
18
import org.eclipse.core.commands.ExecutionEvent;
19
import org.eclipse.core.commands.ExecutionException;
20
import org.eclipse.core.runtime.Status;
21
import org.eclipse.jface.dialogs.MessageDialog;
22
import org.eclipse.jface.text.TextSelection;
23
import org.eclipse.jface.viewers.IStructuredSelection;
24
import org.eclipse.ui.IEditorInput;
25
import org.eclipse.ui.IEditorPart;
26
import org.eclipse.ui.handlers.HandlerUtil;
27
import org.eclipse.ui.texteditor.IDocumentProvider;
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.annotatedlineeditor.IEntityPersistenceService;
52
import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
53
import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
54
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
55
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
56
import eu.etaxonomy.taxeditor.bulkeditor.l10n.Messages;
57
import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
58
import eu.etaxonomy.taxeditor.model.MessagingUtils;
59
import eu.etaxonomy.taxeditor.store.CdmStore;
60
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
61

    
62

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

    
70

    
71

    
72
    private static final String SKIP = "Skip";
73
    private static final String DELETE = "Delete";
74
    private static final String CONFIRM_DELETION = "Confirm Deletion";
75

    
76
    /* (non-Javadoc)
77
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
78
	 */
79
	@Override
80
	public Object execute(ExecutionEvent event) throws ExecutionException {
81

    
82

    
83
		TextSelection selection = (TextSelection) HandlerUtil.getCurrentSelection(event);
84
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
85

    
86
		IEditorInput input = editor.getEditorInput();
87

    
88
		if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
89

    
90

    
91
			IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
92
			LineAnnotationModel model =
93
					(LineAnnotationModel) provider.getAnnotationModel(input);
94

    
95

    
96
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
97
			DeleteConfiguratorBase config = null;
98
			IEntityPersistenceService<?> persistenceService = (IEntityPersistenceService<?>) input;
99
			DeleteResult result = new DeleteResult();
100
			String errorMessage= "The object ";
101
			for(Object object : structuredSelection.toList()){
102
			    if (object instanceof CdmBase){
103
			        CdmBase base = (CdmBase)object;
104
			        LineAnnotation<?> annotation = (LineAnnotation<?>) model.getAnnotation(object);
105
			        if (base.getId() != 0){
106

    
107

    
108
        				try {
109
        				    ICdmRepository controller;
110
        				    controller = CdmStore.getCurrentApplicationConfiguration();
111
        				    if (object instanceof SpecimenOrObservationBase){
112
        				        IOccurrenceService service = controller.getOccurrenceService();
113
        				        config = new SpecimenDeleteConfigurator();
114

    
115
                                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  Messages.BulkEditor_DeleteHandler_reallyDeleteSpecimen, MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
116
                                int result_dialog= dialog.open();
117
                                if (result_dialog != Status.OK){
118
                                    return null;
119
                                }
120
        						if (object != null){
121
        							result = service.isDeletable(((SpecimenOrObservationBase<?>) object).getUuid(), config);
122
        							errorMessage = "The specimen or observation ";
123

    
124
        						}
125
        				    } else if (object instanceof Reference){
126
        						IReferenceService service = controller.getReferenceService();
127
        						if (object != null){
128
        							result = service.isDeletable(((Reference)object).getUuid(), null);
129
        							errorMessage = "The reference ";
130
        						}
131

    
132
        				    } else if (object instanceof Group){
133
        				        errorMessage = "The group ";
134
        				    }else if (object instanceof User){
135
        				        errorMessage = "The user ";
136
         				    } else if (object instanceof TaxonName){
137
         				        TaxonName name = HibernateProxyHelper.deproxy(object, TaxonName.class);
138
         				        if (object != null){
139
         				            config = new NameDeletionConfigurator();
140

    
141
         				            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);
142
         				            int result_dialog= dialog.open();
143
         				            if (result_dialog != Status.OK){
144
         				                return null;
145
         				            }
146
         				            result = controller.getNameService().isDeletable(name.getUuid(), config);
147
         				            errorMessage = "The name ";
148

    
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 = "The synonym ";
157
        							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);
158
                                    int result_dialog= dialog.open();
159
                                    if (result_dialog != Status.OK){
160
                                         return null;
161
                                    }
162
                                    result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
163

    
164
        						}
165
        						else if(object instanceof Taxon ){
166
        							Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
167
        							if (((Taxon)object).getTaxonNodes().isEmpty()){
168
                                            errorMessage = "The taxon ";
169
        							} else{
170
        							    MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
171
        							    return null;
172
        							}
173
        							config = new TaxonDeletionConfigurator();
174
        							((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
175
        							DeleteConfiguratorDialog dialog;
176
        						    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);
177
        							int result_dialog= dialog.open();
178
                                    if (result_dialog != Status.OK){
179
                                         return null;
180
                                    }
181
                                    result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
182

    
183
        						}
184
         				    } else if (object instanceof TeamOrPersonBase){
185
         				        result = controller.getAgentService().isDeletable(((CdmBase) object).getUuid(), null);
186
    						  errorMessage = "The team or person ";
187
         				   } else if (object instanceof Media){
188
         				       config = new MediaDeletionConfigurator();
189
         				       Media media = HibernateProxyHelper.deproxy(object, Media.class);
190
         				       DeleteConfiguratorDialog dialog;
191
        					   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);
192
        					   int result_dialog= dialog.open();
193
                               if (result_dialog != Status.OK){
194
                                   return null;
195
                               }
196

    
197
                               result = controller.getMediaService().isDeletable(media.getUuid(), config);
198
                               errorMessage = "The media ";
199

    
200
                           }
201

    
202

    
203
    				} catch (Exception e){
204
    					MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
205
    				}
206
    				if (result.isError() || result.isAbort()){
207
    					if (!result.getExceptions().isEmpty()) {
208
    						List<String> messages = new ArrayList<String>();
209
    						int i = result.getExceptions().size();
210
    						for (Exception e:result.getExceptions()){
211
    							messages.add(e.getMessage());
212
    						}
213
    						errorMessage += "could not be deleted.";
214
    						//MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
215
    						DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
216
    					}else{
217
    						MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
218
    					}
219
    				}else if (model != null) {
220
    					annotation.markAsDeleted(config);
221
    				}
222
//    					int offset = selection.getOffset();
223
//    					if (offset == 0){
224
//    						offset = 1;
225
//    					}
226
//                        Iterator iter = model.getAnnotationIterator(offset, selection.getLength(), true, true);
227
//                   //     Iterator<Annotation> iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
228
//                        while (iter.hasNext()) {
229
//                            Object next = iter.next();
230
//                            if (next instanceof LineAnnotation) {
231
//                                if (result.isOk()){
232
//                                    ((LineAnnotation)next).markAsDeleted(config);
233
//
234
//                                }
235
//
236
//                            }
237
//                        }
238
//                    }
239

    
240
			    }
241
		        if (result.isOk() ){
242

    
243
                    ((BulkEditor) editor).removeAnnotatedLine(annotation);
244

    
245
                    if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
246
                        List<String> messages = new ArrayList<String>();
247
                        int i = result.getExceptions().size();
248
                        for (Exception e:result.getExceptions()){
249
                            messages.add(e.getMessage());
250
                        }
251
                        errorMessage += "can be deleted but related object(s) could not be deleted. ";
252
                        //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
253
                        DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
254
                    }
255

    
256
                }
257

    
258
			    }
259

    
260
			}
261
		}
262

    
263

    
264
		return null;
265
	}
266

    
267

    
268
}
(3-3/9)