When we work with View Link accessors or Association accessors we can use Groovy for aggregation calculations in a very convenient way. Groovy API provides five predefined functions:
  
1. Create an inner helper class in our ViewObjectImpl
2. Publish aggregation methods
3. Use the methods in jspx
That's it!
- accessor.sum("expr")
- accessor.count("expr")
- accessor.avg("expr")
- accessor.min("expr")
- accessor.max("expr")
1. Create an inner helper class in our ViewObjectImpl
   private class AgrFuncHelper extends HashMap
  {
    private String funcName;
    public AgrFuncHelper(String funcName) 
    {
      super();
      this.funcName = funcName;  
    }
    public Object get(Object key) 
    {
      //Invoke private method
      //of our DefaultRowSet (sum,count,avg,min,max)
      //key is argument expression for the aggr funcion being called
      //sum("Salary")
      return InvokerHelper.invokeMethod(getDefaultRowSet(), funcName, key);
    }
  }
 2. Publish aggregation methods
  public Map getSum() 
  {
    return new AgrFuncHelper("sum");
  }
  
  public Map getCount() 
  {
    return new AgrFuncHelper("count");
  }
 3. Use the methods in jspx
<af:outputText value="#{bindings.EmployeesView1Iterator.viewObject.sum['Salary']}"                        
   id="ot12"/>
<af:outputText value="#{bindings.EmployeesView1Iterator.viewObject.count['*']}" 
  id="ot13"/>
 
That's it!
 
 
Very good, works also for a solution with a filtered table!!! Thx Gregor
ReplyDeleteThis is genius - thank you. I am using this to publish a column in my table to display the percentage total for each row in the table. Do you happen to know how I can use your functionality in an ordinary bar graph where I want to have y-axis mapped as if it were my pseudo percentage column? Pareto seems to be the closest graph type but it has extra bells and whistles that the customer hates.
ReplyDeleteepic - thankyou! I second the genius comment!
ReplyDeleteExcelent!! Thanks!
ReplyDelete