Saturday, March 5, 2011

Validation Rule-An ADF Business Component

A generic validation can be written as a Validation Rule (ADF Business Component)  and can be assigned to various Entity objects (wherever required).  In the current example we are embedding a validation to compare the dates. Once the validation Rule is written, the same can be attached as a business rule at the entity level.

1.       Create a Validation Rule
 Right click on Model Project ->New-> Select Validation Rule

IDE prompts for a new class to be written for the Validation Rule

 
This class will extend AbstractValidator implements JboValidatorInterface.  Create two new attributes in the generated class and generate accessors.


 
Two methods get generated in this validation class viz., validate and validateValue. The key method in this class is validate which gets fired when the page is submitted.Following is the sample implementation of the validate method:
 
As the validationRule will be attached to an entity, the source of the Validation Context would be EntityImpl. Also, while assigning the validation rule to the entity we need to map the attributes of this class to the attributes of the Entity Object. If the attribute “initialDate” of this class is mapped to the StartDate attribute of the Entity, then the getInitialDate() method will retrieve the “StartDate”. Thus the start and end date of the entity object can be calculated as follows:
           Date initialDate = (Date) eo.getAttribute(getInitialDate());
           Date laterDate = (Date) eo.getAttribute(getLaterDate());
Following is the ValidateValue method that is invoked in the Validate method:

2.       Assign Validation Rule to the Entity Object
Click on the Entity object on which the Validation Rule needs to be applied.  Click on Business Rules and Select Entity Validators. Now click on the Add icon to add a validation. In the Rule Type drop down, verify for the newly created Validator class.

 
In the Edit Rule Type validation, verify for the attributes that are added in the class and map them to the attributes of the Entity Object.
 
Now at the runtime, the getInitialDate and getLaterDate methods of the Validation class will retrieve the EO attributes and the validate method gets executed when a page is submitted.
The above method is using the ResourceBundle to throw an exception. 


One advantage with this validation rule is that, the validation code will be central and the maintenance would be easy. Otherwise, if the same validation is implemented in each and every entity, it would be a laborious process to change the code.




1 comment:

sai_explorer said...

Thanks for providing information on usage of Validation rule.
Please upload the project.

Regards,
Sai Yeluri