TreeTable with sub totals – how the SQL query can make life easier for the View

本文介绍如何使用ADF11g TreeTable组件展示复杂数据集,并通过单个视图对象实现部门和职位级别的汇总聚合。利用SQL查询和自连接实现数据层次结构,为用户提供清晰的数据概览。
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:


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) + aggregatingNote: 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


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>
Java是一种具备卓越性能与广泛平台适应性的高级程序设计语言,最初由Sun Microsystems(现属Oracle公司)的James Gosling及其团队于1995年正式发布。该语言在设计上追求简洁性、稳定性、可移植性以及并发处理能力,同时具备动态执行特性。其核心特征与显著优点可归纳如下: **平台无关性**:遵循“一次编写,随处运行”的理念,Java编写的程序能够在多种操作系统与硬件环境中执行,无需针对不同平台进行修改。这一特性主要依赖于Java虚拟机(JVM)的实现,JVM作为程序与底层系统之间的中间层,负责解释并执行编译后的字节码。 **面向对象范式**:Java全面贯彻面向对象的设计原则,提供对封装、继承、多态等机制的完整支持。这种设计方式有助于构建结构清晰、模块独立的代码,提升软件的可维护性与扩展性。 **并发编程支持**:语言层面集成了多线程处理能力,允许开发者构建能够同时执行多项任务的应用程序。这一特性尤其适用于需要高并发处理的场景,例如服务器端软件、网络服务及大规模分布式系统。 **自动内存管理**:通过内置的垃圾回收机制,Java运行时环境能够自动识别并释放不再使用的对象所占用的内存空间。这不仅降低了开发者在内存管理方面的工作负担,也有效减少了因手动管理内存可能引发的内存泄漏问题。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值