Follow by Email

Monday, 7 April 2014

Get updated model values in value change listener instantly, ProcessUpdates in ADF (a beauty)

Hello All,

This posts talks about updating model instantly when a attribute is changed on page level.
In ValueChangeListener of component if we try to get new value from Model layer, it always returns old value, then we have only one option to get new value, use event object (evt.getNewValue();)
but sometimes we need to update model same time
So how to do this-

  • Created a Fusion Web Application using Departments table of HR Schema (Oracle)


  • Dropped Departments Vo on page as a form, and created ValueChangeListener on DepartmnetName field to get new value 





  • Now i have written a code to get DepartmentName from current row of Departments VO (ViewObject) and to get from ValueChangeEvent

  • Method in AMImpl- Called in ValueChangeListener through binding Layer

    /**Method to print Department Name */
        public void getDeptNameAction() {
            ViewObject depart = this.getDepartments1();
            Row curRow = depart.getCurrentRow();
            if (curRow != null) {
                System.out.println("Current Department from model is-" + curRow.getAttribute("DepartmentName"));
            }
        }
    


    Code in ValueChangeListener - AM Method Called and Code to get new value from event itself

        /**Value change listener for Department Name
         * @param vce
         */
        public void deptNmVCE(ValueChangeEvent vce) {
            if (vce.getNewValue() != null) {
                System.out.println("New Value in VCE-" + vce.getNewValue());
                BindingContainer bc = BindingContext.getCurrent().getCurrentBindingsEntry();
                OperationBinding ob = bc.getOperationBinding("getDeptNameAction");
                ob.execute();
            }
        }
    

  • In this case when i have changed value on page 



  • see the result -output from valuechangelistener- Model layer returns old value :-(


  • Now to update value in Model layer , i have just called processUpdates in ValueChangeListener- See the code

  •     /**Value change listener for Department Name
         * @param vce
         */
        public void deptNmVCE(ValueChangeEvent vce) {
            if (vce.getNewValue() != null) {
                //Method to update Model in VCE
                vce.getComponent().processUpdates(FacesContext.getCurrentInstance());
                System.out.println("New Value in VCE-" + vce.getNewValue());
                BindingContainer bc = BindingContext.getCurrent().getCurrentBindingsEntry();
                OperationBinding ob = bc.getOperationBinding("getDeptNameAction");
                ob.execute();
            }
        }
    

  • see the result -Value is updated in model instantly (in VCE)

so this is how processUpdates works, Cheers :-)
See one more post on this concept with different usecase- Update Model Values while Validation (Exception) occurs on page on Value Change event
Happy Learning

3 comments :

  1. Thanks a lot for this post. It helped me. I had a use case where I was trying to capture selected values from multiple check boxes where I was getting old values. So this post solved my problem by adding vce.getComponent().processUpdates(FacesContext.getCurrentInstance());

    ReplyDelete
  2. Hi, I'm trying out a similar example of Search employees by Department Name. Dragged and dropped DepartmentID attribute in DepartmentView as SelectOneChoice(DeprtmentName is showed instead of ID) and EmployeesView as ADF Table (both from from AppModuleDataControl).
    Now Whenever i change the department in SelectOneChoice it should update the Table with employees from selected Deprtment.
    How can I do it? Step by step instruction would be helpful.

    What I've done is,
    1. I created a query in EmployeesView with BindVariable. (FilterDeptwise is my viewCriteria and DeptID is bind variable)
    ( (Employees.DEPARTMENT_ID = :DeptID ) )

    2. Then added a function in AppModuleImpl.

    public void filterEmployee(java.lang.Number DeptID) {
    EmployeesViewImpl employeesViewImpl=(EmployeesViewImpl)getEmployeesView1();
    employeesViewImpl.applyViewCriteria(employeesViewImpl.getViewCriteria("FilterDeptwise"));
    employeesViewImpl.setbindDeptId(DeptID);
    employeesViewImpl.executeQuery();
    }

    3. In Client Interface added the filterEmployee(java.lang.Number DeptID) method

    4. Above step creates filterEmployee(Number) in AppModuleDataControl
    With parameter DeptID


    How to continue from here. It is not possible to bind filterEmployee(Number) method to SelectOneChoice control. How can I achieve search operation this way?

    ReplyDelete
    Replies
    1. Now add this method in pageDef and call on value change listener of Departments lov and pass department id in method using OperationBinding
      Check ADF Basics: How to invoke model layer methods from managed bean (Best Practice to write business logic in ADF)

      Ashish

      Delete