/*******************************************************************************
 * (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.
 *
 * The Apache License is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *******************************************************************************/
package io.cloudslang.lang.runtime.bindings;

import io.cloudslang.lang.entities.AsyncLoopStatement;
import io.cloudslang.lang.runtime.env.Context;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;

/**
 * Date: 3/25/2015
 *
 * @author Bonczidai Levente
 */
@Component
public class AsyncLoopBinding {

    @Autowired
    private ScriptEvaluator scriptEvaluator;

    public List<Serializable> bindAsyncLoopList(AsyncLoopStatement asyncLoopStatement, Context flowContext, String nodeName) {
        Validate.notNull(asyncLoopStatement, "async task statement cannot be null");
        Validate.notNull(flowContext, "flow context cannot be null");
        Validate.notNull(nodeName, "node name cannot be null");

        try {
            @SuppressWarnings("unchecked") List<Serializable> evalResult = (List<Serializable>) scriptEvaluator.evalExpr(
                    asyncLoopStatement.getExpression(),
                    flowContext.getImmutableViewOfVariables());
            if (CollectionUtils.isEmpty(evalResult)) {
                throw new RuntimeException("Expression cannot be empty");
            }
            return evalResult;
        } catch (Throwable t) {
            throw new RuntimeException("Error evaluating async loop expression in task '" + nodeName + "', error is: \n" + t.getMessage(), t);
        }
    }
}

@@ -1054,16 +1054,226 @@
 uator;%0A%0A
+    public static String generateAsyncLoopExpressionMessage(String nodeName, String message) %7B%0A        return %22Error evaluating async loop expression in task '%22 + nodeName + %22', error is: %5Cn%22 + message;%0A    %7D%0A%0A
     publ
@@ -1623,75 +1623,64 @@
     
-try %7B%0A            @SuppressWarnings(%22unchecked%22) List%3CSerializable%3E
+List%3CSerializable%3E evalResult;%0A        try %7B%0A           
  eva
@@ -1868,202 +1868,194 @@
     
-    if (CollectionUtils.isEmpty(evalResult)) %7B%0A                throw new RuntimeException(%22Expression cannot be empty%22);%0A            %7D%0A            return evalResult;%0A        %7D catch (Throwable t
+%7D catch (Throwable t) %7B%0A            throw new RuntimeException(generateAsyncLoopExpressionMessage(nodeName, t.getMessage()), t);%0A        %7D%0A        if (CollectionUtils.isEmpty(evalResult)
 ) %7B%0A
@@ -2097,38 +2097,26 @@
 ion(
-%22Error evaluating a
+generateA
 sync
- loop e
+LoopE
 xpre
@@ -2124,82 +2124,87 @@
 sion
- in task '%22 + nodeName + %22', error is: %5Cn%22 + t.getMessage(), t);%0A        %7D
+Message(nodeName, %22expression is empty%22));%0A        %7D%0A        return evalResult;
 %0A   

throw exception when async loop expression is empty