25 Feb 2014

Dynamic LOV binding

In this post I am going to show how we can create list-of-values binding dynamically and add it to the binding container at run time. In order to create a LOV binding I've got a couple of methods in my managed bean:
public JUCtrlListBinding getJobsLOV() {
    
    //Find listOfValues binding in the current binding container
    //May be it is already there
    DCControlBinding jobsLov = getBindings().findCtrlBinding("JobId");
    
    //Let's create listOfValues binding as we didn't find it 
    if (jobsLov == null) {
        jobsLov = createJobsLOV(); 
    }
    
    return (JUCtrlListBinding) jobsLov;
 }

private JUCtrlListBinding createJobsLOV() {
  //Create an instance of listOfValues binding definition
  //Actually lovDef is going to be an instance of FacesCtrlLOVDef
  DefinitionFactory defFactory = 
      JUMetaObjectManager.getJUMom().getControlDefFactory();
  JUCtrlValueDef lovDef = 
      (JUCtrlValueDef) defFactory.createControlDef(JUTags.PNAME_listOfValues); 

  //Initialize listOfValues binding definition
  HashMap initValues = new HashMap();
  initValues.put(DCControlBindingDef.PNAME_IterBinding, "VEmployeesIterator");
  initValues.put(ListBindingDef.PNAME_ListServerBindingName, "LOV_JobId");  
  initValues.put(ListBindingDef.PNAME_AttrNames, new String[] {"JobId"});
  initValues.put(JUTags.ID, "JobId");
  lovDef.init(initValues);
    
  //Create an instance of listOfValues binding
  JUCtrlListBinding lov = (JUCtrlListBinding) 
         lovDef.createControlBinding(getBindings());
  
  //Add the instance to the current binding container
  getBindings().addControlBinding(lovDef.getName(), lov);
  return lov;
}


And a corresponding LOV component looks like this:
    <af:inputComboboxListOfValues id="jobIdId"
       popupTitle="Search and Select: #{LovBean.jobsLOV.hints.label}"
       value="#{LovBean.jobsLOV.inputValue}"
       label="#{LovBean.jobsLOV.hints.label}"
       model="#{LovBean.jobsLOV.listOfValuesModel}"
       required="#{LovBean.jobsLOV.hints.mandatory}"
       columns="#{LovBean.jobsLOV.hints.displayWidth}"
       shortDesc="#{LovBean.jobsLOV.hints.tooltip}"/>
 
The sample application for this post requires JDeveloper R2.

That's it!