Merge branch 'develop' of https://dev.e-taxonomy.eu/git/taxeditor into develop
[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 result = service.delete(((TaxonNameBase) object).getUuid(), config);
132 errorMessage = "The name ";
133 }
134 } else if (object instanceof TaxonBase){
135 ITaxonService service = controller.getTaxonService();
136 if (object != null){
137 if (object instanceof Taxon){
138 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
139 config.setDeleteInAllClassifications(true);
140 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);
141 int result_dialog= dialog.open();
142 if (result_dialog == 1){
143 return null;
144 }
145 result = service.deleteTaxon(((TaxonBase) object).getUuid(), config, null);
146 errorMessage = "The taxon ";
147 }else{
148 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
149 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);
150 int result_dialog= dialog.open();
151 if (result_dialog == 1){
152 return null;
153 }
154 result = service.deleteSynonym(((Synonym)object).getUuid(), config);
155 errorMessage = "The synonym ";
156 }
157 }
158 } else if (object instanceof TeamOrPersonBase){
159 IAgentService service = controller.getAgentService();
160 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
161 result = service.delete(((TeamOrPersonBase)object).getUuid());
162 errorMessage = "The team or person ";
163 }
164 } catch (Exception e){
165 MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
166 }
167 if (result.isError() || result.isAbort()){
168 if (!result.getExceptions().isEmpty()) {
169 List<String> messages = new ArrayList<String>();
170 int i = result.getExceptions().size();
171 for (Exception e:result.getExceptions()){
172 messages.add(e.getMessage());
173 }
174 errorMessage += "could not be deleted.";
175 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
176 DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
177 }else{
178 MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
179 }
180 }
181 if (result.isOk() ){
182 ((BulkEditor) editor).removeAnnotatedLine(annotation);
183 if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
184 List<String> messages = new ArrayList<String>();
185 int i = result.getExceptions().size();
186 for (Exception e:result.getExceptions()){
187 messages.add(e.getMessage());
188 }
189 errorMessage += "was deleted but related object(s) could not be deleted. ";
190 //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
191 DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
192 }
193
194 }
195
196 }
197 }
198
199
200 return null;
201 }
202
203 }