This discussion is archived
5 Replies Latest reply: Apr 27, 2011 8:59 PM by Neliel RSS

Access the Var Variable in a Collection Model

Neliel Newbie
Currently Being Moderated
Hi All,

Supposed I used an iterator that iterates over a collection model.
The var attribute which in this case is "row" is being used.
<af:iterator id="i1" value="#{bindings.Departments.collectionModel}"
             var="row">
    //call a managed bean method during iteration and used the var="row"???
</af:iterator>
Is there a way in a managed bean method where I could get a handle to this variable?
My goal is to influence the way the GUI is being drawn while the JSF iterator is iterating?

Thanks
  • 1. Re: Access the Var Variable in a Collection Model
    DimitrisStasinopoulos Pro
    Currently Being Moderated
    Hi,
    can you be more specific of you case?
    I believe you could make your changes through the iterator's current row.


    Regards,
    Dimitris.
  • 2. Re: Access the Var Variable in a Collection Model
    NavaneethaKrishnanNataraj Guru
    Currently Being Moderated
    Yes, you can access the var variable in the iterator in the managed bean.
    Below is the sample based on the Departments Table in HR Schema.

    IteratorSample.JSPX:

    <af:form id="f1">
    <af:iterator value="#{bindings.Departments.collectionModel}" var="row"
    rows="#{bindings.Departments.rangeSize}" id="t1">
    *<af:outputText value="#{IteratorSampleBean.departmentValue}" id="ot1"/>*
    <af:spacer height="5" id="s1"/>
    </af:iterator>
    </af:form>


    IteratorSamplePageDef.xml:

    <?xml version="1.0" encoding="UTF-8" ?>
    <pageDefinition xmlns="http://xmlns.oracle.com/adfm/uimodel"
    version="11.1.1.59.23" id="IteratorSamplePageDef"
    Package="view.pageDefs">
    <parameters/>
    <executables>
    <variableIterator id="variables"/>
    <iterator Binds="Departments" RangeSize="25"
    DataControl="AppModuleDataControl" id="DepartmentsIterator"/>
    </executables>
    <bindings>
    <tree IterBinding="DepartmentsIterator" id="Departments">
    <nodeDefinition DefName="model.views.DepartmentsVO">
    <AttrNames>
    <Item Value="DepartmentName"/>
    </AttrNames>
    </nodeDefinition>
    </tree>
    </bindings>
    </pageDefinition>

    IteratorSampleBean.java:

    import javax.el.ELContext;
    import javax.el.ExpressionFactory;
    import javax.el.ValueExpression;

    import javax.faces.context.FacesContext;

    import oracle.jbo.Row;
    import oracle.jbo.uicli.binding.JUCtrlHierNodeBinding;


    public class IteratorSampleBean {
    public IteratorSampleBean() {
    super();
    }

    private String departmentValue;

    public void setDepartmentValue(String departmentValue) {
    this.departmentValue = departmentValue;
    }

    *public String getDepartmentValue() {*
    JUCtrlHierNodeBinding node =
    *(JUCtrlHierNodeBinding)this.evaluateEL("#{row}");*
    *if (node != null) {*
    Row currentRow = node.getRow();
    *if (currentRow != null) {*
    departmentValue =
    *(String)currentRow.getAttribute("DepartmentName");*
    *}*
    *}*
    return departmentValue;
    *}*

    private Object evaluateEL(String el) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ELContext elContext = facesContext.getELContext();
    ExpressionFactory expressionFactory =
    facesContext.getApplication().getExpressionFactory();
    ValueExpression exp =
    expressionFactory.createValueExpression(elContext, el,
    Object.class);
    return exp.getValue(elContext);
    }
    }

    Thanks,
    Navaneeth
  • 3. Re: Access the Var Variable in a Collection Model
    Neliel Newbie
    Currently Being Moderated
    Hi Navaneeth,

    Your answer has got me closer to achieving what I want to do, I just have
    a follow up question if you may allow.

    Suppose the current row has a master detail relationship like below
    -row
         -children
    The children is a child collection of the current row. I tried to get it using this code but I am unable to do so.
    Can I have access also into that?
    public List getTabData() {
         JUCtrlHierNodeBinding node =
              (JUCtrlHierNodeBinding)this.evaluateEL("#{row}");
         if (node != null) {
              Row currentRow = node.getRow();
              if (currentRow != null) {
                   //this causes an error and wont evaluate
                   Object objChart = currentRow.getAttribute("children");
              }
         }
         return null;
    }
    I'd like to navigate also into that and gather all of its content also. Is this possible?
    I dont think getAttribute is the correct approach.

    Thanks

    Edited by: Marky on 27.4.2011 8:09
  • 4. Re: Access the Var Variable in a Collection Model
    NavaneethaKrishnanNataraj Guru
    Currently Being Moderated
    Hi,

    I am NOT clear about your use-case. Why do you want to do all this?

    But here is the sample, that can be used to achieve the same:
    The Sample assumes that you have Departments and Employees View Object Instances where Employees is the child of Departments.

    MultiIteratorPage.Jspx:

    <af:document id="d1">
    <af:messages id="m1"/>
    <af:form id="f1">
    *<af:iterator value="#{bindings.Departments.treeModel}" var="node"*
    id="tt1">
    *<af:outputText value="#{MultiIteratorSampleBean.departmentValue}"*
    id="ot2"/>
    *<af:spacer height="5" id="sp1"/>*
    *</af:iterator>*
    </af:form>
    </af:document>

    MultiIteratorPagePageDef.xml:

    <pageDefinition xmlns="http://xmlns.oracle.com/adfm/uimodel"
    version="11.1.1.59.23" id="MultiIteratorPagePageDef"
    Package="view.pageDefs">
    <parameters/>
    <executables>
    <variableIterator id="variables"/>
    <iterator Binds="Departments" RangeSize="25"
    DataControl="AppModuleDataControl" id="DepartmentsIterator"/>
    </executables>
    <bindings>
    <tree IterBinding="DepartmentsIterator" id="Departments">
    <nodeDefinition DefName="model.views.DepartmentsVO" Name="Departments0">
    <AttrNames>
    <Item Value="DepartmentId"/>
    <Item Value="DepartmentName"/>
    </AttrNames>
    <Accessors>
    <Item Value="EmployeesVO"/>
    </Accessors>
    </nodeDefinition>
    <nodeDefinition DefName="model.views.EmployeesVO" Name="Departments1">
    <AttrNames>
    <Item Value="EmployeeId"/>
    <Item Value="FirstName"/>
    <Item Value="LastName"/>
    </AttrNames>
    </nodeDefinition>
    </tree>
    </bindings>

    MultiIteratorPageBean.java:

    import java.util.List;

    import javax.el.ELContext;
    import javax.el.ExpressionFactory;
    import javax.el.ValueExpression;

    import javax.faces.context.FacesContext;

    import oracle.jbo.Row;
    import oracle.jbo.uicli.binding.JUCtrlHierNodeBinding;


    public class MultiIteratorSampleBean {
    public MultiIteratorSampleBean() {
    super();
    }

    private String departmentValue;

    public void setDepartmentValue(String departmentValue) {
    this.departmentValue = departmentValue;
    }

    *public String getDepartmentValue() {*
    JUCtrlHierNodeBinding rootNode =
    *(JUCtrlHierNodeBinding)this.evaluateEL("#{node}");*
    *if (rootNode != null) {*
    Row currentRow = rootNode.getRow();
    *if (currentRow != null) {*
    departmentValue =
    *(String)currentRow.getAttribute("DepartmentName");*
    *}*
    List<JUCtrlHierNodeBinding> children = rootNode.getChildren();
    *if (children != null) {*
    *for (JUCtrlHierNodeBinding node : children) {*
    *if (node != null) {*
    Row childCurrentRow = node.getRow();
    *if (childCurrentRow != null) {*
    System.out.println(childCurrentRow.getAttribute("FirstName"));
    *}*
    *}*
    *}*
    *}*
    *}*
    return departmentValue;
    *}*

    private Object evaluateEL(String el) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ELContext elContext = facesContext.getELContext();
    ExpressionFactory expressionFactory =
    facesContext.getApplication().getExpressionFactory();
    ValueExpression exp =
    expressionFactory.createValueExpression(elContext, el,
    Object.class);
    return exp.getValue(elContext);
    }
    }

    Thanks,
    Navaneeth
  • 5. Re: Access the Var Variable in a Collection Model
    Neliel Newbie
    Currently Being Moderated
    Hi Navaneeth,

    I got my code working now. Thanks for your help.

    I know that you are scratching your head on what I am doing so I will share with you my use case. ;)

    Here's my requirement.

    Display all the Departments in a Tab and each tab create a Graph that will show all the employees associated with that department
    including their salary.

    I did it with your help. Learned a lot this week.. Kudos.. :)

    Edited by: Marky on Apr 27, 2011 8:54 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points