Retrieving ViewCriteria from a custom queryListener method

You can leverage the built in functionalities of <af:quickQuery> or <af:query> component to build search functionalities for your screen. Sometimes you may need to intercept the query invocation using queryListener of the query component as shown below.

  <af:quickQuery label="Search" id="search"   
               value="#{bean.queryDescriptor}"   
               queryListener="#{someBean.processSomeQuery}">  
        <f:facet name="end">  
          <af:commandLink text="Advanced"/>  
        </f:facet>  
  </af:quickQuery>  

In today's post I'm sharing some code snippets which may help you to retrieve model part of the </af:quickQuery> component.

   
 public void processSomeQuery(QueryEvent queryEvent) {   
     
  QueryDescriptor qd = queryEvent.getDescriptor();   
  AttributeCriterion criterion = qd.getCurrentCriterion();  
 
  /** Gets the currently keyed in search criteria for quickQuery,
   *  You can do manipulate the values here :)
   */
  String attrLabel = criterion.getAttribute().getLabel();   
  Object attrValue = criterion.getValues().get(0);   

  DCBindingContainer bc =   
  (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();   
    
  /** The below call get ViewCriteria from the Search binding */  
  ViewCriteria vc = getViewCriteria(bc, qd);   
   
  /** Manipulate ViewCriteria, if you want and 
   *  then invoke query,optionally 
   */   
  invokeQueryEventMethodExpression("#{bindings.ImplicitViewCriteriaQuery.processQuery}",   
         queryEvent);   
  }   
  /**
   * Gets ViewCriteria used by the QueryModel
   */     
  private ViewCriteria getViewCriteria(DCBindingContainer bc,   
            QueryDescriptor qd) {   
              
   Object execBinding =   
     bc.findExecutableBinding("ImplicitViewCriteriaQuery");   
  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 });   
    
  }   



Learn More ...

There are a lot more points like this. If  you are curious to learn the internals of the ADF Business Components and ADF Binding Layer,  the following book is for you - Oracle ADF Real World Developer’s Guide.
More details about this book can be found in this post- http://jobinesh.blogspot.in/2012/07/pre-order-your-copy-of-oracle-adf-real.html

Comments

Hasim said…
If i want to revrieve multiple selected value from viewcriteria in multi select check box then how do i do. I am just gettin gone value.
Erik Piché said…
Thanks, very handy! :-)
Don Kleppinger said…
The invokeQueryEventMethodExpression method does not appear to work if the table is based on a POJO data control. Has to retrieve search binding and cast to internal oracle class to get it to work. ((FacesCtrlSearchBinding) execBinding).processQuery(queryEvent);
see
https://community.oracle.com/message/12910014#12910014

Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of my employer.