A utility method to print the ViewCriteria returned by the search binding

A couple of years back I blogged about retrieving ViewCriteria(VC) from a custom queryListener method. This post in in continuation of the above said post. Recently while working on an example that deals with af:query component backed up by a VC, I noticed that it may also help developers to narrow down certain issues if they can print the ViewCrtieria returned by the search component. You can use the following code snippet in such scenarios. This may also help you to understand how framework construct ViewCriteria from af:query component. Here is the code snippet:

  
  //In managed bean - TestBean  
  //The customQueryListener(..) is the custom   
  //queryListener specified for the af:query  
  public void customQueryListener(QueryEvent queryEvent) {  
     //Read Query Descriptor  
     QueryDescriptor qd = queryEvent.getDescriptor();  
     DCBindingContainer bc = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();  
     /** The below call get ViewCriteria from the Search binding */  
     ViewCriteriaImpl vc = (ViewCriteriaImpl)getViewCriteria(bc, qd);  
     //Print VC   
     printVC(vc);  
     /** Manipulate ViewCriteria, if you want and  
      * then invoke query,optionally  
      * Note that, #{bindings.EmployeesViewCriteriaQuery.processQuery} is the serach   
      * binding EL generated by IDE when VC is dropped on to the page  
      */  
     invokeQueryEventMethodExpression("#{bindings.EmployeesViewCriteriaQuery.processQuery}", queryEvent);  
   }  

   //Prints VC  
   //This method will help you to   
   //understand how VC is created from the search binding(af:query)   

   public void printVC(ViewCriteria vc) {  
     List list = vc.getRows();  
     Iterator iter = list.iterator();  
     while (iter.hasNext()) {  
       ViewCriteriaRowImpl row = (ViewCriteriaRowImpl)iter.next();  
       ViewCriteriaItem[] vcitems = row.getCriteriaItemArray();  
       for (ViewCriteriaItem vcitem : vcitems) {  
         if (vcitem != null) {  
           System.out.println("[ VC Item :" + vcitem.getColumnName() + " Value: " + vcitem.getValue() + " ]");  
           if (vcitem.getValueCount() > 1) {  
             ArrayList<ViewCriteriaItemValue> values = vcitem.getValues();  
             for (ViewCriteriaItemValue vcItemValue : values)  
               System.out.println(" [[ Multi select value :" + vcItemValue.getValue() + " ]]");  
           } else if ((vcitem.getValue()) instanceof ViewCriteria) {  
             System.out.println("<Nested VC is found...>");  
             //Call recursively   
             printVC((ViewCriteria)vcitem.getValue());  
           }  
         }  
       }  
     }  
   }  
   /**  
    * Gets ViewCriteria used by the QueryModel  
    */  
   private ViewCriteria getViewCriteria(DCBindingContainer bc, QueryDescriptor qd) {  
     //EmployeesViewCriteriaQuery is the search binding name used in page def file  
     Object execBinding = bc.findExecutableBinding("EmployeesViewCriteriaQuery");  
     ViewCriteria vc = JUSearchBindingCustomizer.getViewCriteria((DCBindingContainer)execBinding, qd.getName());  
     return vc;  
   }  
   private void invokeQueryEventMethodExpression(String expression, QueryEvent queryEvent) {  
     FacesContext fctx = FacesContext.getCurrentInstance();  
     ELContext elctx = fctx.getELContext();  
     ExpressionFactory efactory = fctx.getApplication().getExpressionFactory();  
     MethodExpression me =  
       efactory.createMethodExpression(elctx, expression, Object.class, new Class[] { QueryEvent.class });  
     me.invoke(elctx, new Object[] { queryEvent });  
   }  

Download 

You can download the sample workspace from here. [Runs with Oracle JDeveloper 11g R2 11.1.2.3.0 + Oracle XE]
To test this sample, run the test.jsf. You may notice that View Criteria used by the search component is getting printed in the server console window. The printVC(ViewCriteria vc) method in TestBean class will help you to understand how View Criteria is constructed for an af:query component at run time. You can also try modifying the code snippet given in the managed bean (TestBean.java) to alter the View Criteria  before passing it to business component layer for actual query execution.

Comments

  1. Jobinesh, we need to refresh the LOV bind to the View criteria Item In Query. I see a method getAccessorVO(vo) , but for some reason its not working Iam not sure what the parameter vo is .. Do u have any ideas/suggestions..

    ReplyDelete

Post a Comment