Ways to setup Querymode programatically


How to set the query mode for the af:query component programatically?

The query component provides two ways of searching. BASIC and ADVANCED mode.
Basic mode renders the query panel with the viewcriteria defined and allows the user to search based on the query criteria
Advanced mode helps the user to provide advanced options for the user to search based on the operators. It also provides the user to add more fields to the query criteria.

public Boolean setAdvancedMode(){
String expression = "#{bindings.<searchregionbindingfrompagedef>.queryDescriptor}";
FacesContext facesContext = getFacesContext();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExp =
elFactory.createValueExpression(elContext, expression,
Object.class);
QueryDescriptor descriptor =
(QueryDescriptor)valueExp.getValue(elContext)
;
descriptor.changeMode(QueryDescriptor.QueryMode.ADVANCED);
return true;
}

lets drill down deeply into the code to check what are we doing

String expression = "#{bindings.<searchregionbindingfrompagedef>.queryDescriptor}";
  • getting the expression for the query binding from the pagedef and storing it in a string
FacesContext facesContext = getFacesContext();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
  • getting the Facescontext reference from FacesContext.getCurrentInstance();
  • getting the application from the facesContext
  • getting the expresionFactory for the application
  • getting the elContext from the FacesContext
ValueExpression valueExp =
elFactory.createValueExpression(elContext, expression,
Object.class);

create a value expression by passing the elcontext and the expression where Object type is expected

QueryDescriptor descriptor =
(QueryDescriptor)valueExp.getValue(elContext);
descriptor.changeMode(QueryDescriptor.QueryMode.ADVANCED);

The value from the valueExpression object is casted to the QueryDescriptor object
As we got the reference to the QueryDescriptor, we call the changeMode method to change the mode to QueryDescriptor.ADVANCED

7 thoughts on “Ways to setup Querymode programatically

  1. soumik

    hi,
    I have a query regarding query panel created using view criteria.I need to show/hide query critria element depending upon another query criteria element’s value change event.As of now i don’t have access to that drop down element as UIComponent.so i can’t attach valuechange event anagrammatically to that UIcomponent.is there any way to achieve this?

    Reply
    1. vtkrishn Post author

      you can make use of queryOperationListener to handle this case..

      QueryOperationEvent is fired for operations performed on saved searches. The operations include changeMode, create, delete, reset, select and update. The specific operation that triggers this event is stored as part of the QueryOperationEvent object. The query component by default registers an internal QueryOperationListener and invokes specific methods on the QueryModel. Please refer to the QueryModel for more details.

      Reply
  2. Anonymous

    I havea question. How do we find the mode of the QueryDescriptor. The above code changes teh mode. I am looking to find out the mode from the QueryDescriptor.

    Reply
  3. Anonymous

    I have a search region. I would like to display the search region based on a taskflow parameter and set the query mode either advance or basic. I created a bean method and used your above logic. I added the method as methodAction binding and added an invokeaction with refresh=renderModel. I can see the code being executed and changed the querydescriptor to Advanced mode but the search region still show basic when rendered. What could be the issue?

    Reply
  4. harsh kumar

    Hi,

    I have one problem in af:query where I have created one view criteria and I am using this as a query component.The problem here is that the Match Type “Any” is not working every time it executes the query as “All” irrespective of the selection we made in Match Type.

    Can you please tell me what could be the possible problem and how to solve it.

    Thanks in advance
    Harsh

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s