Project

General

Profile

Download (8.98 KB) Statistics
| Branch: | Tag: | Revision:
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.dialogs.MessageDialog;
20
import org.eclipse.jface.viewers.ISelection;
21
import org.eclipse.jface.viewers.IStructuredSelection;
22
import org.eclipse.ui.IEditorInput;
23
import org.eclipse.ui.IEditorPart;
24
import org.eclipse.ui.handlers.HandlerUtil;
25
import org.eclipse.ui.texteditor.IDocumentProvider;
26

    
27
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
28
import eu.etaxonomy.cdm.api.service.DeleteResult;
29
import eu.etaxonomy.cdm.api.service.IAgentService;
30
import eu.etaxonomy.cdm.api.service.IGroupService;
31
import eu.etaxonomy.cdm.api.service.IMediaService;
32
import eu.etaxonomy.cdm.api.service.INameService;
33
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
34
import eu.etaxonomy.cdm.api.service.IReferenceService;
35
import eu.etaxonomy.cdm.api.service.ITaxonService;
36
import eu.etaxonomy.cdm.api.service.IUserService;
37
import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
38
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
39
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
40
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
41
import eu.etaxonomy.cdm.model.common.Group;
42
import eu.etaxonomy.cdm.model.common.User;
43
import eu.etaxonomy.cdm.model.media.Media;
44
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
45
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
46
import eu.etaxonomy.cdm.model.reference.Reference;
47
import eu.etaxonomy.cdm.model.taxon.Synonym;
48
import eu.etaxonomy.cdm.model.taxon.Taxon;
49
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
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.internal.TaxeditorBulkeditorPlugin;
55
import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
56
import eu.etaxonomy.taxeditor.model.MessagingUtils;
57
import eu.etaxonomy.taxeditor.store.CdmStore;
58
import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
59

    
60

    
61
/**
62
 * @author n.hoffmann
63
 * @created Mar 11, 2011
64
 * @version 1.0
65
 */
66
public class DeleteHandler extends AbstractHandler {
67

    
68
	/* (non-Javadoc)
69
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
70
	 */
71
	@Override
72
	public Object execute(ExecutionEvent event) throws ExecutionException {
73

    
74
		ISelection selection = HandlerUtil.getCurrentSelection(event);
75

    
76
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
77

    
78
		IEditorInput input = editor.getEditorInput();
79

    
80
		if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
81

    
82

    
83
			IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
84
			LineAnnotationModel model =
85
					(LineAnnotationModel) provider.getAnnotationModel(input);
86

    
87

    
88
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
89

    
90
			IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
91

    
92

    
93
			for(Object object : structuredSelection.toList()){
94

    
95
				LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
96
				DeleteResult result = new DeleteResult();
97
				String errorMessage= "The object ";
98
				//result.setError();
99
				try {
100
					ICdmApplicationConfiguration controller;
101
					controller = CdmStore.getCurrentApplicationConfiguration();
102

    
103
					if (object instanceof SpecimenOrObservationBase){
104
						IOccurrenceService service = controller.getOccurrenceService();
105
						if (object != null){
106
							result = service.delete(((SpecimenOrObservationBase) object).getUuid());
107
							errorMessage = "The specimen or observation ";
108
						}
109
					} else if (object instanceof Reference){
110
						IReferenceService service = controller.getReferenceService();
111
						if (object != null){
112
							result = service.delete(((Reference) object).getUuid());
113
							errorMessage = "The reference ";
114
						}
115

    
116
					} else if (object instanceof Group){
117
						IGroupService service = controller.getGroupService();
118
						if (object != null){
119
							result = service.delete(((Group) object).getUuid());
120
							errorMessage = "The group ";
121
						}
122
					}else if (object instanceof User){
123
						IUserService service = controller.getUserService();
124
						if (object != null){
125
							result = service.delete(((User) object).getUuid());
126
							errorMessage = "The user ";
127
						}
128
					} else if (object instanceof TaxonNameBase){
129
						INameService service = controller.getNameService();
130
						if (object != null){
131
							NameDeletionConfigurator config = new NameDeletionConfigurator();
132

    
133
							DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), "Confirm Deletion",  null,  "Do you really want to delete the name", MessageDialog.WARNING, new String[] { "Delete", "Skip" }, 0);
134
							int result_dialog= dialog.open();
135
							if (result_dialog == 1){
136
								return null;
137
							}
138
							result = service.delete(((TaxonNameBase) object).getUuid(), config);
139
							errorMessage = "The name ";
140
						}
141
					} else if (object instanceof TaxonBase){
142
						ITaxonService service = controller.getTaxonService();
143
						if (object != null){
144
							if (object instanceof Taxon){
145
								TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
146
								config.setDeleteInAllClassifications(true);
147
								DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), "Confirm Deletion",  null,  "Do you really want to delete the taxon", MessageDialog.WARNING, new String[] { "Delete", "Skip" }, 0);
148
								int result_dialog= dialog.open();
149
								if (result_dialog == 1){
150
									return null;
151
								}
152
								result = service.deleteTaxon(((TaxonBase) object).getUuid(), config, null);
153
								errorMessage = "The taxon ";
154
							}else{
155
								SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
156

    
157
								result = service.deleteSynonym(((Synonym)object).getUuid(), config);
158
								errorMessage = "The synonym ";
159
							}
160
						}
161
					} else if (object instanceof TeamOrPersonBase){
162
						IAgentService service = controller.getAgentService();
163
						//TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
164
						result = service.delete(((TeamOrPersonBase)object).getUuid());
165
						errorMessage = "The team or person ";
166
					} else if (object instanceof Media){
167
                        IMediaService service = controller.getMediaService();
168
                        //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
169
                        result = service.delete(((Media)object).getUuid());
170
                        errorMessage = "The media ";
171
                    }
172

    
173
				} catch (Exception e){
174
					MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
175
				}
176
				if (result.isError() || result.isAbort()){
177
					if (!result.getExceptions().isEmpty()) {
178
						List<String> messages = new ArrayList<String>();
179
						int i = result.getExceptions().size();
180
						for (Exception e:result.getExceptions()){
181
							messages.add(e.getMessage());
182
						}
183
						errorMessage += "could not be deleted.";
184
						//MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
185
						DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
186
					}else{
187
						MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
188
					}
189
				}
190
				if (result.isOk() ){
191
					((BulkEditor) editor).removeAnnotatedLine(annotation);
192
					if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
193
					    List<String> messages = new ArrayList<String>();
194
                        int i = result.getExceptions().size();
195
                        for (Exception e:result.getExceptions()){
196
                            messages.add(e.getMessage());
197
                        }
198
					    errorMessage += "was deleted but related object(s) could not be deleted. ";
199
					    //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
200
					    DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
201
					}
202

    
203
				}
204

    
205
			}
206
		}
207

    
208

    
209
		return null;
210
	}
211

    
212
}
(3-3/9)