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