How to refer to an Attribute in the Currently Selected Table row in an ADF Faces page – implementing a Generic Row Reader

5

A very small article on a very trivial issue. I have a page with a Master-Detail construction with a (master) Table of Departments and a (detail) Table of Employees. My challenge here: how can I display the name of the currently selected Department in the heading for the Employees table? Using ADF Bindings, this should be dead easy? And indeed: it is.

The steps:....

1. First identify the iterator used for the Departments table:

&lt;af:table value=&quot;#{bindings.DeptView1.collectionModel}&quot; var=&quot;row&quot;<br />

2. Then create the reference to the current row in this iterator and the required attribute in that row: 

... text=&quot;Employees in the selected Department #{bindings.DeptView1.currentRow.dname}&quot;&nbsp;

3. However,  the Row that is returned here is the generic ViewRowImpl class. That class has no getDname() method, only a generic getAttribute() method.

The most direct (though least generic) solution here is to generate the specific DeptViewRowImpl class, by checking the appropriate checkbox on the ViewObject wizard:

 

Alternatively, we can create our own AMISViewRowImpl that has getMap() method that returns an object that implements the Map interface and turns any call like view.map['attributeName'] or view.map.attributeName (these two are equivalent for Maps) into the appropriate getAttribute(‘attributeName’) call on the underlying Row instance.

Implementing a Generic RowReader

The alternative approach, that does not require us to generate the ViewRowImpl for every ViewObject, is using a generic RowReader. A Class that accepts a Row as input and returns the value of the requested attribute. I have quickly created such a generic set up, that allows declarative expressions like this one:

... text=&quot;Employees in the selected Department #{RowReader[bindings.DeptView1.currentRow].Dname}&quot;&nbsp;

to access any attribute value on any row object. The Row object is passed into the RowReader managed bean and then the desired attribute is indicated. Note that for attributes of type Number we have to specify the value property for proper display.

The required configuration for this generic RowReader is the following managed bean entry in the faces-config.xml file:

&lt;managed-bean&gt;<br />    &lt;managed-bean-name&gt;RowReader&lt;/managed-bean-name&gt;<br />    &lt;managed-bean-class&gt;nl.amis.adf.model.RowReaderHandler&lt;/managed-bean-class&gt;<br />    &lt;managed-bean-scope&gt;request&lt;/managed-bean-scope&gt;<br />&lt;/managed-bean&gt; <br />

And of course the RowReaderHandler and RowReader classes themselves:

package nl.amis.adf.model;<br /><br />import java.util.Collection;<br />import java.util.Map;<br />import java.util.Set;<br /><br />import oracle.jbo.Row;<br /><br />public class RowReaderHandler implements Map {<br />    public RowReaderHandler() {<br />    }<br /><br />    public Object get(Object key) {<br />        RowReader rr = new RowReader();<br />        rr.setRow((Row)key);<br />        return rr;<br />    }<br />... remainder of Map implementation<br /><p>}<br /><br /></p>
package nl.amis.adf.model;<br /><br />import java.util.Collection;<br />import java.util.Map;<br />import java.util.Set;<br /><br />import oracle.jbo.Row;<br /><br />public class RowReader implements Map {<br /><br />    private Row row;<br />    public RowReader() {<br />    }<br />    /**<br />     * @param key The Name of the Attribute whose value we want to see (key should be a String)<br />     * @return The value of the attribute specified through the key in the row set through setRow()<br />     */<br />    public Object get(Object key) {<br />        return row.getAttribute((String)key);<br />    }<br />    public void setRow(Row row) {<br />        this.row = row;<br />    }<br /><br />    public Row getRow() {<br />        return row;<br />    }<br /><br />... remainder of default Map implementation<br />}<br />&nbsp;

Note how the RowReaderHandler implements the Map interface, get’s setup with the Row instance – through the call from the EL expression to the get method – and then instantiates the real RowHandler, that also happens to implement the Map interface. The RowHandler is ‘injected’ with the Row. Any call on that RowReader object will read the requested value from the Row. 

 

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

5 Comments

  1. Luca,
    CanI have th source code for the Radio buttons and the ajaxing on the master detail
     
    Thanks
    Sagar

  2. Hi,
    I used the above soln “#bindings.EmployeeView1.currentRow.employeeId}” and was able to access the value.
    My doubt is that how “employeeId” got resolved to “EmployeeId”. ie, “E” changed to “e”
    Its very confusing to follow it. Is there convetion used how EL expression resolves the names.

    Regards
    Shiva

  3. Asim solution works great and very simple!
    #{bindings.DeptView1.currentRow.dataProvider.dname}
    thanks

  4. Asim Ghaffar SHAGHAB on

    trivial issues have trivial solutions :)

    text=”Employees in the selected Department #{bindings.DeptView1.currentRow.dname}” <– Wrong: as it is rowImpl
    text=”Employees in the selected Department #{bindings.DeptView1.currentRow.dataProvider.dname}” <– works like charm

  5. If “Dname” is an attribute binding in the current page’s binding container bound to the “Dname” attribute of the DeptViewIterator iterator binding, then you should also just be able to use the EL expression “#{bindings.Dname.inputValue}” to reference the value of the Dname of the current row in that iterator. In your example, you won’t already have that attribute value binding in your page def if you only have two ADF Faces tables (since they both use Table/Range bindings). You can create the attribute value binding by selecting the “bindings” folder in the Structure Window of the page definition and using “Insert Inside bindings > attributeValues” right mouse menu pick.