ADF DataBinding: Yet another thing it does for you.

Today I encountered something that I have seen frequently. While developing an ADF application developers tend to invoke methods on an application module directly, instead of invoking them via the bindings framework. You might get some unexpected results and some additional work too…ADF DataBinding: Yet another thing it does for you. internalservererror1

Correcting this behavior afterwards can be very annoying and time-consuming. In this blog, just to let developers know, I describe what you could do to prevent this error.One of the things the ADF Bindingframework does for you is catch exceptions and show them in a message popup. Let’s see what happens when we build an example.

First create default BC for the EMPLOYEE table in the HR schema. The employee table has (at least it should have) a check constraint that makes sure that whatever happens, an employee at least gets one cent every month (salary > 0).

Next create an Application Module Class and in this class create a method to do salary corrections.

    public void salaryCorrection(Number quantity){
        ViewObject _empVO = this.getEmployeesView1();        
        Row _currentRow = _empVO.getCurrentRow();               
        oracle.jbo.domain.Number _salary = (Number)_currentRow.getAttribute("Salary");                 
        _salary = _salary.add(quantity);                 
        _currentRow.setAttribute("Salary", _salary);                
        getDBTransaction().commit();               
        return;            
        }

Now Publish this method to the client interface. Create a new Page called salaryCorrections.jspx. Create an ADF form based on the employeesView1 collection and make sure to include navigation and submit buttons. Make sure the page works as expected. Drop the salaryCorrection Method from the datacontrol on your page as a ParameterForm.

ADF DataBinding: Yet another thing it does for you. parameterform1

Run the page and see what happens if you make a correction that results in a negative salary…….

ADF DataBinding: Yet another thing it does for you. viabindingserror

You get a popup with a somewhat technical error message that explains what went wrong. You can close the popup, correct the error and you are ok.

Now let’s create a second button that will bypass the bindings framework and invoke the method on the application module directly.

Create another <af:panelFormLayout/>. Drop a button in this panelform. Go to the action property of the button in the property inspector, and let it point ot a new method in a new bean.

ADF DataBinding: Yet another thing it does for you. createbean

Also create an inputtext which holds the value for the correction amount. The value is retrieved from the backingbean. The code in the panelFormLayout should look like this:

          <af:panelFormLayout id="pfl3">         
          <af:inputText label="alternative"
                        value="#{salaryCorrectionBB.correction}"/>
          <af:commandButton text="Direct Call" id="cb7"         
                            action="#{salaryCorrectionBB.invokeMethod}"/>         
          </af:panelFormLayout>

Here’s the bean code:

public class SalaryCorrectionBB {

public oracle.jbo.domain.Number correction;

    public SalaryCorrectionBB() {
    }

    public String invokeMethod() {
        // Add event code here...                   
        getAm().salaryCorrection(getCorrection());
        return null;
    }

    private HrServiceImpl getAm() {
        FacesContext fc = getFacesContext();
        Application app = fc.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = fc.getELContext();
        ValueExpression valueExp =
            elFactory.createValueExpression(elContext, "#{data.HrServiceDataControl.dataProvider}",Object.class);
            return (HrServiceImpl)valueExp.getValue(elContext);
    }
   
    public static FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }

    public void setCorrection(Number correction) {
        this.correction = correction;
    }

    public Number getCorrection() {
        return correction;
    }
}

Run the page again, and see what happens if you make a correction that results in a negative salary…….

ADF DataBinding: Yet another thing it does for you. internalservererror1

An internal server error. Even though you invoked the exact same method as before, you now get an internal server error. The direct call does not handle exceptions and therefore results in an internal server error. So here you go: You have to write “a lot” of code to be able to invoke the method directly, and you have to write even more code to get the error handling in place.  If you use the bindingframework, it will catch the exceptions and show them in a popup.

My Advice: Be smart and use the bindingframework whenever possible, and invoke methods on an application module directly only if strictly necessary.