Automatically return nested records from ADF List of Values – for example allocate all subordinates on project along with manager

1

In a previous post I was telling you about an ADF project we are discussing for a chain of interior decorating shops. One challenge we encountered in the functional specifications is that whenever a salesman is discussing a possible decoration – wallpaper, curtain, carpet etc. – for a room, he can select the (primary) material from a list of values and add it to the actions for that particular room. However, associated with such primary materials are auxillary materials – such as glue, nails, curtain rails etc. When the salesman selects a primary material from the List of Values to have it returned to the set linked to the room decorating action, the auxilliary materials should be added to the set as well.

Turning this to more familiar terms, we could state a similar requirement as follows: we have a master-detail page for Projects and Project Allocations. From the Project Allocations table, we can open a List of Values to select one or multiple Employees. Each employee can have a set of associated employees that should automatically be added to any project the primary employee is added to. As a simple example, let’s assume that whenever we allocate a manager on a project, all of her or his subordinates should automatically be allocated on the project as well. And of course we want to see this automatic allocation right away, without having to save the allocation to the database.

In this example, I have opened the Employee List of Values window for the current allocation for Elizabeth Bates. In the list of values window, I have selected Nancy Greenberg, the Finance Manager. I press the Select button. Nancy Greenberg is returned as selected value and the subordinates of Nancy are also added to the project:

 

As a result, not just Nancy is allocated but her five subordinates as well.

In this article I will describe how I have implemented this functionality, starting from an ADF Application generated by JHeadstart.....

Model 

The first challenge I addressed was the definition of the ‘associated values’. In this example, it is very easy to determine from the database using a simple query which records are associated with any particular employee:

select employee_id<br />,      first_name<br />,      last_name  <br />from   employees subs <br />where  subs.manager_id = :employeeId

I decided to create an attribute Subordinates of type (java.sql.)ResultSet in ViewObject EmployeesView, using the expression:

cursor( select employee_id<br />        ,      first_name<br />        ,      last_name  <br />        from   employees subs <br />        where  subs.manager_id = employees.employee_id<br />        ) subs<br /><br />&nbsp;

However,  this failed with ORA-22902: CURSOR expression not allowed, because of the way ADF BC wraps the Query statement and turns it into a subquery. As a workaround, I created a PL/SQL function:

 create or replace<br /> function get_subordinates<br /> ( p_employee_id in number<br /> ) return sys_refcursor <br /> is<br />     sub_refcur      SYS_REFCURSOR;<br /> BEGIN<br />     OPEN sub_refcur <br />     FOR  select employee_id<br />          ,      first_name<br />          ,      last_name  <br />          from   employees subs <br />          where  subs.manager_id = p_employee_id<br />          ;<br />     return sub_refcur;<br /> end; <br />

that returns the cursor expression as a SYS_REFCURSOR. The Expression for my VO Attribute Subordinates now becomes: get_subordinates(employee_id ).

 

ViewController

The ViewController project is largely generated using JHeadstart. There are three Groups: Projects, Project Allocations (Detail) and LovEmployees (for the List of Values window, allowing multiple selection of employees).  

Now the ‘magic’ starts. The class oracle.jheadstart.controller.jsf.bean.LovItemBean is the one in the JHeadstart runtime framework that takes care of returning the (multiple) selected records from a List of Values popup to the underlying table or form. In order to deal with additional nested records (attributes of type ResultSet tied to a Ref Cursor), I have to extend this class. The extended class is called ExtendedLovItem, and is in package oracle.jheadstart.controller.jsf.bean. Note: It is somewhat unfortunate that method createNewRow() in JhsCollectionModel has been defined as protected; this forces me to create the ExtendedLovItem class in the same package as JhsCollectionModel and its superclass, namely oracle.jheadstart.controller.jsf.bean.

In the ExtendedLovItem I have to override two methods. The first is copyMultiSelectReturnValues(). This method kicks in when the user has made a selection in a multiple select LOV window and has returned to the invoking page. The method copyMultiSelectReturnValues iterates over the selected (LOV) records and turns each one into a new row in the invoking table or form. We need a little intervention in this method: for each record selected in the LOV window, we want to verify whether it has any associated, nested records and if so add those to the invoking table or form as well. The first step is to add a line right after the call to copyReturnValues:

  // LJ: deal with nested records - any key/values of type ResultSet<br />  copyNestedReturnValues(rows[0]);<br />&nbsp;

This is a call to a new method that we will add right now:

    public void copyNestedReturnValues(Row lovRow) {<br />        Iterator keys = getReturnValues().keySet().iterator();<br />        while (keys.hasNext()) {<br />            String lovItem = (String)keys.next();<br />            Object value = lovRow.getAttribute(lovItem);<br />            if (value instanceof ResultSet) {<br />                processResultSet((ResultSet)value);<br />            }<br />        }<br />    }<br />&nbsp;

This method loops over the return values defined in the (Extended)LovItemBean. For every Attribute of type ResultSet it encounters, it will invoke processResultSet.

    private void processResultSet(ResultSet rs) {<br />        // Fetch each row from the result set<br />        ViewObject vo = <br />            getLovPage().getIterBinding().getRowSetIterator().getRowSet().getViewObject();<br />        try {<br />            while (rs.next()) {<br />                    String patternStr = &quot;,&quot;;<br />                    String[] mappings = nestedRecordAttributeMapping.split(patternStr);<br />                    Row newRow = getTableCollectionModel().createNewRow(true);<br />                    for (int i=0;i&lt;mappings.length;i++) {<br />                        System.out.println(mappings[i]);<br />                        String[] sourceTarget = mappings[i].split(&quot;=&quot;);<br />                        newRow.setAttribute(sourceTarget[1], rs.getObject(sourceTarget[0]));                                            <br />                    }<br />            }<br />        } catch (SQLException e) {<br />            // TODO<br />        }<br />        // We need to reset table component tree, because otherwise some of the items<br />        // set on the newly created row are not immediately visible in the page when<br />        // returning from the LOV (bug??)<br />        getTableCollectionModel().refresh();<br />        JsfUtils.getI
nstance().resetComponentTree(getTableCol
lectionModel().getTable());<br />    }<br />&nbsp;

Finally, we need to make sure that the method copyReturnValues() does not choke on attributes of type ResultSet, so we explicitly skip those, by including these lines directly following the declaration and value assignment of the value variable.

  Object value = lovRow != null ? lovRow.getAttribute(lovItem) : selectedRowData.get(lovItem); <br />  // new code to supported nested records          <br />  if (value instanceof ResultSet) {<br />    // do not process this key/item any further<br />    continue;<br />  }<br />&nbsp;

Now we have the runtime processing in place, provided that the proper managed bean definition is created; instead of referencing the regular LovItemBean, we now need ExtendedLovItemBean. And we need to add the Subordinates attributes to the list of attributes to return from the LOV. And we need to specify the mapping between the nested record attributes and the attributes in the new records. We do that in three steps:

  1. create a new custom template for the ExtendedLovItemBean
  2. associate that template to the ProjectAllocations group
  3. add the Subordinates attribute to the list of attributes to return from the LOV

1. The New Custom Template is called extendedLovItemInTableBean.vm and created in the directory ViewController\templates\custom. It has to modifications compared to the default version: it referenced the ExtendedLovItemBean class rather than the ‘plain’ LovItemBean and it has a new managed property nestedRecordAttributeMapping that specifies how values in the cursor records are mapped to the new ProjectAllocation records.

## revision_history<br />## 01-jul-2007   Lucas Jellema<br />## 1.2           Use ExtendedLovItemBean to process cursor with nested items, set mappings for Nested Subordinates<br />## 30-sep-2006   JHeadstart Team<br />## 1.1           Revision history added, base version is 10.1.3.0.97 (SU1)<br />#set ($bean = ${JHS.current.managedBean})<br />#set ($item = ${JHS.current.item})<br /><br />&lt;managed-bean&gt;<br />  &lt;managed-bean-name&gt;${bean.beanName}&lt;/managed-bean-name&gt;<br />  &lt;managed-bean-class&gt;oracle.jheadstart.controller.jsf.bean.ExtendedLovItemBean&lt;/managed-bean-class&gt;<br />  &lt;managed-bean-scope&gt;request&lt;/managed-bean-scope&gt;<br />  &lt;managed-property&gt;<br />    &lt;property-name&gt;lovFieldBinding&lt;/property-name&gt;<br />    &lt;value&gt;#{bindings.${item.bindingName}}&lt;/value&gt;<br />  &lt;/managed-property&gt;<br />  &lt;managed-property&gt;<br />     &lt;property-name&gt;tableCollectionModel&lt;/property-name&gt;<br />    &lt;value&gt;#{${JHS.current.group.name}CollectionModel}&lt;/value&gt;<br />  &lt;/managed-property&gt;<br />  &lt;managed-property&gt;<br />    &lt;property-name&gt;lovPage&lt;/property-name&gt;<br />    &lt;value&gt;#{${item.lovGroupName}Page}&lt;/value&gt;<br />  &lt;/managed-property&gt;        <br />  &lt;managed-property&gt;<br />    &lt;property-name&gt;lovSearchBean&lt;/property-name&gt;<br />    &lt;value&gt;#{search${item.lovGroupName}}&lt;/value&gt;<br />  &lt;/managed-property&gt;<br />  &lt;managed-property&gt;<br />    &lt;property-name&gt;returnValues&lt;/property-name&gt;<br />    &lt;map-entries&gt;<br />      #foreach ($returnValue in ${item.returnValues})<br />      &lt;map-entry&gt;<br />        &lt;key&gt;${returnValue.source}&lt;/key&gt;  <br />        &lt;value&gt;${JHS.current.group.name}${returnValue.target}&lt;/value&gt; <br />      &lt;/map-entry&gt;<br />      #end<br />    &lt;/map-entries&gt;<br />  &lt;/managed-property&gt;<br />&lt;managed-property&gt;<br />    &lt;property-name&gt;nestedRecordAttributeMapping&lt;/property-name&gt;<br />    &lt;value&gt;employee_id=EmpId,employee_id=Id&lt;/value&gt;<br />  &lt;/managed-property&gt;<br />  &lt;managed-property&gt;<br />    &lt;property-name&gt;copyToModel&lt;/property-name&gt;<br />    &lt;value&gt;true&lt;/value&gt;<br />  &lt;/managed-property&gt;<br />  &lt;managed-property&gt;<br />    &lt;property-name&gt;itemsToClear&lt;/property-name&gt;<br />    &lt;list-entries&gt;<br />      #foreach ($item in ${item.dependentItemsToClear})<br />        &lt;value&gt;${item.bindingName}&lt;/value&gt; <br />      #end<br />    &lt;/list-entries&gt;<br />  &lt;/managed-property&gt;<br />&lt;/managed-bean&gt; <br />

2. The template is associated to the ProjectAllocations group by setting the template property in the JHeadstart Application Editor:

 

3. add the Subordinates attribute to the list of attributes to return from the LOV

In the JHeadstart Application Definition editor, I add a new return item for the Subordinates source attribute. It is does not matter which target attribute I link it to, as it is not really mapped to a target attribute but instead results in new (target) records:

 

Resources

Download the JDeveloper 10.1.3.2 Application with the sources for the above described functionality:  LovReturnNestedRecords.zip.

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.

1 Comment

  1. JOSE ALEJANDRO on

    Please, I try to map de type java.sql.ResulSet but generate that the domain java.sql.Resulset not exist, The project has a oracle.jdbc libraries, please help my.