2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.bulkeditor
.handler
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Iterator
;
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
.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
;
28 import eu
.etaxonomy
.cdm
.api
.application
.ICdmRepository
;
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
.configurator
.deleteConfigurator
.DeleteConfiguratorDialog
;
62 * @created Mar 11, 2011
65 public class DeleteHandler
extends AbstractHandler
{
70 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
73 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
76 TextSelection selection
= (TextSelection
) HandlerUtil
.getCurrentSelection(event
);
77 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
79 IEditorInput input
= editor
.getEditorInput();
81 if((input
instanceof IEntityPersistenceService
) && (selection
instanceof IStructuredSelection
)){
84 IDocumentProvider provider
= ((BulkEditor
) editor
).getDocumentProvider();
85 LineAnnotationModel model
=
86 (LineAnnotationModel
) provider
.getAnnotationModel(input
);
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){
102 ICdmRepository controller
;
103 controller
= CdmStore
.getCurrentApplicationConfiguration();
104 if (object
instanceof SpecimenOrObservationBase
){
105 IOccurrenceService service
= controller
.getOccurrenceService();
107 result
= service
.isDeletable(((SpecimenOrObservationBase
) object
).getUuid(), null);
108 errorMessage
= "The specimen or observation ";
111 } else if (object
instanceof Reference
){
112 IReferenceService service
= controller
.getReferenceService();
114 result
= service
.isDeletable(((Reference
)object
).getUuid(), null);
115 errorMessage
= "The reference ";
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);
125 config
= new NameDeletionConfigurator();
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
){
132 result
= controller
.getNameService().isDeletable(name
.getUuid(), config
);
133 errorMessage
= "The name ";
136 } else if (object
instanceof TaxonBase
){
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
){
148 result
= controller
.getTaxonService().isDeletable(synonym
.getUuid(), config
);
151 else if(object
instanceof Taxon
){
152 Taxon taxon
= HibernateProxyHelper
.deproxy(object
, Taxon
.class);
153 if (((Taxon
)object
).getTaxonNodes().isEmpty()){
154 errorMessage
= "The taxon ";
156 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
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
){
167 result
= controller
.getTaxonService().isDeletable(taxon
.getUuid(), config
);
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
){
183 result
= controller
.getMediaService().isDeletable(media
.getUuid(), config
);
184 errorMessage
= "The media ";
189 } catch (Exception e
){
190 MessagingUtils
.errorDialog("Exception occured. Delete not possible", getClass(), e
.getMessage(), TaxeditorBulkeditorPlugin
.PLUGIN_ID
, null, true);
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());
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
);
203 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
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
) {
211 ((LineAnnotation
)next
).markAsDeleted(config
);
222 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
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());
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
);