public Mapping(int numberOfAttributes,String theKeyField,String theKey)
public Mapping(String theKey, String theKeyField, int numberOfAttributes)
public static int lastPosition(String tableName)
public static boolean exists(Vector attributes)
public static Hashtable getObjectIdentifiers(String query)
public String getValue(String fieldName)
public void initialiseTables()
public void changeValue(String fieldName, int value)
public void changeValue(String fieldName, String value)
public void changeValue(String fieldName, double value)
public void displayContents()
public void displayTable()
public static void printRow(Hashtable table)
public String getAttributeSize(Hashtable contents, int position)
public String getAttributeValue(Vector contents, int position)
public String getAttributeName(Hashtable names,int columnNo)
public void createString(FString str)
public void createInt(FInt pint)
public void createDouble(FDouble value)
public void createTable()
public void store()
public void delete()
public String getIdentifier()
public void test()
// Mapping.java
// implements DatabaseConnect.java
// provides a framework for object relational mapping
import java.lang.Class;
import java.util.*;
public class Mapping{
// the attributes/variables
of the Mapping class
public DatabaseConnect
database;
private int fields;
private int fieldCount;
private boolean tableCreated;
private String createSql;
private String attributes;
private String objectName;
private final String query;
//can not be modified
private Class pclass;
private String keyField;
private String key;
private Hashtable sizes;
private Hashtable
columnNames;
private Hashtable
columnTypes;
private Vector table; // contains names and values.
Identified by column
//number
// the vectors can contain
all the values in the database, but I use them
// to hold information about
a single object as specified by the String query.
private Vector contents; // contains values.
private Vector
namedAttributes; // contains
attributes, identified by the attributes
//name.
//******************************************************************
// This constructor is used
to create a new instance of an object
// which has not been
previously stored in the database table.
It is also
// called by the second
constructor.
public Mapping (int
numberOfAttributes,String theKeyField,String theKey){
//System.out.println("Mapping Constructor called (36)");
keyField=theKeyField;
key=theKey;
fields=numberOfAttributes;
createSql="";
attributes="(";
database=new
DatabaseConnect();
database.openConn();
database.createStmt();
tableCreated=false;
fieldCount=0;
pclass=this.getClass();
objectName=pclass.getName();
query="SELECT * FROM
"+objectName+" WHERE "+keyField+" LIKE '" +
key+"'";
// checks if the table
specified in objectName exists
//System.out.println("objectName="+objectName+"
Framework(63)");
String queryTable
="SELECT * FROM "+objectName;
contents =
database.getResults(queryTable);
// Display contents of
contents Vector
//System.out.println("Attributes = (60)");
//database.displayResults(contents);
boolean tableExists;
tableExists =
exists(contents);
if(tableExists)
tableCreated=true;
}// End of first constructor.
//******************************************************************
// This cornstructor is used
to reinitialise an object that are already
// stored in a database
table. It first calls the previous
constructor and
// then proceeds to retrive
the values of the stored object.
public Mapping(String theKey,
String theKeyField, int numberOfAttributes){
this(numberOfAttributes,theKeyField,theKey);
//System.out.println("Two value Constructor called (88)");
//System.out.println("value of key = "+key +"(90));
// tableCreated=true;
query="SELECT * FROM
"+objectName+" WHERE "+keyField+" LIKE '"+
key+"'";
//System.out.println("The query = "+query+" (92)");
// Check if the object
exists in the specified table
table =
database.getDisplayableResults(query);
contents = database.getResults(query);
boolean objectExists;
objectExists =
exists(contents);
if(objectExists){
//System.out.println("the objectExists (103)");
// do this for every
column in table
this.initialiseTables();
int length
=columnNames.size();
// If the column Type
is a "String" use createString(name,value,size).
// If it is a
"double" use createDouble(name,value);
// If it is an
"int" use createInt(name,value)
for(int
i=1;i<=length;i++){
Integer col=new
Integer(i);
String
test=columnTypes.get(col.toString()).toString();
// examine the
database dependent type names
//
System.out.println("test = "+test+"(118)");
// Test for String
// String maps to
TEXT
if(test.toLowerCase().endsWith("t")){
// Type is a
String
//
System.out.println("The columnType is of String (125)");
String
anAttribute=this.getAttributeName(columnNames,i);
//
System.out.println("Attribute Name = "+anAttribute+"
(128)");
String
anAttributeValue=this.getAttributeValue(contents,i);//Vector,int
// System.out.println("Attribute
Value = "+anAttributeValue+" (131)");
String
anAttributeSize=this.getAttributeSize(sizes,i);
Integer
number=new Integer(anAttributeSize);
FString all=new
FString(anAttribute,anAttributeValue,number.intValue());
this.createString(all);
}
// Test for int
// int maps to LONG
else
if(test.toLowerCase().endsWith("g")){
// Type is an
int
// System.out.println("The
columnType is an int (147)");
String
anAttribute=this.getAttributeName(columnNames,i);
//
System.out.println("Attribute Name = "+anAttribute+"
(150)");
String
anAttributeValue=this.getAttributeValue(contents,i);//Vector,int
//
System.out.println("Attribute Value = "+anAttributeValue+"
(153)");
String
anAttributeSize=this.getAttributeSize(sizes,i);
Integer
number=new Integer(anAttributeValue);
FInt all=new
FInt(anAttribute,number.intValue());
this.createInt(all);
}
// Test for double
// double maps to
DOUBLE
else
if(test.toLowerCase().endsWith("e")){
// Type is a double
//
System.out.println("The columnType is a double (170)");
String
anAttribute=this.getAttributeName(columnNames,i);
//
System.out.println("Attribute Name = "+anAttribute+"
(173)");
String
anAttributeValue=this.getAttributeValue(contents,i);//Vector,int
//
System.out.println("Attribute Value = "+anAttributeValue+"
(176)");
String
anAttributeSize=this.getAttributeSize(sizes,i);
Double number=new Double(anAttributeValue);
FDouble all=new
FDouble(anAttribute,number.doubleValue());
this.createDouble(all);
}
else{
// An invalid
parameter has been passed so take appropriate action
System.err.println("The parameters type *** "+test+" ***
is invalid");
}
}// end for loop.
}// end if statement.
else{
// The object specified
by the query key does not exist
System.err.println("The object specified by the identifer
"+key+" does not exist.");
}
}// end of second
constructor.
//******************************************************************
// Class methods
//******************************************************************
// Returns the number of
objects stored in the named table
public static int
lastPosition(String tableName){
String getLast =
"SELECT * FROM "+tableName;
DatabaseConnect db = new
DatabaseConnect();
Vector lastPosition =
db.getResults(getLast);
int last =
lastPosition.size();
System.out.println("Number of objects stored in
"+tableName+" = "+last);
db.closeAll();
return last;
}
//******************************************************************
// Returns true if the Vector
attributes contains any elements
public static boolean
exists(Vector attributes){
if(attributes.isEmpty())
return false;
else
return true;
}
//******************************************************************
// Returns the identifier of
a row in a database table.
// Accepts a String
specifying the SQL query which selects the key field.
public static Hashtable
getObjectIdentifiers(String query){
Hashtable identifiers;
DatabaseConnect db = new
DatabaseConnect();
identifiers=db.getPrimaryKeys(query);
return identifiers;
}
// Instance Methods
//******************************************************************
// Used to return a value of
a specified database column for a stored object.
// Note the value returned
will be a String object and will need an explicit
// cast if the String contains
an int or a double.
public String getValue(String
fieldName){
namedAttributes=database.getNamedAttributes(query);
Hashtable temp
=(Hashtable)namedAttributes.elementAt(0);
//System.out.println("name of column = "+fieldName);
String value=temp.get(fieldName).toString();
//System.out.println("Value of value = "+value+
"FRAMEWORK (253)");
return value;
}
//******************************************************************
// Initialises the hashtable
and vectors to the appropriate values in the database.
// Table contains both column
names and values, column names,types and sizes
//contains information about
the objects attribute names, types and maximum sizes.
public void
initialiseTables(){
//System.out.println(query+" 53");
table =
database.getDisplayableResults(query);
//database.displayResults(table);
columnNames =
database.getColumnNames(query);
// Test the values
contained in columnNames
// printRow(columnNames);
//
System.out.println("(66)");
columnTypes =
database.getColumnTypes(query);
// Test the values
contained in columnNames
// printRow(columnTypes);
//
System.out.println("(72)");
sizes=database.getColumnSizes(query);
// Test the values
contained sizes
// printRow(sizes);
//
System.out.println("(78)");
contents=database.getResults(query);
}
//******************************************************************
// Changes the value of a
specified field. Takes an int as second parameter
public void
changeValue(String fieldName, int value){
String
command="UPDATE "+objectName+" SET
"+fieldName+"="+value+
" WHERE "+keyField+" LIKE '"+key+"'";
database.exeSql(command);
}
//******************************************************************
// Changes the value of a
specified field. Takes a String as second parameter
public void
changeValue(String fieldName, String value){
String
command="UPDATE "+objectName+" SET
"+fieldName+"='"+value+
"' WHERE "+keyField+" LIKE '"+key+"'";
database.exeSql(command);
}
//******************************************************************
// Changes the value of a
specified field. Takes a double as second parameter
public void
changeValue(String fieldName, double value){
String
command="UPDATE "+objectName+" SET "+fieldName+"
="+value+
" WHERE "+keyField+" LIKE '"+key+"'";
database.exeSql(command);
}
//******************************************************************** // Displays the contents of the object,
includes column headings.
public void
displayContents(){
// ensure table contains
the current values
table =
database.getDisplayableResults(query);
database.displayResults(table);
System.out.println("
");
}
//******************************************************************
// Displays the entire
contents of the table containing the clling object
public void displayTable(){
String getAll =
"SELECT * FROM "+objectName;
Vector all =
database.getDisplayableResults(getAll);
database.displayResults(all);
System.out.println(" ");
}
//******************************************************************
// Prints the contents of a
Hashtable. The values are identified by
number.
public static void
printRow(Hashtable table){
for(int j=1;j<=table.size();j++){
Integer i = new
Integer(j);
String
value=table.get(i.toString()).toString();
System.out.print(value);
}
System.out.println();
}
//******************************************************************
// Returns the maximium size
of an attribute as set up in the existing database.
public String
getAttributeSize(Hashtable contents, int position){
Integer i = new
Integer(position);
String
value=contents.get(i.toString()).toString();
return value;
}
//******************************************************************
// Returns an attribute
value, as specified by column position.
// Uses a Vector but only
accesses the first Hashtable in it.
public String getAttributeValue(Vector
contents, int position){
Hashtable table
=(Hashtable)contents.elementAt(0);//changed
Integer i = new
Integer(position);
String
value=table.get(i.toString()).toString();
return value;
}
//******************************************************************
// Returns the column name of
the column specified by columnNo
public String
getAttributeName(Hashtable names,int columnNo){
String attribute;
Integer j=new
Integer(columnNo);
attribute = names.get(j.toString()).toString();
return attribute;
}
//******************************************************************
// Appends String attributes
sizes and type to the createSql and attributes Strings.
public void createString(FString
str){
fieldCount++;
// define the primary key
field
// if str.getName == the
if(tableCreated==false
&& fieldCount!= fields){
createSql+="
"+str.getName()+ " varchar("+str.getLength()+"),";
attributes
+="'"+str.getContents()+"',";
}
else
if(tableCreated==false && fieldCount==fields){
createSql+="
"+str.getName()+" varchar("+str.getLength()+")";
attributes +="
'"+ str.getContents()+"'";
}
else{
attributes +="
'"+ str.getContents()+"'";
}
}
//******************************************************************
// Appends int attributes
sizes and type to the createSql and attributes Strings.
public void createInt(FInt
pint){
fieldCount++;
if(tableCreated==false
&& fieldCount!= fields){
createSql+="
"+pint.getName()+ " int,";
attributes +="
"+pint.getContents()+" ,";
}
else
if(tableCreated==false && fieldCount==fields){
createSql+="
"+pint.getName()+" int";
attributes +="
"+ pint.getContents()+" ";
}
else
{
attributes +="
"+ pint.getContents()+" ";
}
}
//******************************************************************
// Appends double, attributes
sizes and type to the createSql and attributes Strings.
public void
createDouble(FDouble value){
fieldCount++;
if(tableCreated==false
&& fieldCount!= fields){
createSql+="
"+value.getName()+ " float,";
attributes +="
"+value.getContents()+" ,";
}
else
if(tableCreated==false && fieldCount==fields){
createSql+="
"+value.getName()+" float";
attributes +=" "+ value.getContents()+" ";
}
else{
attributes +=" "+ value.getContents()+" ";
}
}
//******************************************************************
// Creates a database table,
uses arguments specified in the createXXX methods
// Defines the primary key
field as specified and appends it.
// Primary key definition is
specifically for a Microsoft Access database.
public void createTable(){
String sql="CREATE
TABLE "+objectName+"(";
createSql+=",CONSTRAINT [index1] PRIMARY
KEY(["+keyField+"]))";
database.openConn();
database.createStmt();
database.exeSql(sql+createSql);
// This can cause problems
if work with the database is not finished
//database.closeAll();
}
//******************************************************************
// Stores the object in a
database.
public void store(){
String sql="INSERT
INTO "+objectName+" VALUES "+attributes+")";
//System.out.println("Query is :"+sql);
database.openConn();
database.createStmt();
database.exeSql(sql);
//System.out.println("Initialising Tables");
this.initialiseTables();
//System.out.println("Tables Initialised");
}
//******************************************************************
// Deletes the attributes of
a stored database
public void delete(){
String sql="DELETE *
FROM "+objectName+" WHERE "+keyField+" LIKE "+
"'"+key+"'";
database.openConn();
database.createStmt();
database.exeSql(sql);
}
//******************************************************************
// Returns the unique
identifier of the object
public String
getIdentifier(){
return key;
}
//******************************************************************
// Displays test information.
public void test(){
System.out.println("The createSql string = "+createSql);
System.out.println("The attribute string = "+attributes);
System.out.println("Class Name= "+objectName);
}
}// End of Mapping.java