fix #5692: adapt delete algorithm to annotatedLineEditor mechanisms
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / handler / DeleteHandler.java
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.commands.common.NotDefinedException;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.text.TextSelection;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.ui.IEditorInput;
28 import org.eclipse.ui.IEditorPart;
29 import org.eclipse.ui.handlers.HandlerUtil;
30 import org.eclipse.ui.texteditor.IDocumentProvider;
31
32 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
33 import eu.etaxonomy.cdm.api.service.DeleteResult;
34 import eu.etaxonomy.cdm.api.service.IAgentService;
35 import eu.etaxonomy.cdm.api.service.IGroupService;
36 import eu.etaxonomy.cdm.api.service.IMediaService;
37 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
38 import eu.etaxonomy.cdm.api.service.IReferenceService;
39 import eu.etaxonomy.cdm.api.service.IUserService;
40 import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
41 import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
42 import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
43 import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
44 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
45 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
46 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
47 import eu.etaxonomy.cdm.model.common.Group;
48 import eu.etaxonomy.cdm.model.common.User;
49 import eu.etaxonomy.cdm.model.media.Media;
50 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
51 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
52 import eu.etaxonomy.cdm.model.reference.Reference;
53 import eu.etaxonomy.cdm.model.taxon.Synonym;
54 import eu.etaxonomy.cdm.model.taxon.Taxon;
55 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
56 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
57 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
58 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
59 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
60 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
61 import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorConstants;
62 import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
63 import eu.etaxonomy.taxeditor.bulkeditor.operation.DeleteTaxonBaseOperation;
64 import eu.etaxonomy.taxeditor.bulkeditor.operation.DeleteTaxonNameOperation;
65 import eu.etaxonomy.taxeditor.model.AbstractUtility;
66 import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
67 import eu.etaxonomy.taxeditor.model.MessagingUtils;
68 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
69 import eu.etaxonomy.taxeditor.operation.CdmDeleteOperation;
70 import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
71 import eu.etaxonomy.taxeditor.store.CdmStore;
72 import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
73
74
75 /**
76 * @author n.hoffmann
77 * @created Mar 11, 2011
78 * @version 1.0
79 */
80 public class DeleteHandler extends AbstractHandler {
81
82
83
84 /* (non-Javadoc)
85 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
86 */
87 @Override
88 public Object execute(ExecutionEvent event) throws ExecutionException {
89
90
91 TextSelection selection = (TextSelection) HandlerUtil.getCurrentSelection(event);
92 IEditorPart editor = HandlerUtil.getActiveEditor(event);
93
94 IEditorInput input = editor.getEditorInput();
95
96 if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
97
98
99 IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
100 LineAnnotationModel model =
101 (LineAnnotationModel) provider.getAnnotationModel(input);
102
103
104 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
105
106 IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
107
108 for(Object object : structuredSelection.toList()){
109
110 LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
111 DeleteResult result = new DeleteResult();
112 String errorMessage= "The object ";
113 //result.setError();
114 try {
115 ICdmApplicationConfiguration controller;
116 controller = CdmStore.getCurrentApplicationConfiguration();
117
118 if (object instanceof SpecimenOrObservationBase){
119 IOccurrenceService service = controller.getOccurrenceService();
120 if (object != null){
121 result = service.isDeletable(((SpecimenOrObservationBase) object), null);
122 errorMessage = "The specimen or observation ";
123 if (model != null) {
124 Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
125 while (iter.hasNext()) {
126 Object next = iter.next();
127 if (next instanceof LineAnnotation) {
128 if (result.isOk()){
129 ((LineAnnotation)next).markAsDeleted(null);
130 }
131
132 }
133 }
134 }
135 }
136 } else if (object instanceof Reference){
137 IReferenceService service = controller.getReferenceService();
138 if (object != null){
139 result = service.isDeletable((Reference)object, null);
140 errorMessage = "The reference ";
141 }
142 if (model != null) {
143 Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
144 while (iter.hasNext()) {
145 Object next = iter.next();
146 if (next instanceof LineAnnotation) {
147 if (result.isOk()){
148 ((LineAnnotation)next).markAsDeleted(null);
149 }
150
151 }
152 }
153 }
154
155 } else if (object instanceof Group){
156 if (model != null) {
157 Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
158 while (iter.hasNext()) {
159 Object next = iter.next();
160 if (next instanceof LineAnnotation) {
161 if (result.isOk()){
162 ((LineAnnotation)next).markAsDeleted(null);
163 }
164
165 }
166 }
167 }
168
169 // IGroupService service = controller.getGroupService();
170 // if (object != null){
171 // result = service.delete(((Group) object).getUuid());
172 // errorMessage = "The group ";
173 // }
174 }else if (object instanceof User){
175 if (model != null) {
176 Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
177 while (iter.hasNext()) {
178 Object next = iter.next();
179 if (next instanceof LineAnnotation) {
180 if (result.isOk()){
181 ((LineAnnotation)next).markAsDeleted(null);
182 }
183
184 }
185 }
186 }
187
188 // IUserService service = controller.getUserService();
189 // if (object != null){
190 // result = service.delete(((User) object).getUuid());
191 // errorMessage = "The user ";
192 // }
193 } else if (object instanceof TaxonNameBase){
194 TaxonNameBase name = HibernateProxyHelper.deproxy(object, TaxonNameBase.class);
195
196
197 if (object != null){
198 NameDeletionConfigurator config = new NameDeletionConfigurator();
199
200 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);
201 int result_dialog= dialog.open();
202 if (result_dialog != Status.OK){
203 return null;
204 }
205
206 //operation = new DeleteTaxonNameOperation(commandName,((BulkEditor) editor).getUndoContext() , name, config, (BulkEditor)editor);
207 errorMessage = "The name ";
208
209 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 result = controller.getNameService().isDeletable(name, config);
215 if (result.isOk()){
216 ((LineAnnotation)next).markAsDeleted(config);
217 }
218
219 }
220 }
221 }
222 }
223 } else if (object instanceof TaxonBase){
224
225 // synonym
226 if(object instanceof Synonym){
227 Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
228 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
229 errorMessage = "The synonym ";
230 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);
231 int result_dialog= dialog.open();
232 if (result_dialog != Status.OK){
233 return null;
234 }
235 //operation = new DeleteTaxonBaseOperation(commandName,((BulkEditor) editor).getUndoContext() , synonym, config, (BulkEditor)editor);
236 if (model != null) {
237 Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
238 while (iter.hasNext()) {
239 Object next = iter.next();
240 if (next instanceof LineAnnotation) {
241 result = controller.getTaxonService().isDeletable(synonym, config);
242 if (result.isOk()){
243 ((LineAnnotation)next).markAsDeleted(config);
244 }
245
246 }
247 }
248 }
249 }
250
251 else if(object instanceof Taxon ){
252 Taxon taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
253 if (((Taxon)object).getTaxonNodes().isEmpty()){
254
255 errorMessage = "The taxon ";
256 } else{
257 MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
258 return null;
259 }
260 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
261 config.setDeleteInAllClassifications(true);
262 DeleteConfiguratorDialog dialog;
263 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);
264 int result_dialog= dialog.open();
265 if (result_dialog != Status.OK){
266 return null;
267 }
268 //operation = new DeleteTaxonBaseOperation(commandName, ((BulkEditor) editor).getUndoContext(), taxon, config, (BulkEditor)editor);
269 if (model != null) {
270 Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
271 while (iter.hasNext()) {
272 Object next = iter.next();
273 if (next instanceof LineAnnotation) {
274 result = controller.getTaxonService().isDeletable(taxon, config);
275 if (result.isOk()){
276 ((LineAnnotation)next).markAsDeleted(config);
277 }
278
279 }
280 }
281 }
282 }
283
284
285 // ITaxonService service = controller.getTaxonService();
286 // if (object != null){
287 // if (object instanceof Taxon){
288 // TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
289 // config.setDeleteInAllClassifications(true);
290 // DeleteConfiguratorDialog dialog;
291 // if (((Taxon)object).getTaxonNodes().isEmpty()){
292 // 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);
293 // int result_dialog= dialog.open();
294 // if (result_dialog != Status.OK){
295 // return null;
296 // }
297 // result = service.deleteTaxon(((TaxonBase) object).getUuid(), config, null);
298 // errorMessage = "The taxon ";
299 // } else{
300 // MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
301 // return null;
302 // }
303 //
304 // }else{
305 // SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
306 // 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);
307 // int result_dialog= dialog.open();
308 // if (result_dialog != Status.OK){
309 // return null;
310 // }
311 // result = service.deleteSynonym(((Synonym)object).getUuid(), config);
312 // errorMessage = "The synonym ";
313 // }
314 // }
315 } else if (object instanceof TeamOrPersonBase){
316
317 if (model != null) {
318 Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
319 while (iter.hasNext()) {
320 Object next = iter.next();
321 if (next instanceof LineAnnotation) {
322
323 result = controller.getAgentService().isDeletable((TeamOrPersonBase) object, null);
324 if (result.isOk()){
325 ((LineAnnotation)next).markAsDeleted(null);
326 }
327
328 }
329 }
330 }
331 //IAgentService service = controller.getAgentService();
332 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
333 //result = service.delete(((TeamOrPersonBase)object).getUuid());
334 errorMessage = "The team or person ";
335 } else if (object instanceof Media){
336 MediaDeletionConfigurator config = new MediaDeletionConfigurator();
337
338 DeleteConfiguratorDialog dialog;
339 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);
340 int result_dialog= dialog.open();
341 if (result_dialog != Status.OK){
342 return null;
343 }
344 IMediaService service = controller.getMediaService();
345 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
346
347 errorMessage = "The media ";
348
349 if (model != null) {
350 Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
351 while (iter.hasNext()) {
352 Object next = iter.next();
353 if (next instanceof LineAnnotation) {
354 result = service.isDeletable(((Media)object), config);
355 if (result.isOk()){
356 ((LineAnnotation)next).markAsDeleted(null);
357 }
358
359 }
360 }
361 }
362 }
363
364 } catch (Exception e){
365 MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
366 }
367 if (result.isError() || result.isAbort()){
368 if (!result.getExceptions().isEmpty()) {
369 List<String> messages = new ArrayList<String>();
370 int i = result.getExceptions().size();
371 for (Exception e:result.getExceptions()){
372 messages.add(e.getMessage());
373 }
374 errorMessage += "could not be deleted.";
375 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
376 DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
377 }else{
378 MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
379 }
380 }
381
382
383 if (result.isOk() ){
384
385 ((BulkEditor) editor).removeAnnotatedLine(annotation);
386
387 if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
388 List<String> messages = new ArrayList<String>();
389 int i = result.getExceptions().size();
390 for (Exception e:result.getExceptions()){
391 messages.add(e.getMessage());
392 }
393 errorMessage += "can be deleted but related object(s) could not be deleted. ";
394 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
395 DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
396 }
397
398 }
399
400 }
401 }
402
403
404 return null;
405 }
406
407
408 }