Thursday, 18 April 2013

How to define view criteria programmatically

The following code snippet will help you how to create a View Criteria programmatic:
  ViewObjectImpl empVOImpl = getEmployeeView1();           
   ViewCriteria vc = empVOImpl.createViewCriteria();
   ViewCriteriaRow vcr = vc.createViewCriteriaRow();           

   //criteria for employee id
   ViewCriteriaItem vci1 = vcr.ensureCriteriaItem("JobId");
   vci1.setValue("SH_CLERK");

   //criteria for showing employees whose salary are more than 10000
   ViewCriteriaItem vci2 = vcr.ensureCriteriaItem("Salary");
   vci2.setOperator(">");
   vci2.setValue(new Number(10000));

   //criteria for department
   int[] ids = {50,100};
   ViewCriteriaItem vci3 = vcr.ensureCriteriaItem("DepartmentId");
   vci3.setOperator("IN");
   int i = 0;
   for(int deptId: ids){
     vci3.setValue(i++, new Number(deptId));
   }
   vc.addElement(vcr);
   empVOImpl.applyViewCriteria(vc);
   System.out.println("Query: " + empVOImpl.getQuery());
   empVOImpl.executeQuery();

No comments: