anderix.datajets
Class GenericDataJet

java.lang.Object
  extended by anderix.datajets.DataJet
      extended by anderix.datajets.GenericDataJet
All Implemented Interfaces:
Removable, Storable, java.lang.Cloneable

public class GenericDataJet
extends DataJet
implements Storable, Removable

A concrete entension of DataJet that implements Storable and Removable and can be used by composition or inheritance. GenericDataJet provides methods for all DataJet operations (create, read, update, and delete). GenericDataJet can be extended to create other DataJet classes, and combined with SqlBuilder to return database records in many different ways.

GenericDataJet, like all classes that extend DataJet, must be initialized with connection, table and field information. The connection information can be supplied as driver and connection string, or as an object that implements ConnectionGenerator. Table and fields must both be defined. If any of this initialization information is missing or incorrect, GenericDataJet will throw exceptions when calling its various methods -- ConnectionException if the connection information is incorrect and DataJetException if the table or field information is incorrect.

When working with GenericDataJet by composition, initialize the class by either calling a static jetSet method, or by instantiating the object with a constructor that also initializes the DataJet. When extending GenericDataJet, it is usually a good idea to create a new jetSet method that encapsulates the initialization information, and explicitly call that method in the default constructor.

The examples below show how to work with GenericDataJet by composition.

To create a new record, follow this example:

//Example: Creating a new record
String driver = [insert database driver];
String connectionString = [insert connection url];
String table = "my_table";
String[] fields = new String[]{"id", "field1", "field2"};
JetSet jetSet = new JetSet(driver, connectionString, table, fields);
GenericDataJet dj = new GenericDataJet(jetSet);
dj.setFieldValue(1, "new value 1");
dj.setFieldValue(2, "new value 2");
dj.store();
To load an existing record and then delete it, follow this example:
//Example: Load an existing record, then delete it
String driver = [insert database driver];
String connectionString = [insert connection url];
String table = "my_table";
String[] fields = new String[]{"id", "field1", "field2"};
JetSet jetSet = new JetSet(driver, connectionString, table, fields);
GenericDataJet dj = GenericDataJet.load(1, jetSet); //load record with id = 1;
System.out.println(dj.getFieldValue(1)); //display a field from the record
dj.remove(); //deletes the record from the database
The following listing shows how to create a basic class that extends GenericDataJet:
import com.datajets.*;

class MyDataJet extends GenericDataJet {

        public static JetSet jetSet() {
                String driver = [insert database driver];
                String connectionString = [insert connection url];
                String table = "my_table";
                String[] fields = new String[]{"id", "field1", "field2"};                       
                return new JetSet(driver, connectionString, table, fields);
        }

        public MyDataJet() {
                super(jetSet());
        }

        public MyDataJet load(int idToLoad) throws DataJetException {
                return (MyDataJet)load(idToLoad, new MyDataJet(), jetSet()); //call load method from DataJet
        }
}
The above DataJet will be able to create, read, update and delete records. For example:
//Example: create new record
MyDataJet dj = new MyDataJet();
dj.setFieldValue(1, "new value 1");
dj.setFieldValue(2, "new value 2");
dj.store();

See Also:
ConnectionGenerator, SqlBuilder

Constructor Summary
GenericDataJet(int id, JetSet jetSet)
          Constructor that sets the id for GenericDataJet to use.
GenericDataJet(JetSet jetSet)
          Constructor that accepts a JetSet.
 
Method Summary
 Datum getFieldValue(int fieldIndex)
          Returns the value of a field.
 java.lang.Object getFieldValueAsObject(int fieldIndex)
          Returns the value of a field.
static GenericDataJet[] loadAllJets(JetSet jetSet)
          Loads an array of all GenericDataJets from the database.
static GenericDataJet loadJet(int id, JetSet jetSet)
          Loads a GenericDataJet with identity field equal to id from the database.
 void remove()
          Removes the current GenericDataJet from the database.
 void setFieldValue(int fieldIndex, boolean newvalue)
          Sets the value of a field.
 void setFieldValue(int fieldIndex, byte newvalue)
          Sets the value of a field.
 void setFieldValue(int fieldIndex, Datum newvalue)
          Sets the value of a field.
 void setFieldValue(int fieldIndex, double newvalue)
          Sets the value of a field.
 void setFieldValue(int fieldIndex, float newvalue)
          Sets the value of a field.
 void setFieldValue(int fieldIndex, int newvalue)
          Sets the value of a field.
 void setFieldValue(int fieldIndex, long newvalue)
          Sets the value of a field.
 void setFieldValue(int fieldIndex, java.lang.Object newvalue)
          Sets the value of a field.
 void setFieldValue(int fieldIndex, short newvalue)
          Sets the value of a field.
 void store()
          Stores the current GenericDataJet in the database.
 
Methods inherited from class anderix.datajets.DataJet
clone, getId, getIdAsDatum
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericDataJet

public GenericDataJet(int id,
                      JetSet jetSet)
Constructor that sets the id for GenericDataJet to use. Please note that this creation method does not load the record into the DataJet from the database. Use the static loadJet method for this, or override DataJet.load.

Parameters:
id - integer representing the identity field value
jetSet - JetSet containing properties for the DataJet
See Also:
loadJet(int, anderix.datajets.JetSet), DataJet.load(int, anderix.datajets.DataJet, anderix.datajets.JetSet)

GenericDataJet

public GenericDataJet(JetSet jetSet)
Constructor that accepts a JetSet.

Parameters:
jetSet - JetSet containing properties for the DataJet
Method Detail

setFieldValue

public void setFieldValue(int fieldIndex,
                          int newvalue)
Sets the value of a field.

Parameters:
fieldIndex - zero-based ordinal position of field in fieldList (defined when initializing the DataJet)
newvalue - int value to set the field to

setFieldValue

public void setFieldValue(int fieldIndex,
                          boolean newvalue)
Sets the value of a field.

Parameters:
fieldIndex - zero-based ordinal position of field in fieldList (defined when initializing the DataJet)
newvalue - boolean value to set the field to

setFieldValue

public void setFieldValue(int fieldIndex,
                          short newvalue)
Sets the value of a field.

Parameters:
fieldIndex - zero-based ordinal position of field in fieldList (defined when initializing the DataJet)
newvalue - short value to set the field to

setFieldValue

public void setFieldValue(int fieldIndex,
                          long newvalue)
Sets the value of a field.

Parameters:
fieldIndex - zero-based ordinal position of field in fieldList (defined when initializing the DataJet)
newvalue - long value to set the field to

setFieldValue

public void setFieldValue(int fieldIndex,
                          float newvalue)
Sets the value of a field.

Parameters:
fieldIndex - zero-based ordinal position of field in fieldList (defined when initializing the DataJet)
newvalue - float value to set the field to

setFieldValue

public void setFieldValue(int fieldIndex,
                          double newvalue)
Sets the value of a field.

Parameters:
fieldIndex - zero-based ordinal position of field in fieldList (defined when initializing the DataJet)
newvalue - double value to set the field to

setFieldValue

public void setFieldValue(int fieldIndex,
                          byte newvalue)
Sets the value of a field.

Parameters:
fieldIndex - zero-based ordinal position of field in fieldList (defined when initializing the DataJet)
newvalue - byte value to set the field to

setFieldValue

public void setFieldValue(int fieldIndex,
                          java.lang.Object newvalue)
Sets the value of a field.

Parameters:
fieldIndex - zero-based ordinal position of field in fieldList (defined when initializing the DataJet)
newvalue - Object value to set the field to

setFieldValue

public void setFieldValue(int fieldIndex,
                          Datum newvalue)
Sets the value of a field.

Parameters:
fieldIndex - zero-based ordinal position of field in fieldList (defined when initializing the DataJet)
newvalue - Datum value to set the field to
See Also:
Datum

getFieldValue

public Datum getFieldValue(int fieldIndex)
Returns the value of a field.

Parameters:
fieldIndex - zero-based ordinal position of field in fieldList (defined when initializing the DataJet)
Returns:
Datum representation of the current value.
See Also:
Datum

getFieldValueAsObject

public java.lang.Object getFieldValueAsObject(int fieldIndex)
Returns the value of a field.

Parameters:
fieldIndex - zero-based ordinal position of field in fieldList (defined when initializing the DataJet)
Returns:
Object representation of the current value

loadJet

public static GenericDataJet loadJet(int id,
                                     JetSet jetSet)
                              throws DataJetException
Loads a GenericDataJet with identity field equal to id from the database.

Parameters:
id - the unique value of the identity field
jetSet - JetSet containing properties for the DataJet
Returns:
GenericDataJet containing data from the database
Throws:
DataJetException - if the connection to the database can not be established or if the DataJet has not been initialized

loadAllJets

public static GenericDataJet[] loadAllJets(JetSet jetSet)
                                    throws DataJetException
Loads an array of all GenericDataJets from the database.

Parameters:
jetSet - JetSet containing properties for the DataJet
Returns:
array of GenericDataJet objects representing all records in the database
Throws:
DataJetException - if the connection to the database can not be established or if the DataJet has not been initialized

store

public void store()
           throws DataJetException
Stores the current GenericDataJet in the database. This method can be used to add a new GenericDataJet or to update an existing one.

Specified by:
store in interface Storable
Throws:
DataJetException - if the connection to the database can not be established or if the DataJet has not been initialized
See Also:
DataJet.store(java.sql.Connection)

remove

public void remove()
            throws DataJetException
Removes the current GenericDataJet from the database.

Specified by:
remove in interface Removable
Throws:
DataJetException - if the connection to the database can not be established if the DataJet has not been initialized
See Also:
DataJet.remove(java.sql.Connection)