Tuesday, December 13, 2011

An EO validation rule that involves a transient attribute

Problem
Say you have a VO with a transient attribute (e.g., AmountSpent), and an entity-based attribute (e.g., MaxAmount).  The value of AmountSpent (the transient attribute) is set when the application starts.  Then, the user enters a value for MaxAmount (the entity-based attribute) in the UI.

Now, say you need to validate that MaxAmount is greater than AmountSpent.  The transient attribute does not exist in the EO, so a simple Compare validation rule is not possible.

How can you do this? 

Solution
First, create a method on the Application Module to return the value of the transient attribute:

  public Number getAmountSpent() {
    AmountVOImpl vo = this.getAmountVO1();
    AmountVORowImpl row = (AmountVORowImpl)vo.getCurrentRow();
    return row.getAmountSpent();
  }

Next, create a validation rule on the entity-based attribute (MaxAmount).  In this rule, compare the value of MaxAmount to a Groovy expression that calls the AM method. Here's the expression:

  source.getDBTransaction().getRootApplicationModule().getAmountSpent()

Note that you need to use "source" at the start of the Groovy expression. The reason for this is explained in the Fusion Dev Guide.

This rule will give you the validation you need.