ref #6480: add deleteConfigurator to bulk editor delete of specimenOrObservation...
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / handler / DeleteHandler.java
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.handler;
11
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15
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.core.runtime.Status;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.text.TextSelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.IEditorPart;
25 import org.eclipse.ui.handlers.HandlerUtil;
26 import org.eclipse.ui.texteditor.IDocumentProvider;
27
28 import eu.etaxonomy.cdm.api.application.ICdmRepository;
29 import eu.etaxonomy.cdm.api.service.DeleteResult;
30 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
31 import eu.etaxonomy.cdm.api.service.IReferenceService;
32 import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
33 import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
34 import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
35 import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
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.CdmBase;
41 import eu.etaxonomy.cdm.model.common.Group;
42 import eu.etaxonomy.cdm.model.common.User;
43 import eu.etaxonomy.cdm.model.media.Media;
44 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
45 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
46 import eu.etaxonomy.cdm.model.reference.Reference;
47 import eu.etaxonomy.cdm.model.taxon.Synonym;
48 import eu.etaxonomy.cdm.model.taxon.Taxon;
49 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
50 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
51 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
52 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
53 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
54 import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
55 import eu.etaxonomy.taxeditor.bulkeditor.l10n.Messages;
56 import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
57 import eu.etaxonomy.taxeditor.model.MessagingUtils;
58 import eu.etaxonomy.taxeditor.store.CdmStore;
59 import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
60
61
62 /**
63 * @author n.hoffmann
64 * @created Mar 11, 2011
65 * @version 1.0
66 */
67 public class DeleteHandler extends AbstractHandler {
68
69
70
71 private static final String SKIP = "Skip";
72 private static final String DELETE = "Delete";
73 private static final String CONFIRM_DELETION = "Confirm Deletion";
74
75 /* (non-Javadoc)
76 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
77 */
78 @Override
79 public Object execute(ExecutionEvent event) throws ExecutionException {
80
81
82 TextSelection selection = (TextSelection) HandlerUtil.getCurrentSelection(event);
83 IEditorPart editor = HandlerUtil.getActiveEditor(event);
84
85 IEditorInput input = editor.getEditorInput();
86
87 if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
88
89
90 IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
91 LineAnnotationModel model =
92 (LineAnnotationModel) provider.getAnnotationModel(input);
93
94
95 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
96 DeleteConfiguratorBase config = null;
97 IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
98 DeleteResult result = new DeleteResult();
99 String errorMessage= "The object ";
100 for(Object object : structuredSelection.toList()){
101 if (object instanceof CdmBase){
102 CdmBase base = (CdmBase)object;
103 LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
104 if (base.getId() != 0){
105
106
107 try {
108 ICdmRepository controller;
109 controller = CdmStore.getCurrentApplicationConfiguration();
110 if (object instanceof SpecimenOrObservationBase){
111 IOccurrenceService service = controller.getOccurrenceService();
112 config = new SpecimenDeleteConfigurator();
113
114 DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION, null, Messages.BulkEditor_DeleteHandler_reallyDeleteSpecimen, MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
115 int result_dialog= dialog.open();
116 if (result_dialog != Status.OK){
117 return null;
118 }
119 if (object != null){
120 result = service.isDeletable(((SpecimenOrObservationBase) object).getUuid(), config);
121 errorMessage = "The specimen or observation ";
122
123 }
124 } else if (object instanceof Reference){
125 IReferenceService service = controller.getReferenceService();
126 if (object != null){
127 result = service.isDeletable(((Reference)object).getUuid(), null);
128 errorMessage = "The reference ";
129 }
130
131 } else if (object instanceof Group){
132 errorMessage = "The group ";
133 }else if (object instanceof User){
134 errorMessage = "The user ";
135 } else if (object instanceof TaxonNameBase){
136 TaxonNameBase name = HibernateProxyHelper.deproxy(object, TaxonNameBase.class);
137 if (object != null){
138 config = new NameDeletionConfigurator();
139
140 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);
141 int result_dialog= dialog.open();
142 if (result_dialog != Status.OK){
143 return null;
144 }
145 result = controller.getNameService().isDeletable(name.getUuid(), config);
146 errorMessage = "The name ";
147
148 }
149 } else if (object instanceof TaxonBase){
150
151 // synonym
152 if(object instanceof Synonym){
153 Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
154 config = new SynonymDeletionConfigurator();
155 errorMessage = "The synonym ";
156 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);
157 int result_dialog= dialog.open();
158 if (result_dialog != Status.OK){
159 return null;
160 }
161 result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
162
163 }
164 else if(object instanceof Taxon ){
165 Taxon taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
166 if (((Taxon)object).getTaxonNodes().isEmpty()){
167 errorMessage = "The taxon ";
168 } else{
169 MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
170 return null;
171 }
172 config = new TaxonDeletionConfigurator();
173 ((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
174 DeleteConfiguratorDialog dialog;
175 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);
176 int result_dialog= dialog.open();
177 if (result_dialog != Status.OK){
178 return null;
179 }
180 result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
181
182 }
183 } else if (object instanceof TeamOrPersonBase){
184 result = controller.getAgentService().isDeletable(((CdmBase) object).getUuid(), null);
185 errorMessage = "The team or person ";
186 } else if (object instanceof Media){
187 config = new MediaDeletionConfigurator();
188 Media media = HibernateProxyHelper.deproxy(object, Media.class);
189 DeleteConfiguratorDialog dialog;
190 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);
191 int result_dialog= dialog.open();
192 if (result_dialog != Status.OK){
193 return null;
194 }
195
196 result = controller.getMediaService().isDeletable(media.getUuid(), config);
197 errorMessage = "The media ";
198
199 }
200
201
202 } catch (Exception e){
203 MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
204 }
205 if (result.isError() || result.isAbort()){
206 if (!result.getExceptions().isEmpty()) {
207 List<String> messages = new ArrayList<String>();
208 int i = result.getExceptions().size();
209 for (Exception e:result.getExceptions()){
210 messages.add(e.getMessage());
211 }
212 errorMessage += "could not be deleted.";
213 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
214 DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
215 }else{
216 MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
217 }
218 }else if (model != null) {
219 Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
220 while (iter.hasNext()) {
221 Object next = iter.next();
222 if (next instanceof LineAnnotation) {
223 if (result.isOk()){
224 ((LineAnnotation)next).markAsDeleted(config);
225
226 }
227
228 }
229 }
230 }
231
232 }
233 if (result.isOk() ){
234
235 ((BulkEditor) editor).removeAnnotatedLine(annotation);
236
237 if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
238 List<String> messages = new ArrayList<String>();
239 int i = result.getExceptions().size();
240 for (Exception e:result.getExceptions()){
241 messages.add(e.getMessage());
242 }
243 errorMessage += "can be deleted but related object(s) could not be deleted. ";
244 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
245 DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
246 }
247
248 }
249
250 }
251
252 }
253 }
254
255
256 return null;
257 }
258
259
260 }