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