Monday, April 28, 2014

EntityLevelOnly Property To Delay Unique Key Validator

Idea for this post was kindly suggested by real ADF BC guru and evangelist - Steve Muench. You can check famous ADF Not Yet Documented Examples on Steve's blog - Dive into ADF.

The main idea is about composite Unique Key, based on multiple EO attributes. Ideally, users should be allowed to change composite Unique Key values, without getting validation errors too early (before trying to save changes). However, this is not the case by default - and I will explain in this post, how to make it work properly with EntityLevelOnly = true property.

Sample application - ComplexUniqueKeyApp.zip, implements alternate key for two attributes from the EO. These attributes are Letter and Digit (SQL script to create required DB table is provided with the sample):


Based on alternate key, Unique Key validation rule is created for Letter and Digit:


Both attributes are set with AutoSubmit = true on the UI, to make sure new value will be submitted immediately:


DB table is populate with unique values for Letter and Digit columns. I could try to change first row and set B for Letter and 3 for Digit. Obviously, I would change one of the values first, if I'm going to change A to B - this could force ADF to raise Unique Key error (there is already B for Letter with Digit 1). So, Unique Key validation error would be generated too early, before I would change Digit to 3:


First row is rendered on UI, with A for Letter and 1 for Digit. I'm going to try to change A to B:


As soon as I would leave Letter field and try to move to Digit, ADF would raise validation error about broken uniqueness:


This behaviour can be improved with EntityLevelOnly = true property in EO definition class. Override standard method - addUniqueKeyValidation to catch Unique Key validation rule definitions. Here we could check by name and apply setEntityLevelOnly(true) method for our Unique Key validator:


Validation rule name:


Can be verified using actual validation rule definition in the EO (this could be made generic, if required):


With EntityLevelOnly = true applied, I can change first part of the Unique Key, and move to change second part of the key to set new unique value:

3 comments:

Anonymous said...

I am looking for similar requirement, thanks for the post. But when i implemented its not working in my example. My scenario is I have foreign key+sequence number combination is Alternate key on my table. So when i am swaping the sequence numbers in UI its giving error please enter unique keys. After overriding addUniqueKeyValidation its still throwing same error on save. I am using jdev 11.1.1.1.6.0 is that is not available in this version?

Andrej Baranovskij said...

Yes, I think it works from 11g R2.

Regards,
Andrejus

Danas Tarnauskas said...

Thank you so much! It was very helpful!
This approach solved issue in ADFDI - false Alternate key validation exceptions were thrown on data load, before any changes applied and with valid data. After setEntityLevelOnly=true, validation works as expected.