Saturday, 22 January 2011

Simple CRUD Tutorial - 5

The knitty details of the personDAO:

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() { //constructor
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>(); // create a vector of person objects
Person personalData; // define person

personalData = new Person(); // init person
personalData.setId(1); // set person attributes
personalData.setName("Ali"); // for the first person
personalData.setLast("SARAL");
personalData.setHobby("Music Composition");
personVect.add(personalData); // add the person to the vector

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

personalData = new Person();
personalData.setId(3);
personalData.setName("Ekrem");
personalData.setLast("CANBEK");
personalData.setHobby("Violin");
personVect.add(personalData);

personalData = new Person();
personalData.setId(4);
personalData.setName("Turan");
personalData.setLast("MUTLUAY");
personalData.setHobby("Choir");
personVect.add(personalData);

personalData = new Person();
personalData.setId(5);
personalData.setName("Rengin");
personalData.setLast("AHISKALI");
personalData.setHobby("Literature");
personVect.add(personalData);

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

personNextId = 6; //next person id to be used
personCurrentId = 5; // current person id
}

public void createRec(int id, String firstName, String lastName, String hobby)
throws PersonDAOSysException {
System.out.println("ARSmsg : createRec began.");
Person person = new Person(); // create empty new person
try {
//getDBConnection();
person.setId(id); //populate the new person
person.setName(firstName);
person.setLast(lastName);
person.setHobby(hobby);
personVect.add(personVect.size(), person); // add person to the DB vector
personCurrentId = personNextId; // set current id to the new person
personNextId++; //increase next available id
} catch (Exception ex) {
throw new PersonDAOSysException("DAOException:" + ex.getMessage());
} finally {
//closeDBConnection();
}
}

public void deleteRec(int personId) throws PersonDAOSysException {
System.out.println("ARSmsg : deleteRec began.");
int posVect = 0;
try {
//getDBConnection();
posVect = findRecSeq(personId); // find the vector pos of the id
System.out.println("personCurrentId=" + personCurrentId);
System.out.println("posVect=" + posVect);
if (posVect != -1) {
personVect.removeElementAt(posVect); // delete vector element
reorganiseVect(); //reorganize vestor element ids
} else {
System.out.println("ARSmsg: Delete - Not found!");
}
} catch (Exception ex) {
throw new PersonDAOSysException("DAOException:" + ex.getMessage());
} finally {
closeDBConnection();
}
}

public Person readRec(int personId)
throws PersonDAOSysException {
System.out.println("ARSmsg : readRec began.");
Person person = new Person();
int posVect = 0;
try {
//getDBConnection();
posVect = findRecSeq(personId); //find vector pos of the id
System.out.println("personCurrentId=" + personCurrentId);
System.out.println("posVect=" + posVect);
if (posVect == -1) {
posVect = personCurrentId; //if not found preserve the current id
} else {
personCurrentId = posVect + 1; //if found return the vector pos
}

return personVect.elementAt(posVect); //return the person object
} catch (Exception ex) {
throw new PersonDAOSysException("DAOException:" + ex.getMessage());
} finally {
//closeDBConnection();
}
}

public Person readNextRec()
throws PersonDAOSysException {
System.out.println("ARSmsg : readNextRec began.");
Person person = new Person();
int posVect = 0;
try {
//getDBConnection();
personCurrentId++; //increase the current id pos
posVect = findRecSeq(personCurrentId); //find the vector pos of the current id
System.out.println("personCurrentId=" + personCurrentId);
System.out.println("posVect=" + posVect);
if (posVect == -1) {
posVect = personVect.size() - 1;
personCurrentId--;
}
person = (Person) personVect.elementAt(posVect); //return the person object
System.out.println("readNextRec person.name =" + (String) person.getName());
return person;

} catch (Exception ex) {
throw new PersonDAOSysException("DAOException:" + ex.getMessage());
} finally {
//closeDBConnection();
}
}

public Person readPrevRec() throws PersonDAOSysException {
System.out.println("ARSmsg : readPrevRec began.");
Person person = new Person();
int posVect = 0;
try {
//getDBConnection();
personCurrentId--;
posVect = findRecSeq(personCurrentId);
System.out.println("personCurrentId=" + personCurrentId);
System.out.println("posVect=" + posVect);
if (posVect == -1) {
posVect = 0;
personCurrentId++;
}
person = (Person) personVect.elementAt(posVect);
System.out.println("readPrevRec person.name =" + (String) person.getName());
return person;

} catch (Exception ex) {
throw new PersonDAOSysException("DAOException:" + ex.getMessage());
} finally {
//closeDBConnection();
}
}

public void updateRec(int id, String firstName, String lastName,
String hobby) throws PersonDAOSysException {
System.out.println("ARSmsg : updateRec began.");
int posVect = 0;
Person person = new Person();

try {
//getDBConnection();

posVect = findRecSeq(id); //find the required vector pos
System.out.println("personNextId=" + personNextId);
System.out.println("posVect=" + posVect);
if (posVect == -1) { //check if it update or create
posVect = findRecSeq(personNextId); //create
}
System.out.println("posVect=" + posVect);
personVect.remove(posVect); //remove the previous object
person.setId(id); //populate the new object
person.setName(firstName);
person.setLast(lastName);
person.setHobby(hobby);
personVect.add(posVect, person); //add the new object
} catch (Exception ex) {
throw new PersonDAOSysException("DAOException:" + ex.getMessage());
} finally {
closeDBConnection();
}
}

private int findRecSeq(int id) { //find the vector pos of the person with id
int posVect = 0;
Person person;
Iterator itr = personVect.iterator();
while (itr.hasNext()) {
person = (Person) itr.next();
//System.out.println("person.name=" + person.getName() + " person.id=" + person.getId());
if (person.getId() == id) {
return posVect;
}
posVect = posVect + 1;
}
return (-1);
}

private void reorganiseVect() { //change all the ids to their vector pos values
int posVect = 0;
Person person;
Iterator itr = personVect.iterator();
while (itr.hasNext()) {
person = (Person) itr.next();
person.setId(posVect + 1);
posVect = posVect + 1;
}
personNextId = posVect + 1;
//personCurrentId = 1;
}

private void getDBConnection() throws PersonDAOSysException { //not used
try {
if (personVect == null) {
//personVect = new Personel();
}
} catch (Exception ex) {
throw new PersonDAOSysException("\\DAOException:" + ex.getMessage() + " " + ex.toString());
}
}

private void closeDBConnection() //not used
throws PersonDAOSysException {
try {
//personelVectDB.close();
} catch (Exception ex) {
throw new PersonDAOSysException("DAOException:" + ex.getMessage());
}
}

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;
}
}