I was blogging about use of domains in ADF Business Components previously - Oracle Object Types in Oracle ADF. However, I have described domains there from Oracle Object Types implementation perspective, but its possible to use domains for validation rules. With domains you can implement custom validated data types for java.lang.String, java.lang.Long and other Java types. Main benefit of validated data types implemented with domains is reusability. Let's say if there are attributes in several Entity or View objects and the same validation rule is applied for those attributes, its more wise not to repeat this validation rule, but to put it into domain. I will explain in this post, how to do this.
You can download developed sample application - ADFDomainValidation.zip. To run this sample, you need to have standard HR schema in your database, I'm personally using Oracle XE. Just right click faces-config.xml file and select Run, application will be started.
I have implemented domain called - SalaryDomain. This domain is based on java.lang.Long type and it validates for positive salary amount.
When JDeveloper generates domain, Java class for this domain is generated as well. This class contains standard method - validate(). You can put your validation code here. Its important to notice, that validation logic will be applied for existing and for new data. This means validation is applied during querying from database process. I have implemented simple validation rule - salary value can't be negative:
And now, when domain is created and validation logic is implemented, we can apply our domain as a Type for attributes. Applying domain for MinSalary and MaxSalary attributes from Jobs Entity object:
Applying domain for Salary attribute from Employees Entity object:
So, you can notice the same validation logic is applied for three different attributes in two different Entity objects.
When application is running and user is trying to assign negative value for MinSalary attribute:
Valdation error message is shown:
And if user will provide negative value for Employee salary:
The same validation error message will be shown: