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