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
.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
.jface
.viewers
.ISelection
;
20 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
21 import org
.eclipse
.ui
.IEditorInput
;
22 import org
.eclipse
.ui
.IEditorPart
;
23 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
24 import org
.eclipse
.ui
.texteditor
.IDocumentProvider
;
26 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
27 import eu
.etaxonomy
.cdm
.api
.service
.DeleteResult
;
28 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.IGroupService
;
30 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
31 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
32 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
33 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
34 import eu
.etaxonomy
.cdm
.api
.service
.IUserService
;
35 import eu
.etaxonomy
.cdm
.api
.service
.config
.NameDeletionConfigurator
;
36 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
37 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
38 import eu
.etaxonomy
.cdm
.model
.common
.User
;
39 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
40 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
41 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
45 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityPersistenceService
;
46 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotation
;
47 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotationModel
;
48 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
49 import eu
.etaxonomy
.taxeditor
.bulkeditor
.internal
.TaxeditorBulkeditorPlugin
;
50 import eu
.etaxonomy
.taxeditor
.model
.DeleteResultMessagingUtils
;
51 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
52 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
57 * @created Mar 11, 2011
60 public class DeleteHandler
extends AbstractHandler
{
63 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
66 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
68 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
70 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
72 IEditorInput input
= editor
.getEditorInput();
74 if((input
instanceof IEntityPersistenceService
) && (selection
instanceof IStructuredSelection
)){
77 IDocumentProvider provider
= ((BulkEditor
) editor
).getDocumentProvider();
78 LineAnnotationModel model
=
79 (LineAnnotationModel
) provider
.getAnnotationModel(input
);
82 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
84 IEntityPersistenceService persistenceService
= (IEntityPersistenceService
) input
;
87 for(Object object
: structuredSelection
.toList()){
89 LineAnnotation annotation
= (LineAnnotation
) model
.getAnnotation(object
);
90 DeleteResult result
= new DeleteResult();
91 String errorMessage
= "The object ";
94 ICdmApplicationConfiguration controller
;
95 controller
= CdmStore
.getCurrentApplicationConfiguration();
97 if (object
instanceof SpecimenOrObservationBase
){
98 IOccurrenceService service
= controller
.getOccurrenceService();
100 result
= service
.delete(((SpecimenOrObservationBase
) object
).getUuid());
101 errorMessage
= "The specimen or observation ";
103 } else if (object
instanceof Reference
){
104 IReferenceService service
= controller
.getReferenceService();
106 result
= service
.delete(((Reference
) object
).getUuid());
107 errorMessage
= "The reference ";
110 } else if (object
instanceof Group
){
111 IGroupService service
= controller
.getGroupService();
113 result
= service
.delete(((Group
) object
).getUuid());
114 errorMessage
= "The group ";
116 }else if (object
instanceof User
){
117 IUserService service
= controller
.getUserService();
119 result
= service
.delete(((User
) object
).getUuid());
120 errorMessage
= "The user ";
122 } else if (object
instanceof TaxonNameBase
){
123 INameService service
= controller
.getNameService();
125 NameDeletionConfigurator config
= new NameDeletionConfigurator();
126 result
= service
.delete(((TaxonNameBase
) object
).getUuid(), config
);
127 errorMessage
= "The name ";
129 } else if (object
instanceof TaxonBase
){
130 ITaxonService service
= controller
.getTaxonService();
132 if (object
instanceof Taxon
){
133 result
= service
.deleteTaxon(((TaxonBase
) object
).getUuid(), null, null);
134 errorMessage
= "The taxon ";
136 result
= service
.deleteSynonym(((Synonym
)object
).getUuid(), null);
137 errorMessage
= "The synonym ";
140 } else if (object
instanceof TeamOrPersonBase
){
141 IAgentService service
= controller
.getAgentService();
142 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
143 result
= service
.delete(((TeamOrPersonBase
)object
).getUuid());
144 errorMessage
= "The team or person ";
146 } catch (Exception e
){
147 MessagingUtils
.errorDialog("Exception occured. Delete not possible", getClass(), e
.getMessage(), TaxeditorBulkeditorPlugin
.PLUGIN_ID
, null, true);
149 if (result
.isError() || result
.isAbort()){
150 if (!result
.getExceptions().isEmpty()) {
151 List
<String
> messages
= new ArrayList
<String
>();
152 int i
= result
.getExceptions().size();
153 for (Exception e
:result
.getExceptions()){
154 messages
.add(e
.getMessage());
156 errorMessage
+= "could not be deleted.";
158 DeleteResultMessagingUtils
.messageDialogWithDetails(result
,errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);
160 MessagingUtils
.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
164 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
165 if(result
.getUpdatedObjects().size() != 0 || !result
.getExceptions().isEmpty()){
166 List
<String
> messages
= new ArrayList
<String
>();
167 int i
= result
.getExceptions().size();
168 for (Exception e
:result
.getExceptions()){
169 messages
.add(e
.getMessage());
171 errorMessage
+= "was deleted but there where updated objects";
172 DeleteResultMessagingUtils
.messageDialogWithDetails(result
, errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);