Sunday, April 20, 2008

Validated Data Types with Domains in Oracle ADF

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:

5 comments:

Chris Muir said...

There is a limitation with custom validation domains, that being if you create an EO/VO attribute based on the domain, and the attribute is populated/fetched from the database via an invalid field value (ie. doesn't meet you domain's validation rules), you can never actually retrieve that value from the database.

A more complete discussion and an ER is discussed on the 11g JDev OTN Forums.

Cheers,

CM.

Andrej Baranovskij said...

Hi Chris,

Yes, that true. Validation rule is applied when querying data from database also.

Regards,
Andrejus

Anonymous said...

Hi

i use Jdev 11.1.1.1.0 and i have
Error: Cannot convert 3000 of type class java.lang.String to class lt.andrejusb.model.domains.common.SalaryDomain.

jm

R said...

hi andrejus,

even with 11.1.1.4, i am getting the same exception like Cannot convert 16000 of type class java.lang.String to class lt.andrejusb.model.domains.common.SalaryDomain, please help us

R said...

andrejus,

i used custom converter as suggested by steve and now its working but there is one more problem that if we try to enter negative values then its throwing the exception in another page with 500 internal server error like this
module:ADFDomainValidation-ViewController-context-root path:/ADFDomainValidation-ViewController-context-root spec-version:2.5]] Servlet failed with Exception
oracle.jbo.JboException: Salary value cant be negative: -98 !
and <_handleException> ADF_FACES-60098:Faces lifecycle receives unhandled exceptions in phase PROCESS_VALIDATIONS 3
oracle.jbo.JboException: Salary value cant be negative: -98 !