/**
 * ****************************************************************************
 * (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0 which accompany this distribution.
 * <p/>
 * The Apache License is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * *****************************************************************************
 */
package io.cloudslang.lang.runtime.bindings;

import io.cloudslang.lang.entities.SystemProperty;
import io.cloudslang.lang.entities.bindings.Argument;
import io.cloudslang.lang.entities.utils.ExpressionUtils;
import io.cloudslang.lang.runtime.bindings.scripts.ScriptEvaluator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Bonczidai Levente
 * @since 8/17/2015
 */
@Component
public class ArgumentsBinding {

    @Autowired
    private ScriptEvaluator scriptEvaluator;

    public Map<String, Serializable> bindArguments(
            List<Argument> arguments,
            Map<String, ? extends Serializable> context,
            Set<SystemProperty> systemProperties) {
        Map<String, Serializable> resultContext = new HashMap<>();

        //we do not want to change original context map
        Map<String, Serializable> srcContext = new HashMap<>(context);

        for (Argument argument : arguments) {
            bindArgument(argument, srcContext, systemProperties, resultContext);
        }

        return resultContext;
    }

    private void bindArgument(
            Argument argument,
            Map<String, ? extends Serializable> srcContext,
            Set<SystemProperty> systemProperties,
            Map<String, Serializable> targetContext) {
        Serializable argumentValue;
        String argumentName = argument.getName();

        try {
            argumentValue = srcContext.get(argumentName);
            if (!argument.isOverridable()) {
                Serializable rawValue = argument.getValue();
                String expressionToEvaluate = ExpressionUtils.extractExpression(rawValue);
                if (expressionToEvaluate != null) {
                    //we do not want to change original context map
                    Map<String, Serializable> scriptContext = new HashMap<>(srcContext);
                    scriptContext.put(argumentName, argumentValue);
                    //so you can resolve previous arguments already bound
                    scriptContext.putAll(targetContext);
                    argumentValue = scriptEvaluator.evalExpr(expressionToEvaluate, scriptContext, systemProperties, argument.getFunctionDependencies());
                } else
                    argumentValue = rawValue;
                }
        } catch (Throwable t) {
            throw new RuntimeException("Error binding task argument: '" + argumentName + "', \n\tError is: " + t.getMessage(), t);
        }
        targetContext.put(argumentName, argumentValue);
    }

}

@@ -2905,16 +2905,18 @@
   %7D else
+ %7B
 %0A       
@@ -2964,32 +2964,46 @@
               %7D%0A
+            %7D%0A
         %7D catch 

fix curly brackets