Skip to main content

Session context

The “sessionContext” is a global variable, which available in each TMS rule.

global SessionContext sessionContext;

The session context provides the rules developer various additional information for decision making such as

  • Information regarding the user within the current session
  • The action which triggers running the rules in particular
    • INPUT, the execution of rules are triggered, if a change in a input mask occurs
    • SAVE, the execution of rules is triggered, if an TMS entity is saved

In following an example the rules developer makes use of the TMS Action to only “assist” in prefilling some fields. Here a very likely email address is proposed based on the name and surname of a person. The context “INPUT” makes sure, that this is just a “convenience” function in the TMS UI and will not change active data in the saving process:

rule "Set Email from name"
when
$r: Resource(getTypeName() == "TEST_ENGINEER", TMS.INPUT.equals(sessionContext.getAction()), $attributes: attributes)
$family: Attribute(name == "FAMILY_NAME", !getValueAsString().isEmpty()) from $attributes
$name: Attribute(name == "NAME", !getValueAsString().isEmpty()) from $attributes
$email: Attribute(name == "EMAIL", getValueAsString().isEmpty()) from $attributes
then
$email.setValueAsString($name.getValueAsString() + "."
+ $family.getValueAsString() + "@peak-solution.de");
end

A typical usage of the session user, is to release or restrict certain fields or values for the user. In the following example only members of the user group “Technician” will have access to the area “test raw data”, for all others the field will made be non-editable.

rule "restrict test raw data to technicians"
when
$attrTestData: Attribute(name == "TEST_RAW_DATA",
!sessionContext.getLoginUser().getUserGroups().contains("Technician"))
then
$attrTestData.setEditable(false);

end

warning

The declaration of the session context is mandatory in each rules snippet. Compilation will fail if is not declared!