Wednesday 26 January 2011

Simple DAO Tutorial - 2

This is the second part of Simple DAO Tutorial (Data Access Object). PersonDAOVectorImpl.java, PersonDAOHSQLDBImpl.java, PersonDAOMySQLDBImpl.java are the three implementations of PersonDAO.java. You can find the details of these implementations in the previous Tutorial abour CRUD. The comments in the code are also enough.

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

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();
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 {
System.out.println("ARSmsg : createRec began.");
Person person = new Person();
try {
//getDBConnection();
person.setId(id);
person.setName(firstName);
person.setLast(lastName);
person.setHobby(hobby);
personVect.add(personVect.size(), person);
personCurrentId = personNextId;
personNextId++;
} 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);
System.out.println("personCurrentId=" + personCurrentId);
System.out.println("posVect=" + posVect);
if (posVect != -1) {
personVect.removeElementAt(posVect);
reorganiseVect();
} else {
System.out.println("ARSmsg: Delete - Not found!");
}
} catch (Exception ex) {
throw new PersonDAOSysException("DAOException:" + ex.getMessage());
} finally {
//closeDBConnection();
}
}
public Collection findByLastName(String lastName) {
int posVect = 0;
Collection persons = new ArrayList();
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.getLast() == lastName) {
persons.add(person.getId());
}
posVect = posVect + 1;
}
System.out.println("FindByLastName id="+persons.toString());
return (persons);
}
public int findTotalNumberOfPersons() throws PersonDAOSysException {
//System.out.println("ARSmsg: findTotalNumberOfPersons began-----------.");
int total = 0;
PreparedStatement stmt = null;
try {
//getDBConnection();
total = personVect.size();
} catch (Exception ex) {
throw new PersonDAOSysException("SQLException:" + ex.getMessage());
} finally {
//closeStatement(stmt);
//closeDBConnection();
}
System.out.println("ARSmsg: findTotalNumberOfPersons="+total);
return total;
}

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);
System.out.println("personCurrentId=" + personCurrentId);
System.out.println("posVect=" + posVect);
if (posVect == -1) {
posVect = personCurrentId;
} else {
personCurrentId = posVect + 1;
}

return personVect.elementAt(posVect);
} 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++;
posVect = findRecSeq(personCurrentId);
System.out.println("personCurrentId=" + personCurrentId);
System.out.println("posVect=" + posVect);
if (posVect == -1) {
posVect = personVect.size() - 1;
personCurrentId--;
}
person = (Person) personVect.elementAt(posVect);
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);
System.out.println("personNextId=" + personNextId);
System.out.println("posVect=" + posVect);
if (posVect == -1) {
posVect = findRecSeq(personNextId);
}
System.out.println("posVect=" + posVect);
personVect.remove(posVect);
person.setId(id);
person.setName(firstName);
person.setLast(lastName);
person.setHobby(hobby);
personVect.add(posVect, person);
} catch (Exception ex) {
throw new PersonDAOSysException("DAOException:" + ex.getMessage());
} finally {
closeDBConnection();
}
}
public boolean findByPrimaryKey(int id) {
int posVect = findRecSeq(id);
System.out.println("ARSmsg : findByPrimaryKey posVect="+posVect);
if (posVect > -1) return(true);
return (false);
}
private int findRecSeq(int 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() {
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 {
try {
if (personVect == null) {
//personVect = new Personel();
}
} catch (Exception ex) {
throw new PersonDAOSysException("\\DAOException:" + ex.getMessage() + " " + ex.toString());
}
}

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

public Vector getPersonVect() {
return personVect;
}

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

public int getPersonNextId() {
return personNextId;
}

public int getPersonCurrentId() {
return personCurrentId;
}
}