2012年7月19日星期四

ADF_161:如何传递参数给Dynamic Region?

开发环境:JDeveloper 11.1.2.2.0 + Oracle XE Database 10gR2。

使用Page Fragment的Bounded TaskFlow可以拖放到其它页面的某个Region,这是在设计时实现的,是静态的。
Dynamic Region特性可以在运行时决定使用哪个Bounded TaskFlow,是动态的。
在《Tree组件使用指南之四:点击不同的树节点显示不同的表单》中,我使用了switcher实现了显示不同的表单。
如果使用Dynamic Region实现同样的功能,该如何做呢?

重点步骤说明:

1. 设计并实现Employee Bounded TaskFlow
(1)点击Employee节点时,要传入EmployeeId,这里使用了SetCurrentRowWihtKeyValue操作

(2)设置SetCurrentRowWihtKeyValue操作的参数rowKey=#{pageFlowScope.employeeId}

(3)设置Employee Bounded TaskFlow的输入参数employeeId=#{pageFlowScope.employeeId}


2. 设计并实现Department Bounded TaskFlow
(1)点击Department节点时,要传入DepartmentId,这里使用了SetCurrentRowWihtKeyValue操作

(2)设置SetCurrentRowWihtKeyValue操作的参数rowKey=#{pageFlowScope.departmentId}

(3)设置Department Bounded TaskFlow的输入参数departmentId=#{pageFlowScope.departmentId}


3. 拖放DeparmentsView1生成Tree
具体设置请参考《Tree组件使用指南之四:点击不同的树节点显示不同的表单》。

4. 拖放Department Bounded TaskFlow到主页面,选择Dynamic Region
(1)会提示你创建一个Managed Bean,并在Managed Bean中创建一个dynamicTaskFlowId属性。
注意,Managed Bean的Scope最好设置成ViewScope,保证在当前页面不用重新实例化Managed Bean。
(2)选择该Region,切换到Bindings Tab,设置taskFlowId,将其绑定到Managed Bean中的dynamicTaskFlowId属性。
(3)设置Region的参数,因为不同的TaskFlow参数不同,我这里使用parameterMap来设置参数,将其绑定到。
Managed Bean中的parameters属性。

(4)定制TreeSelectionListener,并在点击不同的节点时,给parameters赋值。
完整的Managed Bean的代码如下:
package view;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import oracle.adf.controller.TaskFlowId;
import oracle.adf.view.rich.component.rich.data.RichTree;

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

import org.apache.myfaces.trinidad.event.SelectionEvent;
import org.apache.myfaces.trinidad.model.CollectionModel;
import org.apache.myfaces.trinidad.model.RowKeySet;

import view.util.JSFUtils;


public class MyBackingBean {
    private String departmentTaskFlowId = "/WEB-INF/department-btf-definition.xml#department-btf-definition";
    private String employeeTaskFlowId = "/WEB-INF/employee-btf-definition.xml#employee-btf-definition";
    private TaskFlowId dynamicTaskFlowId = TaskFlowId.parse(departmentTaskFlowId);
    private HashMap parameters = new HashMap();

    public MyBackingBean() {
    }

    public void treeSelectionListener(SelectionEvent selectionEvent) {
        JSFUtils.invokeMethodExpression("#{bindings.DepartmentsView1.treeModel.makeCurrent}", Object.class,
                                        SelectionEvent.class, selectionEvent);

        RichTree tree = (RichTree)selectionEvent.getSource();
        RowKeySet rowKeySet = selectionEvent.getAddedSet();
        Iterator rksIterator = rowKeySet.iterator();
        while (rksIterator.hasNext()) {
            List key = (List)rksIterator.next();
            JUCtrlHierBinding treeBinding = null;
            CollectionModel collectionModel = (CollectionModel)tree.getValue();
            treeBinding = (JUCtrlHierBinding)collectionModel.getWrappedData();
            JUCtrlHierNodeBinding nodeBinding = treeBinding.findNodeByKeyPath(key);

            Row rw = nodeBinding.getRow();
            String rowType = rw.getStructureDef().getDefName();
            System.out.println("########################### rowType " + rowType);

            JUCtrlHierTypeBinding typeBinding = nodeBinding.getHierTypeBinding();
            String nodeStuctureDefname = typeBinding.getStructureDefName();
            System.out.println("########################### nodeStuctureDefname " + nodeStuctureDefname);

            if (nodeStuctureDefname.equalsIgnoreCase("model.DepartmentsView")) {
                setDynamicTaskFlowId(TaskFlowId.parse(departmentTaskFlowId));
                parameters.clear();
                parameters.put("departmentId", rw.getAttribute("DepartmentId"));  
                System.out.println("########################### DepartmentId " + rw.getAttribute("DepartmentId"));
            } else {
                setDynamicTaskFlowId(TaskFlowId.parse(employeeTaskFlowId));
                parameters.clear();
                parameters.put("employeeId", rw.getAttribute("EmployeeId"));  
                System.out.println("########################### EmployeeId " + rw.getAttribute("EmployeeId"));
            }
        }
    }

    public void setDynamicTaskFlowId(TaskFlowId dynamicTaskFlowId) {
        this.dynamicTaskFlowId = dynamicTaskFlowId;
    }

    public TaskFlowId getDynamicTaskFlowId() {
        return dynamicTaskFlowId;
    }

    public void setParameters(HashMap parameters) {
        this.parameters = parameters;
    }

    public HashMap getParameters() {
        return parameters;
    }
}


5. 运行
效果和《Tree组件使用指南之四:点击不同的树节点显示不同的表单》一样,不过这里使用的技术是带参数的Dynamic Region。

Project 下载:ADF_Region_Dynamic.7z

1. http://www.baigzeeshan.com/2010/06/working-with-dynamic-regions-in-oracle.html
2. https://cn.forums.oracle.com/forums/thread.jspa?threadID=2270511

没有评论: