
Using Groovy expressions to resolve validation error message tokens
In this recipe, we will expand on the Using a custom validator based on a View Object accessor recipe to demonstrate how to use validation message parameter values based on Groovy expressions. Moreover, we will show how to retrieve the parameter values from a specific parameter bundle.
Groovy is a dynamic language that runs inside the Java Virtual Machine. In the context of the ADF Business Components framework, it can be used to provide declarative expressions that are interpreted at runtime. Groovy expressions can be used in validation rules, validation messages, and parameters, attribute initializations, bind variable initializations, and more.
Getting ready
This recipe builds on the Using a custom validator based on a View Object accessor recipe. It also relies on the recipes Breaking up the application in multiple workspaces and Setting up BC base classes presented in Chapter 1, Pre-requisites to Success: ADF Project Setup and Foundations.
How to do it...
- In the Application Navigator double-click on the Employee entity object definition and go to its Business Rules tab.
- Double-click on the validateDepartmentEmpoyeeCount Method Validator to bring up the Edit Validation Rule dialog and go to the Failure Handling tab.
- Change the Error Message to Department has reached maximum employee limit of {1}.
- For the Message Token 1 Expression in the Token Message Expressions section, enter the following expression:
source.getBundleParameter('DepartmentEmployeeLimit')
- Now, open the
SharedComponets
workspace and locate the entity framework extension classExtEntityImpl
. Add the followinggetBundleParameter()
method to it:public String getBundleParameter(String parameterKey) { // use BundleUtils to load the parameter return BundleUtils.loadParameter(parameterKey); }
- Locate the
BundleUtils
helper class in thecom.packt.jdeveloper.cookbook.shared.bc.exceptions.messages
package and add the followingloadParameter()
method:public static String loadParameter(final String parameterKey) { // get access to the error message parameters bundle final ResourceBundle parametersBundle = ResourceBundle.getBundle(PARAMETERS_BUNDLE, Locale.getDefault()); // get and return the the parameter value return parametersBundle.getString(PARAMETER_PREFIX +parameterKey); }
- Finally, locate the
ErrorParams.properties
property file and add the following text to it:parameter.DepartmentEmployeeLimit=2
How it works...
For this recipe, first we added a parameter to the method validator message. The parameter is indicated by adding parameter placeholders to the message using braces {}
. The parameter name is indicated by the value within the braces. In our case, we defined a parameter called 1
by entering {1}
. We then had to supply the parameter value. Instead of hardcoding the parameter value, we used the following Groovy expression:
source.getBundleParameter('DepartmentEmployeeLimit').
The source
prefix allows us to reference an entity object method from the validator. In this case, the method is called getBundleParameter()
. This method accepts a parameter key which is used to load the actual parameter value from the parameters bundle. In this case, we have used the DepartmentEmployeeLimit
parameter key.
Then we implemented the getBundleParameter()
method. We implemented this method in the base entity custom framework class so that it is available to all entity objects. If you look at the code in getBundleParameter()
, you will see that it loads and returns the parameter value using the helper BundleUtils.loadParameter()
.
Note
We introduced the helper class BundleUtils
while we worked on the Using a generic backing bean actions framework recipe in Chapter 1, Pre-requisites to Success: ADF Project Setup and Foundations.
The BundleUtils.loadParameter()
method pre-pends the parameter with the prefix parameter
.
Finally, we defined the parameter.DepartmentEmployeeLimit
parameter in the ErrorParams.properties
parameters bundle. For further information on this bundle, refer to the Using a custom exception class recipe in Chapter 1, Pre-requisites to Success: ADF Project Setup and Foundations. When the validation is raised at runtime, the message parameter placeholder {1}
, which was originally defined in the message, will be substituted with the actual parameter value (in this case, the number 2).
See also
- Breaking up the application in multiple workspaces, Chapter 1, Pre-requisites to Success: ADF Project Setup and Foundations
- Setting up BC base classes, Chapter 1, Pre-requisites to Success: ADF Project Setup and Foundations
- Using a custom exception class, Chapter 1, Pre-requisites to Success: ADF Project Setup and Foundations
- Using a generic backing bean actions framework, Chapter 1, Pre-requisites to Success: ADF Project Setup and Foundations