Tag Archives: snippets

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.

Explicit validation for af:input components


Here is a simple way to get around with the red box validation that used to appear on af:input Components upon some validation

<af:inputText value="#{row.bindings.<attributeValue>.inputValue}"
 label="#{bindings.<tableBinding>.hints.<attributeValue>.label}"
 required="#{bindings.<tableBinding>.hints.<attributeValue>.mandatory}"
 columns="#{bindings.<tableBinding>.hints.<attributeValue>.displayWidth}"
 maximumLength="#{bindings.<tableBinding>.hints.<attributeValue>.precision}"
 shortDesc="#{bindings.<tableBinding>.hints.<attributeValue>.tooltip}"
 id="it3" validate="#{MyBean.validate}">

and in the MyBean class we have it like

public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) {
        FacesMessage message = new FacesMessage();
        message.setDetail("Some Errror message");
        message.setSummary("Value Error message");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(message);
}

what are we doing here..

  • Getting the handle to the FacesMessage by explicitly instantiating it
  • Set the Detail,Summary and Severity for the message
  • Throwing the message as a ValidatorException

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