Wednesday, 17 June 2015

state machines validation using Groove expressions

Very often there is a business requirement that defines some kind of a state machine. Each business entity has its own lifecycle, it can be created, published, modified, archived and so on. Transitions between states are usually one way. If a bank transfer is complete it cannot be rolled back to the prepared state. In other words, it means that validation depends on new and old value of the same attribute.

Recently I've worked on such requirement and the validation have been implemented as valueChangeListener on af:selectOneChoice. There is simplier solution and development must be done in model layer. Reasonable because valueChangeListener is assigned to one instance of the attribute usage in the project. And such attribute may be used in various pages. And there is no guarantee that developers there know about such validation.

Because new value depends on the previous, it fits to other patterns as well. Employees in a company can have their salary attribute to be updated. But who wants to have the new salary lower than current? :) Nobody I think.

Please open entity Employees, select Salary attribute and specify new script validation rule:

Print command is just to log the new value in the JDev console. Further, specify the error message and run the example page:

Which is the most important here - Groove allows to refer to new and old values by using newValue and oldValue keywords.