Sunday, August 26, 2012

New Record Master-Detail Validation and ADF BC Groovy Use Case

I will explain today implementation for Master-Detail validation rule - new Master record insert is not allowed, if there are no Detail records added.  ADF is powerful framework and there are different ways to implement same requirement - complexity is to choose the most suitable and optimal approach. Such validation rule can be implemented in Java, by overriding doDML() method on Entity Implementation class. While this works, also same rule can be implemented in Groovy script and declared as ADF BC business rule on Entity. Obviously its better to declare validation in standard way as Entity business rule, instead of coding it in Java logic (at least for current requirement) - it will make code maintenance and changes simpler.

Sample application is based on Departments-Employees - MasterDetailGroovyValidationApp.zip. User creates new Department:


Detail list with Employees is refreshed for new Department, based on Master-Detail relationship:


You can see - new Department info is available, but there are no Employees added, as per our validation rule such data change should be prevented. Yes, it works exactly as it should - ADF reports validation error for new Department record without Employees added:


Add Employee record - transaction will be commited without errors:


This validation is implemented with Groovy, as Entity level business rule. I would recommend to read more about Groovy from Grant Ronald guide - Introduction to Groovy Support in JDeveloper and Oracle ADF 11g. Oracle ADF developer guide how to call Entity Implementation class methods from Groovy - 7.5.1 How to Reference Entity Object Methods in Groovy Validation Expressions.

Validation rule is invoked on transaction commit, it is defined as Departments Entity level validation:


I'm using Groovy for rule definition and for validation execution logic. Validation rule definition executes count Groovy function based on Employees accessor. If count returns 0 records, means there are no associated Employees - validation error is reported:


Validation execution logic checks if rule should be executed. It gets current row status for Groovy object - adf.object.entityState. If status is translated (using custom method in Entity Implementation class) into New or Initialized - only in such case validation rule is triggered:


Custom method in Entity Implementation class to translate row status:


Additional tip for insert into Master-Detail, sample application overrides postChanges(...) method for Employees Entity Implementation - it checks if Master Departments record is new and forces post on Master (this is required if Association is not Composite to ensure Master-Detail posting order):


2 comments:

Shree said...

Hi Andre,

I come from a oracle forms/reports and pl/sql background working in the EBS area for over 15 years.

I'm trying to get into ADF. I learned a lot about ADF form your blogs. I know little java and xml so the coding part is not the difficult one.

Actually layout part is kinda confusing to me. If you look at this partucilar application, you have tab wirh a show detail item. that part is clear but you have 2 panel group layouts and with layout of horizontal and one with layout of scroll.

So can you write few posts on the layout part and that will be of huge help for folks like me.

Thank you so much for your help,
Shree.

Andrejus Baranovskis said...

Thanks for idea - I will certainly write on this.

Andrejus