improve the delete configurator dialogs for taxa and synonyms
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / handler / DeleteHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.bulkeditor.handler;
12
13 import java.util.ArrayList;
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.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.handlers.HandlerUtil;
25 import org.eclipse.ui.texteditor.IDocumentProvider;
26
27 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
28 import eu.etaxonomy.cdm.api.service.DeleteResult;
29 import eu.etaxonomy.cdm.api.service.IAgentService;
30 import eu.etaxonomy.cdm.api.service.IGroupService;
31 import eu.etaxonomy.cdm.api.service.IMediaService;
32 import eu.etaxonomy.cdm.api.service.INameService;
33 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
34 import eu.etaxonomy.cdm.api.service.IReferenceService;
35 import eu.etaxonomy.cdm.api.service.ITaxonService;
36 import eu.etaxonomy.cdm.api.service.IUserService;
37 import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
38 import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
39 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
40 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
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.model.DeleteResultMessagingUtils;
56 import eu.etaxonomy.taxeditor.model.MessagingUtils;
57 import eu.etaxonomy.taxeditor.store.CdmStore;
58 import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
59
60
61 /**
62 * @author n.hoffmann
63 * @created Mar 11, 2011
64 * @version 1.0
65 */
66 public class DeleteHandler extends AbstractHandler {
67
68 /* (non-Javadoc)
69 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
70 */
71 @Override
72 public Object execute(ExecutionEvent event) throws ExecutionException {
73
74 ISelection selection = HandlerUtil.getCurrentSelection(event);
75
76 IEditorPart editor = HandlerUtil.getActiveEditor(event);
77
78 IEditorInput input = editor.getEditorInput();
79
80 if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
81
82
83 IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
84 LineAnnotationModel model =
85 (LineAnnotationModel) provider.getAnnotationModel(input);
86
87
88 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
89
90 IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
91
92
93 for(Object object : structuredSelection.toList()){
94
95 LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
96 DeleteResult result = new DeleteResult();
97 String errorMessage= "The object ";
98 //result.setError();
99 try {
100 ICdmApplicationConfiguration controller;
101 controller = CdmStore.getCurrentApplicationConfiguration();
102
103 if (object instanceof SpecimenOrObservationBase){
104 IOccurrenceService service = controller.getOccurrenceService();
105 if (object != null){
106 result = service.delete(((SpecimenOrObservationBase) object).getUuid());
107 errorMessage = "The specimen or observation ";
108 }
109 } else if (object instanceof Reference){
110 IReferenceService service = controller.getReferenceService();
111 if (object != null){
112 result = service.delete(((Reference) object).getUuid());
113 errorMessage = "The reference ";
114 }
115
116 } else if (object instanceof Group){
117 IGroupService service = controller.getGroupService();
118 if (object != null){
119 result = service.delete(((Group) object).getUuid());
120 errorMessage = "The group ";
121 }
122 }else if (object instanceof User){
123 IUserService service = controller.getUserService();
124 if (object != null){
125 result = service.delete(((User) object).getUuid());
126 errorMessage = "The user ";
127 }
128 } else if (object instanceof TaxonNameBase){
129 INameService service = controller.getNameService();
130 if (object != null){
131 NameDeletionConfigurator config = new NameDeletionConfigurator();
132
133 DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", null, "Do you really want to delete the name?", MessageDialog.WARNING, new String[] { "Delete", "Skip" }, 0);
134 int result_dialog= dialog.open();
135 if (result_dialog == 1){
136 return null;
137 }
138 result = service.delete(((TaxonNameBase) object).getUuid(), config);
139 errorMessage = "The name ";
140 }
141 } else if (object instanceof TaxonBase){
142 ITaxonService service = controller.getTaxonService();
143 if (object != null){
144 if (object instanceof Taxon){
145 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
146 config.setDeleteInAllClassifications(true);
147 DeleteConfiguratorDialog dialog;
148 if (((Taxon)object).getTaxonNodes().isEmpty()){
149 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);
150 int result_dialog= dialog.open();
151 if (result_dialog == 1){
152 return null;
153 }
154 result = service.deleteTaxon(((TaxonBase) object).getUuid(), config, null);
155 errorMessage = "The taxon ";
156 } else{
157 MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
158 return null;
159 }
160
161 }else{
162 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
163 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);
164 int result_dialog= dialog.open();
165 if (result_dialog == 1){
166 return null;
167 }
168 result = service.deleteSynonym(((Synonym)object).getUuid(), config);
169 errorMessage = "The synonym ";
170 }
171 }
172 } else if (object instanceof TeamOrPersonBase){
173 IAgentService service = controller.getAgentService();
174 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
175 result = service.delete(((TeamOrPersonBase)object).getUuid());
176 errorMessage = "The team or person ";
177 } else if (object instanceof Media){
178 IMediaService service = controller.getMediaService();
179 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
180 result = service.delete(((Media)object).getUuid(), null);
181 errorMessage = "The media ";
182 }
183
184 } catch (Exception e){
185 MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
186 }
187 if (result.isError() || result.isAbort()){
188 if (!result.getExceptions().isEmpty()) {
189 List<String> messages = new ArrayList<String>();
190 int i = result.getExceptions().size();
191 for (Exception e:result.getExceptions()){
192 messages.add(e.getMessage());
193 }
194 errorMessage += "could not be deleted.";
195 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
196 DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
197 }else{
198 MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
199 }
200 }
201 if (result.isOk() ){
202 ((BulkEditor) editor).removeAnnotatedLine(annotation);
203 if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
204 List<String> messages = new ArrayList<String>();
205 int i = result.getExceptions().size();
206 for (Exception e:result.getExceptions()){
207 messages.add(e.getMessage());
208 }
209 errorMessage += "was deleted but related object(s) could not be deleted. ";
210 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
211 DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
212 }
213
214 }
215
216 }
217 }
218
219
220 return null;
221 }
222
223 }