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
{
69 private static final String SKIP
= "Skip";
70 private static final String DELETE
= "Delete";
71 private static final String CONFIRM_DELETION
= "Confirm Deletion";
74 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
77 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
80 TextSelection selection
= (TextSelection
) HandlerUtil
.getCurrentSelection(event
);
81 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
83 IEditorInput input
= editor
.getEditorInput();
85 if((input
instanceof IEntityPersistenceService
) && (selection
instanceof IStructuredSelection
)){
88 IDocumentProvider provider
= ((BulkEditor
) editor
).getDocumentProvider();
89 LineAnnotationModel model
=
90 (LineAnnotationModel
) provider
.getAnnotationModel(input
);
93 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
94 DeleteConfiguratorBase config
= null;
95 IEntityPersistenceService persistenceService
= (IEntityPersistenceService
) input
;
96 DeleteResult result
= new DeleteResult();
97 String errorMessage
= "The object ";
98 for(Object object
: structuredSelection
.toList()){
99 if (object
instanceof CdmBase
){
100 CdmBase base
= (CdmBase
)object
;
101 LineAnnotation annotation
= (LineAnnotation
) model
.getAnnotation(object
);
102 if (base
.getId() != 0){
106 ICdmRepository controller
;
107 controller
= CdmStore
.getCurrentApplicationConfiguration();
108 if (object
instanceof SpecimenOrObservationBase
){
109 IOccurrenceService service
= controller
.getOccurrenceService();
111 result
= service
.isDeletable(((SpecimenOrObservationBase
) object
).getUuid(), null);
112 errorMessage
= "The specimen or observation ";
115 } else if (object
instanceof Reference
){
116 IReferenceService service
= controller
.getReferenceService();
118 result
= service
.isDeletable(((Reference
)object
).getUuid(), null);
119 errorMessage
= "The reference ";
122 } else if (object
instanceof Group
){
123 errorMessage
= "The group ";
124 }else if (object
instanceof User
){
125 errorMessage
= "The user ";
126 } else if (object
instanceof TaxonNameBase
){
127 TaxonNameBase name
= HibernateProxyHelper
.deproxy(object
, TaxonNameBase
.class);
129 config
= new NameDeletionConfigurator();
131 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);
132 int result_dialog
= dialog
.open();
133 if (result_dialog
!= Status
.OK
){
136 result
= controller
.getNameService().isDeletable(name
.getUuid(), config
);
137 errorMessage
= "The name ";
140 } else if (object
instanceof TaxonBase
){
143 if(object
instanceof Synonym
){
144 Synonym synonym
= HibernateProxyHelper
.deproxy(object
, Synonym
.class);
145 config
= new SynonymDeletionConfigurator();
146 errorMessage
= "The synonym ";
147 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);
148 int result_dialog
= dialog
.open();
149 if (result_dialog
!= Status
.OK
){
152 result
= controller
.getTaxonService().isDeletable(synonym
.getUuid(), config
);
155 else if(object
instanceof Taxon
){
156 Taxon taxon
= HibernateProxyHelper
.deproxy(object
, Taxon
.class);
157 if (((Taxon
)object
).getTaxonNodes().isEmpty()){
158 errorMessage
= "The taxon ";
160 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
163 config
= new TaxonDeletionConfigurator();
164 ((TaxonDeletionConfigurator
) config
).setDeleteInAllClassifications(true);
165 DeleteConfiguratorDialog dialog
;
166 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);
167 int result_dialog
= dialog
.open();
168 if (result_dialog
!= Status
.OK
){
171 result
= controller
.getTaxonService().isDeletable(taxon
.getUuid(), config
);
174 } else if (object
instanceof TeamOrPersonBase
){
175 result
= controller
.getAgentService().isDeletable(((CdmBase
) object
).getUuid(), null);
176 errorMessage
= "The team or person ";
177 } else if (object
instanceof Media
){
178 config
= new MediaDeletionConfigurator();
179 Media media
= HibernateProxyHelper
.deproxy(object
, Media
.class);
180 DeleteConfiguratorDialog dialog
;
181 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);
182 int result_dialog
= dialog
.open();
183 if (result_dialog
!= Status
.OK
){
187 result
= controller
.getMediaService().isDeletable(media
.getUuid(), config
);
188 errorMessage
= "The media ";
193 } catch (Exception e
){
194 MessagingUtils
.errorDialog("Exception occured. Delete not possible", getClass(), e
.getMessage(), TaxeditorBulkeditorPlugin
.PLUGIN_ID
, null, true);
196 if (result
.isError() || result
.isAbort()){
197 if (!result
.getExceptions().isEmpty()) {
198 List
<String
> messages
= new ArrayList
<String
>();
199 int i
= result
.getExceptions().size();
200 for (Exception e
:result
.getExceptions()){
201 messages
.add(e
.getMessage());
203 errorMessage
+= "could not be deleted.";
204 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
205 DeleteResultMessagingUtils
.messageDialogWithDetails(result
,errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);
207 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
209 }else 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
) {
215 ((LineAnnotation
)next
).markAsDeleted(config
);
226 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
228 if(result
.getUpdatedObjects().size() != 0 || !result
.getExceptions().isEmpty()){
229 List
<String
> messages
= new ArrayList
<String
>();
230 int i
= result
.getExceptions().size();
231 for (Exception e
:result
.getExceptions()){
232 messages
.add(e
.getMessage());
234 errorMessage
+= "can be deleted but related object(s) could not be deleted. ";
235 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
236 DeleteResultMessagingUtils
.messageDialogWithDetails(result
, errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);