Sunday, March 2, 2014

ADF Mobile - How to add a record using Constructor option

It’s been a while since my last post, but it doesn't mean that new discoveries were not happening. Well to be fair, I have been really busy with various responsibilities, I hope to re-take blogging again it into my daily routine. Now let’s get into business!

This article is a continuation of "ADF Mobile - Access Device Native SQLite Database to Store Data" and here I will try to explain a way of creating the record using the Constructor option with Bean DataControl.

You can download the sample workspace from here
[Runs with Oracle JDeveloper 11.1.2.4.0]

Implementation Steps:- 

Create an ADF Mobile Application, expand the ViewController project. Locate and expand the Application Sources folder, create a Department.java file and add the below code.
public class Departments {
    protected int departmentId;
    protected String departmentName;
    protected int locationId;
    private transient PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener l) {
        propertyChangeSupport.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener(PropertyChangeListener l) {
        propertyChangeSupport.removePropertyChangeListener(l);
    }

    public Departments() {
        super();
    }

    public String getKey() {
        Integer i = new Integer(departmentId);
        return i.toString();
    }

    public Departments(int departmentId, String departmentName, int locationId) {
        this.setDepartmentId(departmentId);
        this.setDepartmentName(departmentName);
        this.setLocationId(locationId);
    }


    Departments(Departments newDepartments) {
        this.setDepartmentId(newDepartments.getDepartmentId());
        this.setDepartmentName(newDepartments.getDepartmentName());
        this.setLocationId(newDepartments.getLocationId());
    }

    public void setDepartmentId(int departmentId) {
        int oldDepartmentId = this.departmentId;
        this.departmentId = departmentId;
        propertyChangeSupport.firePropertyChange("departmentId", oldDepartmentId, departmentId);
    }


    public int getDepartmentId() {
        return departmentId;
    }

    public void setLocationId(int locationId) {
        int oldLocationId = this.locationId;
        this.locationId = locationId;
        propertyChangeSupport.firePropertyChange("locationId", oldLocationId, locationId);
    }

    public int getLocationId() {
        return locationId;
    }

    public void setDepartmentName(String departmentName) {
        String oldDepartmentName = this.departmentName;
        this.departmentName = departmentName;
        propertyChangeSupport.firePropertyChange("departmentName", oldDepartmentName, departmentName);
    }

    public String getDepartmentName() {
        return departmentName;
    }
}
Create DepartmentList.java file and add the below code. Create DataControl based on DepartmentList.java file.
public class DepartmentsList {
    private static List s_departments = null;
    private Departments editDepartment = new Departments();
    private transient ProviderChangeSupport providerChangeSupport = new ProviderChangeSupport(this);

    private transient PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);


    public void addPropertyChangeListener(PropertyChangeListener l) {
        propertyChangeSupport.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener(PropertyChangeListener l) {
        propertyChangeSupport.removePropertyChangeListener(l);
    }

    public void addProviderChangeListener(ProviderChangeListener l) {
        providerChangeSupport.addProviderChangeListener(l);
    }

    public void removeProviderChangeListener(ProviderChangeListener l) {
        providerChangeSupport.removeProviderChangeListener(l);
    }

    public DepartmentsList() {
        if (s_departments == null) {
            s_departments = new ArrayList();
            Execute();
        }
    }

    public void Execute() {
        Trace.log(Utility.ApplicationLogger, Level.INFO, DepartmentsList.class, "Execute",
                  "!!!!!!!!!!!!!!!!!In DEPARTMENT Execute!!!!!!!!!!!!!!!!!!!!!!!!!");
        try {
            Connection conn = DBConnectionFactory.getConnection();
            s_departments.clear();
            conn.setAutoCommit(false);
            PreparedStatement stat = conn.prepareStatement("SELECT * from DEPARTMENTS ORDER BY DEPARTMENT_NAME");
            ResultSet rs = stat.executeQuery();
            Trace.log(Utility.ApplicationLogger, Level.INFO, DepartmentsList.class, "Execute",
                      "!!!!!!!!!!!!!!!!!Query Executed!!!!!!!!!!!!!!!!!!!!!!!!!");
            while (rs.next()) {
                Trace.log(Utility.ApplicationLogger, Level.INFO, DepartmentsList.class, "Execute",
                          "!!!!!!!!!!!!!!!!!Adding Department!!!!!!!!!!!!!!!!!!!!!!!!!");
                int departmentId = rs.getInt("DEPARTMENT_ID");
                String departmentName = rs.getString("DEPARTMENT_NAME");
                int locationId = rs.getInt("LOCATION_ID");
                s_departments.add(new Departments(departmentId, departmentName, locationId));
            }
            rs.close();
            providerChangeSupport.fireProviderRefresh("departments");
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    public Departments[] getDepartments() {
        Departments d[] = null;
        d = (Departments[])s_departments.toArray(new Departments[s_departments.size()]);
        return d;
    }


    public void persistDepartments(int departmentId, String departmentName, int locationId) {
        Connection conn;
        try {
            conn = DBConnectionFactory.getConnection();
            conn.setAutoCommit(false);
            String insertSQL = "Insert into DEPARTMENTS (DEPARTMENT_ID,DEPARTMENT_NAME,LOCATION_ID) values (?,?,?)";
            PreparedStatement pStmt = conn.prepareStatement(insertSQL);
            pStmt.setInt(1, departmentId);
            pStmt.setString(2, departmentName);
            pStmt.setInt(3, locationId);
            pStmt.execute();
            conn.commit();

            editDepartment.setDepartmentId(departmentId);
            editDepartment.setDepartmentName(departmentName);
            editDepartment.setLocationId(locationId);

            s_departments.add(0, editDepartment);
            providerChangeSupport.fireProviderCreate("departments", editDepartment.getKey(), editDepartment);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
In below section I will not show all screen creation, here I'm interested in DeptAdd.amx page. Take a look at the DepartmentList data control in the below image, drag and drop DepartmentsList->Constructors->com.oracle.hr.Departments->Departments as ADF Mobile Parameter Form.   


Next drag and drop DepartmentsList->persistDepartment->Method as ADF Mobile Button and in Edit Action Binding window map the parameter binding values as shown below.


Application screen looks like below when it deployed to IOS simulator. Displaying the Departments List is fetched from SQLite, click on the Add button to create new department record.


Enter the details in the department form and click on Save button. The Department information will saved to the database and moved to Dept List screen and department list will get updated by newly added department record.


No comments:

Post a Comment