1b0076fbf0f0d57b8771b35ac21c003dd6e0bc71
[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.UUID;
14
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.ui.IEditorInput;
22 import org.eclipse.ui.IEditorPart;
23 import org.eclipse.ui.IViewPart;
24 import org.eclipse.ui.IViewReference;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.handlers.HandlerUtil;
27 import org.eclipse.ui.texteditor.IDocumentProvider;
28 import org.eclipse.ui.IWorkbenchPage;
29
30 import eu.etaxonomy.cdm.api.service.DeleteResult;
31 import eu.etaxonomy.cdm.api.service.IAgentService;
32 import eu.etaxonomy.cdm.api.service.IGroupService;
33 import eu.etaxonomy.cdm.api.service.INameService;
34 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
35 import eu.etaxonomy.cdm.api.service.IReferenceService;
36 import eu.etaxonomy.cdm.api.service.ITaxonService;
37 import eu.etaxonomy.cdm.api.service.IUserService;
38 import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
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
43 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
44 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
45 import eu.etaxonomy.cdm.model.reference.Reference;
46 import eu.etaxonomy.cdm.model.taxon.Synonym;
47 import eu.etaxonomy.cdm.model.taxon.Taxon;
48 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
49 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
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.referencingobjects.ReferencingObjectsView;
55
56
57 import eu.etaxonomy.taxeditor.model.MessagingUtils;
58 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
59 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
60 import eu.etaxonomy.taxeditor.store.CdmStore;
61
62
63 /**
64 * @author n.hoffmann
65 * @created Mar 11, 2011
66 * @version 1.0
67 */
68 public class DeleteHandler extends AbstractHandler {
69
70 /* (non-Javadoc)
71 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
72 */
73 @Override
74 public Object execute(ExecutionEvent event) throws ExecutionException {
75
76 if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_EXPERIMENTAL_FEATURES)){
77 ISelection selection = HandlerUtil.getCurrentSelection(event);
78
79 IEditorPart editor = HandlerUtil.getActiveEditor(event);
80
81 IEditorInput input = editor.getEditorInput();
82
83 if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
84
85
86 IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
87 LineAnnotationModel model =
88 (LineAnnotationModel) provider.getAnnotationModel(input);
89
90
91 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
92
93 IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
94
95
96 for(Object object : structuredSelection.toList()){
97
98 LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
99 DeleteResult result = new DeleteResult();
100 //result.setError();
101 try {
102 ICdmApplicationConfiguration controller;
103 controller = (ICdmApplicationConfiguration) CdmStore.getCurrentApplicationConfiguration();
104 //persistenceService.delete(object);
105 if (object instanceof SpecimenOrObservationBase){
106
107 IOccurrenceService service = controller.getOccurrenceService();
108 service.load(((SpecimenOrObservationBase) object).getUuid());
109 result = service.delete((SpecimenOrObservationBase)object);
110 } else if (object instanceof Reference){
111 IReferenceService service = controller.getReferenceService();
112 service.load(((Reference) object).getUuid());
113 result = service.delete((Reference)object);
114 } else if (object instanceof Group){
115 IGroupService service = controller.getGroupService();
116 service.load(((Group) object).getUuid());
117 result = service.delete((Group) object);
118 }else if (object instanceof User){
119 IUserService service = controller.getUserService();
120 service.load(((User) object).getUuid());
121 result = service.delete((User) object);
122 } else if (object instanceof TaxonNameBase){
123 INameService service = controller.getNameService();
124 TaxonNameBase name = service.load(((TaxonNameBase) object).getUuid());
125 NameDeletionConfigurator config = new NameDeletionConfigurator();
126
127 result = service.delete(name, config);
128 } else if (object instanceof TaxonBase){
129 ITaxonService service = controller.getTaxonService();
130 service.load(((TaxonBase) object).getUuid());
131 if (object instanceof Taxon){
132 result = service.deleteTaxon((Taxon)object, null, null);
133 }else{
134 result = service.deleteSynonym((Synonym)object, null);
135 }
136 } else if (object instanceof TeamOrPersonBase){
137 IAgentService service = controller.getAgentService();
138 object = service.load(((TeamOrPersonBase) object).getUuid());
139 result = service.delete((TeamOrPersonBase)object);
140 }
141 } catch (Exception e){
142 MessagingUtils.messageDialog("Exception occured. Could not delete", getClass(), e.getMessage(), null);
143 }
144 if (result.isError() || result.isAbort()){
145 if (!result.getExceptions().isEmpty()) {
146 MessagingUtils.messageDialog("Could not delete", getClass(), result.getExceptions().toString(), null);
147 }else{
148 MessagingUtils.messageDialog("Could not delete", getClass(), "The object could not be deleted. An exception occured.", null);
149
150 }
151 }
152 if (result.isOk() ){
153 ((BulkEditor) editor).removeAnnotatedLine(annotation);
154 if(result.getUpdatedObjects().size() != 0 && !result.getExceptions().isEmpty()){
155 MessageDialog.openInformation(null, "Delete successfull", "The Synonym could be deleted, but there is an updated object: " + result.getExceptions().toString());
156 }
157
158 }
159 if (part != null){
160 ((ReferencingObjectsView)part).refresh();
161 }
162 part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
163 .findView("eu.etaxonomy.taxeditor.editor.view.descriptive");
164 if (part != null){
165 //((DescriptiveViewPart)part).dispose();
166 }
167 }
168 }
169 }else{
170 MessagingUtils.warningDialog("Feature not enabled", getClass(), "Deletion is currently an experimental feature." +
171 "Enable it via Preferences at your own risk.");
172 }
173
174
175 return null;
176 }
177
178 }