Thursday, July 12, 2012

Bean Data Control - Create Simple Search Form

In Oracle ADF, search form can be created with BC4J/EJB model using "View Criteria/Named Criteria" by dropping "af:query" component in view layer. Here in this article, I'm trying to build simple search form based on bean data control.

The Results page look like below.


Enter the Department Id in search form and click search button will filter results.


You can download the sample workspace from here
[Runs with Oracle JDeveloper 11.1.2.0.0 (11g R2) + HR Schema]

Implementation Steps

Create Fusion Web application, in model project create Employees java class. Open Employees.java and add the below code.
public class Employees {
    private Integer employeeId;
    private String firstName;
    private String lastName;
    private String email;
    private String phoneNo;
    private Date hireDate;
    private String jobId;
    private Integer salary;
    private Integer departmentId;

    public Employees() {
        super();
    }

    public Employees(int employeeId, String firstName, String lastName, String email, String phoneNo, Date hireDate,
                     String jobId, int salary, int departmentId) {
        this.setEmployeeId(employeeId);
        this.setFirstName(firstName);
        this.setLastName(lastName);
        this.setEmail(email);
        this.setPhoneNo(phoneNo);
        this.setHireDate(hireDate);
        this.setJobId(jobId);
        this.setSalary(salary);
        this.setDepartmentId(departmentId);
    }

    public void setEmployeeId(Integer employeeId) {
        this.employeeId = employeeId;
    }

    public Integer getEmployeeId() {
        return employeeId;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }

    public String getPhoneNo() {
        return phoneNo;
    }

    public void setHireDate(Date hireDate) {
        this.hireDate = hireDate;
    }

    public Date getHireDate() {
        return hireDate;
    }

    public void setJobId(String jobId) {
        this.jobId = jobId;
    }

    public String getJobId() {
        return jobId;
    }

    public void setSalary(Integer salary) {
        this.salary = salary;
    }

    public Integer getSalary() {
        return salary;
    }

    public void setDepartmentId(Integer departmentId) {
        this.departmentId = departmentId;
    }

    public Integer getDepartmentId() {
        return departmentId;
    }
}
Create PopulateData java class, open PopulateData.java and add the below code.
public class PopulateData {
    private static List allEmployees;
    static {
        allEmployees = new ArrayList();
        allEmployees.add(new Employees(100, "Steven", "King", "SKING", "515.123.4567", new Date(), "AD_PRES", 24000,
                                       20));
        allEmployees.add(new Employees(101, "Neena ", "Kochhar", "NKOCHHAR", "515.123.4568", new Date(), "AD_VP",
                                       17000, 90));
        allEmployees.add(new Employees(102, "Lex", "De Haan", "AHUNOLD", "515.423.4567", new Date(), "IT_PROG", 15000,
                                       90));
        allEmployees.add(new Employees(103, "Alexander", "Hunold", "AHUNOLD", "515.423.4567", new Date(), "IT_PROG",
                                       10000, 20));
        allEmployees.add(new Employees(104, "Bruce", "Ernst", "BERNST", "515.423.4568", new Date(), "IT_PROG", 5000,
                                       50));
        allEmployees.add(new Employees(105, "David", "Austin", "DAUSTIN", "515.423.4569", new Date(), "IT_PROG", 7000,
                                       30));
        allEmployees.add(new Employees(106, "Valli", "Pataballa", "VPATABAL", "515.423.4560", new Date(), "IT_PROG",
                                       8000, 50));
        allEmployees.add(new Employees(107, "Diana", "Lorentz", "DLORENTZ", "515.423.5567", new Date(), "IT_PROG",
                                       9000, 50));
        allEmployees.add(new Employees(108, "Nancy", "Greenberg", "NGREENBE", "515.124.4569", new Date(), "FI_MGR",
                                       10000, 100));
        allEmployees.add(new Employees(109, "Daniel", "Faviet", "DFAVIET", "515.124.4169", new Date(), "FI_ACCOUNT",
                                       13000, 100));
        allEmployees.add(new Employees(110, "John", "Chen", "JCHEN", "515.124.4269", new Date(), "FI_ACCOUNT", 14000,
                                       100));
        allEmployees.add(new Employees(111, "Ismael", "Sciarra", "ISCIARRA", "515.124.4369 ", new Date(), "FI_ACCOUNT",
                                       12000, 60));
        allEmployees.add(new Employees(112, "Jose Manuel", "Urman", "JMURMAN", "515.124.4469", new Date(),
                                       "FI_ACCOUNT", 4000, 60));
        allEmployees.add(new Employees(112, "Jose Manuel", "Urman", "JMURMAN", "515.124.4469", new Date(),
                                       "FI_ACCOUNT", 7800, 60));
        allEmployees.add(new Employees(113, "Luis", "Popp", "LPOPP", "515.124.4567", new Date(), "FI_ACCOUNT", 6900,
                                       70));
        allEmployees.add(new Employees(114, "Den", "Raphaely", "DRAPHEAL", "515.127.4561", new Date(), "PU_MAN", 11000,
                                       90));
    }

    public PopulateData() {
        super();
    }

    public static List getAllEmployees() {
        return allEmployees;
    }
}
Create EmployeeServicejava class, open EmployeeService.java and add the below code. Create data control from EmployeeService Bean class.
public List filterByDeptId(Integer departmentId) {
        try {
            List allEmployees = PopulateData.getAllEmployees();
            if (departmentId != null) {
                List filteredEmployees = new ArrayList();
                for (Employees e : allEmployees) {
                    if (e.getDepartmentId() == departmentId) {
                        filteredEmployees.add(e);
                    }
                }
                return filteredEmployees;
            } else {
                return allEmployees;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
}

public List getAllEmployees() {
    return PopulateData.getAllEmployees();
}
In ViewController project, create jspx page and from data control palette
  1. Drop filterByDeptId as ADF Method Parameter
  2. Drop filterByDeptId->Employees->Table as ADF Read-only Table

Monday, July 2, 2012

How to add a new row at the end of the ADF Table

I was reading through OTN Discussion Forums where I found one topic "How to add a new row at the end of the ADF Table", I found couple of articles which explains how to insert new rows to the end of ADF Table. Here is the list below.

Here is one more way to insert new rows to the end of ADF table by overriding the view object's(ViewImpl) insertRow() method.
/**
 * This method will overrides the view object's insertRow()
 * First create a new row
 * Removes the row from the collection and then retain it for insertion into another location.
 * Navigates to the last row in the row set.
 * Navigates to next row
 * Inserts the same row to the row set
 * @param row
 */
@Override
public void insertRow(Row row) {
 super.insertRow(row);
 row.removeAndRetain();
 last();
 next();
 getDefaultRowSet().insertRow(row);
}

You can download the sample workspace from here
[Runs with Oracle JDeveloper 11.1.2.0.0 (11g R2) + HR Schema]

Run the index.jspx page and navigate the departments records, now click on Create button. Notice in employee table new row is added to end of the ADF Table.