This article is not up to date and it has been substitute with new up to date content.

Please look the new up to date content.

Getting started with the Script unit

9,292 views Published on Oct 24, 2011
Applies to: All versions
Table of contents

Each time you place a script unit in your Web project you have to specify also the script source file. This file has to be written in Groovy. Here are some guidelines to write the Groovy script properly.

  • at the beginning of the script, you can place comments that declare the input and output of the script unit. In this way you can avoid to manually add inputs and outputs in the web project. Note that the declaration in the script has more priority respect the declaration in the web model. To declare inputs and outputs use the following syntax

//inputs=INPUT1|INPUT2|INPUT3

//outputs=OUTPUT1|OUTPUT2

You can also choose to declare in this way only the inputs or only the outputs.

  • you can avoid to end each line of code with the ";" character
  • whenever you want to use a particular Java class you have to import it at the beginning of the script.
  • you can define new variables without defining their type using the syntax

  def myVariable = ..

  • you can directly use in the script the variables representing the script unit inputs that you have specified for the unit in the Web project or that you have declared at the beginning of the script
  • the type of a input variable depends on which type of unit provides the input to the script unit, as well as on the placement of the script unit and of the unit providing the input, and therefore the script must be adapted to cope with different types of input variables (strings, array of strings, array of objects, objects, and so on)
  • you have to write always a return instruction
  • more specifically, if in the web project you haven't added any output to the script unit, you are using the default one, which is named "result". To return the result of your script you have only to write

  return myVariable

  • if you have added different outputs to the unit, then the syntax is quite different, because you have to return a map

  return ["output1":outputVariable1 ,"output2":outputVariable1]

  • if you have to return a list of values in one of the output, the previous sintax become:

  return ["output1":outputVariable1.toArray() ,"output2":outputVariable1.toArray()]

Declaring typed inputs and outputs

WebRatio 5.1.1 introduced a new way of declaring input and output parameters in Script Unit, optionally enabling automatic conversion of values to a specified Java type.

It's possible to declare inputs and outputs along with their Java type using the new declaration syntax:


#input String foo, String bar, int baz, String[] fooArray, RTXBLOBData blob

#input double fooBar

#input fooBaz

And similar for output, using #output.

If the type of the declared parameter and the actual value passed are not matching, a type conversion is performed. Moreover, for inputs/outputs with no specified type, no conversion is performed, using the same behaviour of the old // syntax. In order to use array as input/output parameters, simpy add "[]" nex to the data type, refer to the "fooArray" input parameter in the example above.

For the declaration it's possible to use the following data types:

Alias Converted data type
boolean, Boolean java.lang.Boolean
Date java.sql.Date
BigDecimal java.math.BigDecimal
double, Double java.lang.Double
int, Integer java.lang.Integer
String java.lang.String
Time java.sql.Time
Timestamp java.sql.Timestamp
RTXBLOBDATA com.webratio.rtx.RTXBLOBData
Document org.dom4j.Document
Element org.dom4j.Element

The types Document and Element are used for handling XML.

 
 

This article is not up to date and it has been substitute with new up to date content.

Please look the new up to date content.