package eu.etaxonomy.taxeditor.bulkeditor.handler;
+import java.util.Iterator;
+import java.util.Set;
import java.util.UUID;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
+import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.IWorkbenchPage;
import eu.etaxonomy.cdm.api.service.IReferenceService;
import eu.etaxonomy.cdm.api.service.ITaxonService;
import eu.etaxonomy.cdm.api.service.IUserService;
+import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
+import eu.etaxonomy.cdm.model.common.CdmBase;
import eu.etaxonomy.cdm.model.common.Group;
+import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
import eu.etaxonomy.cdm.model.common.User;
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
import eu.etaxonomy.cdm.model.taxon.Synonym;
import eu.etaxonomy.cdm.model.taxon.Taxon;
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
+import eu.etaxonomy.cdm.persistence.dao.hibernate.HibernateBeanInitializer;
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
import eu.etaxonomy.taxeditor.store.CdmStore;
+
/**
* @author n.hoffmann
* @created Mar 11, 2011
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
- if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_EXPERIMENTAL_FEATURES)){
- ISelection selection = HandlerUtil.getCurrentSelection(event);
+ ISelection selection = HandlerUtil.getCurrentSelection(event);
+
+ IEditorPart editor = HandlerUtil.getActiveEditor(event);
+
+ IEditorInput input = editor.getEditorInput();
+
+ if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
- IEditorPart editor = HandlerUtil.getActiveEditor(event);
- IEditorInput input = editor.getEditorInput();
+ IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
+ LineAnnotationModel model =
+ (LineAnnotationModel) provider.getAnnotationModel(input);
- if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
-
-
- IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
- LineAnnotationModel model =
- (LineAnnotationModel) provider.getAnnotationModel(input);
-
-
- IStructuredSelection structuredSelection = (IStructuredSelection) selection;
-
- IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
+
+ IStructuredSelection structuredSelection = (IStructuredSelection) selection;
+
+ IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
+
+
+ for(Object object : structuredSelection.toList()){
- for(Object object : structuredSelection.toList()){
-
- LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
- DeleteResult result = new DeleteResult();
- //result.setError();
- try {
- ICdmApplicationConfiguration controller;
- controller = (ICdmApplicationConfiguration) CdmStore.getCurrentApplicationConfiguration();
- //persistenceService.delete(object);
- if (object instanceof SpecimenOrObservationBase){
-
- IOccurrenceService service = controller.getOccurrenceService();
- service.load(((SpecimenOrObservationBase) object).getUuid());
- result = service.delete((SpecimenOrObservationBase)object);
- } else if (object instanceof Reference){
- IReferenceService service = controller.getReferenceService();
- service.load(((Reference) object).getUuid());
- result = service.delete((Reference)object);
- } else if (object instanceof Group){
- IGroupService service = controller.getGroupService();
- service.load(((Group) object).getUuid());
- result = service.delete((Group) object);
- }else if (object instanceof User){
- IUserService service = controller.getUserService();
- service.load(((User) object).getUuid());
- result = service.delete((User) object);
- } else if (object instanceof TaxonNameBase){
- INameService service = controller.getNameService();
- service.load(((TaxonNameBase) object).getUuid());
- result = service.delete((TaxonNameBase)object);
- } else if (object instanceof TaxonBase){
- ITaxonService service = controller.getTaxonService();
- service.load(((TaxonBase) object).getUuid());
- if (object instanceof Taxon){
- result = service.deleteTaxon((Taxon)object, null, null);
- }else{
- result = service.deleteSynonym((Synonym)object, null);
- }
- } else if (object instanceof TeamOrPersonBase){
- IAgentService service = controller.getAgentService();
- object = service.load(((TeamOrPersonBase) object).getUuid());
- result = service.delete((TeamOrPersonBase)object);
- }
- } catch (Exception e){
- MessagingUtils.messageDialog("Exception occured. Could not delete", getClass(), e.getMessage(), null);
- }
- if (result.isError() || !result.getExceptions().isEmpty()){
- if (!result.getExceptions().isEmpty()) {
- MessagingUtils.messageDialog("Could not delete", getClass(), result.getExceptions().get(0).getMessage(), null);
+ LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
+ DeleteResult result = new DeleteResult();
+ //result.setError();
+ try {
+ ICdmApplicationConfiguration controller;
+ controller = (ICdmApplicationConfiguration) CdmStore.getCurrentApplicationConfiguration();
+ //persistenceService.delete(object);
+ if (object instanceof SpecimenOrObservationBase){
+
+ IOccurrenceService service = controller.getOccurrenceService();
+ service.load(((SpecimenOrObservationBase) object).getUuid());
+ result = service.delete((SpecimenOrObservationBase)object);
+ } else if (object instanceof Reference){
+ IReferenceService service = controller.getReferenceService();
+ service.load(((Reference) object).getUuid());
+ result = service.delete((Reference)object);
+ } else if (object instanceof Group){
+ IGroupService service = controller.getGroupService();
+ service.load(((Group) object).getUuid());
+ result = service.delete((Group) object);
+ }else if (object instanceof User){
+ IUserService service = controller.getUserService();
+ service.load(((User) object).getUuid());
+ result = service.delete((User) object);
+ } else if (object instanceof TaxonNameBase){
+ INameService service = controller.getNameService();
+ TaxonNameBase name = service.load(((TaxonNameBase) object).getUuid());
+ NameDeletionConfigurator config = new NameDeletionConfigurator();
+
+ result = service.delete(name, config);
+ } else if (object instanceof TaxonBase){
+ ITaxonService service = controller.getTaxonService();
+ service.load(((TaxonBase) object).getUuid());
+ if (object instanceof Taxon){
+ result = service.deleteTaxon((Taxon)object, null, null);
}else{
- MessagingUtils.messageDialog("Could not delete", getClass(), "The object could not be deleted. An exception occured.", null);
-
+ result = service.deleteSynonym((Synonym)object, null);
}
+ } else if (object instanceof TeamOrPersonBase){
+ IAgentService service = controller.getAgentService();
+ object = service.load(((TeamOrPersonBase) object).getUuid());
+ result = service.delete((TeamOrPersonBase)object);
}
- if (result.isOk() && result.getExceptions().isEmpty()){
- ((BulkEditor) editor).removeAnnotatedLine(annotation);
-
+ } catch (Exception e){
+ MessagingUtils.messageDialog("Exception occured. Could not delete", getClass(), e.getMessage(), null);
+ }
+ if (result.isError() || result.isAbort()){
+ if (!result.getExceptions().isEmpty()) {
+ MessagingUtils.messageDialog("Could not delete", getClass(), result.getExceptions().toString(), null);
+ }else{
+ MessagingUtils.messageDialog("Could not delete", getClass(), "The object could not be deleted. An exception occured.", null);
+
+ }
+ }
+ if (result.isOk() ){
+ ((BulkEditor) editor).removeAnnotatedLine(annotation);
+ if(result.getUpdatedObjects().size() != 0 && !result.getExceptions().isEmpty()){
+ MessagingUtils.informationDialog("Delete successfull", "The object is deleted but there are updated objects: " + result.toString());
}
- }
- }
- }else{
- MessagingUtils.warningDialog("Feature not enabled", getClass(), "Deletion is currently an experimental feature." +
- "Enable it via Preferences at your own risk.");
+
+ }
+
+ }
}
-
+
return null;
}