ADF 11g – TreeTable with sub totals – how the SQL query can make life easier for the View developer

The ADF 11g Tree Table component can be used for the compact presentation of fairly complex data sets. It allows the user to quickly drill down to a specific area of interest. For example to find all Roles within a specific Department and for any Role all Employees in that role.

On many occasions, it may be desired to show sub-totals at the various levels in the tree. For example in the way shown in this screenshot:

ADF 11g - TreeTable with sub totals - how the SQL query can make life easier for the View developer treetablesubtotals02

In this article we will see how we can achieve this fairly easily, by creating a ViewObject with a SQL query that does most of the heavy lifting.

This example is – again – based on EMP and DEPT (the age old SCOTT schema). Additionally, I have created this entire tree table using a single ViewObject. I now believe the result could have been achieved in a more straightforward fashion by using different ViewObjects for each level in the tree. However, what I did works and serves well as an example.

The steps:

1. Set Up: create JDeveloper 11g Fusion Web Application; create database connection to Scott Schema.

2. Create ViewObject to retrieve the dataset with Department and Job level Rollup Aggegation

3. Create self-referencing ViewLink to tie parent and children together

4. Add ViewObject usages to Application Module data model

5. Create new JSF page

6. Drag collection from Data Control Panel and drop as Tree Table

7. Extend the tree binding in the Page Definition for this page

8. Add some columns to the Tree Table; also add some styling to the table.

9. Run the page and see the results.

Let’s go over the more interesting steps in detail:

2. Create ViewObject to retrieve the dataset with Department and Job level Rollup Aggegation

The query uses the ROLLUP aggregation operator to add rows that the various grouping levels; The group by expression is

group
by     rollup( (d.deptno, d.dname) , job, (ename,empno))

which means subtotals are added for Job and Department and a Grand Total is added as well (aggregation over all records). Note: the inclusion of (ename, empno) in the ROLLUP expression means that we get all Employee records; since they are at the lowest level and empno is the primary, in all cases this level entails aggregating a single record.

The key to the solution is the creation of additional records for the various aggregation levels.We want to have the subtotal rows twice: once to include the Department node or the Job node within the Department, and a second time to include a Subtotal over the entire Department or all Employees in a certain Job. This is achieved through the join with an inline query that returns two records: one for the normal row and one for the aggregated row; that second row is only joined for rows that are aggregates (subtotals):

... all records from EMP complemented with subtotals at JOB and DEPT levels
join
( select 'Subtotal'  label
  ,      2 aggregating
  from   dual
  union
  select to_char(null)  label
  ,      0
  from   dual
) totaler
on (agg_deptno + agg_job + agg_ename>= totaler.aggregating or totaler.aggregating = 0)

I have added a condition to the where clause that uses a bind parameter. This will be used to return the first level of the tree. The ViewLink is used to access the detail nodes.

where nvl(:bind_treelevel, 3 - (agg_deptno + agg_job + agg_ename) + aggregating) = 3 - (agg_deptno + agg_job + agg_ename) + aggregating

Note: I am sure the SQL can be made more elegant and efficient. . In addition it was a little overly complex I believe to try to produce the entire tree structure with a single ViewObject. It is probably easier to make this work with separate ViewObjects for the different levels.

The entire query:

select case aggregating
       when 1
       then case (agg_deptno + agg_job + agg_ename)
            when 3
            then 'Grand Total'
            when 2
            then 'Department Total'
            else 'SubTotal (for Job)'
            end
       else nvl(ename, nvl(job, dname))
       end  node_label
,      deptno
,      dname
,      job
,      ename
,      salary node_value
,      salary_average
,      label
,      case aggregating when 1 then 'aggregate' else 'data' end node_type
,      3 - (agg_deptno + agg_job + agg_ename) + aggregating tree_level
,      deptno||';'||job||';'||empno||case aggregating when 1 then 'aggregate' end node_id
,      case agg_job when 1 then case aggregating when 1 then to_char(deptno) else '' end else to_char(deptno) end
       ||';'
       ||case agg_ename when 1 then case aggregating when 1 then job else '' end else job end
       ||';'
       parent_node_id
from (
select d.deptno
,      d.dname
,      job
,      ename
,      empno
,      grouping(d.deptno) agg_deptno
,      grouping(e.job) agg_job
,      grouping(e.ename) agg_ename
,      sum(sal) salary
,      avg(sal) salary_average
from   emp e
       right outer join
       dept d
       on (e.deptno = d.deptno)
group
by     rollup( (d.deptno, d.dname) , job, (ename,empno))
) hrm_agg
join
( select 'Subtotal'  label
  ,      2 aggregating -- every record is already an aggregation on agg_ename
  from   dual
  union
  select to_char(null)  label
  ,      0
  from   dual
) totaler
on (agg_deptno + agg_job + agg_ename>= totaler.aggregating or totaler.aggregating = 0)
where nvl(:bind_treelevel, 3 - (agg_deptno + agg_job + agg_ename) + aggregating) = 3 - (agg_deptno + agg_job + agg_ename) + aggregating

Note the support this SQL offers the to the View developer that wants to leverage it. It specifies the node type (data or aggregate), returns a pretty aggregation node label, provides a generic NodeId and ParentNodeId reference, and is altogether helpful to its consumers.

3. Create self-referencing ViewLink to tie parent and children together

The ViewLink is from the ViewObject and to the ViewObject. It is based on the NodeId attribute in the Source and the ParentNodeId attribute in the Destination.

Its join-condition:

parent_node_id = :Bind_NodeId

4. Add ViewObject usages to Application Module data model

ADF 11g - TreeTable with sub totals - how the SQL query can make life easier for the View developer treetablesubtotals03

7. Extend the tree binding in the Page Definition for this page

The self referencing ViewLink for some reason is not interpreted correctly by the tree binding editor – I can not successfully add tree rules. Perhaps I do not even need them? Anyways, I end up with the following tree binding definition:

  <bindings>
    <tree IterBinding="MasterTotalingView1Iterator" id="MasterTotalingView1">
      <nodeDefinition DefName="model.MasterTotalingView">
        <AttrNames>
          <Item Value="NodeLabel"/>
          <Item Value="NodeType"/>
          <Item Value="NodeValue"/>
          <Item Value="Deptno"/>
          <Item Value="TreeLevel"/>
          <Item Value="SalaryAverage"/>
          <Item Value="Job"/>
        </AttrNames>
        <Accessors>
          <Item Value="TreeLevelMasterTotalingView"></Item>
        </Accessors>
      </nodeDefinition>
      <nodeDefinition DefName="model.MasterTotalingView">
        <AttrNames>
          <Item Value="NodeLabel"/>
          <Item Value="NodeType"/>
          <Item Value="NodeValue"/>
          <Item Value="Deptno"/>
          <Item Value="TreeLevel"/>
          <Item Value="SalaryAverage"/>
          <Item Value="Job"/>
        </AttrNames>
      </nodeDefinition>
    </tree>

I made some additional changes in the Page Definition: I want the ViewObject to be initially queried for the level 1 nodes (with bind parameter bind_treelevel equals 1). This is achieved with an ActionBinding for the ExecuteWithParams that is invoked through an InvokeAction:

<?xml version="1.0" encoding="UTF-8" ?>
<pageDefinition xmlns="http://xmlns.oracle.com/adfm/uimodel"
                version="11.1.1.51.56" id="HrmTreeTablePageDef"
                Package="view.pageDefs">
  <parameters/>
  <executables>
...
    <invokeAction Binds="ExecuteWithParams" id="initializeHrmTree"
Refresh="ifNeeded"/>
</executables> <bindings> ... <action IterBinding="MasterTotalingView1Iterator" id="ExecuteWithParams"
RequiresUpdateModel="true" Action="executeWithParams">
<NamedData NDName="bind_treelevel" NDType="java.lang.String"
NDValue="${1}"/>
</action>
</bindings> </pageDefinition>

Note the value of 1 being passed to the bind_treelevel bind parameter.

8. Add some columns to the Tree Table; also add some styling to the table.

        <af:treeTable value="#{bindings.MasterTotalingView1.treeModel}"
                      var="node"
                      selectionListener="#{bindings.MasterTotalingView1.treeModel.makeCurrent}"
                      rowSelection="single" id="treeTable1">
          <f:facet name="nodeStamp">
            <af:column align="#{node.NodeType=='aggregate'?'right':'left'}"
                       inlineStyle="#{node.NodeType=='aggregate'?'font-weight:bold;':''}">
              <af:outputText value="#{node.NodeLabel}"/>
            </af:column>
          </f:facet>
          <f:facet name="pathStamp">
            <af:outputText value="#{node}"/>
          </f:facet>
          <af:column>
            <f:facet name="header">
              <af:outputText value="Department"/>
            </f:facet>
            <af:outputText value="#{node.Deptno}"/>
          </af:column>
          <af:column>
            <f:facet name="header">
              <af:outputText value="Job"/>
            </f:facet>
            <af:outputText value="#{node.Job}"/>
          </af:column>
          <af:column inlineStyle="#{node.NodeType=='aggregate'?'font-weight:bold;':''}; text-align:right;">
            <f:facet name="header">
              <af:outputText value="Salary Average"/>
            </f:facet>
            <af:outputText value="#{node.SalaryAverage}"/>
          </af:column>
          <af:column inlineStyle="#{node.NodeType=='aggregate'?'font-weight:bold;':''}; text-align:right;">
            <f:facet name="header">
              <af:outputText value="Salary"/>
            </f:facet>
            <af:outputText value="#{node.NodeValue}"/>
          </af:column>
        </af:treeTable>
 

ADF 11g - TreeTable with sub totals - how the SQL query can make life easier for the View developer treetablesubtotals02

Resources

Download JDeveloper 11g Application: to be provided

11 Comments

  1. Srinivas February 23, 2010
  2. Priya December 4, 2009
  3. Stefan October 27, 2009
  4. US August 26, 2009
  5. Lucas Jellema August 1, 2009
  6. DS July 28, 2009
  7. Haany Boyke May 19, 2009
  8. James L April 9, 2009
  9. Lucas Jellema December 1, 2008
  10. Rob van Wijk December 1, 2008