使用ADF-BC 实现查询功能之八:通过代码动态设置Criteria条件参数

187 篇文章 2 订阅
开发环境:JDevloper 11.1.2.1.0+ Oracle Database 10g Express Edition 10.2.0.1。

有时我们需要在代码中动态设置View Criteria上绑定的参数值。
更有一种情况是:动态为VO增加View Criteria并设置参数值。
以下我就以《 使用ADF-BC 实现查询功能之七:使用View Criteria 》的项目为基础,说明如何实现动态查询。

1. 查找已有的View Criteria,并设置参数值。
        String amDef = "model.AppModule";
        String amConfig = "AppModuleLocal";
        ApplicationModule am = Configuration.createRootApplicationModule(amDef, amConfig);
        ViewObject vo = am.findViewObject("EmployeesView1");

        ViewCriteriaManager vcm = vo.getViewCriteriaManager();
        ViewCriteria vc = vcm.getViewCriteria("EmpByEmailCriteria");
        VariableValueManager vvm = vc.ensureVariableManager();
        vvm.setVariableValue("bv_email", "M");
        vo.executeQuery();
        while (vo.hasNext()) {
            Row emp = vo.next();
            System.out.println(emp.getAttribute("EmployeeId") + " " + emp.getAttribute("FirstName") + " " +
                               emp.getAttribute("LastName") + " " + emp.getAttribute("Email") + " " +
                               emp.getAttribute("DepartmentId"));
        }
        Configuration.releaseRootApplicationModule(am, true);


2. 创建新的View Criteria
你可以创建多个ViewCriteriaRow,每个vr对象的条件之间的关系是“AND”,多个vr对象之间的关系是“OR”。
        String amDef = "model.AppModule";
        String amConfig = "AppModuleLocal";
        ApplicationModule am = Configuration.createRootApplicationModule(amDef, amConfig);
        ViewObject vo = am.findViewObject("EmployeesView1");

        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow vcr1 = vc.createViewCriteriaRow();
        ViewCriteriaRow vcr2 = vc.createViewCriteriaRow();
        vcr1.setAttribute("EmployeeId", "> 100");
        vcr1.setAttribute("Email", "M%");
        vcr2.setAttribute("DepartmentId", "IN (20,50)");
        vcr2.setAttribute("Email", "S%");
        vc.add(vcr1);
        vc.add(vcr2);
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        while (vo.hasNext()) {
            Row emp = vo.next();
            System.out.println(emp.getAttribute("EmployeeId") + " " + emp.getAttribute("FirstName") + " " +
                               emp.getAttribute("LastName") + " " + emp.getAttribute("Email") + " " +
                               emp.getAttribute("DepartmentId"));
        }
        Configuration.releaseRootApplicationModule(am, true);


3. 创建新的View Criteria(使用findByViewCriteria)
你可以创建多个ViewCriteriaRow,每个vr对象的条件之间的关系是“AND”,多个vr对象之间的关系是“OR”。
        String amDef = "model.AppModule";
        String amConfig = "AppModuleLocal";
        ApplicationModule am = Configuration.createRootApplicationModule(amDef, amConfig);
        ViewObject vo = am.findViewObject("EmployeesView1");

        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow vcr1 = vc.createViewCriteriaRow();
        ViewCriteriaRow vcr2 = vc.createViewCriteriaRow();
        vcr1.setAttribute("EmployeeId", "> 100");
        vcr1.setAttribute("Email", "M%");
        vcr2.setAttribute("DepartmentId", "IN (20,50)");
        vcr2.setAttribute("Email", "S%");
        vc.add(vcr1);
        vc.add(vcr2);
        vo.applyViewCriteria(vc);

        RowIterator rowItr = vo.findByViewCriteria(vc, -1, ViewObject.QUERY_MODE_SCAN_DATABASE_TABLES);      
        while (rowItr.hasNext()) {
            Row emp = rowItr.next();
            System.out.println(emp.getAttribute("EmployeeId") + " " + emp.getAttribute("FirstName") + " " +
                               emp.getAttribute("LastName") + " " + emp.getAttribute("Email") + " " +
                               emp.getAttribute("DepartmentId"));
        }
        Configuration.releaseRootApplicationModule(am, true);


4. 完整的代码和运行结果如下:
package model;

import oracle.jbo.ApplicationModule;
import oracle.jbo.Row;
import oracle.jbo.RowIterator;
import oracle.jbo.VariableValueManager;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaManager;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;
import oracle.jbo.client.Configuration;

public class TestViewCriteriaClient {
    public TestViewCriteriaClient() {
        super();
    }

    public static void testCase1() {
        String amDef = "model.AppModule";
        String amConfig = "AppModuleLocal";
        ApplicationModule am = Configuration.createRootApplicationModule(amDef, amConfig);
        ViewObject vo = am.findViewObject("EmployeesView1");

        ViewCriteriaManager vcm = vo.getViewCriteriaManager();
        ViewCriteria vc = vcm.getViewCriteria("EmpByEmailCriteria");
        VariableValueManager vvm = vc.ensureVariableManager();
        vvm.setVariableValue("bv_email", "M");
        vo.executeQuery();
        while (vo.hasNext()) {
            Row emp = vo.next();
            System.out.println(emp.getAttribute("EmployeeId") + " " + emp.getAttribute("FirstName") + " " +
                               emp.getAttribute("LastName") + " " + emp.getAttribute("Email") + " " +
                               emp.getAttribute("DepartmentId"));
        }
        Configuration.releaseRootApplicationModule(am, true);
    }

    public static void testCase2() {
        String amDef = "model.AppModule";
        String amConfig = "AppModuleLocal";
        ApplicationModule am = Configuration.createRootApplicationModule(amDef, amConfig);
        ViewObject vo = am.findViewObject("EmployeesView1");

        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow vcr1 = vc.createViewCriteriaRow();
        ViewCriteriaRow vcr2 = vc.createViewCriteriaRow();
        vcr1.setAttribute("EmployeeId", "> 100");
        vcr1.setAttribute("Email", "M%");
        vcr2.setAttribute("DepartmentId", "IN (20,50)");
        vcr2.setAttribute("Email", "S%");
        vc.add(vcr1);
        vc.add(vcr2);
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        while (vo.hasNext()) {
            Row emp = vo.next();
            System.out.println(emp.getAttribute("EmployeeId") + " " + emp.getAttribute("FirstName") + " " +
                               emp.getAttribute("LastName") + " " + emp.getAttribute("Email") + " " +
                               emp.getAttribute("DepartmentId"));
        }
        Configuration.releaseRootApplicationModule(am, true);
    }

    public static void testCase3() {
        String amDef = "model.AppModule";
        String amConfig = "AppModuleLocal";
        ApplicationModule am = Configuration.createRootApplicationModule(amDef, amConfig);
        ViewObject vo = am.findViewObject("EmployeesView1");

        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow vcr1 = vc.createViewCriteriaRow();
        ViewCriteriaRow vcr2 = vc.createViewCriteriaRow();
        vcr1.setAttribute("EmployeeId", "> 100");
        vcr1.setAttribute("Email", "M%");
        vcr2.setAttribute("DepartmentId", "IN (20,50)");
        vcr2.setAttribute("Email", "S%");
        vc.add(vcr1);
        vc.add(vcr2);
        vo.applyViewCriteria(vc);

        RowIterator rowItr = vo.findByViewCriteria(vc, -1, ViewObject.QUERY_MODE_SCAN_DATABASE_TABLES);
        //        Row emp = rowItr.first();
        //        System.out.println(emp.getAttribute("EmployeeId") + " " + emp.getAttribute("FirstName") + " " +
        //                           emp.getAttribute("LastName") + " " + emp.getAttribute("Email") + " " +
        //                           emp.getAttribute("DepartmentId"));
        while (rowItr.hasNext()) {
            Row emp = rowItr.next();
            System.out.println(emp.getAttribute("EmployeeId") + " " + emp.getAttribute("FirstName") + " " +
                               emp.getAttribute("LastName") + " " + emp.getAttribute("Email") + " " +
                               emp.getAttribute("DepartmentId"));
        }
        Configuration.releaseRootApplicationModule(am, true);
    }

    public static void main(String[] args) {
        System.out.println("-----------Test Case 1---------------");
        testCase1();
        System.out.println("-----------Test Case 2---------------");
        testCase2();
        System.out.println("-----------Test Case 3---------------");
        testCase3();
    }
}

输出如下:
-----------Test Case 1---------------
2012-3-3 23:32:00 oracle.adf.share.ADFContext getCurrent
警告: 自动初始化 DefaultContext 以执行 getCurrent。
调用方应确保 DefaultContext 适合此用法。
如果未正确执行自动初始化, 则可能会出现内存泄露和/或意外行为。
在使用 getCurrent() 之前执行 initADFContext 可避免此消息。
有关详细信息, 请在 FINEST 级别对 oracle.adf.share.ADFContext 启用日志记录。
120 Matthew Weiss MWEISS 50
130 Mozhe Atkinson MATKINSO 50
134 Michael Rogers MROGERS 50
164 Mattea Marvins MMARVINS 80
182 Martha Sullivan MSULLIVA 50
201 Michael Hartstein MHARTSTE 20
-----------Test Case 2---------------
120 Matthew Weiss MWEISS 50
123 Shanta Vollman SVOLLMAN 50
128 Steven Markle SMARKLE 50
130 Mozhe Atkinson MATKINSO 50
134 Michael Rogers MROGERS 50
138 Stephen Stiles SSTILES 50
164 Mattea Marvins MMARVINS 80
182 Martha Sullivan MSULLIVA 50
192 Sarah Bell SBELL 50
194 Samuel McCain SMCCAIN 50
201 Michael Hartstein MHARTSTE 20
-----------Test Case 3---------------
120 Matthew Weiss MWEISS 50
123 Shanta Vollman SVOLLMAN 50
128 Steven Markle SMARKLE 50
130 Mozhe Atkinson MATKINSO 50
134 Michael Rogers MROGERS 50
138 Stephen Stiles SSTILES 50
164 Mattea Marvins MMARVINS 80
182 Martha Sullivan MSULLIVA 50
192 Sarah Bell SBELL 50
194 Samuel McCain SMCCAIN 50
201 Michael Hartstein MHARTSTE 20
Process exited with exit code 0.

Project 下载: ADF_Query_Criteria(2).7z

参考文献:
1. http://jobinesh.blogspot.com/2010/10/creating-view-criteria-having-bind.html

2. http://docs.oracle.com/cd/E15523_01/web.1111/b31974/bcadvvo.htm#BCGFHAGA

http://maping930883.blogspot.com/2010/04/adf068adf-bc-criteria.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值