downey
Class SimpleDB

java.lang.Object
  |
  +--downey.SimpleDB

public class SimpleDB
extends java.lang.Object

This class allows for read and write access to a simple database. It is in a package named downey

The database is stored as a HashMap, so it can store any object. There are some helper functions to assist for changing a String[] to an ArrayList and from an ArrayList to a String[]. Since the database is being serialized, it should only contain objects that belong to the standard collections, and String[] is not one of them.


Field Summary
protected  boolean isOpen
           
protected  java.util.HashMap m_database
           
protected  java.lang.String m_fileName
           
 
Constructor Summary
SimpleDB()
          A default constructor for use with a tag lib.
SimpleDB(java.lang.String fileName)
          Creates a new instance of SimpleDB
 
Method Summary
 void eraseDatabase()
          Delete all the elements in the database.
 java.lang.String getFileName()
          Used in a tag lib.
 void init()
          init with no parameters.
 void init(java.lang.String fileName)
          Reinitializes a DB.
 java.util.Iterator iterator()
          Returns an Iterator of the keys in the DB
 java.lang.String[] makeArray(java.util.ArrayList list)
          This is a helper function for creating a String[] from an ArrayList.
 java.util.ArrayList makeList(java.lang.String[] values)
          This is a helper function for creating an ArrayList from a String[].
protected  void openDatabase()
           
 java.lang.Object readRecord(java.lang.String key)
          Read a record object from the database.
protected  void saveDatabase()
           
 void setFileName(java.lang.String fileName)
          Used in a tag lib.
 void updateRecord(java.lang.String key, java.lang.Object obj)
          This will either add or update the object associated with the given key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_fileName

protected java.lang.String m_fileName

m_database

protected java.util.HashMap m_database

isOpen

protected boolean isOpen
Constructor Detail

SimpleDB

public SimpleDB(java.lang.String fileName)
Creates a new instance of SimpleDB

Parameters:
fileName - The name of the file to open. The file should already exist and should be readable and writable by the world, but not by the group (606)

Create an instance variable for the database

downey.SimpleDB m_database;

Construct the object only once in your servlet. The best place to call it is in the init method

m_database = new downey.SimpleDB("/aul/homes/dlett004/cgs4825/java-servlets/WEB-INF/classes/myFile.db");


SimpleDB

public SimpleDB()
A default constructor for use with a tag lib.

Method Detail

init

public void init(java.lang.String fileName)
Reinitializes a DB. This does not need to be called. It is here only to assist in using the SimpleDB with a tag lib.

Parameters:
fileName - The name of the file to open. The file should already exist and should be readable and writable by the world, but not by the group (606)

init

public void init()
init with no parameters. It will only create a DB if the setFileName method has already been called. Used with a tag lib


setFileName

public void setFileName(java.lang.String fileName)
Used in a tag lib. Sets the fileName for a subsequent call to init()


getFileName

public java.lang.String getFileName()
Used in a tag lib. Completes the propery for fileName


openDatabase

protected void openDatabase()

saveDatabase

protected void saveDatabase()

readRecord

public java.lang.Object readRecord(java.lang.String key)
Read a record object from the database.

Parameters:
key - This is the string key used to access the database.
Returns:
The Object that is associated with the key

It will be necessary to cast the return type to the correct class in your code. It is up to you to insure that you retrieve the correct type.

MyRecordClass record = (MyRecordClass) m_database.readRecord("fred");


updateRecord

public void updateRecord(java.lang.String key,
                         java.lang.Object obj)
This will either add or update the object associated with the given key.

Parameters:
key - The string used to access that database
obj - The object to be added to the database

It is not necessary to cast the parameter to any type, since all objects are derived from Object.

m_database.updateRecord("fred", record);


makeList

public java.util.ArrayList makeList(java.lang.String[] values)
This is a helper function for creating an ArrayList from a String[].

Parameters:
values - The String[] to be converted
Returns:
The ArrayList that is created with all the elements of the String[]

Since String[] are not of the standard collections, it is more difficult to serialize. If you need to save a String[] into the database, first convert it to an ArrayList. ArrayList is part of the standard collections and is easily serialized.

String str[] = request.getParamterValues("team");
ArrayList list = m_database.makeList(str);


makeArray

public java.lang.String[] makeArray(java.util.ArrayList list)
This is a helper function for creating a String[] from an ArrayList. This is the counterpart of makeList.

Parameters:
list - The ArrayList to be converted
Returns:
The String[] that is created with all the elements of the ArrayList

If a String[] has been converted to an ArrayList and stored in the database, then retrieve the ArrayList from the database and use the method to recreate the original String[]
String[] str = Downey.makeArray(list);


eraseDatabase

public void eraseDatabase()
Delete all the elements in the database. If you need to erase all the elements in the database, then call this method. It will erase the contents of the HashMap and of the file.

m_database.eraseDatabase();


iterator

public java.util.Iterator iterator()
Returns an Iterator of the keys in the DB

Returns:
Iterator of keys