1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.bulkeditor.e4.handler;
|
11
|
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
|
16
|
import javax.inject.Named;
|
17
|
|
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.menu.MHandledMenuItem;
|
22
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
23
|
import org.eclipse.jface.dialogs.MessageDialog;
|
24
|
import org.eclipse.swt.widgets.Shell;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.application.ICdmRepository;
|
27
|
import eu.etaxonomy.cdm.api.service.DeleteResult;
|
28
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
29
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
30
|
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
|
31
|
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
|
32
|
import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
|
33
|
import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
|
34
|
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
|
35
|
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
|
36
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
37
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
38
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
39
|
import eu.etaxonomy.cdm.model.common.Group;
|
40
|
import eu.etaxonomy.cdm.model.common.User;
|
41
|
import eu.etaxonomy.cdm.model.media.Media;
|
42
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
43
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
44
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
45
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
46
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
47
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
48
|
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
|
49
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
50
|
import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
|
51
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
52
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
53
|
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
|
54
|
|
55
|
|
56
|
/**
|
57
|
*
|
58
|
* @author pplitzner
|
59
|
* @date 11.09.2017
|
60
|
*
|
61
|
*/
|
62
|
public class DeleteHandlerE4 {
|
63
|
|
64
|
private static final String COULD_NOT_BE_DELETED = eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_COULD_NOT_BE_DELETED;
|
65
|
private static final String AN_EXCEPTION_OCCURED = Messages.DeleteHandler_EXCEPTION;
|
66
|
private static final String DELETE_NOT_POSSIBLE = Messages.DeleteHandler_DELETE_NOT_POSSIBLE;
|
67
|
private static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_S = Messages.DeleteHandler_REALLY_DELETE;
|
68
|
private static final String MEDIA = Messages.DeleteHandler_MEDIA;
|
69
|
private static final String TEAM_OR_PERSON = Messages.DeleteHandler_TEAM_OR_PERSON;
|
70
|
private static final String TAXON = Messages.DeleteHandler_TAXON;
|
71
|
private static final String SYNONYM = Messages.DeleteHandler_SYNONYM;
|
72
|
private static final String NAME = Messages.DeleteHandler_NAME;
|
73
|
private static final String USER = Messages.DeleteHandler_USER;
|
74
|
private static final String GROUP = Messages.DeleteHandler_GROUP;
|
75
|
private static final String REFERENCE = Messages.DeleteHandler_REFERENCE;
|
76
|
private static final String SPECIMEN_OR_OBSERVATION = Messages.DeleteHandler_SPECIMEN;
|
77
|
private static final String OBJECT = Messages.DeleteHandler_OBJECT;
|
78
|
private static final String SKIP = Messages.DeleteHandler_SKIP;
|
79
|
private static final String DELETE = eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_DELETE;
|
80
|
private static final String CONFIRM_DELETION = Messages.DeleteHandler_CONFIRM;
|
81
|
|
82
|
@Execute
|
83
|
public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)Object object,
|
84
|
@Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
|
85
|
|
86
|
DeleteConfiguratorBase config = null;
|
87
|
DeleteResult result = new DeleteResult();
|
88
|
String errorMessage= OBJECT;
|
89
|
if (object instanceof CdmBase){
|
90
|
CdmBase base = (CdmBase)object;
|
91
|
if (base.getId() != 0){
|
92
|
try {
|
93
|
ICdmRepository controller;
|
94
|
controller = CdmStore.getCurrentApplicationConfiguration();
|
95
|
if (object instanceof SpecimenOrObservationBase){
|
96
|
IOccurrenceService service = controller.getOccurrenceService();
|
97
|
config = new SpecimenDeleteConfigurator();
|
98
|
|
99
|
DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
|
100
|
shell, CONFIRM_DELETION, null,
|
101
|
String.format(
|
102
|
DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
|
103
|
SPECIMEN_OR_OBSERVATION),
|
104
|
MessageDialog.WARNING,
|
105
|
new String[] { DELETE, SKIP }, 0, true);
|
106
|
int result_dialog = dialog.open();
|
107
|
if (result_dialog != IStatus.OK){
|
108
|
return;
|
109
|
}
|
110
|
result = service.isDeletable(((SpecimenOrObservationBase<?>) object).getUuid(), config);
|
111
|
errorMessage = SPECIMEN_OR_OBSERVATION;
|
112
|
|
113
|
} else if (object instanceof Reference){
|
114
|
IReferenceService service = controller.getReferenceService();
|
115
|
result = service.isDeletable(((Reference)object).getUuid(), null);
|
116
|
errorMessage = REFERENCE;
|
117
|
|
118
|
} else if (object instanceof Group){
|
119
|
errorMessage = GROUP;
|
120
|
}else if (object instanceof User){
|
121
|
errorMessage = USER;
|
122
|
} else if (object instanceof TaxonName){
|
123
|
TaxonName name = HibernateProxyHelper.deproxy(object, TaxonName.class);
|
124
|
config = new NameDeletionConfigurator();
|
125
|
|
126
|
DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
|
127
|
shell, CONFIRM_DELETION, null,
|
128
|
String.format(
|
129
|
DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
|
130
|
NAME),
|
131
|
MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
|
132
|
int result_dialog = dialog.open();
|
133
|
if (result_dialog != IStatus.OK){
|
134
|
return;
|
135
|
}
|
136
|
result = controller.getNameService().isDeletable(name.getUuid(), config);
|
137
|
errorMessage = NAME;
|
138
|
|
139
|
} else if (object instanceof TaxonBase){
|
140
|
|
141
|
// synonym
|
142
|
if(object instanceof Synonym){
|
143
|
Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
|
144
|
config = new SynonymDeletionConfigurator();
|
145
|
errorMessage = SYNONYM;
|
146
|
DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
|
147
|
shell, CONFIRM_DELETION, null,
|
148
|
String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, SYNONYM),
|
149
|
MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
|
150
|
int result_dialog = dialog.open();
|
151
|
if (result_dialog != IStatus.OK){
|
152
|
return;
|
153
|
}
|
154
|
result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
|
155
|
|
156
|
}
|
157
|
else if(object instanceof Taxon ){
|
158
|
Taxon taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
|
159
|
if (((Taxon)object).getTaxonNodes().isEmpty()){
|
160
|
errorMessage = TAXON;
|
161
|
} else {
|
162
|
MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(),
|
163
|
Messages.DeleteHandler_TAXON_CAN_NOT_BE_DELETED,
|
164
|
null);
|
165
|
return;
|
166
|
}
|
167
|
config = new TaxonDeletionConfigurator();
|
168
|
((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
|
169
|
DeleteConfiguratorDialog dialog;
|
170
|
dialog = new DeleteConfiguratorDialog(config, shell,
|
171
|
CONFIRM_DELETION, null,
|
172
|
String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, TAXON),
|
173
|
MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
|
174
|
int result_dialog = dialog.open();
|
175
|
if (result_dialog != IStatus.OK){
|
176
|
return;
|
177
|
}
|
178
|
result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
|
179
|
|
180
|
}
|
181
|
} else if (object instanceof TeamOrPersonBase){
|
182
|
result = controller.getAgentService().isDeletable(((CdmBase) object).getUuid(), null);
|
183
|
errorMessage = TEAM_OR_PERSON;
|
184
|
} else if (object instanceof Media){
|
185
|
config = new MediaDeletionConfigurator();
|
186
|
|
187
|
Media media = HibernateProxyHelper.deproxy(object, Media.class);
|
188
|
((MediaDeletionConfigurator)config).setDeleteFrom(media);
|
189
|
DeleteConfiguratorDialog dialog;
|
190
|
dialog = new DeleteConfiguratorDialog(config, shell,
|
191
|
CONFIRM_DELETION, null,
|
192
|
String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, MEDIA),
|
193
|
MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
|
194
|
int result_dialog = dialog.open();
|
195
|
if (result_dialog != IStatus.OK){
|
196
|
return;
|
197
|
}
|
198
|
|
199
|
result = controller.getMediaService().isDeletable(media.getUuid(), config);
|
200
|
errorMessage = MEDIA;
|
201
|
|
202
|
}
|
203
|
|
204
|
|
205
|
} catch (Exception e){
|
206
|
MessagingUtils.errorDialog(AN_EXCEPTION_OCCURED+" "+DELETE_NOT_POSSIBLE, getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true); //$NON-NLS-1$
|
207
|
}
|
208
|
if (result.isError() || result.isAbort()){
|
209
|
//convert first letter to upper case
|
210
|
errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
|
211
|
if (!result.getExceptions().isEmpty()) {
|
212
|
List<String> messages = new ArrayList<String>();
|
213
|
for (Exception e:result.getExceptions()){
|
214
|
messages.add(e.getMessage());
|
215
|
}
|
216
|
errorMessage += " " +COULD_NOT_BE_DELETED; //$NON-NLS-1$
|
217
|
//MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
|
218
|
DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
|
219
|
}else{
|
220
|
MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(), errorMessage+" "+COULD_NOT_BE_DELETED+" "+AN_EXCEPTION_OCCURED, null); //$NON-NLS-1$ //$NON-NLS-2$
|
221
|
}
|
222
|
}
|
223
|
}
|
224
|
if (result.isOk() ){
|
225
|
|
226
|
if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
|
227
|
List<String> messages = new ArrayList<String>();
|
228
|
for (Exception e:result.getExceptions()){
|
229
|
messages.add(e.getMessage());
|
230
|
}
|
231
|
//convert first letter to upper case
|
232
|
errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
|
233
|
errorMessage += eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_CAN_BE_DELETED;
|
234
|
//MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
|
235
|
DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
|
236
|
}
|
237
|
}
|
238
|
}
|
239
|
}
|
240
|
|
241
|
@CanExecute
|
242
|
public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
|
243
|
MHandledMenuItem menuItem){
|
244
|
boolean canExecute = false;
|
245
|
canExecute = selection!=null;
|
246
|
menuItem.setVisible(canExecute);
|
247
|
return canExecute;
|
248
|
}
|
249
|
|
250
|
|
251
|
}
|