package org.rundeck.plugin.vars;

import com.dtolabs.rundeck.core.data.DataContext;
import com.dtolabs.rundeck.core.data.MultiDataContext;
import com.dtolabs.rundeck.core.dispatcher.ContextView;
import com.dtolabs.rundeck.core.execution.workflow.steps.StepException;
import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription;
import com.dtolabs.rundeck.plugins.descriptions.PluginProperty;
import com.dtolabs.rundeck.plugins.step.PluginStepContext;
import com.dtolabs.rundeck.plugins.step.StepPlugin;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

@Plugin(name = UpvarWorkflowStep.PROVIDER_NAME, service = ServiceNameConstants.WorkflowStep)
@PluginDescription(title = "Up Var",
        description = "Up vars.")


public class UpvarWorkflowStep implements StepPlugin {
    public static final String PROVIDER_NAME = "upvar";


    @Override
    public void executeStep(
            final PluginStepContext context, final Map<String, Object> configuration
    ) throws StepException
    {
        MultiDataContext<ContextView, DataContext> sharedDataContext = context.getExecutionContext()
                .getSharedDataContext()
                .consolidate();
        ObjectMapper objectMapper = new ObjectMapper();
        Set<ContextView> keys = new TreeSet<>(sharedDataContext.getKeys());
        for (ContextView view : keys) {
            if(!view.isGlobal()){
                DataContext data = sharedDataContext.getData(view);
                if(null != data.get("export")){
                    context.getOutputContext().addOutput(ContextView.global(),"globals",data.get("export"));
                }

            }
        }

    }


}

@@ -664,90 +664,8 @@
 r;%0A%0A
-import java.io.IOException;%0Aimport java.io.StringWriter;%0Aimport java.util.HashMap;
 %0Aimp
@@ -859,12 +859,22 @@
  %22Up
- Var
+lift Variables
 %22,%0A 
@@ -901,13 +901,60 @@
  %22Up
- vars
+lift to global all the variables on the export group
 .%22)%0A
@@ -1406,64 +1406,8 @@
 ();%0A
-        ObjectMapper objectMapper = new ObjectMapper();%0A
     

remove unused code