First page Back Continue Last page Graphics

Groovy Rule Definition


Notes:

Groovy Rule Definition

The Boolean expression in the if statement references the DepartmentId attribute of the current Emp object that is validated to access the associated Dept entity object (if it exists). In this case, the default name of the association for the employee’s department is Dept1. You can see this name if you look at the definition of the EmpDeptFkAssoc object in the Accessor section. In general, you should change the name of all your associations to represent the business (such as EmpWorkingInDept).

Then the if statement references the DepartmentName attribute found at the end of the Dept1 association, converts it to uppercase to perform a non-case-sensitive comparison, and uses the String class endsWith() method to test whether the DepartmentName value ends with an S.

Note that instead of writing Dept1.DepartmentName.toUpperCase().endsWith(), the normal dot operator has been replaced with Groovy’s “?.” safe-navigation operator. This operator works in the same way as the dot operator to allow navigation from an object to properties on the object. The “?” prevents a null pointer exception from happening if the value is null.

However, if the left-side value is null, the Groovy operator does not throw NullPointerException, but instead evaluates to null. Conveniently, when null is encountered in Groovy as a Boolean value, it evaluates to false, so using the “?.” operator can make many expressions more compact. After this initial check, the expression uses the integer modulo operator (%) to test whether the salary is a multiple of 5.