1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.taxeditor.bulkeditor.handler;
|
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.ICdmApplicationConfiguration;
|
30
|
import eu.etaxonomy.cdm.api.service.DeleteResult;
|
31
|
import eu.etaxonomy.cdm.api.service.IMediaService;
|
32
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
33
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
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.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.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
|
|
91
|
IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
|
92
|
|
93
|
for(Object object : structuredSelection.toList()){
|
94
|
|
95
|
LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
|
96
|
DeleteResult result = new DeleteResult();
|
97
|
String errorMessage= "The object ";
|
98
|
//result.setError();
|
99
|
try {
|
100
|
ICdmApplicationConfiguration controller;
|
101
|
controller = CdmStore.getCurrentApplicationConfiguration();
|
102
|
|
103
|
if (object instanceof SpecimenOrObservationBase){
|
104
|
IOccurrenceService service = controller.getOccurrenceService();
|
105
|
if (object != null){
|
106
|
result = service.isDeletable(((SpecimenOrObservationBase) object), null);
|
107
|
errorMessage = "The specimen or observation ";
|
108
|
if (model != null) {
|
109
|
Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
|
110
|
while (iter.hasNext()) {
|
111
|
Object next = iter.next();
|
112
|
if (next instanceof LineAnnotation) {
|
113
|
if (result.isOk()){
|
114
|
((LineAnnotation)next).markAsDeleted(null);
|
115
|
}
|
116
|
|
117
|
}
|
118
|
}
|
119
|
}
|
120
|
}
|
121
|
} else if (object instanceof Reference){
|
122
|
IReferenceService service = controller.getReferenceService();
|
123
|
if (object != null){
|
124
|
result = service.isDeletable((Reference)object, null);
|
125
|
errorMessage = "The reference ";
|
126
|
}
|
127
|
if (model != null) {
|
128
|
Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
|
129
|
while (iter.hasNext()) {
|
130
|
Object next = iter.next();
|
131
|
if (next instanceof LineAnnotation) {
|
132
|
if (result.isOk()){
|
133
|
((LineAnnotation)next).markAsDeleted(null);
|
134
|
}
|
135
|
|
136
|
}
|
137
|
}
|
138
|
}
|
139
|
|
140
|
} else if (object instanceof Group){
|
141
|
if (model != null) {
|
142
|
Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
|
143
|
while (iter.hasNext()) {
|
144
|
Object next = iter.next();
|
145
|
if (next instanceof LineAnnotation) {
|
146
|
if (result.isOk()){
|
147
|
((LineAnnotation)next).markAsDeleted(null);
|
148
|
}
|
149
|
|
150
|
}
|
151
|
}
|
152
|
}
|
153
|
|
154
|
// IGroupService service = controller.getGroupService();
|
155
|
// if (object != null){
|
156
|
// result = service.delete(((Group) object).getUuid());
|
157
|
// errorMessage = "The group ";
|
158
|
// }
|
159
|
}else if (object instanceof User){
|
160
|
if (model != null) {
|
161
|
Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
|
162
|
while (iter.hasNext()) {
|
163
|
Object next = iter.next();
|
164
|
if (next instanceof LineAnnotation) {
|
165
|
if (result.isOk()){
|
166
|
((LineAnnotation)next).markAsDeleted(null);
|
167
|
}
|
168
|
|
169
|
}
|
170
|
}
|
171
|
}
|
172
|
|
173
|
// IUserService service = controller.getUserService();
|
174
|
// if (object != null){
|
175
|
// result = service.delete(((User) object).getUuid());
|
176
|
// errorMessage = "The user ";
|
177
|
// }
|
178
|
} else if (object instanceof TaxonNameBase){
|
179
|
TaxonNameBase name = HibernateProxyHelper.deproxy(object, TaxonNameBase.class);
|
180
|
|
181
|
|
182
|
if (object != null){
|
183
|
NameDeletionConfigurator config = new NameDeletionConfigurator();
|
184
|
|
185
|
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);
|
186
|
int result_dialog= dialog.open();
|
187
|
if (result_dialog != Status.OK){
|
188
|
return null;
|
189
|
}
|
190
|
|
191
|
//operation = new DeleteTaxonNameOperation(commandName,((BulkEditor) editor).getUndoContext() , name, config, (BulkEditor)editor);
|
192
|
errorMessage = "The name ";
|
193
|
|
194
|
if (model != null) {
|
195
|
Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
|
196
|
while (iter.hasNext()) {
|
197
|
Object next = iter.next();
|
198
|
if (next instanceof LineAnnotation) {
|
199
|
result = controller.getNameService().isDeletable(name, config);
|
200
|
if (result.isOk()){
|
201
|
((LineAnnotation)next).markAsDeleted(config);
|
202
|
}
|
203
|
|
204
|
}
|
205
|
}
|
206
|
}
|
207
|
}
|
208
|
} else if (object instanceof TaxonBase){
|
209
|
|
210
|
// synonym
|
211
|
if(object instanceof Synonym){
|
212
|
Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
|
213
|
SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
|
214
|
errorMessage = "The synonym ";
|
215
|
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);
|
216
|
int result_dialog= dialog.open();
|
217
|
if (result_dialog != Status.OK){
|
218
|
return null;
|
219
|
}
|
220
|
//operation = new DeleteTaxonBaseOperation(commandName,((BulkEditor) editor).getUndoContext() , synonym, config, (BulkEditor)editor);
|
221
|
if (model != null) {
|
222
|
Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
|
223
|
while (iter.hasNext()) {
|
224
|
Object next = iter.next();
|
225
|
if (next instanceof LineAnnotation) {
|
226
|
result = controller.getTaxonService().isDeletable(synonym, config);
|
227
|
if (result.isOk()){
|
228
|
((LineAnnotation)next).markAsDeleted(config);
|
229
|
}
|
230
|
|
231
|
}
|
232
|
}
|
233
|
}
|
234
|
}
|
235
|
|
236
|
else if(object instanceof Taxon ){
|
237
|
Taxon taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
|
238
|
if (((Taxon)object).getTaxonNodes().isEmpty()){
|
239
|
|
240
|
errorMessage = "The taxon ";
|
241
|
} else{
|
242
|
MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
|
243
|
return null;
|
244
|
}
|
245
|
TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
|
246
|
config.setDeleteInAllClassifications(true);
|
247
|
DeleteConfiguratorDialog dialog;
|
248
|
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);
|
249
|
int result_dialog= dialog.open();
|
250
|
if (result_dialog != Status.OK){
|
251
|
return null;
|
252
|
}
|
253
|
//operation = new DeleteTaxonBaseOperation(commandName, ((BulkEditor) editor).getUndoContext(), taxon, config, (BulkEditor)editor);
|
254
|
if (model != null) {
|
255
|
Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
|
256
|
while (iter.hasNext()) {
|
257
|
Object next = iter.next();
|
258
|
if (next instanceof LineAnnotation) {
|
259
|
result = controller.getTaxonService().isDeletable(taxon, config);
|
260
|
if (result.isOk()){
|
261
|
((LineAnnotation)next).markAsDeleted(config);
|
262
|
}
|
263
|
|
264
|
}
|
265
|
}
|
266
|
}
|
267
|
}
|
268
|
|
269
|
|
270
|
// ITaxonService service = controller.getTaxonService();
|
271
|
// if (object != null){
|
272
|
// if (object instanceof Taxon){
|
273
|
// TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
|
274
|
// config.setDeleteInAllClassifications(true);
|
275
|
// DeleteConfiguratorDialog dialog;
|
276
|
// if (((Taxon)object).getTaxonNodes().isEmpty()){
|
277
|
// 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);
|
278
|
// int result_dialog= dialog.open();
|
279
|
// if (result_dialog != Status.OK){
|
280
|
// return null;
|
281
|
// }
|
282
|
// result = service.deleteTaxon(((TaxonBase) object).getUuid(), config, null);
|
283
|
// errorMessage = "The taxon ";
|
284
|
// } else{
|
285
|
// MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
|
286
|
// return null;
|
287
|
// }
|
288
|
//
|
289
|
// }else{
|
290
|
// SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
|
291
|
// 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);
|
292
|
// int result_dialog= dialog.open();
|
293
|
// if (result_dialog != Status.OK){
|
294
|
// return null;
|
295
|
// }
|
296
|
// result = service.deleteSynonym(((Synonym)object).getUuid(), config);
|
297
|
// errorMessage = "The synonym ";
|
298
|
// }
|
299
|
// }
|
300
|
} else if (object instanceof TeamOrPersonBase){
|
301
|
|
302
|
if (model != null) {
|
303
|
Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
|
304
|
while (iter.hasNext()) {
|
305
|
Object next = iter.next();
|
306
|
if (next instanceof LineAnnotation) {
|
307
|
|
308
|
result = controller.getAgentService().isDeletable((TeamOrPersonBase) object, null);
|
309
|
if (result.isOk()){
|
310
|
((LineAnnotation)next).markAsDeleted(null);
|
311
|
}
|
312
|
|
313
|
}
|
314
|
}
|
315
|
}
|
316
|
//IAgentService service = controller.getAgentService();
|
317
|
//TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
|
318
|
//result = service.delete(((TeamOrPersonBase)object).getUuid());
|
319
|
errorMessage = "The team or person ";
|
320
|
} else if (object instanceof Media){
|
321
|
MediaDeletionConfigurator config = new MediaDeletionConfigurator();
|
322
|
|
323
|
DeleteConfiguratorDialog dialog;
|
324
|
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);
|
325
|
int result_dialog= dialog.open();
|
326
|
if (result_dialog != Status.OK){
|
327
|
return null;
|
328
|
}
|
329
|
IMediaService service = controller.getMediaService();
|
330
|
//TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
|
331
|
|
332
|
errorMessage = "The media ";
|
333
|
|
334
|
if (model != null) {
|
335
|
Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
|
336
|
while (iter.hasNext()) {
|
337
|
Object next = iter.next();
|
338
|
if (next instanceof LineAnnotation) {
|
339
|
result = service.isDeletable(((Media)object), config);
|
340
|
if (result.isOk()){
|
341
|
((LineAnnotation)next).markAsDeleted(config);
|
342
|
}
|
343
|
|
344
|
}
|
345
|
}
|
346
|
}
|
347
|
}
|
348
|
|
349
|
} catch (Exception e){
|
350
|
MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
|
351
|
}
|
352
|
if (result.isError() || result.isAbort()){
|
353
|
if (!result.getExceptions().isEmpty()) {
|
354
|
List<String> messages = new ArrayList<String>();
|
355
|
int i = result.getExceptions().size();
|
356
|
for (Exception e:result.getExceptions()){
|
357
|
messages.add(e.getMessage());
|
358
|
}
|
359
|
errorMessage += "could not be deleted.";
|
360
|
//MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
|
361
|
DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
|
362
|
}else{
|
363
|
MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
|
364
|
}
|
365
|
}
|
366
|
|
367
|
|
368
|
if (result.isOk() ){
|
369
|
|
370
|
((BulkEditor) editor).removeAnnotatedLine(annotation);
|
371
|
|
372
|
if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
|
373
|
List<String> messages = new ArrayList<String>();
|
374
|
int i = result.getExceptions().size();
|
375
|
for (Exception e:result.getExceptions()){
|
376
|
messages.add(e.getMessage());
|
377
|
}
|
378
|
errorMessage += "can be deleted but related object(s) could not be deleted. ";
|
379
|
//MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
|
380
|
DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
|
381
|
}
|
382
|
|
383
|
}
|
384
|
|
385
|
}
|
386
|
}
|
387
|
|
388
|
|
389
|
return null;
|
390
|
}
|
391
|
|
392
|
|
393
|
}
|