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