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
;
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
.runtime
.Status
;
21 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
22 import org
.eclipse
.jface
.text
.TextSelection
;
23 import org
.eclipse
.jface
.text
.source
.Annotation
;
24 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
25 import org
.eclipse
.ui
.IEditorInput
;
26 import org
.eclipse
.ui
.IEditorPart
;
27 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
28 import org
.eclipse
.ui
.texteditor
.IDocumentProvider
;
30 import eu
.etaxonomy
.cdm
.api
.application
.ICdmRepository
;
31 import eu
.etaxonomy
.cdm
.api
.service
.DeleteResult
;
32 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
33 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
34 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteConfiguratorBase
;
35 import eu
.etaxonomy
.cdm
.api
.service
.config
.MediaDeletionConfigurator
;
36 import eu
.etaxonomy
.cdm
.api
.service
.config
.NameDeletionConfigurator
;
37 import eu
.etaxonomy
.cdm
.api
.service
.config
.SpecimenDeleteConfigurator
;
38 import eu
.etaxonomy
.cdm
.api
.service
.config
.SynonymDeletionConfigurator
;
39 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonDeletionConfigurator
;
40 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
41 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
42 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
43 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
44 import eu
.etaxonomy
.cdm
.model
.common
.User
;
45 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
46 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
47 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
48 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
49 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
52 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityPersistenceService
;
53 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotation
;
54 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotationModel
;
55 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
56 import eu
.etaxonomy
.taxeditor
.bulkeditor
.internal
.TaxeditorBulkeditorPlugin
;
57 import eu
.etaxonomy
.taxeditor
.bulkeditor
.l10n
.Messages
;
58 import eu
.etaxonomy
.taxeditor
.model
.DeleteResultMessagingUtils
;
59 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
60 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
61 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.configurator
.deleteConfigurator
.DeleteConfiguratorDialog
;
66 * @created Mar 11, 2011
69 public class DeleteHandler
extends AbstractHandler
{
73 private static final String SKIP
= "Skip";
74 private static final String DELETE
= "Delete";
75 private static final String CONFIRM_DELETION
= "Confirm Deletion";
78 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
81 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
84 TextSelection selection
= (TextSelection
) HandlerUtil
.getCurrentSelection(event
);
85 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
87 IEditorInput input
= editor
.getEditorInput();
89 if((input
instanceof IEntityPersistenceService
) && (selection
instanceof IStructuredSelection
)){
92 IDocumentProvider provider
= ((BulkEditor
) editor
).getDocumentProvider();
93 LineAnnotationModel model
=
94 (LineAnnotationModel
) provider
.getAnnotationModel(input
);
97 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
98 DeleteConfiguratorBase config
= null;
99 IEntityPersistenceService persistenceService
= (IEntityPersistenceService
) input
;
100 DeleteResult result
= new DeleteResult();
101 String errorMessage
= "The object ";
102 for(Object object
: structuredSelection
.toList()){
103 if (object
instanceof CdmBase
){
104 CdmBase base
= (CdmBase
)object
;
105 LineAnnotation annotation
= (LineAnnotation
) model
.getAnnotation(object
);
106 if (base
.getId() != 0){
110 ICdmRepository controller
;
111 controller
= CdmStore
.getCurrentApplicationConfiguration();
112 if (object
instanceof SpecimenOrObservationBase
){
113 IOccurrenceService service
= controller
.getOccurrenceService();
114 config
= new SpecimenDeleteConfigurator();
116 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(config
, HandlerUtil
.getActiveShell(event
), CONFIRM_DELETION
, null, Messages
.BulkEditor_DeleteHandler_reallyDeleteSpecimen
, MessageDialog
.WARNING
, new String
[] { DELETE
, SKIP
}, 0, true);
117 int result_dialog
= dialog
.open();
118 if (result_dialog
!= Status
.OK
){
122 result
= service
.isDeletable(((SpecimenOrObservationBase
) object
).getUuid(), config
);
123 errorMessage
= "The specimen or observation ";
126 } else if (object
instanceof Reference
){
127 IReferenceService service
= controller
.getReferenceService();
129 result
= service
.isDeletable(((Reference
)object
).getUuid(), null);
130 errorMessage
= "The reference ";
133 } else if (object
instanceof Group
){
134 errorMessage
= "The group ";
135 }else if (object
instanceof User
){
136 errorMessage
= "The user ";
137 } else if (object
instanceof TaxonNameBase
){
138 TaxonNameBase name
= HibernateProxyHelper
.deproxy(object
, TaxonNameBase
.class);
140 config
= new NameDeletionConfigurator();
142 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);
143 int result_dialog
= dialog
.open();
144 if (result_dialog
!= Status
.OK
){
147 result
= controller
.getNameService().isDeletable(name
.getUuid(), config
);
148 errorMessage
= "The name ";
151 } else if (object
instanceof TaxonBase
){
154 if(object
instanceof Synonym
){
155 Synonym synonym
= HibernateProxyHelper
.deproxy(object
, Synonym
.class);
156 config
= new SynonymDeletionConfigurator();
157 errorMessage
= "The synonym ";
158 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);
159 int result_dialog
= dialog
.open();
160 if (result_dialog
!= Status
.OK
){
163 result
= controller
.getTaxonService().isDeletable(synonym
.getUuid(), config
);
166 else if(object
instanceof Taxon
){
167 Taxon taxon
= HibernateProxyHelper
.deproxy(object
, Taxon
.class);
168 if (((Taxon
)object
).getTaxonNodes().isEmpty()){
169 errorMessage
= "The taxon ";
171 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
174 config
= new TaxonDeletionConfigurator();
175 ((TaxonDeletionConfigurator
) config
).setDeleteInAllClassifications(true);
176 DeleteConfiguratorDialog dialog
;
177 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);
178 int result_dialog
= dialog
.open();
179 if (result_dialog
!= Status
.OK
){
182 result
= controller
.getTaxonService().isDeletable(taxon
.getUuid(), config
);
185 } else if (object
instanceof TeamOrPersonBase
){
186 result
= controller
.getAgentService().isDeletable(((CdmBase
) object
).getUuid(), null);
187 errorMessage
= "The team or person ";
188 } else if (object
instanceof Media
){
189 config
= new MediaDeletionConfigurator();
190 Media media
= HibernateProxyHelper
.deproxy(object
, Media
.class);
191 DeleteConfiguratorDialog dialog
;
192 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);
193 int result_dialog
= dialog
.open();
194 if (result_dialog
!= Status
.OK
){
198 result
= controller
.getMediaService().isDeletable(media
.getUuid(), config
);
199 errorMessage
= "The media ";
204 } catch (Exception e
){
205 MessagingUtils
.errorDialog("Exception occured. Delete not possible", getClass(), e
.getMessage(), TaxeditorBulkeditorPlugin
.PLUGIN_ID
, null, true);
207 if (result
.isError() || result
.isAbort()){
208 if (!result
.getExceptions().isEmpty()) {
209 List
<String
> messages
= new ArrayList
<String
>();
210 int i
= result
.getExceptions().size();
211 for (Exception e
:result
.getExceptions()){
212 messages
.add(e
.getMessage());
214 errorMessage
+= "could not be deleted.";
215 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
216 DeleteResultMessagingUtils
.messageDialogWithDetails(result
,errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);
218 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
220 }else if (model
!= null) {
221 int offset
= selection
.getOffset();
225 Iterator iter
= model
.getAnnotationIterator(offset
, selection
.getLength(), true, true);
226 // Iterator<Annotation> iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
227 while (iter
.hasNext()) {
228 Object next
= iter
.next();
229 if (next
instanceof LineAnnotation
) {
231 ((LineAnnotation
)next
).markAsDeleted(config
);
242 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
244 if(result
.getUpdatedObjects().size() != 0 || !result
.getExceptions().isEmpty()){
245 List
<String
> messages
= new ArrayList
<String
>();
246 int i
= result
.getExceptions().size();
247 for (Exception e
:result
.getExceptions()){
248 messages
.add(e
.getMessage());
250 errorMessage
+= "can be deleted but related object(s) could not be deleted. ";
251 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
252 DeleteResultMessagingUtils
.messageDialogWithDetails(result
, errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);