Anil's Blog is Best Viewed on GOOGLE CHROME

Saturday, May 7, 2011

How to add ViewAttribute in ViewObject (VO) dynamically

We were having a requirement to add a column in an existing table region and client wants to show value in it based upon some calculation with other view attribute value.

At the first sight we thought of extending the ViewObject and adding a transient attribute.But that seems to be very complex process as this ViewObject is based on 5 EntityObjects.

So we thought of some workaround and come to a solution by adding a ViewAttribute in ViewObect dynamically.Here is the approach …

We have extended the Controller and add a ViewAttribute dynamically in the ViewObject and created a MessageStyledText bean through personalization.


public void processRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processRequest(pageContext, webBean);
    OAApplicationModule am =   (OAApplicationModule)pageContext.getApplicationModule(webBean);
OAViewObject vo = (OAViewObject)am.findViewObject("AllocationsVO1");
   if (vo != null)
   {
   vo.addDynamicAttribute("XXSalary"); //Adding ViewAttribute to VO
    }
     vo.reset();
     vo.next();

     //Starting do-while for setting value in all the table row...
     do
     {
      //Doing some calculation...  
      int xxnumber =  Integer.parseInt(vo.getCurrentRow().getAttribute("Empno").toString());      
      double d  = xxnumber*(17.5/100)/2;
      String aString = Double.toString(d);
      
      //Setting the calculated derived value in the newly created VO Attribute 
      vo.getCurrentRow().setAttribute("XXSalary", aString);
      vo.next();
     }
      while (vo.hasNext());
       vo.first();

//Finally adding ViewAttribute and ViewInstance with MessageTextInput bean which we have created through personalization 
  OAMessageTextInputBean mst = (OAMessageTextInputBean)webBean.findChildRecursive("Salary");
    mst.setViewUsageName("AllocationsVO1");
    mst.setViewAttributeName("XXSalary");
    mst.setReadOnly(true);

}

Thanks
--Anil