Saturday 22 January 2011

Simple CRUD Tutorial - 3

In this issue of the Simple CRuD Tutorial, I will
provide the data back ground of the application.

The basic data structure that supports the application
is the person.java. Person.java is very simple.
Three fields for name, lastname, hobby and an integer
id field for keeping track of the records.

These attributes are private so there are getter and
setter functions to reach them.

mainPackage/Person.java
-----------------------

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mainPackage;

/**
*
* @author Ali Riza SARAL
*/
public class Person {

private String name;
private String last;
private String hobby;
private int id;

public Person(){}
public Person(int id, String name, String last, String hobby){
this.name = name;
this.last = last;
this.hobby = hobby;
this.id = id;
}
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getLast() {
return this.last;
}

public void setLast(String last) {
this.last = last;
}

public String getHobby() {
return this.hobby;
}

public void setHobby(String hobby) {
this.hobby = hobby;
}

public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}
}


This application uses a vector to store the DB data.
There is a DAO(Data Access Object) layer that provides
the data to the application. It does not matter
how the DAO gets the required data whether from
HSQLDB or MySQL or a simple vector as in this case.

The HSQLDB and MySQL versions will be provided
subsequently.

The abstract definition of the DAO is provided in

./daoPackage/PersonDAO.java
---------------------------
package daoPackage;

import java.util.Collection;
import mainPackage.*;

public interface PersonDAO {

public void createRec(int id, String firstName,
String lastName, String hobby)
throws PersonDAOSysException;

public Person readRec(int personId)
throws PersonDAOSysException;

public void updateRec(int id, String firstName,
String lastName, String hobby)
throws PersonDAOSysException;

public void deleteRec(int personId)
throws PersonDAOSysException;
}


The implementation of PersonDAO is done in

./daoPackage/PersonDAOVectorImpl.java
-------------------------------------
package daoPackage;

import java.util.*;
import javax.naming.*;
import java.sql.*;
import javax.sql.*;
import mainPackage.*;

public class PersonDAOVectorImpl implements PersonDAO {
private static Vector<Person> personVect;
private static int personNextId = 1;
private static int personCurrentId = 1;

public PersonDAOVectorImpl() {
System.out.println("PersonDAOVectorImpl constructor began.");
//check if it exists as serialized
//if not
// personelVectDB.populate();
// if it exists deserialize and populate?
// personelVectDB.deserialize();

personVect = new Vector<Person>();
Person personalData;

personalData = new Person();
personalData.setId(1);
personalData.setName("Ali");
personalData.setLast("SARAL");
personalData.setHobby("Music Composition");
personVect.add(personalData);

personalData = new Person();
personalData.setId(2);
personalData.setName("Tamer");
personalData.setLast("ÜNAL");
personalData.setHobby("Folk Music");
personVect.add(personalData);
...
...

Enumeration vEnum = personVect.elements();
while (vEnum.hasMoreElements()) {
Person person = (Person) vEnum.nextElement();
System.out.println("ARSmsg: PersonDAOVectorImpl constructor person.name=" + person.getName() + " person.id=" + person.getId());
}

personNextId = 6;
personCurrentId = 5;
}
...
...
}

public void createRec(int id, String firstName, String lastName, String hobby)
throws PersonDAOSysException {
...
...
}

public void deleteRec(int personId) throws PersonDAOSysException {
...
...
}

public Person readRec(int personId)
throws PersonDAOSysException {
...
...
}

public Person readNextRec()
throws PersonDAOSysException {
...
...
}

public Person readPrevRec() throws PersonDAOSysException {
...
...
}

public void updateRec(int id, String firstName, String lastName,
String hobby) throws PersonDAOSysException {
...
...
}

private int findRecSeq(int id) {
...
...
}

private void reorganiseVect() {
...
...
}

private void getDBConnection() throws PersonDAOSysException {
...
...
}

private void closeDBConnection()
throws PersonDAOSysException {
...
...
}

public Vector<Person> getPersonVect() {
return personVect;
}

public void addPerson(Person personalData) {
personVect.add(personalData);
personNextId++;
}

public int getPersonNextId() {
return personNextId;
}

public int getPersonCurrentId() {
return personCurrentId;
}
}

As seen above, the data is kept in

private static Vector<Person> personVect;
private static int personNextId = 1;
private static int personCurrentId = 1;

Here, personNextId indicates the next available id
for the new function. personCurrentId keeps track of
the currently displayed Person.

Beginning with the findRecSeq(id) function till the
end are utility functions.

updateRec(..), readNextRec(...) etc. functions are
called from the Servlets. I will explain the calling
mechanism in the next issue.