Scripting context reference

When a Semarchy xDI process interprets a script, an object is passed to this script, allowing the script to access the runtime engine features. This object is called the context object

The context object provides:

  • A return value used for code substitution and condition evaluation.

  • Methods to manipulate variables and values.

The context object is accessed with the syntax __ctx__, followed by a ., then the requested feature. For example, access methods with the syntax __ctx__.method()

The following sections describes the elements available in this object.

Return value

The scripting context provides a retValue variable. Use this String variable to:

  • Return a Boolean value to the condition interpreter.

  • Return a String that replaces the script with its result during code generation.

__ctx__.retValue

Publish variable values

The publishVariable method publishes a session variable.

public void publishVariable(String name, String value) {}
public void publishVariable(String name, String value, String type) {}
Table 1. List of parameters
Parameter Description

name

Path of the variable.

value

Value of the variable.

type

Type of the variable. The default value is String. Possible values are:

  • Float

  • Integer

  • Long

  • Boolean

  • String

Publish a value with implicit typing

The following example publishes a string variable named INCREMENTAL_MODE, with the value ACTIVE, to the parent process of the current action.

__ctx__.publishVariable("../INCREMENTAL_MODE","ACTIVE");
Publish values with explicit typing

The following example publishes multiple variables to the parent process of the current action, with explicit type declarations.

__ctx__.publishVariable("../OUT_CODE", "0", "Integer");
__ctx__.publishVariable("../OUT_MSG", "Success", "String");

Get aggregated variable values

The following methods return aggregated values for a variable. This aggregate is either a Sum, Average, Count, Min, or Max. The aggregation is done within a given path.

public String sumVariable(String name) {}
public String sumVariable(String name, String startingPath) {}
public String averageVariable(String name) {}
public String averageVariable(String name, String startingPath) {}
public String countVariable(String name) {}
public String countVariable(String name, String startingPath) {}
public String minVariable(String name) {}
public String minVariable(String name, String startingPath) {}
public String maxVariable(String name) {}
public String maxVariable(String name, String startingPath) {}
Table 2. List of parameters
Parameter Description

name

Name of the variable

startingPath

Path into which the variable values are aggregated. If this parameter is omitted, the values are aggregated for the entire Session.

Aggregate variable values

The following example aggregates the numbers of rows processed for the LOAD_DIMENSION process and all its sub-processes and steps.

nb_rows = __ctx__.sumVariable("SQL_NB_ROWS","../LOAD_DIMENSION");

The getCurrentBindIteration method returns the current bind iteration number for a bind link.

public long getCurrentBindIteration() {}

Get variable values

Session variables

The getVariableValue method returns the value of a session variable.

public String getVariableValue(String path) {}
Table 3. List of parameters
Parameter Description

path

Variable path.

Retrieve a variable value

The following sample retrieves the value of the CORE_SESSION_ID variable.

session_id = __ctx__.getVariableValue("/CORE_SESSION_ID");

Bind variables

Use the getSourceResultSet method to return a ResultSet object that holds the results of a bind link.

public ResultSet getSourceResultSet() {}

The getString method of that object can retrieve the value of a bind variable.

public String getSourceResultSet().getString(String name) {}
Table 4. List of parameters
Parameter Description

name

The name of a bind variable within the object context.

Retrieve a variable value

The following sample creates a result object, and assigns the value of two bind variables to local script variables.

ResultSet result = __ctx__.getSourceResultSet();

LAST_MODIFIED=result.getString("LAST_MODIFICATION_TIMESTAMP");
PROJECT_NAME=result.getString("PROJECT_NAME");

Get variable cumulative value

When bind or a loop makes an action iterate, variables store their value for the last iteration.

The getVariableCumulativeValue method retrieves the cumulated values for numeric variables.

public Object getVariableCumulativeValue(String name) {}
Table 5. List of parameters
Parameter Description

Name

Path of the numeric variable.

Get variable tree

The getVariableTreeByName method returns a Java treeMap object that contains variables corresponding to certain criteria.

public Map<String, IVariable> getVariableTreeByName(String name) {}
public Map<String, IVariable> getVariableTreeByName(String name, String startingPath) {}
public Map<String, IVariable> getVariableTreeByName(String name, boolean withErrors) {}
public Map<String, IVariable> getVariableTreeByName(String name, String startingPath, boolean withErrors)
Table 6. List of parameters
Parameter Description

name

Name of the published variable.

startingPath

Path from which the variable must be searched. Defaults to ~/.

withErrors

Boolean value. If set to true, only retrieves the variables from steps with errors. Defaults to false.

The returned Java Map object has the name of the action as the key, and its value is a variable object with the following methods:

public Object getCumulativeValue(); // variable cumulated value (numbers only)
public String getShortName();       // variable name.
public String getName();            // variable name with path.
public String getActionName();      // action name with path.
public String getType();            // variable type.
public String getValue();           // variable value.
Retrieve variables tree

The following sample retrieves the stack trace for all the steps in error.

Example 1. Retrieve stack traces (Groovy)
%e(groovy){
    def a = ""
    def tree = __ctx__.getVariableTreeByName("CORE_STACK_TRACE","~/",true)
    if (tree.size() != 0) {
        def es=tree.entrySet()
        es.each{
            a = a+ "-- ACTION --> " + it.key + "\n"
            a = a+ it.value.getValue() +"\n\n"
        }
        __ctx__.retValue = a
    }
}e(groovy)%
Example 2. Retrieve stack traces (Javascript)
%e(rhino){
    importPackage(java.util);
    a = "";
    tree = __ctx__.getVariableTreeByName("CORE_STACK_TRACE","~/",true);
    if (tree.size() != 0) {
        for (i= tree.keySet().iterator() ; i.hasNext() ; ){
            action = i.next();
            maVar = tree.get(action);
            a = a+ "-- ACTION --> " + action + "\n";
            a = a+ maVar.getValue() +"\n\n";
        }
        __ctx__.retValue = a
    }
}e(rhino)%

Get variable list

The getLstVariablesByName method works like getVariableTreeByName, but returns a list of variables instead of a Java Map.

public List<IVariable> getLstVariablesByName(String name) {}
public List<IVariable> getLstVariablesByName(String name, boolean withErrors) {}
public List<IVariable> getLstVariablesByName(String name, String startingPath) {}
public List<IVariable> getLstVariablesByName(String name, String startingPath, boolean withErrors) {}
Table 7. List of parameters
Parameter Description

name

Name of the published variable.

startingPath

Path from which the variable must be searched. Defaults to ~/.

withErrors

Boolean value. If set to true, only retrieves the variables from steps with errors. Defaults to false.

Retrieve variables list
Example 3. Retrieve stack traces (Groovy)
%e(groovy){
    def a = ""
    def lst = __ctx__.getLstVariablesByName("V1","~/")
    if (lst.size() != 0) {
        for (var in lst) {
            a =a + var.getValue() + "\n"
        }
        __ctx__.retValue = a
    }
}e(groovy)%
Example 4. Retrieve stack traces (Javascript)
%e(rhino){
    importPackage(java.util);
    a = "";
    lst = __ctx__.getLstVariablesByName("V1","~/");
    if (lst.size() != 0) {
        for (i=0;i<lst.size();i++){
            a = a+ "-- Value --> " + lst.get(i).getValue() +"\n";
        }
        __ctx__.retValue = a;
    }
}e(rhino)%

Create bind prepared statement

The createBindedPreparedStatement method returns an object that can produce a custom set of bind columns in a scripting. These can then be used in an outgoing Bind link.

public PreparedStatement createBindedPreparedStatement() {}

This object allows you to manipulate column definitions as well as publishing rows.

Manage column definition

Use the setColumn method to define properties of a column.

public void setColumn(int columnId, String columnName);
public void setColumn(int columnId, String columnName, String dataType)
public void setColumn(int columnId, String columnName, String dataType, int precision);
public void setColumn(int columnId, String columnName, String dataType, int precision, int scale);

Update column properties

Use the following methods to update the properties of a column.

public void setColumnName(int columnId, String columnName);
public void setColumnPrecision(int columnId, int precision);
public void setColumnType(int columnId, String dataType);

Set the value

Use the following methods to set or update the value of a column in the current row.

public void setBigDecimal(int columnId, BigDecimal value);
public void setBoolean(int columnId, boolean value);
public void setBytes(int columnId, byte[] value);
public void setDate(int columnId, Date value);
public void setDouble(int columnId, double value);
public void setInt(int columnId, int value);
public void setLong(int columnId, long value);
public void setString(int columnId, String value);
public void setTime(int columnId, Time value);
public void setTimestamp(int columnId, Timestamp value);

Publish a new row

The executeUpdate method can publishes a new row.

public int executeUpdate()
Example 5. Example: Create, execute a bind statement.
%e(rhino){
    // Create the statement
    ps=__ctx__.createBindedPreparedStatement();
    // Definition of the columns
        ps.setColumn(1,"TEST1"); // Set column 1
        ps.setColumn(2,"TEST2","VARCHAR",255); // Set column 2
    // First Bind Iteration
        ps.setString(1,"VALUE1.1");
        ps.setString(2,"VALUE2.1");
        ps.executeUpdate();
    // Second Bind Iteration
        ps.setString(1,"VALUE3.1");
        ps.setString(2,"VALUE3.2");
        ps.executeUpdate();
}e(rhino)%
Use this method in Scripting Actions to create your own Bind columns. For example, this can be useful to iterate on a list of values in a script, and use the result as Bind values in the target action.

Execute commands

The following methods execute commands on runtimes:

  • executeCommand runs a command on the current runtime.

  • executeCommands runs a list of commands, separated by a defined separator character, on the current runtime.

  • executeRemoteCommand runs a command on a remote runtime.

  • executeRemoteCommands runs a list of commands, separated by a defined separator character, on a remote runtime.

The available commands are the same as with the startcommand program.

public String executeCommand(String command) {}
public String executeCommands(String commands, String separator) {}
public String executeRemoteCommand(String host, int port, String command) {}
public String executeRemoteCommand(String host, int port, String command, User, encrypted Password) {}
public String executeRemoteCommands(String host, int port, String commands, String separator) {}
public String executeRemoteCommands(String host, int port, String commands, String separator, User, encrypted Password) {}

These methods return the standard output produced by the command execution process.

Table 8. List of parameters
Parameter Description

command

Command executed by the runtime.

commands

List of commands executed by the runtime, separated by the separator.

separator

Separator used to separate the commands.

host

Hostname or IP address of the remote runtime that executes the commands. The protocol must be explicitly declared in front, such as http://hostname or https://hostname.

port

Port of the remote Runtime.

user

User to authenticate to the runtime.

Encrypted password

The user’s encrypted password.

Example 6. Example: run commands on the runtime.
%e(rhino){

    // Run commands on the current runtime
    __ctx__.executeCommand("versions"); (1)
    __ctx__.executeCommands("versions;get deliveries",";"); (2)

    // Run commands on a remote runtime
    __ctx__.executeRemoteCommand("http://hostname","42200","versions"); (3)
    __ctx__.executeRemoteCommand("https://hostname","42200","versions","user","encrypted password"); (4)
    __ctx__.executeRemoteCommands("http://hostname","42200","versions;get deliveries",";"); (5)
    __ctx__.executeRemoteCommands("https://hostname","42200","versions;get deliveries",";","user","encrypted password); (6)

}e(rhino)%
1 Run a single command on the current runtime.
2 Run multiple commands on the current runtime.
3 Run a single command on a remote runtime.
4 Run a single command on a remote runtime, with HTTPS protocol and a user password.
5 Run multiple commands on a remote runtime.
6 Run multiple commands on a remote runtime, with HTTPS protocol and a user password.