Project

General

Profile

Download (11.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
import java.util.ArrayList;
13
import java.util.Iterator;
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.Status;
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.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.TaxonNameBase;
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.annotatedlineeditor.IEntityPersistenceService;
50
import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
51
import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
52
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
53
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
54
import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
55
import eu.etaxonomy.taxeditor.model.MessagingUtils;
56
import eu.etaxonomy.taxeditor.store.CdmStore;
57
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
58

    
59

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

    
67

    
68

    
69
    private static final String SKIP = "Skip";
70
    private static final String DELETE = "Delete";
71
    private static final String CONFIRM_DELETION = "Confirm Deletion";
72

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

    
79

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

    
83
		IEditorInput input = editor.getEditorInput();
84

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

    
87

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

    
92

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

    
104

    
105
        				try {
106
        				    ICdmRepository controller;
107
        				    controller = CdmStore.getCurrentApplicationConfiguration();
108
        				    if (object instanceof SpecimenOrObservationBase){
109
        				        IOccurrenceService service = controller.getOccurrenceService();
110
        						if (object != null){
111
        							result = service.isDeletable(((SpecimenOrObservationBase) object).getUuid(), null);
112
        							errorMessage = "The specimen or observation ";
113

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

    
122
        				    } else if (object instanceof Group){
123
        				        errorMessage = "The group ";
124
        				    }else if (object instanceof User){
125
        				        errorMessage = "The user ";
126
         				    } else if (object instanceof TaxonNameBase){
127
         				        TaxonNameBase name = HibernateProxyHelper.deproxy(object, TaxonNameBase.class);
128
         				        if (object != null){
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 != Status.OK){
134
         				                return null;
135
         				            }
136
         				            result = controller.getNameService().isDeletable(name.getUuid(), config);
137
         				            errorMessage = "The name ";
138

    
139
         				        }
140
         				    } else if (object instanceof TaxonBase){
141

    
142
        						// synonym
143
        						if(object instanceof Synonym){
144
        							Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
145
        							config = new SynonymDeletionConfigurator();
146
        							errorMessage = "The synonym ";
147
        							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);
148
                                    int result_dialog= dialog.open();
149
                                    if (result_dialog != Status.OK){
150
                                         return null;
151
                                    }
152
                                    result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
153

    
154
        						}
155
        						else if(object instanceof Taxon ){
156
        							Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
157
        							if (((Taxon)object).getTaxonNodes().isEmpty()){
158
                                            errorMessage = "The taxon ";
159
        							} else{
160
        							    MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
161
        							    return null;
162
        							}
163
        							config = new TaxonDeletionConfigurator();
164
        							((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
165
        							DeleteConfiguratorDialog dialog;
166
        						    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);
167
        							int result_dialog= dialog.open();
168
                                    if (result_dialog != Status.OK){
169
                                         return null;
170
                                    }
171
                                    result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
172

    
173
        						}
174
         				    } else if (object instanceof TeamOrPersonBase){
175
         				        result = controller.getAgentService().isDeletable(((CdmBase) object).getUuid(), null);
176
    						  errorMessage = "The team or person ";
177
         				   } else if (object instanceof Media){
178
         				       config = new MediaDeletionConfigurator();
179
         				       Media media = HibernateProxyHelper.deproxy(object, Media.class);
180
         				       DeleteConfiguratorDialog dialog;
181
        					   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);
182
        					   int result_dialog= dialog.open();
183
                               if (result_dialog != Status.OK){
184
                                   return null;
185
                               }
186

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

    
190
                           }
191

    
192

    
193
    				} catch (Exception e){
194
    					MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
195
    				}
196
    				if (result.isError() || result.isAbort()){
197
    					if (!result.getExceptions().isEmpty()) {
198
    						List<String> messages = new ArrayList<String>();
199
    						int i = result.getExceptions().size();
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
    				}else if (model != null) {
210
                        Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
211
                        while (iter.hasNext()) {
212
                            Object next = iter.next();
213
                            if (next instanceof LineAnnotation) {
214
                                if (result.isOk()){
215
                                    ((LineAnnotation)next).markAsDeleted(config);
216

    
217
                                }
218

    
219
                            }
220
                        }
221
                    }
222

    
223
			    }
224
		        if (result.isOk() ){
225

    
226
                    ((BulkEditor) editor).removeAnnotatedLine(annotation);
227

    
228
                    if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
229
                        List<String> messages = new ArrayList<String>();
230
                        int i = result.getExceptions().size();
231
                        for (Exception e:result.getExceptions()){
232
                            messages.add(e.getMessage());
233
                        }
234
                        errorMessage += "can be deleted but related object(s) could not be deleted. ";
235
                        //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
236
                        DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
237
                    }
238

    
239
                }
240

    
241
			    }
242

    
243
			}
244
		}
245

    
246

    
247
		return null;
248
	}
249

    
250

    
251
}
(3-3/9)