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
.SpecimenDeleteConfigurator
;
36 import eu
.etaxonomy
.cdm
.api
.service
.config
.SynonymDeletionConfigurator
;
37 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonDeletionConfigurator
;
38 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
39 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
40 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
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
.bulkeditor
.l10n
.Messages
;
56 import eu
.etaxonomy
.taxeditor
.model
.DeleteResultMessagingUtils
;
57 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
58 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
59 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.configurator
.deleteConfigurator
.DeleteConfiguratorDialog
;
64 * @created Mar 11, 2011
67 public class DeleteHandler
extends AbstractHandler
{
71 private static final String SKIP
= "Skip";
72 private static final String DELETE
= "Delete";
73 private static final String CONFIRM_DELETION
= "Confirm Deletion";
76 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
79 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
82 TextSelection selection
= (TextSelection
) HandlerUtil
.getCurrentSelection(event
);
83 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
85 IEditorInput input
= editor
.getEditorInput();
87 if((input
instanceof IEntityPersistenceService
) && (selection
instanceof IStructuredSelection
)){
90 IDocumentProvider provider
= ((BulkEditor
) editor
).getDocumentProvider();
91 LineAnnotationModel model
=
92 (LineAnnotationModel
) provider
.getAnnotationModel(input
);
95 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
96 DeleteConfiguratorBase config
= null;
97 IEntityPersistenceService persistenceService
= (IEntityPersistenceService
) input
;
98 DeleteResult result
= new DeleteResult();
99 String errorMessage
= "The object ";
100 for(Object object
: structuredSelection
.toList()){
101 if (object
instanceof CdmBase
){
102 CdmBase base
= (CdmBase
)object
;
103 LineAnnotation annotation
= (LineAnnotation
) model
.getAnnotation(object
);
104 if (base
.getId() != 0){
108 ICdmRepository controller
;
109 controller
= CdmStore
.getCurrentApplicationConfiguration();
110 if (object
instanceof SpecimenOrObservationBase
){
111 IOccurrenceService service
= controller
.getOccurrenceService();
112 config
= new SpecimenDeleteConfigurator();
114 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(config
, HandlerUtil
.getActiveShell(event
), CONFIRM_DELETION
, null, Messages
.BulkEditor_DeleteHandler_reallyDeleteSpecimen
, MessageDialog
.WARNING
, new String
[] { DELETE
, SKIP
}, 0, true);
115 int result_dialog
= dialog
.open();
116 if (result_dialog
!= Status
.OK
){
120 result
= service
.isDeletable(((SpecimenOrObservationBase
) object
).getUuid(), config
);
121 errorMessage
= "The specimen or observation ";
124 } else if (object
instanceof Reference
){
125 IReferenceService service
= controller
.getReferenceService();
127 result
= service
.isDeletable(((Reference
)object
).getUuid(), null);
128 errorMessage
= "The reference ";
131 } else if (object
instanceof Group
){
132 errorMessage
= "The group ";
133 }else if (object
instanceof User
){
134 errorMessage
= "The user ";
135 } else if (object
instanceof TaxonNameBase
){
136 TaxonNameBase name
= HibernateProxyHelper
.deproxy(object
, TaxonNameBase
.class);
138 config
= new NameDeletionConfigurator();
140 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);
141 int result_dialog
= dialog
.open();
142 if (result_dialog
!= Status
.OK
){
145 result
= controller
.getNameService().isDeletable(name
.getUuid(), config
);
146 errorMessage
= "The name ";
149 } else if (object
instanceof TaxonBase
){
152 if(object
instanceof Synonym
){
153 Synonym synonym
= HibernateProxyHelper
.deproxy(object
, Synonym
.class);
154 config
= new SynonymDeletionConfigurator();
155 errorMessage
= "The synonym ";
156 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);
157 int result_dialog
= dialog
.open();
158 if (result_dialog
!= Status
.OK
){
161 result
= controller
.getTaxonService().isDeletable(synonym
.getUuid(), config
);
164 else if(object
instanceof Taxon
){
165 Taxon taxon
= HibernateProxyHelper
.deproxy(object
, Taxon
.class);
166 if (((Taxon
)object
).getTaxonNodes().isEmpty()){
167 errorMessage
= "The taxon ";
169 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
172 config
= new TaxonDeletionConfigurator();
173 ((TaxonDeletionConfigurator
) config
).setDeleteInAllClassifications(true);
174 DeleteConfiguratorDialog dialog
;
175 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);
176 int result_dialog
= dialog
.open();
177 if (result_dialog
!= Status
.OK
){
180 result
= controller
.getTaxonService().isDeletable(taxon
.getUuid(), config
);
183 } else if (object
instanceof TeamOrPersonBase
){
184 result
= controller
.getAgentService().isDeletable(((CdmBase
) object
).getUuid(), null);
185 errorMessage
= "The team or person ";
186 } else if (object
instanceof Media
){
187 config
= new MediaDeletionConfigurator();
188 Media media
= HibernateProxyHelper
.deproxy(object
, Media
.class);
189 DeleteConfiguratorDialog dialog
;
190 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);
191 int result_dialog
= dialog
.open();
192 if (result_dialog
!= Status
.OK
){
196 result
= controller
.getMediaService().isDeletable(media
.getUuid(), config
);
197 errorMessage
= "The media ";
202 } catch (Exception e
){
203 MessagingUtils
.errorDialog("Exception occured. Delete not possible", getClass(), e
.getMessage(), TaxeditorBulkeditorPlugin
.PLUGIN_ID
, null, true);
205 if (result
.isError() || result
.isAbort()){
206 if (!result
.getExceptions().isEmpty()) {
207 List
<String
> messages
= new ArrayList
<String
>();
208 int i
= result
.getExceptions().size();
209 for (Exception e
:result
.getExceptions()){
210 messages
.add(e
.getMessage());
212 errorMessage
+= "could not be deleted.";
213 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
214 DeleteResultMessagingUtils
.messageDialogWithDetails(result
,errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);
216 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
218 }else if (model
!= null) {
219 Iterator iter
= model
.getAnnotationIterator(selection
.getOffset(), selection
.getLength(), true, true);
220 while (iter
.hasNext()) {
221 Object next
= iter
.next();
222 if (next
instanceof LineAnnotation
) {
224 ((LineAnnotation
)next
).markAsDeleted(config
);
235 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
237 if(result
.getUpdatedObjects().size() != 0 || !result
.getExceptions().isEmpty()){
238 List
<String
> messages
= new ArrayList
<String
>();
239 int i
= result
.getExceptions().size();
240 for (Exception e
:result
.getExceptions()){
241 messages
.add(e
.getMessage());
243 errorMessage
+= "can be deleted but related object(s) could not be deleted. ";
244 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
245 DeleteResultMessagingUtils
.messageDialogWithDetails(result
, errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);