Java Command Annotations – @Persistent
Java command annotations are used for the developing Java commands in PAK.
This chapter describes the functionality of the @Persistent
annotation.
It also gives an example of how you can use this annotation in your implementation.
1. Basics
The @Persistent
annotation marks a variable of a java class as relevant input or output for PAK.
Thus, it is injected from the datastore before or stored to the datastore after the successful command execution.
Without any further options, a mandatory and read-only persistent variable is created with the variable name as the datastore key.
In case the variable is initialized with a value it will be used unless another value was found in the datastore.
2. Properties
The @Persistent
annotation has several properties defined which are described in the following.
-
mandatory
:
This property marks whether the variable is mandatory or not.
In case a variable is mandatory you cannot execute the command as long as no value is assigned to that variable in the datastore.
By default, the value istrue
which means that the variable is mandatory. -
scope
:
This property defines the scope of the variable as well as the writing direction.
The scope is defined by the enumFieldScope
and can either beREAD_ONLY
,WRITE_ONLY
orREAD_WRITE
.
ForREAD_ONLY
variables the values are read from the datastore before the command execution.
Values forWRITE_ONLY
variables are written to the datastore after the command execution.
Lastly, values ofREAD_WRITE
variables are read from and written to the datastore.
A variable is by default read-only. -
name
:
This property gives the possibility to define a custom name for the variable.
In case thename
property is set it will be also used as datastore key.
By default, thename
property is an empty String which means the variable name is used. -
getter
andsetter
:
This properties allow to define the name of a customized getter/setter method that will be used for fetching/setting the variable value.
By default, no customized method getter/setter name is given.
This means that if existing, the getter/setter derived from the variable name will be used or, in case this getter/setter is not present, the variable will be accessed directly.
In case a customizedgetter
/setter
is given, the method with this name will be used.
Please ensure that no other annotated Method, like @Run , @PostConstruct or @PreDestroy , is named like a possible getter.This will cause a compile error. An example of that case can be found in Listing 2. |
3. Usage
The example code piece in Listing 1 shows the usage of the @Persistent
annotation and its properties.
/**
* @workflowDocu This command gets an element by its id.
*/
@JavaCommand
@CommandGroup("Element")
public class GetElement {
[...]
/**
* @workflowDocu Id of the element to fetch
*/
@Persistent (1)
private String id;
/**
* @workflowDocu Username of the current user
*/
@Persistent(getter = "getInputUsername", setter = "setInputUsername") (1)
private String username;
/**
* @workflowDocu Name of the element
*/
@Persistent(mandatory = false, scope = FieldScope.READ_WRITE, name = "elementName") (2)
private String name;
/**
* @workflowDocu Fetched element
*/
@Persistent(scope = FieldScope.WRITE_ONLY) (3)
private Element element;
public String getInputUsername() {
return "username/" + this.username;
}
public void setInputUsername(final String username) {
this.username = "username_" + this.username;
}
[...]
}
1 | Mandatory variable with default read-only scope |
2 | Optional variable with read-write scope and customized name for the datastore |
3 | Mandatory variable with write-only scope |
In the single command runner of the Workflow Executor the example of Listing 1 will look like shown in Figure 1.
As you can see, there are separate collapsible boxes for input (marked in red) and output (marked in purple) variables.
Since id
and username
are input variables those variables are placed in the box for input variables.
Additionally, they are surrounded by a red border to mark that the variables are mandatory, which will disappear if a value is given by the user.
For the variable element
the alias elementName
is displayed and it is marked as optional in the box for input variables.
Since this variable is marked with the scope READ_WRITE
it also appears in the output variables box.
Here you can define a custom variable name.
That name represents the key for the datastore the value will be written to after the command execution.
The same applies for the WRITE_ONLY
marked variable element
whereby it only appears in the box for output variables.
The following Listing 2 shows an example how you must not implement a command as it causes a compile error. |
In Listing 2 no customized getter/setter is given for the variable element
that is annotated with @Persistent
.
Thus, the implementation of reflections tries to find a getter with the name getElement
.
As the method, which is annotated with @Run
is also named getElement
this causes a compile error.
/**
* @workflowDocu This command gets an element by its name.
*/
@JavaCommand
@CommandGroup("Element")
public class GetElement {
[...]
/**
* @workflowDocu Name of the element to fetch
*/
@Persistent (1)
private String element;
/**
* Fetch the element with a given name.
*/
@Run
public void getElement() { (2)
// Command functionality
[...]
}
}
1 | Mark element as persistence variable |
2 | @Run method named as the getter of the persistence variable |