- added "Move Factual Data" option to navigator context menu
authorPatric Plitzner <p.plitzner@bgbm.org>
Mon, 26 Jan 2015 14:23:45 +0000 (14:23 +0000)
committerPatric Plitzner <p.plitzner@bgbm.org>
Mon, 26 Jan 2015 14:23:45 +0000 (14:23 +0000)
.gitattributes
eu.etaxonomy.taxeditor.navigation/plugin.xml
eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/navigator/handler/MoveFactualDataHandler.java [new file with mode: 0644]

index ea07532d9916e3fab2f6adb3441656f771ace7e3..95a3ad7ce836e1860c308e63e7724a6f912f3a98 100644 (file)
@@ -981,6 +981,7 @@ eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigatio
 eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/navigator/handler/CopyHandler.java -text
 eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/navigator/handler/DeleteHandler.java -text
 eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/navigator/handler/EditHandler.java -text
+eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/navigator/handler/MoveFactualDataHandler.java -text
 eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/navigator/handler/MoveTaxonHandler.java -text
 eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/navigator/handler/NewClassificationHandler.java -text
 eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/navigator/handler/NewTaxonNodeHandler.java -text
index 8b061b761c113e8558ee20d8cccc25c126f281d7..91eb7bbe25f06bb93606148ca65522afcc66936b 100644 (file)
                   </reference>
             </visibleWhen>
          </command>
+         <command
+               commandId="eu.etaxonomy.taxeditor.navigation.moveFactualData"
+               label="Move factual data to another taxon"
+               style="push">
+         </command>
          <separator
                name="taxeditor-navigation.separator2"
                visible="true">
             id="eu.etaxonomy.taxeditor.navigation.key.polytomous.refreshKeyNodes"
             name="%command.name.9">
       </command>
+      <command
+            defaultHandler="eu.etaxonomy.taxeditor.navigation.navigator.handler.MoveFactualDataHandler"
+            id="eu.etaxonomy.taxeditor.navigation.moveFactualData"
+            name="Move Factual Data">
+      </command>
    </extension>
    <extension
          point="org.eclipse.ui.handlers">
diff --git a/eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/navigator/handler/MoveFactualDataHandler.java b/eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/navigator/handler/MoveFactualDataHandler.java
new file mode 100644 (file)
index 0000000..ce5b2bf
--- /dev/null
@@ -0,0 +1,134 @@
+package eu.etaxonomy.taxeditor.navigation.navigator.handler;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+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.TreeSelection;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+import eu.etaxonomy.cdm.api.service.IDescriptionService;
+import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
+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.Language;
+import eu.etaxonomy.cdm.model.description.TaxonDescription;
+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.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
+import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
+
+public class MoveFactualDataHandler extends AbstractHandler {
+
+    @SuppressWarnings("unused")
+    private static final Logger logger = Logger.getLogger(MoveFactualDataHandler.class);
+
+    @Override
+    public Object execute(ExecutionEvent event) throws ExecutionException {
+        ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
+        IWorkbenchPart activePart = AbstractUtility.getActivePart();
+        TaxonNavigator navigator = null;
+        if(activePart instanceof TaxonNavigator){
+            navigator = (TaxonNavigator)activePart;
+        }
+        if(navigator!=null && currentSelection instanceof TreeSelection){
+            Object object = ((TreeSelection) currentSelection).getFirstElement();
+            if(object instanceof TaxonNode){
+                TaxonNode taxonNode = HibernateProxyHelper.deproxy(object, TaxonNode.class);
+                final TaxonNode sourceTaxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid());
+                if(NavigationUtil.isDirty(sourceTaxonNode)){
+                    MessageDialog.openWarning(HandlerUtil.getActiveShell(event), "Unsaved Source Taxon", "There are unsaved " +
+                            "changes in the source taxon. Please save first.");
+                    return null;
+                }
+                //reload to avoid session conflicts
+                Taxon taxon = HibernateProxyHelper.deproxy(CdmStore.getService(ITaxonService.class).load(sourceTaxonNode.getTaxon().getUuid()), Taxon.class);
+                if(taxon!=null){
+                 // Choose the target taxon
+                    List<UUID> excludeTaxa = new ArrayList<UUID>();
+                    excludeTaxa.add(taxon.getUuid());
+                    TaxonNode dialogTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
+                            navigator.getConversationHolder(),
+                            "Choose the accepted taxon",
+                            excludeTaxa,
+                            null,
+                            null);
+                    if (dialogTaxonNode == null) {
+                        return null;
+                    }
+                    //reload to avoid session conflicts
+                    final TaxonNode targetTaxonNode = CdmStore.getService(ITaxonNodeService.class).load(dialogTaxonNode.getUuid());
+                    if(NavigationUtil.isDirty(targetTaxonNode)){
+                        MessageDialog.openWarning(HandlerUtil.getActiveShell(event), "Unsaved Target Taxon", "There are unsaved " +
+                                "changes in the target taxon. Please save first.");
+                        return null;
+                    }
+                    for(TaxonDescription description : taxon.getDescriptions()){
+                        //reload to avoid session conflicts
+                        description = HibernateProxyHelper.deproxy(CdmStore.getService(IDescriptionService.class).load(description.getUuid()), TaxonDescription.class);
+
+                        String moveMessage = String.format("Description moved from %s", taxon);
+                        if(description.isProtectedTitleCache()){
+                            String separator = "";
+                            if(!StringUtils.isBlank(description.getTitleCache())){
+                                separator = " - ";
+                            }
+                            description.setTitleCache(description.getTitleCache() + separator + moveMessage, true);
+                        }
+                        Annotation annotation = Annotation.NewInstance(moveMessage, Language.getDefaultLanguage());
+                        annotation.setAnnotationType(AnnotationType.TECHNICAL());
+                        description.addAnnotation(annotation);
+                        targetTaxonNode.getTaxon().addDescription(description);
+                        CdmStore.getService(IDescriptionService.class).saveOrUpdate(description);
+                        navigator.getConversationHolder().bind();
+                        navigator.getConversationHolder().commit();
+
+                        Display.getDefault().asyncExec(new Runnable(){
+
+                            @Override
+                            public void run() {
+                                try {
+                                    //close and re-open to refresh factual data view
+                                    MultiPageTaxonEditor sourceEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(sourceTaxonNode.getUuid());
+                                    MultiPageTaxonEditor targetEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(targetTaxonNode.getUuid());
+                                    if(targetEditor != null){
+                                        AbstractUtility.close(sourceEditor);
+                                        AbstractUtility.close(targetEditor);
+                                    }
+                                    EditorUtil.openTaxonNode(sourceTaxonNode.getUuid());
+                                    EditorUtil.openTaxonNode(targetTaxonNode.getUuid());
+                                } catch (PartInitException e) {
+                                    MessagingUtils.error(this.getClass(), e);
+                                    throw new RuntimeException(e);
+                                } catch (Exception e) {
+                                    MessagingUtils.warningDialog("Could not create Taxon", this, e.getMessage());
+                                }
+                            }
+
+                        });
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+}