Merge branch 'hotfix/3.12.4'
[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.INameService;
32 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
33 import eu.etaxonomy.cdm.api.service.IReferenceService;
34 import eu.etaxonomy.cdm.api.service.ITaxonService;
35 import eu.etaxonomy.cdm.api.service.IUserService;
36 import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
37 import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
38 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
39 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
40 import eu.etaxonomy.cdm.model.common.Group;
41 import eu.etaxonomy.cdm.model.common.User;
42 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
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.annotatedlineeditor.IEntityPersistenceService;
49 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
50 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
51 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
52 import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
53 import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
54 import eu.etaxonomy.taxeditor.model.MessagingUtils;
55 import eu.etaxonomy.taxeditor.store.CdmStore;
56 import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
57 import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteTaxonConfiguratorComposite;
58
59
60 /**
61 * @author n.hoffmann
62 * @created Mar 11, 2011
63 * @version 1.0
64 */
65 public class DeleteHandler extends AbstractHandler {
66
67 /* (non-Javadoc)
68 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
69 */
70 @Override
71 public Object execute(ExecutionEvent event) throws ExecutionException {
72
73 ISelection selection = HandlerUtil.getCurrentSelection(event);
74
75 IEditorPart editor = HandlerUtil.getActiveEditor(event);
76
77 IEditorInput input = editor.getEditorInput();
78
79 if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
80
81
82 IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
83 LineAnnotationModel model =
84 (LineAnnotationModel) provider.getAnnotationModel(input);
85
86
87 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
88
89 IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
90
91
92 for(Object object : structuredSelection.toList()){
93
94 LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
95 DeleteResult result = new DeleteResult();
96 String errorMessage= "The object ";
97 //result.setError();
98 try {
99 ICdmApplicationConfiguration controller;
100 controller = CdmStore.getCurrentApplicationConfiguration();
101
102 if (object instanceof SpecimenOrObservationBase){
103 IOccurrenceService service = controller.getOccurrenceService();
104 if (object != null){
105 result = service.delete(((SpecimenOrObservationBase) object).getUuid());
106 errorMessage = "The specimen or observation ";
107 }
108 } else if (object instanceof Reference){
109 IReferenceService service = controller.getReferenceService();
110 if (object != null){
111 result = service.delete(((Reference) object).getUuid());
112 errorMessage = "The reference ";
113 }
114
115 } else if (object instanceof Group){
116 IGroupService service = controller.getGroupService();
117 if (object != null){
118 result = service.delete(((Group) object).getUuid());
119 errorMessage = "The group ";
120 }
121 }else if (object instanceof User){
122 IUserService service = controller.getUserService();
123 if (object != null){
124 result = service.delete(((User) object).getUuid());
125 errorMessage = "The user ";
126 }
127 } else if (object instanceof TaxonNameBase){
128 INameService service = controller.getNameService();
129 if (object != null){
130 NameDeletionConfigurator config = new NameDeletionConfigurator();
131
132 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);
133 int result_dialog= dialog.open();
134 if (result_dialog == 1){
135 return null;
136 }
137 result = service.delete(((TaxonNameBase) object).getUuid(), config);
138 errorMessage = "The name ";
139 }
140 } else if (object instanceof TaxonBase){
141 ITaxonService service = controller.getTaxonService();
142 if (object != null){
143 if (object instanceof Taxon){
144 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
145 config.setDeleteInAllClassifications(true);
146 DeleteConfiguratorDialog 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);
147 int result_dialog= dialog.open();
148 if (result_dialog == 1){
149 return null;
150 }
151 result = service.deleteTaxon(((TaxonBase) object).getUuid(), config, null);
152 errorMessage = "The taxon ";
153 }else{
154 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
155
156 result = service.deleteSynonym(((Synonym)object).getUuid(), config);
157 errorMessage = "The synonym ";
158 }
159 }
160 } else if (object instanceof TeamOrPersonBase){
161 IAgentService service = controller.getAgentService();
162 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
163 result = service.delete(((TeamOrPersonBase)object).getUuid());
164 errorMessage = "The team or person ";
165 }
166 } catch (Exception e){
167 MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
168 }
169 if (result.isError() || result.isAbort()){
170 if (!result.getExceptions().isEmpty()) {
171 List<String> messages = new ArrayList<String>();
172 int i = result.getExceptions().size();
173 for (Exception e:result.getExceptions()){
174 messages.add(e.getMessage());
175 }
176 errorMessage += "could not be deleted.";
177 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
178 DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
179 }else{
180 MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
181 }
182 }
183 if (result.isOk() ){
184 ((BulkEditor) editor).removeAnnotatedLine(annotation);
185 if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
186 List<String> messages = new ArrayList<String>();
187 int i = result.getExceptions().size();
188 for (Exception e:result.getExceptions()){
189 messages.add(e.getMessage());
190 }
191 errorMessage += "was deleted but related object(s) could not be deleted. ";
192 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
193 DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
194 }
195
196 }
197
198 }
199 }
200
201
202 return null;
203 }
204
205 }