Thursday, May 9, 2013

ADF - Populate ADF Table using managed Bean and DTO


In this use case, we will be building an employee form where the user will enter details and then click on Save. Once the data is saved, the results will be displayed in the table below.


For every field displayed on the form, we need to declare a variable in the managed bean, and create getters setters.
    private String firstName;
    private String lastName;
    private String email;
    private Date hireDate;
    private String jobID;

Now for every inputField, bind the value property with the variable defined. For example, for the First Name field, I have set the value property as #{pageFlowScope.myBean.firstName}.

This is the method defined in the managed bean for the Save button:
    public void onClickSave(ActionEvent actionEvent) {
        // Add event code here...
        Map searchValueMap = new HashMap();
        searchValueMap.put(FIRST_NAME, firstName);
        searchValueMap.put(LAST_NAME, lastName);
        searchValueMap.put(EMAIL, email);
        searchValueMap.put(HIRE_DATE, hireDate);
        searchValueMap.put(JOB_ID, jobID);

        Map parameters = new HashMap();
        parameters.put("params", searchValueMap);

        BindingContainer bindings = getBindings();
        OperationBinding operationBinding =
            bindings.getOperationBinding("saveEmployee");
        operationBinding.getParamsMap().putAll(parameters);
        emplist = (List)operationBinding.execute();

        AdfFacesContext.getCurrentInstance().addPartialTarget(empTable);
    }

This is from the Application module:
    public List saveEmployee(Map params) {
        ViewObjectImpl vo = getEmployeesView1();
        ViewRowImpl vor = (ViewRowImpl)vo.createRow();
        getEmployeesView1().insertRow(vor);
        //vor.setAttribute("EmployeeId", params.get("employeeID"));
        vor.setAttribute("FirstName", params.get("firstName"));
        vor.setAttribute("LastName", params.get("lastName"));
        vor.setAttribute("Email", params.get("email"));
        vor.setAttribute("HireDate", params.get("hireDate"));
        vor.setAttribute("JobId", params.get("jobID"));
        vor.getApplicationModule().getTransaction().commit();

        vo.executeQuery();
        
        List employeeList = null;
        EmployeeDTO employeeDTO = null;
        
        RowSet rowSet = vo.getRowSet();
        Row row = null;
        if (null != rowSet) {
            employeeList = new ArrayList();
            row = rowSet.first();
            while (null != row) {
                employeeDTO = getEmployeeDTOFromRow(row);
                employeeList.add(employeeDTO);
                row = rowSet.next();
            }
        }
        return employeeList;
    }
    
    private EmployeeDTO getEmployeeDTOFromRow(Row row) {
        EmployeeDTO employeeDTO = new EmployeeDTO();
        employeeDTO.setEmployeeID((Number)row.getAttribute("EmployeeId"));
        employeeDTO.setFirstName((String)row.getAttribute("FirstName"));
        employeeDTO.setLastName((String)row.getAttribute("LastName"));
        employeeDTO.setEmail((String)row.getAttribute("Email"));
        employeeDTO.setHireDate((Date)row.getAttribute("HireDate"));
        employeeDTO.setJobID((String)row.getAttribute("JobId"));
        return employeeDTO;
    }

For the table, I have set the value property as #{pageFlowScope.myBean.emplist} and binding property as #{pageFlowScope.myBean.empTable}. Getters and setters are created for both:
    private RichTable empTable;

    private List emplist;

Download the sample application from File Cabinet: ProgrammaticTableDTO.rar

4 comments:

  1. Replies
    1. Download from the link below:
      http://www31.zippyshare.com/v/91100274/file.html

      Delete
  2. Hi Sonal,

    Congrats, a very informative entry.

    By the way, i don't know how to download the sample application.

    ReplyDelete
  3. Please Upload ehis again!
    Nice Example!

    ReplyDelete