3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.bulkeditor
.handler
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
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
;
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
;
77 * @created Mar 11, 2011
80 public class DeleteHandler
extends AbstractHandler
{
85 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
88 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
91 TextSelection selection
= (TextSelection
) HandlerUtil
.getCurrentSelection(event
);
92 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
94 IEditorInput input
= editor
.getEditorInput();
96 if((input
instanceof IEntityPersistenceService
) && (selection
instanceof IStructuredSelection
)){
99 IDocumentProvider provider
= ((BulkEditor
) editor
).getDocumentProvider();
100 LineAnnotationModel model
=
101 (LineAnnotationModel
) provider
.getAnnotationModel(input
);
104 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
106 IEntityPersistenceService persistenceService
= (IEntityPersistenceService
) input
;
108 for(Object object
: structuredSelection
.toList()){
110 LineAnnotation annotation
= (LineAnnotation
) model
.getAnnotation(object
);
111 DeleteResult result
= new DeleteResult();
112 String errorMessage
= "The object ";
115 ICdmApplicationConfiguration controller
;
116 controller
= CdmStore
.getCurrentApplicationConfiguration();
118 if (object
instanceof SpecimenOrObservationBase
){
119 IOccurrenceService service
= controller
.getOccurrenceService();
121 result
= service
.isDeletable(((SpecimenOrObservationBase
) object
), null);
122 errorMessage
= "The specimen or observation ";
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
) {
129 ((LineAnnotation
)next
).markAsDeleted(null);
136 } else if (object
instanceof Reference
){
137 IReferenceService service
= controller
.getReferenceService();
139 result
= service
.isDeletable((Reference
)object
, null);
140 errorMessage
= "The reference ";
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
) {
148 ((LineAnnotation
)next
).markAsDeleted(null);
155 } else if (object
instanceof Group
){
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
) {
162 ((LineAnnotation
)next
).markAsDeleted(null);
169 // IGroupService service = controller.getGroupService();
170 // if (object != null){
171 // result = service.delete(((Group) object).getUuid());
172 // errorMessage = "The group ";
174 }else if (object
instanceof User
){
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
) {
181 ((LineAnnotation
)next
).markAsDeleted(null);
188 // IUserService service = controller.getUserService();
189 // if (object != null){
190 // result = service.delete(((User) object).getUuid());
191 // errorMessage = "The user ";
193 } else if (object
instanceof TaxonNameBase
){
194 TaxonNameBase name
= HibernateProxyHelper
.deproxy(object
, TaxonNameBase
.class);
198 NameDeletionConfigurator config
= new NameDeletionConfigurator();
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
){
206 //operation = new DeleteTaxonNameOperation(commandName,((BulkEditor) editor).getUndoContext() , name, config, (BulkEditor)editor);
207 errorMessage
= "The name ";
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
);
216 ((LineAnnotation
)next
).markAsDeleted(config
);
223 } else if (object
instanceof TaxonBase
){
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
){
235 //operation = new DeleteTaxonBaseOperation(commandName,((BulkEditor) editor).getUndoContext() , synonym, config, (BulkEditor)editor);
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
);
243 ((LineAnnotation
)next
).markAsDeleted(config
);
251 else if(object
instanceof Taxon
){
252 Taxon taxon
= HibernateProxyHelper
.deproxy(object
, Taxon
.class);
253 if (((Taxon
)object
).getTaxonNodes().isEmpty()){
255 errorMessage
= "The taxon ";
257 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
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
){
268 //operation = new DeleteTaxonBaseOperation(commandName, ((BulkEditor) editor).getUndoContext(), taxon, config, (BulkEditor)editor);
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
);
276 ((LineAnnotation
)next
).markAsDeleted(config
);
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){
297 // result = service.deleteTaxon(((TaxonBase) object).getUuid(), config, null);
298 // errorMessage = "The taxon ";
300 // MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
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){
311 // result = service.deleteSynonym(((Synonym)object).getUuid(), config);
312 // errorMessage = "The synonym ";
315 } else if (object
instanceof TeamOrPersonBase
){
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
) {
323 result
= controller
.getAgentService().isDeletable((TeamOrPersonBase
) object
, null);
325 ((LineAnnotation
)next
).markAsDeleted(null);
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();
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
){
344 IMediaService service
= controller
.getMediaService();
345 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
347 errorMessage
= "The media ";
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
);
356 ((LineAnnotation
)next
).markAsDeleted(null);
364 } catch (Exception e
){
365 MessagingUtils
.errorDialog("Exception occured. Delete not possible", getClass(), e
.getMessage(), TaxeditorBulkeditorPlugin
.PLUGIN_ID
, null, true);
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());
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
);
378 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
385 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
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());
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
);