Skip to end of metadata
Go to start of metadata

 

Class setting variables

Class setting variables are user components, allowing you to set the values of variables before the task acceptance is completed.

The created variable setting classes are available in the process editor and can be easily used in the business process enabling the execution of business logic. Their behavior can be further configured using parameters.


Defining the setter

Setters are created based on their definition created by the user. Such a definition must contain the following elements:

  1. Annotation @VariableSetter (if the setter is not defined in the plugin, it must come from the com.suncode package)
  2. A public method annotated @Define with a single parameter VariableSetterDefinitionBuilder
  3. A public method called set, which is responsible for setting the values of variables.

The setter can also provide a script that builds the appearance of parameters when it is defined in the PWE. To do this, add another annotation for the class @ComponentsFormScript with the passed path to the script (from classpath).

An example definition is shown below. The setter calculates the gross value based on the net value and VAT value and will set it to the variable specified in the parameter.

@VariableSetter
@ComponentsFormScript( "path/example-form.js" )
public class VariableSetterExample
{
    @Define
    public void definition( VariableSetterDefinitionBuilder builder )
    {
        builder
            .id( "example-setter" )
            .name( "setter.example" )
            .description( "setter.example.desc" )
            .category( Categories.TEST )
            .parameter()
                .id( "netValue" )
                .name( "setter.parameter.net.name" )
                .description( "setter.parameter.net.desc" )
                .type( Types.FLOAT)
                .create()
            .parameter()
                .id( "vat" )
                .name( "setter.parameter.vat.name" )
                .description( "setter.parameter.vat.desc" )
                .type( Types.FLOAT)
                .create()
            .parameter()
                .id( "grossValue" )
                .name( "setter.parameter.gross.name" )
                .description( "setter.parameter.gross.desc" )
                .type( Types.VARIABLE)
                .create();
    }
 
    public void set( @Param Double netValue, @Param Double vat, @Param Variable grossValue )
    {
        if ( netValue == null )
        {
            return;
        }
        grossValue.setValue( netValue * vat );
    }
}

Implementation of a function that sets variables

The setter must have a method set. The method can have the following types of parameters:

  • a single validator parameter - the type of the parameter must be consistent with the defined type and must be preceded by an annotation @Param,
  • parameter mapping two array parameters (key, value) - the type of the parameter must be of type java.util.Map<K,V>, where K is the defined type of the component parameter being the key, and V is the defined type of the component parameter being the value for the key. The parameter must be annotatedPairedParam with defined properties (key - id of the parameter being the key, value - id of the parameter being the value).
  • a single parameter type DataSourceInstance marked @Param- if you have such a type, the DataSourceInstance object will be injected based on the value of this parameter, which must be the id of the data source.
  • Parameters - contains all the defined validator parameters with their values, this is an alternative to retrieving the parameter using the above-mentioned annotation @Param,
  • AcceptanceContext - task acceptance context, the process and task identifier can be read from it.
  • ActivityContextMap - an object storing the variables of the form with their current values and the process and task identifier; all variables can be set values.
  • ContextVariables - the context variables available in this validator. More information here,
  • Translator - a translator for this component, so that the messages returned by the validator can be translated. More information here,
  • UserInfo - an object containing information about the user accepting the task.

 

Display the contents of the error to the user

Available from version 3.2.44.

When the setter call fails, we can display information to the user on the form with the reason for the error. The condition is that the task must not have parallel acceptance set (that is, taking place in the background). To display the information to the user, throw an AcceptanceException exception in the setter with the error message set.

public void set( @Param Double netValue, @Param Double vat, @Param Variable grossValue )
{
    try
	{
		grossValue.setValue( netValue * vat );
	}
	catch( Exception exception )
	{
		throw new AcceptanceException( "Nie udało się obliczyć wartości.", exception );
	}
}

Registration of the setter in the plugin

If the setter is defined in the plugin, then it is necessary to additionally indicate that the plugin provides components. To do this, add an entry in the suncode-plugin.xml file:

<!-- Sharing setters --> 
<workflow-components key="components" />

  • No labels