有时我们需要在代码中动态设置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
没有评论:
发表评论