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.ICdmApplicationConfiguration;
|
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.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
|
/* (non-Javadoc)
|
70
|
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
|
71
|
*/
|
72
|
@Override
|
73
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
74
|
|
75
|
|
76
|
TextSelection selection = (TextSelection) HandlerUtil.getCurrentSelection(event);
|
77
|
IEditorPart editor = HandlerUtil.getActiveEditor(event);
|
78
|
|
79
|
IEditorInput input = editor.getEditorInput();
|
80
|
|
81
|
if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
|
82
|
|
83
|
|
84
|
IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
|
85
|
LineAnnotationModel model =
|
86
|
(LineAnnotationModel) provider.getAnnotationModel(input);
|
87
|
|
88
|
|
89
|
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
|
90
|
DeleteConfiguratorBase config = null;
|
91
|
IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
|
92
|
DeleteResult result = new DeleteResult();
|
93
|
String errorMessage= "The object ";
|
94
|
for(Object object : structuredSelection.toList()){
|
95
|
if (object instanceof CdmBase){
|
96
|
CdmBase base = (CdmBase)object;
|
97
|
LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
|
98
|
if (base.getId() != 0){
|
99
|
|
100
|
|
101
|
try {
|
102
|
ICdmApplicationConfiguration controller;
|
103
|
controller = CdmStore.getCurrentApplicationConfiguration();
|
104
|
if (object instanceof SpecimenOrObservationBase){
|
105
|
IOccurrenceService service = controller.getOccurrenceService();
|
106
|
if (object != null){
|
107
|
result = service.isDeletable(((SpecimenOrObservationBase) object).getUuid(), null);
|
108
|
errorMessage = "The specimen or observation ";
|
109
|
|
110
|
}
|
111
|
} else if (object instanceof Reference){
|
112
|
IReferenceService service = controller.getReferenceService();
|
113
|
if (object != null){
|
114
|
result = service.isDeletable(((Reference)object).getUuid(), null);
|
115
|
errorMessage = "The reference ";
|
116
|
}
|
117
|
|
118
|
} else if (object instanceof Group){
|
119
|
errorMessage = "The group ";
|
120
|
}else if (object instanceof User){
|
121
|
errorMessage = "The user ";
|
122
|
} else if (object instanceof TaxonNameBase){
|
123
|
TaxonNameBase name = HibernateProxyHelper.deproxy(object, TaxonNameBase.class);
|
124
|
if (object != null){
|
125
|
config = new NameDeletionConfigurator();
|
126
|
|
127
|
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);
|
128
|
int result_dialog= dialog.open();
|
129
|
if (result_dialog != Status.OK){
|
130
|
return null;
|
131
|
}
|
132
|
result = controller.getNameService().isDeletable(name.getUuid(), config);
|
133
|
errorMessage = "The name ";
|
134
|
|
135
|
}
|
136
|
} else if (object instanceof TaxonBase){
|
137
|
|
138
|
// synonym
|
139
|
if(object instanceof Synonym){
|
140
|
Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
|
141
|
config = new SynonymDeletionConfigurator();
|
142
|
errorMessage = "The synonym ";
|
143
|
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);
|
144
|
int result_dialog= dialog.open();
|
145
|
if (result_dialog != Status.OK){
|
146
|
return null;
|
147
|
}
|
148
|
result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
|
149
|
|
150
|
}
|
151
|
else if(object instanceof Taxon ){
|
152
|
Taxon taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
|
153
|
if (((Taxon)object).getTaxonNodes().isEmpty()){
|
154
|
errorMessage = "The taxon ";
|
155
|
} else{
|
156
|
MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
|
157
|
return null;
|
158
|
}
|
159
|
config = new TaxonDeletionConfigurator();
|
160
|
((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
|
161
|
DeleteConfiguratorDialog dialog;
|
162
|
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);
|
163
|
int result_dialog= dialog.open();
|
164
|
if (result_dialog != Status.OK){
|
165
|
return null;
|
166
|
}
|
167
|
result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
|
168
|
|
169
|
}
|
170
|
} else if (object instanceof TeamOrPersonBase){
|
171
|
result = controller.getAgentService().isDeletable(((CdmBase) object).getUuid(), null);
|
172
|
errorMessage = "The team or person ";
|
173
|
} else if (object instanceof Media){
|
174
|
config = new MediaDeletionConfigurator();
|
175
|
Media media = HibernateProxyHelper.deproxy(object, Media.class);
|
176
|
DeleteConfiguratorDialog dialog;
|
177
|
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);
|
178
|
int result_dialog= dialog.open();
|
179
|
if (result_dialog != Status.OK){
|
180
|
return null;
|
181
|
}
|
182
|
|
183
|
result = controller.getMediaService().isDeletable(media.getUuid(), config);
|
184
|
errorMessage = "The media ";
|
185
|
|
186
|
}
|
187
|
|
188
|
|
189
|
} catch (Exception e){
|
190
|
MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
|
191
|
}
|
192
|
if (result.isError() || result.isAbort()){
|
193
|
if (!result.getExceptions().isEmpty()) {
|
194
|
List<String> messages = new ArrayList<String>();
|
195
|
int i = result.getExceptions().size();
|
196
|
for (Exception e:result.getExceptions()){
|
197
|
messages.add(e.getMessage());
|
198
|
}
|
199
|
errorMessage += "could not be deleted.";
|
200
|
//MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
|
201
|
DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
|
202
|
}else{
|
203
|
MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
|
204
|
}
|
205
|
}else if (model != null) {
|
206
|
Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
|
207
|
while (iter.hasNext()) {
|
208
|
Object next = iter.next();
|
209
|
if (next instanceof LineAnnotation) {
|
210
|
if (result.isOk()){
|
211
|
((LineAnnotation)next).markAsDeleted(config);
|
212
|
|
213
|
}
|
214
|
|
215
|
}
|
216
|
}
|
217
|
}
|
218
|
|
219
|
}
|
220
|
if (result.isOk() ){
|
221
|
|
222
|
((BulkEditor) editor).removeAnnotatedLine(annotation);
|
223
|
|
224
|
if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
|
225
|
List<String> messages = new ArrayList<String>();
|
226
|
int i = result.getExceptions().size();
|
227
|
for (Exception e:result.getExceptions()){
|
228
|
messages.add(e.getMessage());
|
229
|
}
|
230
|
errorMessage += "can be deleted but related object(s) could not be deleted. ";
|
231
|
//MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
|
232
|
DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
|
233
|
}
|
234
|
|
235
|
}
|
236
|
|
237
|
}
|
238
|
|
239
|
}
|
240
|
}
|
241
|
|
242
|
|
243
|
return null;
|
244
|
}
|
245
|
|
246
|
|
247
|
}
|