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