Project

General

Profile

« Previous | Next » 

Revision 734218f9

Added by Katja Luther over 7 years ago

fix #6175: transient objects are not marked as deleted in bulk editor only removed from model

View differences:

eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/handler/DeleteHandler.java
28 28

  
29 29
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
30 30
import eu.etaxonomy.cdm.api.service.DeleteResult;
31
import eu.etaxonomy.cdm.api.service.IMediaService;
32 31
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
33 32
import eu.etaxonomy.cdm.api.service.IReferenceService;
33
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
34 34
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
35 35
import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
36 36
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
37 37
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
38 38
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
39 39
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
40
import eu.etaxonomy.cdm.model.common.CdmBase;
40 41
import eu.etaxonomy.cdm.model.common.Group;
41 42
import eu.etaxonomy.cdm.model.common.User;
42 43
import eu.etaxonomy.cdm.model.media.Media;
......
87 88

  
88 89

  
89 90
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
90

  
91
			DeleteConfiguratorBase config = null;
91 92
			IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
92

  
93
			DeleteResult result = new DeleteResult();
94
			String errorMessage= "The object ";
93 95
			for(Object object : structuredSelection.toList()){
96
			    if (object instanceof CdmBase){
97
			        CdmBase base = (CdmBase)object;
98
			        LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
99
			        if (base.getId() != 0){
100

  
101

  
102
        				try {
103
        				    ICdmApplicationConfiguration controller;
104
        				    controller = CdmStore.getCurrentApplicationConfiguration();
105
        				    if (object instanceof SpecimenOrObservationBase){
106
        				        IOccurrenceService service = controller.getOccurrenceService();
107
        						if (object != null){
108
        							result = service.isDeletable(((SpecimenOrObservationBase) object).getUuid(), null);
109
        							errorMessage = "The specimen or observation ";
110

  
111
        						}
112
        				    } else if (object instanceof Reference){
113
        						IReferenceService service = controller.getReferenceService();
114
        						if (object != null){
115
        							result = service.isDeletable(((Reference)object).getUuid(), null);
116
        							errorMessage = "The reference ";
117
        						}
118

  
119
        				    } else if (object instanceof Group){
120
        				        errorMessage = "The group ";
121
        				    }else if (object instanceof User){
122
        				        errorMessage = "The user ";
123
         				    } else if (object instanceof TaxonNameBase){
124
         				        TaxonNameBase name = HibernateProxyHelper.deproxy(object, TaxonNameBase.class);
125
         				        if (object != null){
126
         				            config = new NameDeletionConfigurator();
127

  
128
         				            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);
129
         				            int result_dialog= dialog.open();
130
         				            if (result_dialog != Status.OK){
131
         				                return null;
132
         				            }
133
         				            result = controller.getNameService().isDeletable(name.getUuid(), config);
134
         				            errorMessage = "The name ";
135

  
136
         				        }
137
         				    } else if (object instanceof TaxonBase){
138

  
139
        						// synonym
140
        						if(object instanceof Synonym){
141
        							Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
142
        							config = new SynonymDeletionConfigurator();
143
        							errorMessage = "The synonym ";
144
        							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);
145
                                    int result_dialog= dialog.open();
146
                                    if (result_dialog != Status.OK){
147
                                         return null;
148
                                    }
149
                                    result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
150

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

  
170
        						}
171
         				    } else if (object instanceof TeamOrPersonBase){
172

  
173
    						  errorMessage = "The team or person ";
174
         				   } else if (object instanceof Media){
175
         				       config = new MediaDeletionConfigurator();
176
         				       Media media = HibernateProxyHelper.deproxy(object, Media.class);
177
         				       DeleteConfiguratorDialog dialog;
178
        					   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);
179
        					   int result_dialog= dialog.open();
180
                               if (result_dialog != Status.OK){
181
                                   return null;
182
                               }
183

  
184
                               result = controller.getMediaService().isDeletable(media.getUuid(), config);
185
                               errorMessage = "The media ";
186

  
187
                           }
188

  
189

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

  
214
                                }
94 215

  
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.isDeletable(((SpecimenOrObservationBase) object), null);
107
							errorMessage = "The specimen or observation ";
108
							if (model != null) {
109
								Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
110
								while (iter.hasNext()) {
111
									Object next = iter.next();
112
									if (next instanceof LineAnnotation) {
113
										if (result.isOk()){
114
											((LineAnnotation)next).markAsDeleted(null);
115
										}
116

  
117
									}
118
								}
119
							}
120
						}
121
					} else if (object instanceof Reference){
122
						IReferenceService service = controller.getReferenceService();
123
						if (object != null){
124
							result = service.isDeletable((Reference)object, null);
125
							errorMessage = "The reference ";
126
						}
127
						if (model != null) {
128
							Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
129
							while (iter.hasNext()) {
130
								Object next = iter.next();
131
								if (next instanceof LineAnnotation) {
132
									if (result.isOk()){
133
										((LineAnnotation)next).markAsDeleted(null);
134
									}
135

  
136
								}
137
							}
138
						}
139

  
140
					} else if (object instanceof Group){
141
						if (model != null) {
142
							Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
143
							while (iter.hasNext()) {
144
								Object next = iter.next();
145
								if (next instanceof LineAnnotation) {
146
									if (result.isOk()){
147
										((LineAnnotation)next).markAsDeleted(null);
148
									}
149

  
150
								}
151
							}
152
						}
153

  
154
//						IGroupService service = controller.getGroupService();
155
//						if (object != null){
156
//							result = service.delete(((Group) object).getUuid());
157
//							errorMessage = "The group ";
158
//						}
159
					}else if (object instanceof User){
160
						if (model != null) {
161
							Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
162
							while (iter.hasNext()) {
163
								Object next = iter.next();
164
								if (next instanceof LineAnnotation) {
165
									if (result.isOk()){
166
										((LineAnnotation)next).markAsDeleted(null);
167
									}
168

  
169
								}
170
							}
171
						}
172

  
173
//						IUserService service = controller.getUserService();
174
//						if (object != null){
175
//							result = service.delete(((User) object).getUuid());
176
//							errorMessage = "The user ";
177
//						}
178
					} else if (object instanceof TaxonNameBase){
179
						TaxonNameBase name = HibernateProxyHelper.deproxy(object, TaxonNameBase.class);
180

  
181

  
182
						if (object != null){
183
							NameDeletionConfigurator config = new NameDeletionConfigurator();
184

  
185
							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);
186
							int result_dialog= dialog.open();
187
							if (result_dialog != Status.OK){
188
								return null;
189
							}
190

  
191
							//operation = new DeleteTaxonNameOperation(commandName,((BulkEditor) editor).getUndoContext() , name, config, (BulkEditor)editor);
192
							errorMessage = "The name ";
193

  
194
							if (model != null) {
195
								Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
196
								while (iter.hasNext()) {
197
									Object next = iter.next();
198
									if (next instanceof LineAnnotation) {
199
										result = controller.getNameService().isDeletable(name, config);
200
										if (result.isOk()){
201
											((LineAnnotation)next).markAsDeleted(config);
202
										}
203

  
204
									}
205
								}
206
							}
207
						}
208
					} else if (object instanceof TaxonBase){
209

  
210
						// synonym
211
						if(object instanceof Synonym){
212
							Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
213
							SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
214
							errorMessage = "The synonym ";
215
							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);
216
                            int result_dialog= dialog.open();
217
                            if (result_dialog != Status.OK){
218
                                 return null;
219 216
                            }
220
							//operation = new DeleteTaxonBaseOperation(commandName,((BulkEditor) editor).getUndoContext() , synonym, config, (BulkEditor)editor);
221
							if (model != null) {
222
								Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
223
								while (iter.hasNext()) {
224
									Object next = iter.next();
225
									if (next instanceof LineAnnotation) {
226
										result = controller.getTaxonService().isDeletable(synonym, config);
227
										if (result.isOk()){
228
											((LineAnnotation)next).markAsDeleted(config);
229
										}
230

  
231
									}
232
								}
233
							}
234
						}
235

  
236
						else if(object instanceof Taxon ){
237
							Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
238
							if (((Taxon)object).getTaxonNodes().isEmpty()){
239

  
240
                                errorMessage = "The taxon ";
241
							} else{
242
							    MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
243
							    return null;
244
							}
245
							TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
246
							config.setDeleteInAllClassifications(true);
247
							DeleteConfiguratorDialog dialog;
248
						    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);
249
							int result_dialog= dialog.open();
250
                            if (result_dialog != Status.OK){
251
                                 return null;
252
                            }
253
                            //operation = new DeleteTaxonBaseOperation(commandName, ((BulkEditor) editor).getUndoContext(), taxon, config, (BulkEditor)editor);
254
                            if (model != null) {
255
								Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
256
								while (iter.hasNext()) {
257
									Object next = iter.next();
258
									if (next instanceof LineAnnotation) {
259
										result = controller.getTaxonService().isDeletable(taxon, config);
260
										if (result.isOk()){
261
											((LineAnnotation)next).markAsDeleted(config);
262
										}
263

  
264
									}
265
								}
266
							}
267
						}
268

  
269

  
270
//						ITaxonService service = controller.getTaxonService();
271
//						if (object != null){
272
//							if (object instanceof Taxon){
273
//							    TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
274
//								config.setDeleteInAllClassifications(true);
275
//								DeleteConfiguratorDialog dialog;
276
//								if (((Taxon)object).getTaxonNodes().isEmpty()){
277
//								    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);
278
//								    int result_dialog= dialog.open();
279
//	                                if (result_dialog != Status.OK){
280
//	                                    return null;
281
//	                                }
282
//	                                result = service.deleteTaxon(((TaxonBase) object).getUuid(), config, null);
283
//	                                errorMessage = "The taxon ";
284
//								} else{
285
//								    MessagingUtils.messageDialog("Delete not possible", getClass(), "The taxon can not be deleted in bulk editor. It is used in a classification.", null);
286
//								    return null;
287
//								}
288
//
289
//							}else{
290
//								SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
291
//								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);
292
//                                int result_dialog= dialog.open();
293
//                                if (result_dialog != Status.OK){
294
//                                     return null;
295
//                                }
296
//								result = service.deleteSynonym(((Synonym)object).getUuid(), config);
297
//								errorMessage = "The synonym ";
298
//							}
299
//						}
300
					} else if (object instanceof TeamOrPersonBase){
301

  
302
						  if (model != null) {
303
								Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
304
								while (iter.hasNext()) {
305
									Object next = iter.next();
306
									if (next instanceof LineAnnotation) {
307

  
308
										result = controller.getAgentService().isDeletable((TeamOrPersonBase) object, null);
309
										if (result.isOk()){
310
											((LineAnnotation)next).markAsDeleted(null);
311
										}
312

  
313
									}
314
								}
315
							}
316
						//IAgentService service = controller.getAgentService();
317
						//TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
318
					//result = service.delete(((TeamOrPersonBase)object).getUuid());
319
						errorMessage = "The team or person ";
320
					} else if (object instanceof Media){
321
						MediaDeletionConfigurator config = new MediaDeletionConfigurator();
322

  
323
						DeleteConfiguratorDialog dialog;
324
					    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);
325
						int result_dialog= dialog.open();
326
                        if (result_dialog != Status.OK){
327
                             return null;
328 217
                        }
329
                        IMediaService service = controller.getMediaService();
330
                        //TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)service.load(((TeamOrPersonBase) object).getUuid());
331

  
332
                        errorMessage = "The media ";
333

  
334
                        if (model != null) {
335
							Iterator iter = model.getAnnotationIterator(selection.getOffset(), selection.getLength(), true, true);
336
							while (iter.hasNext()) {
337
								Object next = iter.next();
338
								if (next instanceof LineAnnotation) {
339
									 result = service.isDeletable(((Media)object), config);
340
									if (result.isOk()){
341
										((LineAnnotation)next).markAsDeleted(config);
342
									}
343

  
344
								}
345
							}
346
						}
347 218
                    }
348 219

  
349
				} catch (Exception e){
350
					MessagingUtils.errorDialog("Exception occured. Delete not possible", getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true);
351
				}
352
				if (result.isError() || result.isAbort()){
353
					if (!result.getExceptions().isEmpty()) {
354
						List<String> messages = new ArrayList<String>();
355
						int i = result.getExceptions().size();
356
						for (Exception e:result.getExceptions()){
357
							messages.add(e.getMessage());
358
						}
359
						errorMessage += "could not be deleted.";
360
						//MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
361
						DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
362
					}else{
363
						MessagingUtils.messageDialog("Delete not possible", getClass(), "The object could not be deleted. An exception occured.", null);
364
					}
365
				}
366

  
367

  
368
				if (result.isOk() ){
369

  
370
					((BulkEditor) editor).removeAnnotatedLine(annotation);
371

  
372
					if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
373
					    List<String> messages = new ArrayList<String>();
220
			    }
221
		        if (result.isOk() ){
222

  
223
                    ((BulkEditor) editor).removeAnnotatedLine(annotation);
224

  
225
                    if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
226
                        List<String> messages = new ArrayList<String>();
374 227
                        int i = result.getExceptions().size();
375 228
                        for (Exception e:result.getExceptions()){
376 229
                            messages.add(e.getMessage());
377 230
                        }
378
					    errorMessage += "can be deleted but related object(s) could not be deleted. ";
379
					    //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
380
					    DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
381
					}
231
                        errorMessage += "can be deleted but related object(s) could not be deleted. ";
232
                        //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
233
                        DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
234
                    }
235

  
236
                }
382 237

  
383
				}
238
			    }
384 239

  
385 240
			}
386 241
		}

Also available in: Unified diff