/*
 * Smart GWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * Smart GWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  Smart GWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software 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
 * Lesser General Public License for more details.
 */
/* sgwtgen */
 
package com.smartgwt.client.util.workflow;


import com.smartgwt.client.event.*;
import com.smartgwt.client.core.*;
import com.smartgwt.client.types.*;
import com.smartgwt.client.data.*;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.events.*;
import com.smartgwt.client.browser.window.*;
import com.smartgwt.client.rpc.*;
import com.smartgwt.client.ai.*;
import com.smartgwt.client.callbacks.*;
import com.smartgwt.client.tools.*;
import com.smartgwt.client.bean.*;
import com.smartgwt.client.widgets.*;
import com.smartgwt.client.widgets.ai.*;
import com.smartgwt.client.widgets.events.*;
import com.smartgwt.client.widgets.form.*;
import com.smartgwt.client.widgets.form.validator.*;
import com.smartgwt.client.widgets.form.fields.*;
import com.smartgwt.client.widgets.tile.*;
import com.smartgwt.client.widgets.tile.events.*;
import com.smartgwt.client.widgets.grid.*;
import com.smartgwt.client.widgets.grid.events.*;
import com.smartgwt.client.widgets.chart.*;
import com.smartgwt.client.widgets.layout.*;
import com.smartgwt.client.widgets.layout.events.*;
import com.smartgwt.client.widgets.menu.*;
import com.smartgwt.client.widgets.tour.*;
import com.smartgwt.client.widgets.notify.*;
import com.smartgwt.client.widgets.rte.*;
import com.smartgwt.client.widgets.rte.events.*;
import com.smartgwt.client.widgets.ace.*;
import com.smartgwt.client.widgets.ace.events.*;
import com.smartgwt.client.widgets.tab.*;
import com.smartgwt.client.widgets.toolbar.*;
import com.smartgwt.client.widgets.tree.*;
import com.smartgwt.client.widgets.tree.events.*;
import com.smartgwt.client.widgets.tableview.*;
import com.smartgwt.client.widgets.viewer.*;
import com.smartgwt.client.widgets.calendar.*;
import com.smartgwt.client.widgets.calendar.events.*;
import com.smartgwt.client.widgets.cube.*;
import com.smartgwt.client.widgets.notify.*;
import com.smartgwt.client.widgets.drawing.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gwt.event.shared.*;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.core.client.JavaScriptObject;

import com.smartgwt.client.util.*;
import com.smartgwt.client.util.events.*;
import com.smartgwt.client.util.workflow.*;
import com.smartgwt.client.util.workflow.Process; // required to override java.lang.Process
import com.smartgwt.client.util.tour.*;


/**
 * Task that executes arbitrary code, either synchronous or asynchronous.  Override the {@link
 * com.smartgwt.client.util.workflow.ScriptTask#execute execute()} method to provide custom logic.
 * @see com.smartgwt.client.docs.ServerProcess Server-Side Process Execution
 */
@BeanFactory.FrameworkClass
@BeanFactory.ScClassName("ScriptTask")
public class ScriptTask extends Task {

    public static ScriptTask getOrCreateRef(JavaScriptObject jsObj) {
        if(jsObj == null) return null;
        BaseClass obj = BaseClass.getRef(jsObj);
        if(obj != null) {
            return (ScriptTask) obj;
        } else {
            return new ScriptTask(jsObj);
        }
    }
        


    public ScriptTask(){
        scClassName = "ScriptTask";
    }

    public ScriptTask(JavaScriptObject jsObj){
        scClassName = "ScriptTask";
        setJavaScriptObject(jsObj);
    }


    public ScriptTask(String ID) {
        setID(ID);
                scClassName = "ScriptTask";
    }


    public ScriptTask(String ID, String nextElement) {
        setID(ID);
		setNextElement(nextElement);
                scClassName = "ScriptTask";
    }

    public native JavaScriptObject create()/*-{
        var config = this.@com.smartgwt.client.core.BaseClass::getConfig()();
        var scClassName = this.@com.smartgwt.client.core.BaseClass::scClassName;
        return $wnd.isc[scClassName].create(config);
    }-*/;


    // ********************* Properties / Attributes ***********************

    /**
     * Comma-separated list of DataSource IDs to pre-load into the server-side scripting context before this task executes. 
     * The listed DataSources become available as DataSource instances and same-named local variables. <P> This is the
     * task-level equivalent of {@link com.smartgwt.client.docs.serverds.OperationBinding#dataSources
     * OperationBinding.dataSources} and {@link com.smartgwt.client.util.workflow.Process#getDataSources Process.dataSources};
     * it scopes the pre-load to a single ScriptTask rather than the entire operationBinding or process. <P> DataSources
     * already listed on the operationBinding or process do not need to be repeated here.
     *
     * @param dataSources New dataSources value. Default value is null
     * @return {@link com.smartgwt.client.util.workflow.ScriptTask ScriptTask} instance, for chaining setter calls
     * @throws IllegalStateException this property cannot be changed after the underlying component has been created
     * @see com.smartgwt.client.docs.serverds.OperationBinding#dataSources
     * @see com.smartgwt.client.util.workflow.Process#setDataSources
     * @see com.smartgwt.client.docs.ServerProcess Server-Side Process Execution
     */
    public ScriptTask setDataSources(String dataSources)  throws IllegalStateException {
        return (ScriptTask)setAttribute("dataSources", dataSources, false);
    }

    /**
     * Comma-separated list of DataSource IDs to pre-load into the server-side scripting context before this task executes. 
     * The listed DataSources become available as DataSource instances and same-named local variables. <P> This is the
     * task-level equivalent of {@link com.smartgwt.client.docs.serverds.OperationBinding#dataSources
     * OperationBinding.dataSources} and {@link com.smartgwt.client.util.workflow.Process#getDataSources Process.dataSources};
     * it scopes the pre-load to a single ScriptTask rather than the entire operationBinding or process. <P> DataSources
     * already listed on the operationBinding or process do not need to be repeated here.
     *
     * @return Current dataSources value. Default value is null
     * @see com.smartgwt.client.docs.serverds.OperationBinding#dataSources
     * @see com.smartgwt.client.util.workflow.Process#getDataSources
     * @see com.smartgwt.client.docs.ServerProcess Server-Side Process Execution
     */
    public String getDataSources()  {
        return getAttributeAsString("dataSources");
    }
    

    /**
     * Whether the script task is asynchronous.  A synchronous task is expected to return data directly from execute() and is
     * considered complete once the execute() method exits. <P> An asynchronous task is expected to start processing in
     * execute(), and will not be considered complete {@link com.smartgwt.client.util.workflow.ScriptTask#setOutput
     * setOutput()} is called.
     *
     * @param isAsync New isAsync value. Default value is false
     * @return {@link com.smartgwt.client.util.workflow.ScriptTask ScriptTask} instance, for chaining setter calls
     * @throws IllegalStateException this property cannot be changed after the underlying component has been created
     */
    public ScriptTask setIsAsync(Boolean isAsync)  throws IllegalStateException {
        return (ScriptTask)setAttribute("isAsync", isAsync, false);
    }

    /**
     * Whether the script task is asynchronous.  A synchronous task is expected to return data directly from execute() and is
     * considered complete once the execute() method exits. <P> An asynchronous task is expected to start processing in
     * execute(), and will not be considered complete {@link com.smartgwt.client.util.workflow.ScriptTask#setOutput
     * setOutput()} is called.
     *
     * @return Current isAsync value. Default value is false
     */
    public Boolean getIsAsync()  {
        Boolean result = getAttributeAsBoolean("isAsync");
        return result == null ? false : result;
    }
    

    /**
     * Scripting language for the {@link com.smartgwt.client.util.workflow.ScriptTask#execute execute()} body.  When running
     * inside an {@link com.smartgwt.client.docs.serverds.OperationBinding#process OperationBinding.process}, this controls
     * which script engine evaluates the code: <ul> <li><code>null</code> or <code>"graal.js"</code>     (default) &mdash;
     * execute in the current GraalJS     server context, with full access to Smart GWT     APIs and {@link
     * com.smartgwt.client.util.workflow.Process#getState Process.state}</li> <li>Any other value (e.g. <code>"groovy"</code>) 
     * &mdash; delegate to the JSR-223     <code>ScriptEngine</code> registered under that     name.  The engine receives these
     * bindings:     <code>dsRequest</code>, <code>criteria</code>,     <code>values</code>, <code>oldValues</code>,    
     * <code>input</code>, <code>inputRecord</code>,     <code>processState</code> (the     {@link
     * com.smartgwt.client.util.workflow.Process#getState Process.state} map),     <code>session</code>, <code>config</code>,  
     *   <code>log</code>, and <code>ds</code></li> </ul> <P> This attribute has no effect in client-side workflows.
     *
     * @param language New language value. Default value is null
     * @return {@link com.smartgwt.client.util.workflow.ScriptTask ScriptTask} instance, for chaining setter calls
     * @throws IllegalStateException this property cannot be changed after the underlying component has been created
     * @see com.smartgwt.client.docs.serverds.OperationBinding#script
     * @see com.smartgwt.client.docs.ServerProcess Server-Side Process Execution
     */
    public ScriptTask setLanguage(String language)  throws IllegalStateException {
        return (ScriptTask)setAttribute("language", language, false);
    }

    /**
     * Scripting language for the {@link com.smartgwt.client.util.workflow.ScriptTask#execute execute()} body.  When running
     * inside an {@link com.smartgwt.client.docs.serverds.OperationBinding#process OperationBinding.process}, this controls
     * which script engine evaluates the code: <ul> <li><code>null</code> or <code>"graal.js"</code>     (default) &mdash;
     * execute in the current GraalJS     server context, with full access to Smart GWT     APIs and {@link
     * com.smartgwt.client.util.workflow.Process#getState Process.state}</li> <li>Any other value (e.g. <code>"groovy"</code>) 
     * &mdash; delegate to the JSR-223     <code>ScriptEngine</code> registered under that     name.  The engine receives these
     * bindings:     <code>dsRequest</code>, <code>criteria</code>,     <code>values</code>, <code>oldValues</code>,    
     * <code>input</code>, <code>inputRecord</code>,     <code>processState</code> (the     {@link
     * com.smartgwt.client.util.workflow.Process#getState Process.state} map),     <code>session</code>, <code>config</code>,  
     *   <code>log</code>, and <code>ds</code></li> </ul> <P> This attribute has no effect in client-side workflows.
     *
     * @return Current language value. Default value is null
     * @see com.smartgwt.client.docs.serverds.OperationBinding#script
     * @see com.smartgwt.client.docs.ServerProcess Server-Side Process Execution
     */
    public String getLanguage()  {
        return getAttributeAsString("language");
    }
    

    /**
     * Does this processElement pass through output from the last executed task (i.e. transient state)? <P> See {@link
     * com.smartgwt.client.docs.TaskInputExpression taskInputExpressions} for details on the transient state outputs. <p> Note
     * that this property does not affect the task at all but is an indicator to the user and to the workflow editor of the
     * behavior of the task as coded (See {@link com.smartgwt.client.util.workflow.Process#passThruTaskOutput
     * Process.passThruTaskOutput()}).
     *
     * @param passThruOutput New passThruOutput value. Default value is false
     * @return {@link com.smartgwt.client.util.workflow.ScriptTask ScriptTask} instance, for chaining setter calls
     * @throws IllegalStateException this property cannot be changed after the underlying component has been created
     */
    public ScriptTask setPassThruOutput(Boolean passThruOutput)  throws IllegalStateException {
        return (ScriptTask)setAttribute("passThruOutput", passThruOutput, false);
    }

    /**
     * Does this processElement pass through output from the last executed task (i.e. transient state)? <P> See {@link
     * com.smartgwt.client.docs.TaskInputExpression taskInputExpressions} for details on the transient state outputs. <p> Note
     * that this property does not affect the task at all but is an indicator to the user and to the workflow editor of the
     * behavior of the task as coded (See {@link com.smartgwt.client.util.workflow.Process#passThruTaskOutput
     * Process.passThruTaskOutput()}).
     *
     * @return Current passThruOutput value. Default value is false
     */
    public Boolean getPassThruOutput()  {
        Boolean result = getAttributeAsBoolean("passThruOutput");
        return result == null ? false : result;
    }
    

    // ********************* Methods ***********************
	/**
     * Execute the task. The return value, if not <code>undefined</code>, will be written as the output. <p> For an
     * asynchronous task, the return value is ignored and the task should call {@link
     * com.smartgwt.client.util.workflow.ScriptTask#setOutput setOutput()} to continue the process with the next task.
     * @param input the task input
     * @param inputRecord the task input record if <code>inputs</code> is specified. See {@link com.smartgwt.client.docs.TaskIO}
     *
     * @return the task output.  For multiple field output, call  {@link com.smartgwt.client.util.workflow.ScriptTask#setOutput
     * setOutput()} instead, and return null
     */
    public native Object execute(Object input, Record inputRecord) /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "execute", "Object,Record");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        var ret = self.execute(input, inputRecord.@com.smartgwt.client.core.DataClass::getJsObj()());
        return $wnd.SmartGWT.convertToJavaType(ret);
    }-*/;

	/**
     * Sets the task output available to the next task via {@link com.smartgwt.client.util.workflow.Process#getLastTaskOutput
     * Process.getLastTaskOutput()} or more commonly, with a {@link com.smartgwt.client.docs.TaskInputExpression} property. If
     * {@link com.smartgwt.client.util.workflow.Task#getOutputFieldList outputFieldList} is specified, the output will also be
     * written to the specified fields in the process state (See {@link com.smartgwt.client.docs.TaskIO}). <p> To have the
     * output written as-is to the process state, see {@link com.smartgwt.client.util.workflow.ProcessElement#getBindOutput
     * bindOutput}. <P> NOTE: for an {@link com.smartgwt.client.util.workflow.ScriptTask#getIsAsync asynchronous task}, calling
     * <code>setOutput()</code> indicates the task is complete.
     * @see com.smartgwt.client.docs.TaskIO Task Input / Output
     */
    public native void setOutput() /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "setOutput", "");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        self.setOutput();
    }-*/;

	/**
     * Sets the task output available to the next task via {@link com.smartgwt.client.util.workflow.Process#getLastTaskOutput
     * Process.getLastTaskOutput()} or more commonly, with a {@link com.smartgwt.client.docs.TaskInputExpression} property. If
     * {@link com.smartgwt.client.util.workflow.Task#getOutputFieldList outputFieldList} is specified, the output will also be
     * written to the specified fields in the process state (See {@link com.smartgwt.client.docs.TaskIO}). <p> To have the
     * output written as-is to the process state, see {@link com.smartgwt.client.util.workflow.ProcessElement#getBindOutput
     * bindOutput}. <P> NOTE: for an {@link com.smartgwt.client.util.workflow.ScriptTask#getIsAsync asynchronous task}, calling
     * <code>setOutput()</code> indicates the task is complete.
     * @param output output record to provide to the next task
     * @see com.smartgwt.client.docs.TaskIO Task Input / Output
     */
    public native void setOutput(Object output) /*-{
        if (this.@com.smartgwt.client.core.BaseClass::isConfigOnly()()) {
            @com.smartgwt.client.util.ConfigUtil::warnOfPostConfigInstantiation(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)(this.@java.lang.Object::getClass()(), "setOutput", "Object");
        }
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        self.setOutput(output);
    }-*/;
	

    // ********************* Static Methods ***********************

    // ***********************************************************



    public ScriptTask(String ID, ProcessElement nextElement) {
        this(ID, nextElement.getID());
    }

    /**
     * Set the task output as specified by {@link com.smartgwt.client.util.workflow.Task#getOutputField outputField}. <P> NOTE:
     * for an  asychronous task, calling <code>setOutputData()</code> indicates the task is complete.  For a task with {@link
     * com.smartgwt.client.util.workflow.Task#getOutputFieldList multiple outputs}, call {@link
     * com.smartgwt.client.util.workflow.ScriptTask#setOutputRecord ScriptTask.setOutputRecord} instead.
     * @param taskOutput task output
     */
    public native void setOutputData(Object taskOutput) /*-{
        var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
        self.setOutputData($wnd.SmartGWT.convertToPrimitiveType(taskOutput));
    }-*/;

    protected native void onInit_ScriptTask() /*-{
        var self = this.@com.smartgwt.client.core.BaseClass::getJsObj()();
        self.__execute = self.execute;
        self.execute = function(input, inputRecord) {
            var jObj = this.__ref;
            var inputRecordJ = null;
            if (inputRecord != null) {
                inputRecordJ = @com.smartgwt.client.data.Record::new(Lcom/google/gwt/core/client/JavaScriptObject;)(inputRecord);
            }
            var inputJ = $wnd.SmartGWT.convertToJavaType(input);
            var resJ = jObj.@com.smartgwt.client.util.workflow.ScriptTask::execute(Ljava/lang/Object;Lcom/smartgwt/client/data/Record;)(inputJ, inputRecordJ);
            if (resJ == null) return;
            var res = $wnd.SmartGWT.convertToPrimitiveType(resJ);
            if (typeof res == 'object') {
                res = resJ.@com.smartgwt.client.core.JsObject::getJsObj()();
            }
            return res;
        };
    }-*/;

    protected void onInit() {
        onInit_ScriptTask();
    };

}
