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

10 comments:

  1. Hi Anil,
    Thanks for such a wonderful article. I used this code today. First time the output comes properly (data shows up in messageStyleText). from second time it throws an error "oracle.jbo.NameClashException: JBO-25001: Name ExpAcct of object type Attribute already exists". Any idea why it comes, please let me know.

    ReplyDelete
  2. Please Help me Even I am also facing the same problem

    ReplyDelete
  3. Hi,

    In above IF condition i.e. if (vo != null), handle the exception by using try ..catch block as shown below:

    try
    {
    String l_att= vo.findAttributeDef("XXSalary").toString();
    }
    catch(Exception exception)
    {
    vo.addDynamicAttribute("XXSalary"); //Adding ViewAttribute to VO
    }

    - L.Nishanth, Apps Associates

    ReplyDelete
  4. Thanks Nishanth for your input. Yes, I mistakenly forget to check that while adding dynamic attribute we have to check its existence first.

    Thanks
    --Anil

    ReplyDelete
  5. Here is what dev guide says about it:

    // Check whether the attribute with the same name exists or not.
    // Note that for attributes, we need to use try-catch clause.
    If (vo.lookupAttributeDef("MyAttr") == null)
    {
    vo.addDynamicAttribute("MyAttr");
    }

    ReplyDelete
  6. Hi Anil,

    I used your code to check the existence of attribute.

    If (vo.lookupAttributeDef("MyAttr") == null)
    {
    vo.addDynamicAttribute("MyAttr");
    }

    In my case It is throwing OA exception.

    When I tried with Nishanth's solution it is working fine.

    Thank you,
    Keshav

    ReplyDelete
  7. Keshav,

    Please read my reply properly you have to use this in Try catch block.....

    ReplyDelete
  8. great addition in my knowledge Anil

    ReplyDelete
  9. really it is a nice example |

    ReplyDelete
  10. Hi Anil it's very good post so informative.

    If possible I need your help,
    Scenario comes under OAF Extensions.
    user would enter data on 2 fields on say page x and I need to display that data on say page z so for that I need to create those fields. But then how to handle VO ??
    How to achieve this task ??

    you can mail me at sudhirbhilar@gmail.com

    Please give your valuable wordings

    ReplyDelete

Note: Only a member of this blog may post a comment.