Thursday, December 29, 2011

Groovy with View Link Accessor

My last post this year, I'm closing 2011 with #106 posts and will resume early 2012 (already next week).

This post is based on question logged on our public Wiki (Red Samurai Ora-Bug Tracker) - reference attributes in other view objects using groovy. Developer was asking how to access attributes from different View Objects using Groovy. Answer - attributes can be referenced using Groovy by View Link Accessor name, similar as we can reference in Entity Object by Association name. Pros and cons are discussed as well, and why I prefer standard View Object join.

Download sample application - In order to reference another View Object attribute through View Link Accessor, first we need to check if View Link Accessor is exposed in View Object. In this example I would like to reference City attribute from Locations, in Departments View Object. As we can see, View Link Accessor is not exposed yet in Departments:

Let's edit accessors and select to be exposed in Departments, as LocationsView name:

View Link Accessor is exposed in Departments and can be referenced through Groovy:

Add new transient attribute in Departments View Object - LocationCity:

Set default value as Expression and type such Groovy statement - LocationsView.City:

This will access referenced View Object (Locations) on runtime. You should be aware, ADF will execute two SQL statements to render Departments data (instead of one by default). Second executed statement will be generated to populate Groovy script expression:

I would recommend better to use standard View Object join, based on Association - it will retrieve same value directly using SQL join, without forcing to execute second SQL statement from Groovy.

Open Entity Objects section and define SQL join based on Entity Object association:

Add new attribute from joined Entity - City attribute:

Referenced attribute will retrieved through SQL join:

I wish you great start in 2012 !

No comments: