GroovySupport

From RunaWFE
Jump to navigation Jump to search

Groovy support

RunaWFE Free Workflow System (BPMS) Version 4.5.0

© 2003 - 2015, Consulting Group Runa

© 2015 - 2024, "Process Technologies" Ltd, this document is available under GNU FDL license. RunaWFE Free is an open source system distributed under a LGPL license (http://www.gnu.org/licenses/lgpl.html).


# Introduction

Groovy is a programming language written in Java and executed in JVM. Groovy scripts use Java syntax.

In RunaWFE Groovy is used

  • in handlers (GrooveActionHandler)
  • in global validators
  • in decision nodes

Note. In version 3.x BeanShell script was used for the same purposes. The syntax difference between BeanShell and Groove is in void keyword (in BeanShell) and null (in Groovy) for undefined values. All scripts written for version 3.x will work in version 4.x (validators need some additional measures, see Note).

# GroovyActionHandler usage

ru.runa.wfe.extension.handler.GroovyActionHandler can be used to write script with logic in some cases.

Exaple of handler configuration (Groovy script):

My_date = new java.util.Date();
My_rnd = new java.util.Random(1000).nextInt();
My_time = java.lang.System.currentTimeMillis();

Variables values that are changed in the script keep the changed values in the process instance.

# Groovy usage in Global validator

The validator script is executed to get the variable validation result.

script execution result validation result
true (java.lang.Boolean) passed
false (java.lang.Boolean), null or object of any other type failed
java.lang.Exception thrown failed, ERROR message in log

Note. For validators written in version 3.x it's necessary to rewrite validator class that is used. It can be done by placing wfe.custom.validators.xml file in the extensions folder(see more about extensions)

<?xml version="1.0" encoding="UTF-8"?>
<validators>
 <validator name="expression" class="ru.runa.wfe.validation.impl.BSHExpressionValidator"/>
</validators>

Example. Check if current date is before deadline date

java.util.Date deadlineDate = new java.text.SimpleDateFormat("dd.MM.yyyy").parse("01.01.2014");
return new java.util.Date().before(deadlineDate);

Example. Check if current time is before deadline time

java.util.Calendar deadlineTime = ru.runa.wfe.commons.CalendarUtil.convertToCalendar("17:30", ru.runa.wfe.commons.CalendarUtil.HOURS_MINUTES_FORMAT);
java.util.Calendar nowTime = java.util.Calendar.getInstance();
return ru.runa.wfe.commons.CalendarUtil.compareTime(nowTime, deadlineTime) < 0;

Example Dependent variables values validation

Note. When working with strings it's necessary to check both if string is null and if it's an empty string ("").

Variable2 (String) is required if Variable1 (Date):

if (Variable1 != null) {
 return Variable2 != null && Variable2.length() > 0;
}
return true;

Variable2 (String) is required if Variable1 (String):

if (Variable1 != null && Variable1.length() > 0) {
 return Variable2 != null && Variable2.length() > 0;
}
return true;

Variable2 (Long) is required if Variable1 (Long):

if (Variable1 != null) {
 return Variable2 != null;
}
return true;

Variable2 (Long) is required or not depending on Variable1 (Long) value

if (Variable1 == 100) {
 return Variable2 != null;
}
return true;

From (Date) and To (Date) are both optional, but must be related in a certain way if defined


if (From != null && To != null) {
 return !From.after(To);
}
return true;

Variable2 (List) is required or not depending on Variable1 (String) value

If validator "Container values are required" is not set user can submit null as List element.

Example File:ListValidation.par.

if (Variable1 != null && Variable1.length() > 0) {
 return ListVariable != null && ListVariable.size() > 0;
}
return true;

# Groovy usage in Decision nodes

There's a configurator in GPD for simple conditions construction.