31 Mar 2014

Groovy Expressions in Custom Attribute Properties

Custom attributes properties is a handy ADF BC feature which is often used in order to add some extra information to the attribute definition. We can use custom attribute properties in our business logic and in the UI as well, referring to the custom properties as to the common attribute hints. These custom properties along with their values can be defined at either View definition level or Entity definition level. So the values of the custom properties are shared across all user sessions, as they are defined at the definition level. But it would be cool, if the values can be evaluated for each usage dynamically at run-time, depending on some context.  Actually, that's possible. We can use Groovy expressions in custom attribute properties.

There is no way to setup a Groovy expression for a custom property at design time, but we can do it easily at run-time.  Let's consider a simple VO for a flight booking form:

The Returndate attribute has a custom property Visible. At this point we don't bother about its value. Just because it can't be empty, its value is "Value".
What we need to do is to override the resolveDefObject() method in our custom ViewDef class and  setup a Groovy expression for the Visible property:

public class VFlightBookingDefImpl extends ViewDefImpl {

  public void resolveDefObject() {
    AttributeDefImpl attrDef =  (AttributeDefImpl) findAttributeDef("Returndate");          
    attrDef.setPropertyExpression("Visible", "Roundtrip == 'Y'");  

The Visible custom property can be used for defining the visible attribute of the inputDate ADF Faces component:
<af:inputDate value="#{bindings.Returndate.inputValue}"

So, the input component for the Returndate attribute is going to be visible only for a round trip flight.

The sample application for this post requires JDeveloper R2.

That's it!