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.model.MessagingUtils;
47 import eu.etaxonomy.taxeditor.store.CdmStore;
48
49
50 /**
51 * @author n.hoffmann
52 * @created Mar 11, 2011
53 * @version 1.0
54 */
55 public class DeleteHandler extends AbstractHandler {
56
57 /* (non-Javadoc)
58 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
59 */
60 @Override
61 public Object execute(ExecutionEvent event) throws ExecutionException {
62
63 ISelection selection = HandlerUtil.getCurrentSelection(event);
64
65 IEditorPart editor = HandlerUtil.getActiveEditor(event);
66
67 IEditorInput input = editor.getEditorInput();
68
69 if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
70
71
72 IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
73 LineAnnotationModel model =
74 (LineAnnotationModel) provider.getAnnotationModel(input);
75
76
77 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
78
79 IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
80
81
82 for(Object object : structuredSelection.toList()){
83
84 LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
85 DeleteResult result = new DeleteResult();
86 //result.setError();
87 try {
88 ICdmApplicationConfiguration controller;
89 controller = CdmStore.getCurrentApplicationConfiguration();
90
91 if (object instanceof SpecimenOrObservationBase){
92 IOccurrenceService service = controller.getOccurrenceService();
93 if (object != null){
94 result = service.delete(((SpecimenOrObservationBase) object).getUuid());
95 }
96 } else if (object instanceof Reference){
97 IReferenceService service = controller.getReferenceService();
98 if (object != null){
99 result = service.delete(((Reference) object).getUuid());
100 }
101
102 } else if (object instanceof Group){
103 IGroupService service = controller.getGroupService();
104 if (object != null){
105 result = service.delete(((Group) object).getUuid());
106 }
107 }else if (object instanceof User){
108 IUserService service = controller.getUserService();
109 if (object != null){
110 result = service.delete(((User) object).getUuid());
111 }
112 } else if (object instanceof TaxonNameBase){
113 INameService service = controller.getNameService();
114 if (object != null){
115 NameDeletionConfigurator config = new NameDeletionConfigurator();
116 result = service.delete(((TaxonNameBase) object).getUuid(), config);
117 }
118 } else if (object instanceof TaxonBase){
119 ITaxonService service = controller.getTaxonService();
120 if (object != null){
121 if (object instanceof Taxon){
122 result = service.deleteTaxon(((TaxonBase) object).getUuid(), null, null);
123 }else{
124 result = service.deleteSynonym(((Synonym)object).getUuid(), null);
125 }
126 }
127 } else if (object instanceof TeamOrPersonBase){
128 IAgentService service = controller.getAgentService();
129 //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
130 result = service.delete(((TeamOrPersonBase)object).getUuid());
131 }
132 } catch (Exception e){
133 MessagingUtils.messageDialog("Exception occured. Could not delete", getClass(), e.getMessage(), null);
134 }
135 if (result.isError() || result.isAbort()){
136 if (!result.getExceptions().isEmpty()) {
137 MessagingUtils.messageDialog("Could not delete", getClass(), result.getExceptions().toString(), null);
138 }else{
139 MessagingUtils.messageDialog("Could not delete", getClass(), "The object could not be deleted. An exception occured.", null);
140
141 }
142 }
143 if (result.isOk() ){
144 ((BulkEditor) editor).removeAnnotatedLine(annotation);
145 if(result.getUpdatedObjects().size() != 0 && !result.getExceptions().isEmpty()){
146 MessagingUtils.informationDialog("Delete successfull", "The object is deleted but there are updated objects: " + result.toString());
147 }
148
149 }
150
151 }
152 }
153
154
155 return null;
156 }
157
158 }