Tag Archives: af:query

Getting the value of Saved search – programatic way


QueryModel qm = (QueryModel)evaluateEL("#{bindings.SearchViewCriteriaQuery.queryModel}");
 for(int indexCount=0; indexCount
 {
 QueryDescriptor qd = qm.getUserQueries().get(indexCount);
 if(qd!=null){
 String queryDescriptorName = qd.getName();
 System.out.println(queryDesctiptorName);
 m = qd.getUIHints();
 isDefault = (Boolean) m.get(QueryDescriptor.UIHINT_DEFAULT);
 System.out.println(isDefault);
 isRunAutomatically = (Boolean) m.get(QueryDescriptor.UIHINT_AUTO_EXECUTE);
 System.out.println(isRunAutomatically);
 if (Boolean.TRUE.equals(isDefault) && Boolean.TRUE.equals(isRunAutomatically){
 break();
 }
 else{
 queryDescriptorName = null;
 }
 System.out.println(isDefault);
 }

How to hide the viewcriteria from the af:query panel


If you want to hide the view criteria, then here is the easy way
The code is written in the setter of the af:query binding

public void setQuery_binding(RichQuery query_binding) {

 this.query_binding = query_binding;

 DCBindingContainer bindings = (DCBindingContainer)this.getBindings();
 DCIteratorBinding iter =
 bindings.findIteratorBinding("<TableIterator>");

 ViewObjectImpl voimpl = (ViewObjectImpl)iter.getViewObject();
 ViewCriteria vc =  voimpl.getViewCriteriaManager().getViewCriteria("");
 vc.setProperty(ViewCriteriaHints.CRITERIA_SHOW_IN_LIST, ViewCriteriaHints.CRITERIA_HINT_FALSE);

 }
  • getting the handle to the iterator
  • getting handle to the ViewCriteriaManager, and then getting handle to the specific view criteria that we wanted to hide
  • modifying the ViewCriteriaHints.CRITERIA_SHOW_IN_LIST property to false

note: helper method – getBindings()

 private BindingContainer getBindings() {
 if (this.bindings == null) {
 FacesContext fc = FacesContext.getCurrentInstance();
 this.bindings =
 (BindingContainer)fc.getApplication().evaluateExpressionGet(fc,
 "#{bindings}",
 BindingContainer.class);
 }
 return this.bindings;
 }

Getting the value of Saved search in programatic way


This code can be added to the setter of the component from where you want to access the saved search of the af:query panel

QueryModel qm = (QueryModel)evaluateEL("#{bindings.<searchRegionFromThePageDef>.queryModel}");
for(int indexCount=0; indexCount<qm.getUserQueries().size();indexCount++)
{
 QueryDescriptor qd = qm.getUserQueries().get(indexCount);
 if(qd!=null){
 String queryDescriptorName = qd.getName();
 System.out.println(queryDesctiptorName);
 m = qd.getUIHints();
 isDefault = (Boolean) m.get(QueryDescriptor.UIHINT_DEFAULT);
 isRunAutomatically = (Boolean) m.get(QueryDescriptor.UIHINT_AUTO_EXECUTE);

 if (Boolean.TRUE.equals(isDefault) && Boolean.TRUE.equals(isRunAutomatically){
 //code goes here
 }
 else{
 //else code
 }
 }
}

quick walk through..

  • Getting the handle to QueryModel
  • Iterating through the userQueries. ie. the Saved Searches
  • Get handle to the QueryDescriptor
  • get handle to UIHints from the QueryDescriptor
  • check for QueryDescriptor.UIHINT_DEFAULT and QueryDescriptor.UIHINT_AUTO_EXECUTE and implement the logic based on the value obtained.

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