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
.e4
.handler
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
16 import javax
.inject
.Named
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.e4
.core
.di
.annotations
.CanExecute
;
20 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
21 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
22 import org
.eclipse
.e4
.ui
.model
.application
.ui
.menu
.MHandledMenuItem
;
23 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
24 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
25 import org
.eclipse
.swt
.widgets
.Shell
;
27 import eu
.etaxonomy
.cdm
.api
.application
.ICdmRepository
;
28 import eu
.etaxonomy
.cdm
.api
.service
.DeleteResult
;
29 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
30 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
31 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteConfiguratorBase
;
32 import eu
.etaxonomy
.cdm
.api
.service
.config
.MediaDeletionConfigurator
;
33 import eu
.etaxonomy
.cdm
.api
.service
.config
.NameDeletionConfigurator
;
34 import eu
.etaxonomy
.cdm
.api
.service
.config
.SpecimenDeleteConfigurator
;
35 import eu
.etaxonomy
.cdm
.api
.service
.config
.SynonymDeletionConfigurator
;
36 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonDeletionConfigurator
;
37 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
38 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
39 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
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
.TaxonName
;
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
.bulkeditor
.internal
.TaxeditorBulkeditorPlugin
;
50 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
51 import eu
.etaxonomy
.taxeditor
.model
.DeleteResultMessagingUtils
;
52 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
53 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
54 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.configurator
.deleteConfigurator
.DeleteConfiguratorDialog
;
63 public class DeleteHandlerE4
{
65 private static final String COULD_NOT_BE_DELETED
= eu
.etaxonomy
.taxeditor
.l10n
.Messages
.DeleteHandler_COULD_NOT_BE_DELETED
;
66 private static final String AN_EXCEPTION_OCCURED
= Messages
.DeleteHandler_EXCEPTION
;
67 private static final String DELETE_NOT_POSSIBLE
= Messages
.DeleteHandler_DELETE_NOT_POSSIBLE
;
68 private static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_S
= Messages
.DeleteHandler_REALLY_DELETE
;
69 private static final String MEDIA
= Messages
.DeleteHandler_MEDIA
;
70 private static final String TEAM_OR_PERSON
= Messages
.DeleteHandler_TEAM_OR_PERSON
;
71 private static final String TAXON
= Messages
.DeleteHandler_TAXON
;
72 private static final String SYNONYM
= Messages
.DeleteHandler_SYNONYM
;
73 private static final String NAME
= Messages
.DeleteHandler_NAME
;
74 private static final String USER
= Messages
.DeleteHandler_USER
;
75 private static final String GROUP
= Messages
.DeleteHandler_GROUP
;
76 private static final String REFERENCE
= Messages
.DeleteHandler_REFERENCE
;
77 private static final String SPECIMEN_OR_OBSERVATION
= Messages
.DeleteHandler_SPECIMEN
;
78 private static final String OBJECT
= Messages
.DeleteHandler_OBJECT
;
79 private static final String SKIP
= Messages
.DeleteHandler_SKIP
;
80 private static final String DELETE
= eu
.etaxonomy
.taxeditor
.l10n
.Messages
.DeleteHandler_DELETE
;
81 private static final String CONFIRM_DELETION
= Messages
.DeleteHandler_CONFIRM
;
84 public void execute(@Named(IServiceConstants
.ACTIVE_SELECTION
)Object object
,
85 @Named(IServiceConstants
.ACTIVE_PART
)MPart activePart
,
86 @Named(IServiceConstants
.ACTIVE_SHELL
)Shell shell
) {
88 DeleteConfiguratorBase config
= null;
89 DeleteResult result
= new DeleteResult();
90 String errorMessage
= OBJECT
;
91 if (object
instanceof CdmBase
){
92 CdmBase base
= (CdmBase
)object
;
93 if (base
.getId() != 0){
95 ICdmRepository controller
;
96 controller
= CdmStore
.getCurrentApplicationConfiguration();
97 if (object
instanceof SpecimenOrObservationBase
){
98 IOccurrenceService service
= controller
.getOccurrenceService();
99 config
= new SpecimenDeleteConfigurator();
101 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(config
,
102 shell
, CONFIRM_DELETION
, null,
104 DO_YOU_REALLY_WANT_TO_DELETE_THE_S
+"\n"+Messages
.DeleteHandler_IRREVERSIBLE
,
105 SPECIMEN_OR_OBSERVATION
),
106 MessageDialog
.WARNING
,
107 new String
[] { DELETE
, SKIP
}, 0, true);
108 int result_dialog
= dialog
.open();
109 if (result_dialog
!= IStatus
.OK
){
112 result
= service
.isDeletable(((SpecimenOrObservationBase
<?
>) object
).getUuid(), config
);
113 errorMessage
= SPECIMEN_OR_OBSERVATION
;
115 } else if (object
instanceof Reference
){
116 IReferenceService service
= controller
.getReferenceService();
117 result
= service
.isDeletable(((Reference
)object
).getUuid(), null);
118 errorMessage
= REFERENCE
;
120 } else if (object
instanceof Group
){
121 errorMessage
= GROUP
;
122 }else if (object
instanceof User
){
124 } else if (object
instanceof TaxonName
){
125 TaxonName name
= HibernateProxyHelper
.deproxy(object
, TaxonName
.class);
126 config
= new NameDeletionConfigurator();
128 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(config
,
129 shell
, CONFIRM_DELETION
, null,
131 DO_YOU_REALLY_WANT_TO_DELETE_THE_S
+"\n"+Messages
.DeleteHandler_IRREVERSIBLE
,
133 MessageDialog
.WARNING
, new String
[] { DELETE
, SKIP
}, 0, true);
134 int result_dialog
= dialog
.open();
135 if (result_dialog
!= IStatus
.OK
){
138 result
= controller
.getNameService().isDeletable(name
.getUuid(), config
);
141 } else if (object
instanceof TaxonBase
){
144 if(object
instanceof Synonym
){
145 Synonym synonym
= HibernateProxyHelper
.deproxy(object
, Synonym
.class);
146 config
= new SynonymDeletionConfigurator();
147 errorMessage
= SYNONYM
;
148 DeleteConfiguratorDialog dialog
= new DeleteConfiguratorDialog(config
,
149 shell
, CONFIRM_DELETION
, null,
150 String
.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S
, SYNONYM
),
151 MessageDialog
.WARNING
, new String
[] { DELETE
, SKIP
}, 0, true);
152 int result_dialog
= dialog
.open();
153 if (result_dialog
!= IStatus
.OK
){
156 result
= controller
.getTaxonService().isDeletable(synonym
.getUuid(), config
);
159 else if(object
instanceof Taxon
){
160 Taxon taxon
= HibernateProxyHelper
.deproxy(object
, Taxon
.class);
161 if (((Taxon
)object
).getTaxonNodes().isEmpty()){
162 errorMessage
= TAXON
;
164 MessagingUtils
.messageDialog(DELETE_NOT_POSSIBLE
, getClass(),
165 Messages
.DeleteHandler_TAXON_CAN_NOT_BE_DELETED
,
169 config
= new TaxonDeletionConfigurator();
170 ((TaxonDeletionConfigurator
) config
).setDeleteInAllClassifications(true);
171 DeleteConfiguratorDialog dialog
;
172 dialog
= new DeleteConfiguratorDialog(config
, shell
,
173 CONFIRM_DELETION
, null,
174 String
.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S
, TAXON
),
175 MessageDialog
.WARNING
, new String
[] { DELETE
, SKIP
}, 0, true);
176 int result_dialog
= dialog
.open();
177 if (result_dialog
!= IStatus
.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
= TEAM_OR_PERSON
;
186 } else if (object
instanceof Media
){
187 config
= new MediaDeletionConfigurator();
189 Media media
= HibernateProxyHelper
.deproxy(object
, Media
.class);
190 ((MediaDeletionConfigurator
)config
).setDeleteFrom(media
);
191 DeleteConfiguratorDialog dialog
;
192 dialog
= new DeleteConfiguratorDialog(config
, shell
,
193 CONFIRM_DELETION
, null,
194 String
.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S
, MEDIA
),
195 MessageDialog
.WARNING
, new String
[] { DELETE
, SKIP
}, 0, true);
196 int result_dialog
= dialog
.open();
197 if (result_dialog
!= IStatus
.OK
){
201 result
= controller
.getMediaService().isDeletable(media
.getUuid(), config
);
202 errorMessage
= MEDIA
;
207 } catch (Exception e
){
208 MessagingUtils
.errorDialog(AN_EXCEPTION_OCCURED
+" "+DELETE_NOT_POSSIBLE
, getClass(), e
.getMessage(), TaxeditorBulkeditorPlugin
.PLUGIN_ID
, null, true); //$NON-NLS-1$
210 if (result
.isError() || result
.isAbort()){
211 //convert first letter to upper case
212 errorMessage
= errorMessage
.substring(0, 1).toUpperCase()+errorMessage
.substring(1);
213 if (!result
.getExceptions().isEmpty()) {
214 List
<String
> messages
= new ArrayList
<String
>();
215 for (Exception e
:result
.getExceptions()){
216 messages
.add(e
.getMessage());
218 errorMessage
+= " " +COULD_NOT_BE_DELETED
; //$NON-NLS-1$
219 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
220 DeleteResultMessagingUtils
.messageDialogWithDetails(result
,errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);
222 MessagingUtils
.messageDialog(DELETE_NOT_POSSIBLE
, getClass(), errorMessage
+" "+COULD_NOT_BE_DELETED
+" "+AN_EXCEPTION_OCCURED
, null); //$NON-NLS-1$ //$NON-NLS-2$
228 if(result
.getUpdatedObjects().size() != 0 || !result
.getExceptions().isEmpty()){
229 List
<String
> messages
= new ArrayList
<String
>();
230 for (Exception e
:result
.getExceptions()){
231 messages
.add(e
.getMessage());
233 //convert first letter to upper case
234 errorMessage
= errorMessage
.substring(0, 1).toUpperCase()+errorMessage
.substring(1);
235 errorMessage
+= eu
.etaxonomy
.taxeditor
.l10n
.Messages
.DeleteHandler_CAN_BE_DELETED
;
236 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
237 DeleteResultMessagingUtils
.messageDialogWithDetails(result
, errorMessage
, TaxeditorBulkeditorPlugin
.PLUGIN_ID
);
244 public boolean canExecute(@Named(IServiceConstants
.ACTIVE_SELECTION
)Object selection
,
245 MHandledMenuItem menuItem
){
246 boolean canExecute
= false;
247 canExecute
= selection
!=null;
248 menuItem
.setVisible(canExecute
);