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
.viewers
.IStructuredSelection
;
24 import org
.eclipse
.ui
.IEditorInput
;
25 import org
.eclipse
.ui
.IEditorPart
;
26 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
27 import org
.eclipse
.ui
.texteditor
.IDocumentProvider
;
29 import eu
.etaxonomy
.cdm
.api
.application
.ICdmRepository
;
30 import eu
.etaxonomy
.cdm
.api
.service
.DeleteResult
;
31 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
32 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
33 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteConfiguratorBase
;
34 import eu
.etaxonomy
.cdm
.api
.service
.config
.MediaDeletionConfigurator
;
35 import eu
.etaxonomy
.cdm
.api
.service
.config
.NameDeletionConfigurator
;
36 import eu
.etaxonomy
.cdm
.api
.service
.config
.SpecimenDeleteConfigurator
;
37 import eu
.etaxonomy
.cdm
.api
.service
.config
.SynonymDeletionConfigurator
;
38 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonDeletionConfigurator
;
39 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
40 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
41 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
42 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
43 import eu
.etaxonomy
.cdm
.model
.common
.User
;
44 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
45 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
46 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
47 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
49 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
51 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityPersistenceService
;
52 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotation
;
53 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotationModel
;
54 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
55 import eu
.etaxonomy
.taxeditor
.bulkeditor
.internal
.TaxeditorBulkeditorPlugin
;
56 import eu
.etaxonomy
.taxeditor
.bulkeditor
.l10n
.Messages
;
57 import eu
.etaxonomy
.taxeditor
.model
.DeleteResultMessagingUtils
;
58 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
59 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
60 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.configurator
.deleteConfigurator
.DeleteConfiguratorDialog
;
65 * @created Mar 11, 2011
68 public class DeleteHandler
extends AbstractHandler
{
72 private static final String SKIP
= "Skip";
73 private static final String DELETE
= "Delete";
74 private static final String CONFIRM_DELETION
= "Confirm Deletion";
77 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
80 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
83 TextSelection selection
= (TextSelection
) HandlerUtil
.getCurrentSelection(event
);
84 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
86 IEditorInput input
= editor
.getEditorInput();
88 if((input
instanceof IEntityPersistenceService
) && (selection
instanceof IStructuredSelection
)){
91 IDocumentProvider provider
= ((BulkEditor
) editor
).getDocumentProvider();
92 LineAnnotationModel model
=
93 (LineAnnotationModel
) provider
.getAnnotationModel(input
);
96 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
97 DeleteConfiguratorBase config
= null;
98 IEntityPersistenceService
<?
> persistenceService
= (IEntityPersistenceService
<?
>) input
;
99 DeleteResult result
= new DeleteResult();
100 String errorMessage
= "The object ";
101 for(Object object
: structuredSelection
.toList()){
102 if (object
instanceof CdmBase
){
103 CdmBase base
= (CdmBase
)object
;
104 LineAnnotation
<?
> annotation
= (LineAnnotation
<?
>) model
.getAnnotation(object
);
105 if (base
.getId() != 0){
109 ICdmRepository controller
;
110 controller
= CdmStore
.getCurrentApplicationConfiguration();
111 if (object
instanceof SpecimenOrObservationBase
){
112 IOccurrenceService service
= controller
.getOccurrenceService();
113 config
= new SpecimenDeleteConfigurator();
115 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(config
, HandlerUtil
.getActiveShell(event
), CONFIRM_DELETION
, null, Messages
.BulkEditor_DeleteHandler_reallyDeleteSpecimen
, MessageDialog
.WARNING
, new String
[] { DELETE
, SKIP
}, 0, true);
116 int result_dialog
= dialog
.open();
117 if (result_dialog
!= Status
.OK
){
121 result
= service
.isDeletable(((SpecimenOrObservationBase
<?
>) object
).getUuid(), config
);
122 errorMessage
= "The specimen or observation ";
125 } else if (object
instanceof Reference
){
126 IReferenceService service
= controller
.getReferenceService();
128 result
= service
.isDeletable(((Reference
)object
).getUuid(), null);
129 errorMessage
= "The reference ";
132 } else if (object
instanceof Group
){
133 errorMessage
= "The group ";
134 }else if (object
instanceof User
){
135 errorMessage
= "The user ";
136 } else if (object
instanceof TaxonName
){
137 TaxonName name
= HibernateProxyHelper
.deproxy(object
, TaxonName
.class);
139 config
= new NameDeletionConfigurator();
141 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);
142 int result_dialog
= dialog
.open();
143 if (result_dialog
!= Status
.OK
){
146 result
= controller
.getNameService().isDeletable(name
.getUuid(), config
);
147 errorMessage
= "The name ";
150 } else if (object
instanceof TaxonBase
){
153 if(object
instanceof Synonym
){
154 Synonym synonym
= HibernateProxyHelper
.deproxy(object
, Synonym
.class);
155 config
= new SynonymDeletionConfigurator();
156 errorMessage
= "The synonym ";
157 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);
158 int result_dialog
= dialog
.open();
159 if (result_dialog
!= Status
.OK
){
162 result
= controller
.getTaxonService().isDeletable(synonym
.getUuid(), config
);
165 else if(object
instanceof Taxon
){
166 Taxon taxon
= HibernateProxyHelper
.deproxy(object
, Taxon
.class);
167 if (((Taxon
)object
).getTaxonNodes().isEmpty()){
168 errorMessage
= "The taxon ";
170 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
173 config
= new TaxonDeletionConfigurator();
174 ((TaxonDeletionConfigurator
) config
).setDeleteInAllClassifications(true);
175 DeleteConfiguratorDialog dialog
;
176 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);
177 int result_dialog
= dialog
.open();
178 if (result_dialog
!= Status
.OK
){
181 result
= controller
.getTaxonService().isDeletable(taxon
.getUuid(), config
);
184 } else if (object
instanceof TeamOrPersonBase
){
185 result
= controller
.getAgentService().isDeletable(((CdmBase
) object
).getUuid(), null);
186 errorMessage
= "The team or person ";
187 } else if (object
instanceof Media
){
188 config
= new MediaDeletionConfigurator();
189 Media media
= HibernateProxyHelper
.deproxy(object
, Media
.class);
190 DeleteConfiguratorDialog dialog
;
191 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);
192 int result_dialog
= dialog
.open();
193 if (result_dialog
!= Status
.OK
){
197 result
= controller
.getMediaService().isDeletable(media
.getUuid(), config
);
198 errorMessage
= "The media ";
203 } catch (Exception e
){
204 MessagingUtils
.errorDialog("Exception occured. Delete not possible", getClass(), e
.getMessage(), TaxeditorBulkeditorPlugin
.PLUGIN_ID
, null, true);
206 if (result
.isError() || result
.isAbort()){
207 if (!result
.getExceptions().isEmpty()) {
208 List
<String
> messages
= new ArrayList
<String
>();
209 int i
= result
.getExceptions().size();
210 for (Exception e
:result
.getExceptions()){
211 messages
.add(e
.getMessage());
213 errorMessage
+= "could not be deleted.";
214 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
215 DeleteResultMessagingUtils
.messageDialogWithDetails(result
,errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);
217 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
219 }else if (model
!= null) {
220 annotation
.markAsDeleted(config
);
222 // int offset = selection.getOffset();
226 // Iterator iter = model.getAnnotationIterator(offset, selection.getLength(), true, true);
227 // // Iterator<Annotation> iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
228 // while (iter.hasNext()) {
229 // Object next = iter.next();
230 // if (next instanceof LineAnnotation) {
231 // if (result.isOk()){
232 // ((LineAnnotation)next).markAsDeleted(config);
243 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
245 if(result
.getUpdatedObjects().size() != 0 || !result
.getExceptions().isEmpty()){
246 List
<String
> messages
= new ArrayList
<String
>();
247 int i
= result
.getExceptions().size();
248 for (Exception e
:result
.getExceptions()){
249 messages
.add(e
.getMessage());
251 errorMessage
+= "can be deleted but related object(s) could not be deleted. ";
252 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
253 DeleteResultMessagingUtils
.messageDialogWithDetails(result
, errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);