How-to tell the ViewCriteria a user chose in an af:query component

Posted by frank.nimphius on Oracle Blogs See other posts from Oracle Blogs or by frank.nimphius
Published on Thu, 16 Dec 2010 09:49:49 +0000 Indexed on 2010/12/16 21:12 UTC
Read the original article Hit count: 569

Filed under:
|

The af:query component defines a search form for application users to enter search conditions for a selected View Criteria. A View Criteria is a named where clauses that you can create declaratively on the ADF Business Component View Object.

A default View Criteria that allows users to search in all attributes exists by default and exposed in the Data Controls panel.

To create an ADF Faces search form, expand the View Object node that contains the View Criteria definition in the Data Controls panel. Drag the View Criteria that should be displayed as the default criteria onto the page and choose Query in the opened context menu. One of the options within the Query option is to create an ADF Query Panel with Table, which displays the result set in a table view, which can have additional column filters defined.

To intercept the user query for modification, or just to know about the selected View Criteria, you override the QueryListener property on the af:query component of the af:table component. Overriding the QueryListener on the table makes sense if the table allows users to further filter the result set using column filters.

To override the default QueryListener, copy the existing string referencing the binding layer to the clipboard and then select Edit from the field context menu (press the arrow icon to open it) to selecte or create a new managed bean and method to handle the query event.

 The code below is from a managed bean with custom query listener handlers defined for the af:query component and the af:table component. The default listener entry copied to the clipboard was "#{bindings.ImplicitViewCriteriaQuery.processQuery}"

 public void onQueryList(QueryEvent queryEvent) {
  // The generated QueryListener replaced by this method
  //#{bindings.ImplicitViewCriteriaQuery.processQuery}     
  QueryDescriptor qdes = queryEvent.getDescriptor();     
 
  //print or log selected View Criteria
  System.out.println("NAME "+qdes.getName());

       
  //call default Query Event     
  invokeQueryEventMethodExpression("
     #{bindings.ImplicitViewCriteriaQuery.processQuery}",queryEvent);

 }

public void onQueryTable(QueryEvent queryEvent) {
  // The generated QueryListener replaced by this method
  //#{bindings.ImplicitViewCriteriaQuery.processQuery}
  QueryDescriptor qdes = queryEvent.getDescriptor();

  //print or log selected View Criteria
  System.out.println("NAME "+qdes.getName());     
          
  invokeQueryEventMethodExpression(
    "#{bindings.ImplicitViewCriteriaQuery.processQuery}",queryEvent);
}

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});
}

Of course, this code also can be used as a starting point for other query manipulations and also works with saved custom criterias.

To read more about the af:query component, see:

http://download.oracle.com/docs/cd/E15523_01/apirefs.1111/e12419/tagdoc/af_query.html

© Oracle Blogs or respective owner

Related posts about ADFm

Related posts about ADFv