Project

General

Profile

Revision 240080af

ID240080affb98467d0a10c2b890bb05af5a2256e3
Parent 99bb324a
Child 3070af14

Added by Patrick Plitzner about 3 years ago

code cleanup

View differences:

eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/handler/DeleteHandler.java
1 1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9 9

  
10 10
package eu.etaxonomy.taxeditor.bulkeditor.handler;
11 11

  
12 12

  
13 13
import java.util.ArrayList;
14
import java.util.Iterator;
15 14
import java.util.List;
16 15

  
17 16
import org.eclipse.core.commands.AbstractHandler;
18 17
import org.eclipse.core.commands.ExecutionEvent;
19 18
import org.eclipse.core.commands.ExecutionException;
20
import org.eclipse.core.runtime.Status;
19
import org.eclipse.core.runtime.IStatus;
21 20
import org.eclipse.jface.dialogs.MessageDialog;
22 21
import org.eclipse.jface.text.TextSelection;
23 22
import org.eclipse.jface.viewers.IStructuredSelection;
......
73 72
    private static final String DELETE = "Delete";
74 73
    private static final String CONFIRM_DELETION = "Confirm Deletion";
75 74

  
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 {
75
    @Override
76
    public Object execute(ExecutionEvent event) throws ExecutionException {
81 77

  
82 78

  
83
		TextSelection selection = (TextSelection) HandlerUtil.getCurrentSelection(event);
84
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
79
        TextSelection selection = (TextSelection) HandlerUtil.getCurrentSelection(event);
80
        IEditorPart editor = HandlerUtil.getActiveEditor(event);
85 81

  
86
		IEditorInput input = editor.getEditorInput();
82
        IEditorInput input = editor.getEditorInput();
87 83

  
88
		if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
84
        if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
89 85

  
90 86

  
91
			IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
92
			LineAnnotationModel model =
93
					(LineAnnotationModel) provider.getAnnotationModel(input);
87
            IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
88
            LineAnnotationModel model =
89
                    (LineAnnotationModel) provider.getAnnotationModel(input);
94 90

  
95 91

  
96
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
97
			DeleteConfiguratorBase config = null;
98
			IEntityPersistenceService<?> persistenceService = (IEntityPersistenceService<?>) input;
99
			DeleteResult result = new DeleteResult();
100
			String errorMessage= "The object ";
101
			for(Object object : structuredSelection.toList()){
102
			    if (object instanceof CdmBase){
103
			        CdmBase base = (CdmBase)object;
104
			        LineAnnotation<?> annotation = (LineAnnotation<?>) model.getAnnotation(object);
105
			        if (base.getId() != 0){
92
            IStructuredSelection structuredSelection = (IStructuredSelection) selection;
93
            DeleteConfiguratorBase config = null;
94
            DeleteResult result = new DeleteResult();
95
            String errorMessage= "The object ";
96
            for(Object object : structuredSelection.toList()){
97
                if (object instanceof CdmBase){
98
                    CdmBase base = (CdmBase)object;
99
                    LineAnnotation<?> annotation = (LineAnnotation<?>) model.getAnnotation(object);
100
                    if (base.getId() != 0){
106 101

  
107 102

  
108
        				try {
109
        				    ICdmRepository controller;
110
        				    controller = CdmStore.getCurrentApplicationConfiguration();
111
        				    if (object instanceof SpecimenOrObservationBase){
112
        				        IOccurrenceService service = controller.getOccurrenceService();
113
        				        config = new SpecimenDeleteConfigurator();
103
                        try {
104
                            ICdmRepository controller;
105
                            controller = CdmStore.getCurrentApplicationConfiguration();
106
                            if (object instanceof SpecimenOrObservationBase){
107
                                IOccurrenceService service = controller.getOccurrenceService();
108
                                config = new SpecimenDeleteConfigurator();
114 109

  
115 110
                                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  Messages.BulkEditor_DeleteHandler_reallyDeleteSpecimen, MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
116 111
                                int result_dialog= dialog.open();
117
                                if (result_dialog != Status.OK){
112
                                if (result_dialog != IStatus.OK){
113
                                    return null;
114
                                }
115
                                result = service.isDeletable(((SpecimenOrObservationBase<?>) object).getUuid(), config);
116
                                errorMessage = "The specimen or observation ";
117

  
118
                            } else if (object instanceof Reference){
119
                                IReferenceService service = controller.getReferenceService();
120
                                result = service.isDeletable(((Reference)object).getUuid(), null);
121
                                errorMessage = "The reference ";
122

  
123
                            } else if (object instanceof Group){
124
                                errorMessage = "The group ";
125
                            }else if (object instanceof User){
126
                                errorMessage = "The user ";
127
                            } else if (object instanceof TaxonName){
128
                                TaxonName name = HibernateProxyHelper.deproxy(object, TaxonName.class);
129
                                config = new NameDeletionConfigurator();
130

  
131
                                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  "Do you really want to delete the name?\nThis operation is irreversible!", MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
132
                                int result_dialog= dialog.open();
133
                                if (result_dialog != IStatus.OK){
118 134
                                    return null;
119 135
                                }
120
        						if (object != null){
121
        							result = service.isDeletable(((SpecimenOrObservationBase<?>) object).getUuid(), config);
122
        							errorMessage = "The specimen or observation ";
123

  
124
        						}
125
        				    } else if (object instanceof Reference){
126
        						IReferenceService service = controller.getReferenceService();
127
        						if (object != null){
128
        							result = service.isDeletable(((Reference)object).getUuid(), null);
129
        							errorMessage = "The reference ";
130
        						}
131

  
132
        				    } else if (object instanceof Group){
133
        				        errorMessage = "The group ";
134
        				    }else if (object instanceof User){
135
        				        errorMessage = "The user ";
136
         				    } else if (object instanceof TaxonName){
137
         				        TaxonName name = HibernateProxyHelper.deproxy(object, TaxonName.class);
138
         				        if (object != null){
139
         				            config = new NameDeletionConfigurator();
140

  
141
         				            DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  "Do you really want to delete the name?\nThis operation is irreversible!", MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
142
         				            int result_dialog= dialog.open();
143
         				            if (result_dialog != Status.OK){
144
         				                return null;
145
         				            }
146
         				            result = controller.getNameService().isDeletable(name.getUuid(), config);
147
         				            errorMessage = "The name ";
148

  
149
         				        }
150
         				    } else if (object instanceof TaxonBase){
151

  
152
        						// synonym
153
        						if(object instanceof Synonym){
154
        							Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
155
        							config = new SynonymDeletionConfigurator();
156
        							errorMessage = "The synonym ";
157
        							DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  "Do you really want to delete the synonym?", MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
136
                                result = controller.getNameService().isDeletable(name.getUuid(), config);
137
                                errorMessage = "The name ";
138

  
139
                            } else if (object instanceof TaxonBase){
140

  
141
                                // synonym
142
                                if(object instanceof Synonym){
143
                                    Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
144
                                    config = new SynonymDeletionConfigurator();
145
                                    errorMessage = "The synonym ";
146
                                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  "Do you really want to delete the synonym?", MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
158 147
                                    int result_dialog= dialog.open();
159
                                    if (result_dialog != Status.OK){
160
                                         return null;
148
                                    if (result_dialog != IStatus.OK){
149
                                        return null;
161 150
                                    }
162 151
                                    result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
163 152

  
164
        						}
165
        						else if(object instanceof Taxon ){
166
        							Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
167
        							if (((Taxon)object).getTaxonNodes().isEmpty()){
168
                                            errorMessage = "The taxon ";
169
        							} else{
170
        							    MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
171
        							    return null;
172
        							}
173
        							config = new TaxonDeletionConfigurator();
174
        							((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
175
        							DeleteConfiguratorDialog dialog;
176
        						    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, true);
177
        							int result_dialog= dialog.open();
178
                                    if (result_dialog != Status.OK){
179
                                         return null;
153
                                }
154
                                else if(object instanceof Taxon ){
155
                                    Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
156
                                    if (((Taxon)object).getTaxonNodes().isEmpty()){
157
                                        errorMessage = "The taxon ";
158
                                    } else{
159
                                        MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
160
                                        return null;
161
                                    }
162
                                    config = new TaxonDeletionConfigurator();
163
                                    ((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
164
                                    DeleteConfiguratorDialog dialog;
165
                                    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, true);
166
                                    int result_dialog= dialog.open();
167
                                    if (result_dialog != IStatus.OK){
168
                                        return null;
180 169
                                    }
181 170
                                    result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
182 171

  
183
        						}
184
         				    } else if (object instanceof TeamOrPersonBase){
185
         				        result = controller.getAgentService().isDeletable(((CdmBase) object).getUuid(), null);
186
    						  errorMessage = "The team or person ";
187
         				   } else if (object instanceof Media){
188
         				       config = new MediaDeletionConfigurator();
189
         				      
190
         				       Media media = HibernateProxyHelper.deproxy(object, Media.class);
191
         				      ((MediaDeletionConfigurator)config).setDeleteFrom(media);
192
         				       DeleteConfiguratorDialog dialog;
193
        					   dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  "Do you really want to delete the media?", MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
194
        					   int result_dialog= dialog.open();
195
                               if (result_dialog != Status.OK){
196
                                   return null;
197
                               }
198

  
199
                               result = controller.getMediaService().isDeletable(media.getUuid(), config);
200
                               errorMessage = "The media ";
201

  
202
                           }
203

  
204

  
205
    				} catch (Exception e){
206
    					MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
207
    				}
208
    				if (result.isError() || result.isAbort()){
209
    					if (!result.getExceptions().isEmpty()) {
210
    						List<String> messages = new ArrayList<String>();
211
    						int i = result.getExceptions().size();
212
    						for (Exception e:result.getExceptions()){
213
    							messages.add(e.getMessage());
214
    						}
215
    						errorMessage += "could not be deleted.";
216
    						//MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
217
    						DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
218
    					}else{
219
    						MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
220
    					}
221
    				}else if (model != null) {
222
    					annotation.markAsDeleted(config);
223
    				}
224
//    					int offset = selection.getOffset();
225
//    					if (offset == 0){
226
//    						offset = 1;
227
//    					}
228
//                        Iterator iter = model.getAnnotationIterator(offset, selection.getLength(), true, true);
229
//                   //     Iterator<Annotation> iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
230
//                        while (iter.hasNext()) {
231
//                            Object next = iter.next();
232
//                            if (next instanceof LineAnnotation) {
233
//                                if (result.isOk()){
234
//                                    ((LineAnnotation)next).markAsDeleted(config);
235
//
236
//                                }
237
//
238
//                            }
239
//                        }
240
//                    }
241

  
242
			    }
243
		        if (result.isOk() ){
244

  
245
                    ((BulkEditor) editor).removeAnnotatedLine(annotation);
246

  
247
                    if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
248
                        List<String> messages = new ArrayList<String>();
249
                        int i = result.getExceptions().size();
250
                        for (Exception e:result.getExceptions()){
251
                            messages.add(e.getMessage());
252
                        }
253
                        errorMessage += "can be deleted but related object(s) could not be deleted. ";
254
                        //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
255
                        DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
256
                    }
257

  
258
                }
259

  
260
			    }
172
                                }
173
                            } else if (object instanceof TeamOrPersonBase){
174
                                result = controller.getAgentService().isDeletable(((CdmBase) object).getUuid(), null);
175
                                errorMessage = "The team or person ";
176
                            } else if (object instanceof Media){
177
                                config = new MediaDeletionConfigurator();
178

  
179
                                Media media = HibernateProxyHelper.deproxy(object, Media.class);
180
                                ((MediaDeletionConfigurator)config).setDeleteFrom(media);
181
                                DeleteConfiguratorDialog dialog;
182
                                dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), CONFIRM_DELETION,  null,  "Do you really want to delete the media?", MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
183
                                int result_dialog= dialog.open();
184
                                if (result_dialog != IStatus.OK){
185
                                    return null;
186
                                }
261 187

  
262
			}
263
		}
188
                                result = controller.getMediaService().isDeletable(media.getUuid(), config);
189
                                errorMessage = "The media ";
264 190

  
191
                            }
265 192

  
266
		return null;
267
	}
268 193

  
194
                        } catch (Exception e){
195
                            MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
196
                        }
197
                        if (result.isError() || result.isAbort()){
198
                            if (!result.getExceptions().isEmpty()) {
199
                                List<String> messages = new ArrayList<String>();
200
                                for (Exception e:result.getExceptions()){
201
                                    messages.add(e.getMessage());
202
                                }
203
                                errorMessage += "could not be deleted.";
204
                                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
205
                                DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
206
                            }else{
207
                                MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
208
                            }
209
                        }
210
                        annotation.markAsDeleted(config);
211
                    }
212
                    if (result.isOk() ){
213

  
214
                        ((BulkEditor) editor).removeAnnotatedLine(annotation);
215

  
216
                        if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
217
                            List<String> messages = new ArrayList<String>();
218
                            for (Exception e:result.getExceptions()){
219
                                messages.add(e.getMessage());
220
                            }
221
                            errorMessage += "can be deleted but related object(s) could not be deleted. ";
222
                            //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
223
                            DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
224
                        }
225
                    }
226
                }
227
            }
228
        }
229
        return null;
230
    }
269 231

  
270 232
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)