Java scripts and Groovy scripts are embedded in the process through scripting activity and executed by the automatic client. The associated component de.aristaflow.tools.Scripting->executeScript->executeScript is integrated into the process like all other activities from the Activity Repository using the drag and drop principle. BSH scripts have been deprecated and are no longer supported. We recommend using Groovy scripts, and we'll take a closer look at that activity here.
After embedding the activity, the wizard pages open immediately, where you should enter the script to be executed. We use groovy scripts in our examples because Groovy is a powerful and dynamic language with which you can easily edit the process data. The data to be processed in the activity are entered as input parameters where the optional parameters may be null. The output parameters must be set in the script, only the optional ones can remain null. The parameters should be named in a Java-compliant way.
All parameters are available in the script and can be added to the script by double-clicking. The input parameters are marked in green, the output parameters in red. After entering the script, you can use Execute Script to check for syntax errors or do a test execution on the test execution page by clicking Set test input values. The errors that have occurred are then displayed on the left above the wizard page.
In the test execution, enter the values for the input parameters by clicking the cell editor. Run the script using the Execute Script button and check the result in the Output Values area below.
More detailed documentation of the Scripting activity can be found directly on the activity's wizard page by clicking the Docs tab.
Example Parsing and Producing JSON¶
In this example we will see how to encapsulate parameter values of simple primitive data types in a JsonObject. In the following template in the second step To JSON, the parameters ageIn and nameIn are combined into a JSON object and in the third step From JSON these parameters are fetched from the JSON Object.
As Json builder we use the class JsonOutput which is responsible for String serializing of the possible values of a JSON structure. In this case we serialise Groovy objects (String and Integer variables) into Json String. But we need the method toJson that take the various parameter types and returns a string representing the Json Array. First of all you have to import the JsonOutput with a import statement.
$json = JsonOutput.toJson([name: $name, age: $age])
To access this JSON text (parameter $json) like a JSON object, we parse it into a groovy object using Jsonslurper. This class parses Json text into Groovy data structure such as maps or lists etc. For that there are some parse methods with appropiate data types. We use parseText to convert this Json string to a list or map of object. Then we can access the individual elements of this object.
def jsonSlurper = new JsonSlurper()
def person = jsonSlurper.parseText($json)
$name = person.name
$age = person.age
Example Userdefined Lists in Groovy¶
In the following example we show how easy it is to handle various userdefined lists in Groovy.
By default, when defining a list without specifying a type, groovy creates a list of java.util.ArrayList. The list elements are assigned to a variable in square brackets separated by commas. Groovy offers certain shortcuts when working with lists e.g. >> is used to add an item to the list. For more information about list handling see Groovy Documentation.
In the following process we calculate the age from date of birth of a user form. We generate a list from the user inputs for $name and $dateOfBirth and add it later the calculated age. At the end process step we display this age from the last element of the list in a new user form.
For the output $age, the period between today and birthday is calculated with the help of the LocalDate class. The value of the variable age is added to the list and written from the list to the output parameter $age. The associated script looks like this.
LocalDate today = LocalDate.now();
LocalDate birthday = new java.sql.Date($dateOfBirth.getTime()).toLocalDate();
def age = Period.between(birthday, today).years;
def list = [$name, $dateOfBirth]
list << age
// same as: list.add(age)
$age = list.getAt(list.size()-1);