Tuesday, June 5, 2012

A smart method to access bindings from Java code in ADF

Say we have a jspx page bound to a ViewObject. Also there’s a backing bean for this page. What we want is to access from the backing bean a binding defined in the page definition.

First, the managed bean declaration in faces-config.xml or in your taskFlow must include a managed-property element.
    <managed-bean>
       <managed-bean-name>testBean</managed-bean-name>
       <managed-bean-class>com.datamoil.view.backing.Test</managed-bean-class>
       <managed-bean-scope>request</managed-bean-scope>
       <managed-property>
          <property-name>bindings</property-name>
          <value>#{bindings}</value>
       </managed-property>
    </managed-bean>
Second, it’s only left to create a field in the bean Java class.
//the field itself
oracle.binding.BindingContainer bindings; 
//and its accessors
public void setBindings(oracle.binding.BindingContainer bindings) {
     this.bindings = bindings;
}
public oracle.binding.BindingContainer getBindings() {
     return bindings;
}

Amazingly simple as well. That’s pretty much it. Now things from the page definition can be accessed using Java code.
To e.g. get an Iterator called TestView1Iterator the code would be
oracle.jbo.uicli.binding.JUIteratorBinding testViewIter = (oracle.jbo.uicli.binding.JUIteratorBinding) this.bindings.get("TestView1Iterator");
or to get an Action called TestAction
oracle.adfinternal.view.faces.model.binding.FacesCtrlActionBinding testAction = (oracle.adfinternal.view.faces.model.binding.FacesCtrlActionBinding) this.bindings.get("TestAction");
Wonderful isn’t it? Just keep in mind that a binding is always looked up by its name/id, meaning there’s no guarantee one will be found. So in case there’s nothing under that name in the page definition, a NullPointerException will be thrown.