Friday 28 January 2011

CRUD with Serial Persistence - 3

This section will concentrate on the Serial Persistence
of the CRUD. I will also provide the complete code so
that you can compile and run it. The other option is
you are wellcome to make a request to get the WAR file
from arsaral (at) yahoo.com.


If you pay attention you may easily notice that the DAO
related sources are the same as the previous section.
Nevertheless I include them once more.

Please note that I may have corrected some minor discrepancies.





PersonDAO
---------
package daoPackage;

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

public interface PersonDAO {

public void createRec(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;

public boolean findByPrimaryKey(int personId)
throws PersonDAOSysException;

public Collection findByLastName(String lastName)
throws PersonDAOSysException;

public int findTotalNumberOfPersons()
throws PersonDAOSysException;
}


PersonDAOSysException.java
--------------------------
package daoPackage;

public class PersonDAOSysException extends RuntimeException {
public PersonDAOSysException (String str) {
super(str);
}
public PersonDAOSysException () {
super();
}
}


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

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;
import javax.naming.*;
import java.sql.*;
import javax.sql.*;
import java.io.Serializable;
import mainPackage.*;

public class PersonDAOVectorImpl implements PersonDAO, Serializable {

private static Vector personVect;
private static int personNextId = 1;
private static int personCurrentId = 1;

public PersonDAOVectorImpl() {
System.out.println("PersonDAOVectorImpl constructor began.");
personVect = getDBConnection();

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());
}
closeDBConnection();
}

public void createRec(String firstName, String lastName, String hobby)
throws PersonDAOSysException {
System.out.println("ARSmsg : createRec began.");

Person person = new Person();
try {
personVect = getDBConnection();
personNextId = personVect.size() + 1;
person.setId(personNextId);
person.setName(firstName);
person.setLast(lastName);
person.setHobby(hobby);
personVect.add(personVect.size(), person);
personCurrentId = 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 {
personVect = getDBConnection();
posVect = findRecSeq(personId);
System.out.println("personCurrentId=" + personCurrentId);
System.out.println("posVect=" + posVect);
if (posVect != -1) {
personVect.removeElementAt(posVect - 1);
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;

personVect = getDBConnection();
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().equals(lastName)) {
persons.add(person.getId());
}
posVect = posVect + 1;
}
System.out.println("FindByLastName id=" + persons.toString());
closeDBConnection();
return (persons);
}

public int findTotalNumberOfPersons() throws PersonDAOSysException {
//System.out.println("ARSmsg: findTotalNumberOfPersons began-----------.");
int total = 0;
PreparedStatement stmt = null;
try {
personVect = getDBConnection();
total = personVect.size();
} catch (Exception ex) {
throw new PersonDAOSysException("SQLException:" + ex.getMessage());
} finally {
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 = 1;
try {
personVect = getDBConnection();
posVect = findRecSeq(personId);
System.out.println("read Rec personCurrentId=" + personCurrentId);
System.out.println("read Rec personNextId=" + personNextId);
System.out.println("readRec posVect=" + posVect);
if (posVect == -1) {
posVect = personCurrentId;
} else {
personCurrentId = posVect;
}
closeDBConnection();
return personVect.elementAt(posVect - 1);
} 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 {
personVect = getDBConnection();
personCurrentId++;
posVect = findRecSeq(personCurrentId);
System.out.println("personCurrentId=" + personCurrentId);
System.out.println("posVect=" + posVect);
if (posVect == -1) {
posVect = personVect.size();
personCurrentId--;
personCurrentId--;
}
person = (Person) personVect.elementAt(posVect - 1);
System.out.println("readNextRec person.name =" + (String) person.getName());
System.out.println();
closeDBConnection();
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 {
personVect = 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());
closeDBConnection();
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 {
personVect = getDBConnection();

posVect = findRecSeq(id);
System.out.println("personNextId=" + personNextId);
System.out.println("posVect=" + posVect);

if (posVect == -1) {
posVect = personNextId;
}
System.out.println("posVect=" + posVect);

if (posVect < personVect.size() + 1) {
personVect.remove(posVect - 1);
person.setId(id);
} else {
person.setId(personNextId);
}
person.setName(firstName);
person.setLast(lastName);
person.setHobby(hobby);
personVect.add(posVect - 1, 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;

personVect = getDBConnection();
Iterator itr = personVect.iterator();

while (itr.hasNext()) {
person = (Person) itr.next();
posVect = posVect + 1;
//System.out.println("person.name=" + person.getName() + " person.id=" + person.getId());
if (person.getId() == id) {
closeDBConnection();
return posVect;
}
}
closeDBConnection();
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;
}

public void dumpVect() {
int posVect = 0;
Person person;

personVect = getDBConnection();
Iterator itr = personVect.iterator();
while (itr.hasNext()) {
person = (Person) itr.next();
System.out.println("id=" + person.getId()
+ " name=" + person.getName()
+ " lastName=" + person.getLast()
+ " hobby=" + person.getHobby());
}
closeDBConnection();
}

private Vector getDBConnection() throws PersonDAOSysException {
String filename = "personVect.ser";

Vector personVect = null;
FileInputStream fis = null;
ObjectInputStream in = null;

try {
fis = new FileInputStream(filename);
in = new ObjectInputStream(fis);
personVect = (Vector) in.readObject();
in.close();
personNextId = personVect.size() + 1;
return personVect;
} catch (IOException ex) {
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);

personNextId = 6;
personCurrentId = 5;

System.out.println("ARSmsg: personVect.ser does not exist. A new one is created.");
return personVect;
//throw new PersonDAOSysException("\\DAOException:" + ex.getMessage() + " " + ex.toString());
} catch (ClassNotFoundException ex) {
throw new PersonDAOSysException("\\DAOException:" + ex.getMessage() + " " + ex.toString());
}
}

private void closeDBConnection() throws PersonDAOSysException {
String filename = "personVect.ser";

FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(filename);
out = new ObjectOutputStream(fos);
out.writeObject(personVect);
out.close();
} catch (IOException 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;
}
}