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
.List
;
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
.jface
.dialogs
.MessageDialog
;
20 import org
.eclipse
.jface
.viewers
.ISelection
;
21 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
22 import org
.eclipse
.ui
.IEditorInput
;
23 import org
.eclipse
.ui
.IEditorPart
;
24 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
25 import org
.eclipse
.ui
.texteditor
.IDocumentProvider
;
27 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
28 import eu
.etaxonomy
.cdm
.api
.service
.DeleteResult
;
29 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
30 import eu
.etaxonomy
.cdm
.api
.service
.IGroupService
;
31 import eu
.etaxonomy
.cdm
.api
.service
.IMediaService
;
32 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
33 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
34 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
35 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
36 import eu
.etaxonomy
.cdm
.api
.service
.IUserService
;
37 import eu
.etaxonomy
.cdm
.api
.service
.config
.NameDeletionConfigurator
;
38 import eu
.etaxonomy
.cdm
.api
.service
.config
.SynonymDeletionConfigurator
;
39 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonDeletionConfigurator
;
40 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
41 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
42 import eu
.etaxonomy
.cdm
.model
.common
.User
;
43 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
44 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
45 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
46 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
49 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
50 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityPersistenceService
;
51 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotation
;
52 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotationModel
;
53 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
54 import eu
.etaxonomy
.taxeditor
.bulkeditor
.internal
.TaxeditorBulkeditorPlugin
;
55 import eu
.etaxonomy
.taxeditor
.model
.DeleteResultMessagingUtils
;
56 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
57 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
58 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.deleteConfigurator
.DeleteConfiguratorDialog
;
63 * @created Mar 11, 2011
66 public class DeleteHandler
extends AbstractHandler
{
69 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
72 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
74 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
76 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
78 IEditorInput input
= editor
.getEditorInput();
80 if((input
instanceof IEntityPersistenceService
) && (selection
instanceof IStructuredSelection
)){
83 IDocumentProvider provider
= ((BulkEditor
) editor
).getDocumentProvider();
84 LineAnnotationModel model
=
85 (LineAnnotationModel
) provider
.getAnnotationModel(input
);
88 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
90 IEntityPersistenceService persistenceService
= (IEntityPersistenceService
) input
;
93 for(Object object
: structuredSelection
.toList()){
95 LineAnnotation annotation
= (LineAnnotation
) model
.getAnnotation(object
);
96 DeleteResult result
= new DeleteResult();
97 String errorMessage
= "The object ";
100 ICdmApplicationConfiguration controller
;
101 controller
= CdmStore
.getCurrentApplicationConfiguration();
103 if (object
instanceof SpecimenOrObservationBase
){
104 IOccurrenceService service
= controller
.getOccurrenceService();
106 result
= service
.delete(((SpecimenOrObservationBase
) object
).getUuid());
107 errorMessage
= "The specimen or observation ";
109 } else if (object
instanceof Reference
){
110 IReferenceService service
= controller
.getReferenceService();
112 result
= service
.delete(((Reference
) object
).getUuid());
113 errorMessage
= "The reference ";
116 } else if (object
instanceof Group
){
117 IGroupService service
= controller
.getGroupService();
119 result
= service
.delete(((Group
) object
).getUuid());
120 errorMessage
= "The group ";
122 }else if (object
instanceof User
){
123 IUserService service
= controller
.getUserService();
125 result
= service
.delete(((User
) object
).getUuid());
126 errorMessage
= "The user ";
128 } else if (object
instanceof TaxonNameBase
){
129 INameService service
= controller
.getNameService();
131 NameDeletionConfigurator config
= new NameDeletionConfigurator();
133 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(config
, HandlerUtil
.getActiveShell(event
), "Confirm Deletion", null, "Do you really want to delete the name?", MessageDialog
.WARNING
, new String
[] { "Delete", "Skip" }, 0);
134 int result_dialog
= dialog
.open();
135 if (result_dialog
== 1){
138 result
= service
.delete(((TaxonNameBase
) object
).getUuid(), config
);
139 errorMessage
= "The name ";
141 } else if (object
instanceof TaxonBase
){
142 ITaxonService service
= controller
.getTaxonService();
144 if (object
instanceof Taxon
){
145 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
146 config
.setDeleteInAllClassifications(true);
147 DeleteConfiguratorDialog dialog
;
148 if (((Taxon
)object
).getTaxonNodes().isEmpty()){
149 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);
150 int result_dialog
= dialog
.open();
151 if (result_dialog
== 1){
154 result
= service
.deleteTaxon(((TaxonBase
) object
).getUuid(), config
, null);
155 errorMessage
= "The taxon ";
157 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
162 SynonymDeletionConfigurator config
= new SynonymDeletionConfigurator();
163 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);
164 int result_dialog
= dialog
.open();
165 if (result_dialog
== 1){
168 result
= service
.deleteSynonym(((Synonym
)object
).getUuid(), config
);
169 errorMessage
= "The synonym ";
172 } else if (object
instanceof TeamOrPersonBase
){
173 IAgentService service
= controller
.getAgentService();
174 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
175 result
= service
.delete(((TeamOrPersonBase
)object
).getUuid());
176 errorMessage
= "The team or person ";
177 } else if (object
instanceof Media
){
178 IMediaService service
= controller
.getMediaService();
179 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
180 result
= service
.delete(((Media
)object
).getUuid(), null);
181 errorMessage
= "The media ";
184 } catch (Exception e
){
185 MessagingUtils
.errorDialog("Exception occured. Delete not possible", getClass(), e
.getMessage(), TaxeditorBulkeditorPlugin
.PLUGIN_ID
, null, true);
187 if (result
.isError() || result
.isAbort()){
188 if (!result
.getExceptions().isEmpty()) {
189 List
<String
> messages
= new ArrayList
<String
>();
190 int i
= result
.getExceptions().size();
191 for (Exception e
:result
.getExceptions()){
192 messages
.add(e
.getMessage());
194 errorMessage
+= "could not be deleted.";
195 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
196 DeleteResultMessagingUtils
.messageDialogWithDetails(result
,errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);
198 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
202 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
203 if(result
.getUpdatedObjects().size() != 0 || !result
.getExceptions().isEmpty()){
204 List
<String
> messages
= new ArrayList
<String
>();
205 int i
= result
.getExceptions().size();
206 for (Exception e
:result
.getExceptions()){
207 messages
.add(e
.getMessage());
209 errorMessage
+= "was deleted but related object(s) could not be deleted. ";
210 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
211 DeleteResultMessagingUtils
.messageDialogWithDetails(result
, errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);