3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 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
.ICdmApplicationConfiguration
;
30 import eu
.etaxonomy
.cdm
.api
.service
.DeleteResult
;
31 import eu
.etaxonomy
.cdm
.api
.service
.IMediaService
;
32 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
33 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
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
.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
.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
.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
;
91 IEntityPersistenceService persistenceService
= (IEntityPersistenceService
) input
;
93 for(Object object
: structuredSelection
.toList()){
95 LineAnnotation annotation
= (LineAnnotation
) model
.getAnnotation(object
);
96 DeleteResult result
= new DeleteResult();
97 String errorMessage
= "The object ";
100 ICdmApplicationConfiguration controller
;
101 controller
= CdmStore
.getCurrentApplicationConfiguration();
103 if (object
instanceof SpecimenOrObservationBase
){
104 IOccurrenceService service
= controller
.getOccurrenceService();
106 result
= service
.isDeletable(((SpecimenOrObservationBase
) object
), null);
107 errorMessage
= "The specimen or observation ";
109 Iterator iter
= model
.getAnnotationIterator(selection
.getOffset(), selection
.getLength(), true, true);
110 while (iter
.hasNext()) {
111 Object next
= iter
.next();
112 if (next
instanceof LineAnnotation
) {
114 ((LineAnnotation
)next
).markAsDeleted(null);
121 } else if (object
instanceof Reference
){
122 IReferenceService service
= controller
.getReferenceService();
124 result
= service
.isDeletable((Reference
)object
, null);
125 errorMessage
= "The reference ";
128 Iterator iter
= model
.getAnnotationIterator(selection
.getOffset(), selection
.getLength(), true, true);
129 while (iter
.hasNext()) {
130 Object next
= iter
.next();
131 if (next
instanceof LineAnnotation
) {
133 ((LineAnnotation
)next
).markAsDeleted(null);
140 } else if (object
instanceof Group
){
142 Iterator iter
= model
.getAnnotationIterator(selection
.getOffset(), selection
.getLength(), true, true);
143 while (iter
.hasNext()) {
144 Object next
= iter
.next();
145 if (next
instanceof LineAnnotation
) {
147 ((LineAnnotation
)next
).markAsDeleted(null);
154 // IGroupService service = controller.getGroupService();
155 // if (object != null){
156 // result = service.delete(((Group) object).getUuid());
157 // errorMessage = "The group ";
159 }else if (object
instanceof User
){
161 Iterator iter
= model
.getAnnotationIterator(selection
.getOffset(), selection
.getLength(), true, true);
162 while (iter
.hasNext()) {
163 Object next
= iter
.next();
164 if (next
instanceof LineAnnotation
) {
166 ((LineAnnotation
)next
).markAsDeleted(null);
173 // IUserService service = controller.getUserService();
174 // if (object != null){
175 // result = service.delete(((User) object).getUuid());
176 // errorMessage = "The user ";
178 } else if (object
instanceof TaxonNameBase
){
179 TaxonNameBase name
= HibernateProxyHelper
.deproxy(object
, TaxonNameBase
.class);
183 NameDeletionConfigurator config
= new NameDeletionConfigurator();
185 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);
186 int result_dialog
= dialog
.open();
187 if (result_dialog
!= Status
.OK
){
191 //operation = new DeleteTaxonNameOperation(commandName,((BulkEditor) editor).getUndoContext() , name, config, (BulkEditor)editor);
192 errorMessage
= "The name ";
195 Iterator iter
= model
.getAnnotationIterator(selection
.getOffset(), selection
.getLength(), true, true);
196 while (iter
.hasNext()) {
197 Object next
= iter
.next();
198 if (next
instanceof LineAnnotation
) {
199 result
= controller
.getNameService().isDeletable(name
, config
);
201 ((LineAnnotation
)next
).markAsDeleted(config
);
208 } else if (object
instanceof TaxonBase
){
211 if(object
instanceof Synonym
){
212 Synonym synonym
= HibernateProxyHelper
.deproxy(object
, Synonym
.class);
213 SynonymDeletionConfigurator config
= new SynonymDeletionConfigurator();
214 errorMessage
= "The synonym ";
215 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);
216 int result_dialog
= dialog
.open();
217 if (result_dialog
!= Status
.OK
){
220 //operation = new DeleteTaxonBaseOperation(commandName,((BulkEditor) editor).getUndoContext() , synonym, config, (BulkEditor)editor);
222 Iterator iter
= model
.getAnnotationIterator(selection
.getOffset(), selection
.getLength(), true, true);
223 while (iter
.hasNext()) {
224 Object next
= iter
.next();
225 if (next
instanceof LineAnnotation
) {
226 result
= controller
.getTaxonService().isDeletable(synonym
, config
);
228 ((LineAnnotation
)next
).markAsDeleted(config
);
236 else if(object
instanceof Taxon
){
237 Taxon taxon
= HibernateProxyHelper
.deproxy(object
, Taxon
.class);
238 if (((Taxon
)object
).getTaxonNodes().isEmpty()){
240 errorMessage
= "The taxon ";
242 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
245 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
246 config
.setDeleteInAllClassifications(true);
247 DeleteConfiguratorDialog dialog
;
248 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);
249 int result_dialog
= dialog
.open();
250 if (result_dialog
!= Status
.OK
){
253 //operation = new DeleteTaxonBaseOperation(commandName, ((BulkEditor) editor).getUndoContext(), taxon, config, (BulkEditor)editor);
255 Iterator iter
= model
.getAnnotationIterator(selection
.getOffset(), selection
.getLength(), true, true);
256 while (iter
.hasNext()) {
257 Object next
= iter
.next();
258 if (next
instanceof LineAnnotation
) {
259 result
= controller
.getTaxonService().isDeletable(taxon
, config
);
261 ((LineAnnotation
)next
).markAsDeleted(config
);
270 // ITaxonService service = controller.getTaxonService();
271 // if (object != null){
272 // if (object instanceof Taxon){
273 // TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
274 // config.setDeleteInAllClassifications(true);
275 // DeleteConfiguratorDialog dialog;
276 // if (((Taxon)object).getTaxonNodes().isEmpty()){
277 // 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);
278 // int result_dialog= dialog.open();
279 // if (result_dialog != Status.OK){
282 // result = service.deleteTaxon(((TaxonBase) object).getUuid(), config, null);
283 // errorMessage = "The taxon ";
285 // MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
290 // SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
291 // 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);
292 // int result_dialog= dialog.open();
293 // if (result_dialog != Status.OK){
296 // result = service.deleteSynonym(((Synonym)object).getUuid(), config);
297 // errorMessage = "The synonym ";
300 } else if (object
instanceof TeamOrPersonBase
){
303 Iterator iter
= model
.getAnnotationIterator(selection
.getOffset(), selection
.getLength(), true, true);
304 while (iter
.hasNext()) {
305 Object next
= iter
.next();
306 if (next
instanceof LineAnnotation
) {
308 result
= controller
.getAgentService().isDeletable((TeamOrPersonBase
) object
, null);
310 ((LineAnnotation
)next
).markAsDeleted(null);
316 //IAgentService service = controller.getAgentService();
317 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
318 //result = service.delete(((TeamOrPersonBase)object).getUuid());
319 errorMessage
= "The team or person ";
320 } else if (object
instanceof Media
){
321 MediaDeletionConfigurator config
= new MediaDeletionConfigurator();
323 DeleteConfiguratorDialog dialog
;
324 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);
325 int result_dialog
= dialog
.open();
326 if (result_dialog
!= Status
.OK
){
329 IMediaService service
= controller
.getMediaService();
330 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
332 errorMessage
= "The media ";
335 Iterator iter
= model
.getAnnotationIterator(selection
.getOffset(), selection
.getLength(), true, true);
336 while (iter
.hasNext()) {
337 Object next
= iter
.next();
338 if (next
instanceof LineAnnotation
) {
339 result
= service
.isDeletable(((Media
)object
), config
);
341 ((LineAnnotation
)next
).markAsDeleted(config
);
349 } catch (Exception e
){
350 MessagingUtils
.errorDialog("Exception occured. Delete not possible", getClass(), e
.getMessage(), TaxeditorBulkeditorPlugin
.PLUGIN_ID
, null, true);
352 if (result
.isError() || result
.isAbort()){
353 if (!result
.getExceptions().isEmpty()) {
354 List
<String
> messages
= new ArrayList
<String
>();
355 int i
= result
.getExceptions().size();
356 for (Exception e
:result
.getExceptions()){
357 messages
.add(e
.getMessage());
359 errorMessage
+= "could not be deleted.";
360 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
361 DeleteResultMessagingUtils
.messageDialogWithDetails(result
,errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);
363 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
370 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
372 if(result
.getUpdatedObjects().size() != 0 || !result
.getExceptions().isEmpty()){
373 List
<String
> messages
= new ArrayList
<String
>();
374 int i
= result
.getExceptions().size();
375 for (Exception e
:result
.getExceptions()){
376 messages
.add(e
.getMessage());
378 errorMessage
+= "can be deleted but related object(s) could not be deleted. ";
379 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
380 DeleteResultMessagingUtils
.messageDialogWithDetails(result
, errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);