/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2017 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.meta.schema.actions;

import javax.script.Bindings;
import javax.script.SimpleBindings;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;

import org.eclipse.persistence.oxm.annotations.XmlCDATA;

import com.axelor.meta.ActionHandler;
import com.axelor.rpc.ActionRequest;
import com.axelor.rpc.ActionResponse;
import com.axelor.script.GroovyScriptHelper;
import com.axelor.script.NashornScriptHelper;
import com.axelor.script.ScriptHelper;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class ActionScript extends Action {

	private static final String LANGUAGE_JS = "js";

	private static final String KEY_REQUEST = "$request";
	private static final String KEY_RESPONSE = "$response";

	@JsonIgnore
	@XmlElement(name = "script")
	private ActScript script;

	public ActScript getScript() {
		return script;
	}

	public void setScript(ActScript script) {
		this.script = script;
	}

	private ScriptHelper getScriptHelper(Bindings context) {
		return LANGUAGE_JS.equalsIgnoreCase(script.language)
				? new NashornScriptHelper(context)
				: new GroovyScriptHelper(context);
	}

	@Override
	public Object evaluate(ActionHandler handler) {
		final Bindings context = new SimpleBindings();
		final ActionRequest request = handler.getRequest();
		final ActionResponse response = new ActionResponse();
		context.put(KEY_REQUEST, request);
		context.put(KEY_RESPONSE, response);
		try {
			getScriptHelper(context).eval(script.code.trim());
		} finally {
		}
		return response;
	}

	@Override
	public Object wrap(ActionHandler handler) {
		return evaluate(handler);
	}

	@XmlType
	public static class ActScript {

		@XmlAttribute
		private String language;

		@XmlCDATA
		@XmlValue
		public String code;

		public String getLanguage() {
			return language;
		}

		public void setLanguage(String language) {
			this.language = language;
		}
	}
}

@@ -761,32 +761,69 @@
 cript.Bindings;%0A
+import javax.script.ScriptException;%0A
 import javax.scr
@@ -1823,23 +1823,24 @@
 indings 
-context
+bindings
 ) %7B%0A%09%09re
@@ -1920,23 +1920,24 @@
 tHelper(
-context
+bindings
 )%0A%09%09%09%09: 
@@ -1959,23 +1959,24 @@
 tHelper(
-context
+bindings
 );%0A%09%7D%0A%0A%09
@@ -2051,23 +2051,24 @@
 indings 
-context
+bindings
  = new S
@@ -2192,31 +2192,32 @@
 sponse();%0A%09%09
-context
+bindings
 .put(KEY_REQ
@@ -2234,23 +2234,24 @@
 est);%0A%09%09
-context
+bindings
 .put(KEY
@@ -2303,15 +2303,16 @@
 per(
-context
+bindings
 ).ev
@@ -2336,24 +2336,318 @@
 im()
-);%0A%09%09%7D finally %7B
+, bindings);%0A%09%09%7D catch (ScriptException e) %7B%0A%09%09%09if (%22%3Ceval%3E%22.equals(e.getFileName())) %7B%0A%09%09%09%09e = new ScriptException(e.getMessage().replace(%22%3Ceval%3E%22,%0A%09%09%09%09%09%09%22%3Cstrong%3E&lt;action-script name=%22 + getName() + %22&gt;%3C/strong%3E%22));%0A%09%09%09%7D%0A%09%09%09response.setException(e);%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09response.setException(e);
 %0A%09%09%7D

Improve error handling of action-script