fixing #5324 and #5325
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / handler / MoveDescriptionElementsHandler.java
index 9a801a4d412797995c6899f6ae3f8c6045152438..459cd29bba13a3a685e091e2a81ab968f240ca69 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
@@ -25,22 +25,18 @@ import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.handlers.HandlerUtil;
 
 import eu.etaxonomy.cdm.api.service.IDescriptionService;
-import eu.etaxonomy.cdm.api.service.ITaxonService;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
-import eu.etaxonomy.cdm.model.common.Annotation;
-import eu.etaxonomy.cdm.model.common.AnnotationType;
 import eu.etaxonomy.cdm.model.common.CdmBase;
-import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
+import eu.etaxonomy.cdm.model.taxon.Classification;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
 import eu.etaxonomy.taxeditor.editor.Page;
 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
-import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewPart;
 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
@@ -48,7 +44,6 @@ import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 import eu.etaxonomy.taxeditor.store.CdmStore;
-import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonBaseSelectionDialog;
 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
 
 
@@ -60,96 +55,97 @@ import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
 public class MoveDescriptionElementsHandler extends AbstractHandler implements IPostOperationEnabled{
        private UUID newAcceptedTaxonNodeUuid;
        private TaxonNameEditor editor;
-       /* (non-Javadoc)
-        * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
-        */
+
        @Override
        public Object execute(ExecutionEvent event) throws ExecutionException {
-               
+
 //             ConversationHolder conversation = CdmStore.createConversation();
                editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
                ISelection selection = HandlerUtil.getCurrentSelection(event);
-               
+               Taxon actualTaxon= null;
                if(selection instanceof IStructuredSelection){
 
                        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
-                       
+
                        List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
-                       
+
                        for(Object element : structuredSelection.toArray()){
                                if(element instanceof DescriptionElementBase){
                                        UUID uuid = ((DescriptionElementBase) element).getUuid();
-                                       
+
                                        elements.add(CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null));
                                } else if(element instanceof FeatureNodeContainer){
                                        for(DescriptionElementBase de : ((FeatureNodeContainer)element).getDescriptionElements()){
                                                elements.add(
-                                                               (DescriptionElementBase)CdmStore.getService(IDescriptionService.class).loadDescriptionElement(de.getUuid(), null)
+                                                               CdmStore.getService(IDescriptionService.class).loadDescriptionElement(de.getUuid(), null)
                                                        );
                                        }
                                }
                        }
-                       
+
                        if(elements.size() == 0){
                                return null;
                        }
                        DescriptionBase description = elements.get(0).getInDescription();
                        List<UUID> excludeTaxa = new ArrayList<UUID>();
-                       if (description.isInstanceOf(TaxonDescription.class)){
+                       if (description!=null && description.isInstanceOf(TaxonDescription.class)){
                                TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
-                               Taxon actualTaxon = taxonDescription.getTaxon();
+                               actualTaxon = taxonDescription.getTaxon();
                                excludeTaxa.add(actualTaxon.getUuid());
                        }
-                       
-                       
+                       Classification classification = null;
+                       TaxonNode actualNode = null;
+                       if (actualTaxon != null){
+                           if (!actualTaxon.getTaxonNodes().isEmpty() && actualTaxon.getTaxonNodes().size() ==1){
+                               actualNode = actualTaxon.getTaxonNodes().iterator().next();
+                               classification = actualNode.getClassification();
+
+                           }
+                       }
                        TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
                                        editor.getConversationHolder(),
                                        "Choose the accepted taxon",
                                        excludeTaxa,
-                                       null,
-                                       null);
-                       Taxon targetTaxon = newAcceptedTaxonNode.getTaxon();
-                       
-                       if(targetTaxon == null){
-                               // canceled
-                               return null;
-                       }
-                       newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();                      
-                       TaxonDescription targetDescription = TaxonDescription.NewInstance(targetTaxon);
-                       String moveMessage = String.format("Elements moved from %s", EditorUtil.getActiveMultiPageTaxonEditor().getTaxon());
-                       targetDescription.setTitleCache(moveMessage, true);
-                       Annotation annotation = Annotation.NewInstance(moveMessage, Language.getDefaultLanguage());
-                       annotation.setAnnotationType(AnnotationType.TECHNICAL());
-                       targetDescription.addAnnotation(annotation);
-                       
-                       try {
-                               AbstractPostOperation operation = new MoveDescriptionElementsOperation(
-                                               event.getCommand().getName(), EditorUtil.getUndoContext(), 
-                                               targetDescription, elements, false, this);
-                               
-                               EditorUtil.executeOperation(operation);
-                               
-                               //CdmStore.getService(ITaxonService.class).saveOrUpdate(targetTaxon);
-                               
-                       } catch (NotDefinedException e) {
-                               MessagingUtils.error(getClass(), e);
+                                       null, classification
+                                       );
+                       if (newAcceptedTaxonNode != null){
+                       Taxon targetTaxon = newAcceptedTaxonNode.getTaxon();
+
+                       if(targetTaxon == null){
+                               // canceled
+                               return null;
+                       }
+                       newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
+
+                       String moveMessage = String.format("Elements moved from %s", EditorUtil.getActiveMultiPageTaxonEditor().getTaxon());
+
+                       try {
+                               AbstractPostOperation operation = new MoveDescriptionElementsOperation(
+                                               event.getCommand().getName(), EditorUtil.getUndoContext(),
+                                               targetTaxon.getUuid(), moveMessage, elements, false, this);
+
+                               AbstractUtility.executeOperation(operation);
+
+                               //CdmStore.getService(ITaxonService.class).saveOrUpdate(targetTaxon);
+
+                       } catch (NotDefinedException e) {
+                               MessagingUtils.error(getClass(), e);
+                       }
                        }
                }
-               
+
                return null;
        }
 
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
-        */
        /** {@inheritDoc} */
        @Override
     public boolean postOperation(CdmBase objectAffectedByOperation) {
-               
+
                editor.getConversationHolder().bind();
                editor.getConversationHolder().commit(true);
                Display.getDefault().asyncExec(new Runnable(){
-                       
+
+            @Override
             public void run() {
                                //AbstractUtility.close(editor.getMultiPageTaxonEditor());
 
@@ -175,8 +171,7 @@ public class MoveDescriptionElementsHandler extends AbstractHandler implements I
 
        @Override
        public boolean onComplete() {
-               // TODO Auto-generated method stub
                return false;
        }
-       
+
 }