import java.util.*;
import java.text.DecimalFormat;
import java.io.*;
/**
* This class contains the collection of TT objects in a table.
* @author Genjava v1.0
*/
public class TTtable
{
private String Abbrev;
private String Name;
public Vector Rows;
private int Rowcount;
private int Maxver;
private Database TheSchema;
/**
* TTtable constructor
* Initializes table name, abbreviation, row count and allocates
* space for the row objects in the table
* @param mySchema schema to which this table belongs
*/
public TTtable(Database mySchema)
{
Abbrev = "TT";
Name = "TableTypes";
Rowcount = 0;
Rows = new Vector(1);
TheSchema = mySchema;
}
/**
* Gets the name of the table
* @return a String containing the table name
*/
public String getName()
{
return Name;
}
/**
* Gets the abbreviation for the table
* @return a String containing the table name
*/
public String getAbbrev()
{
return Abbrev;
}
/**
* Gets the number of rows in table
* @return an integer containing the number of rows
*/
public int getRowcount()
{
return Rowcount;
}
/**
* Get the first row in the table
* @return a TT which is that row
*/
public TT getFirstRow()
{
if( Rows.isEmpty() )
return null;
return (TT)Rows.firstElement();
}
/**
* Get the last row in the table
* @return a TT which is that row
*/
public TT getLastRow()
{
if( Rows.isEmpty() )
return null;
return (TT)Rows.lastElement();
}
/**
* Given a row in the table, returns the next row
* @param thisrow the current row
* @return the next row, null if the row does not exist or if
* the current row is the last row.
*/
public TT getNextRow(TT thisrow)
{
if( Rows.isEmpty() )
return null;
if( Rows.indexOf( thisrow ) == ( Rows.size() - 1 ) )
return null;
return (TT)Rows.elementAt( Rows.indexOf( thisrow ) + 1 );
}
/**
* Given a row in the table, returns the previous row
* @param thisrow the current row
* @return the next row, null if the row does not exist or if
* the current row is the first row.
*/
public TT getPrevRow(TT thisrow)
{
if( Rows.isEmpty() )
return null;
if( Rows.indexOf( thisrow ) == 0 )
return null;
return (TT)Rows.elementAt( Rows.indexOf( thisrow ) - 1 );
}
/**
* Inserts a new row in the table.
* Expects that the new row's linkRow method
* will be called to link the new row to its sibling rows in turn
* @param newRow the row to add
*/
public void insertRow(TT newRow)
{
int i;
newRow.setTable(this);
if( newRow.getPkid() == null ) {
String keystr;
DecimalFormat twoDigits = new DecimalFormat( "00" );
DecimalFormat fourDigits = new DecimalFormat( "0000" );
keystr = getAbbrev();
keystr += twoDigits.format(1);
keystr += fourDigits.format(Rowcount + 1);
Key newRowKey = new Key(keystr, TheSchema);
newRow.setPkid(newRowKey);
}
for( i = 0; i < Rows.size(); ++i ) {
TT currRow = (TT)Rows.elementAt(i);
if( currRow.getPkid().getValue() >= newRow.getPkid().getValue() )
break;
}
Rows.insertElementAt(newRow, i);
Rowcount++;
if( newRow.getPkid().getVersion() > Maxver )
Maxver = newRow.getPkid().getVersion();
}
/**
* Deletes a row from the table.
* Expects that the old row's unlinkRow method
* will be called to unlink the old row from its sibling rows in turn
* @param newRow the row to add
*/
public void removeRow(RC oldRow)
{
Rows.removeElement(oldRow);
}
/**
* Writes the contents of the table row by row to a given output stream.
* @param output the output stream being
* written to
* @exception IOException if an error writing is encountered
*/
public void dumpTable(BufferedWriter output) throws IOException
{
for( TT curr = getFirstRow();
curr != null;
curr = getNextRow(curr)) {
curr.dumpRow(output);
}
}
/**
* @return the Database object this table belongs to
*/
public Database getTheSchema()
{
return TheSchema;
}
/**
* Attaches the table to a particular database schema
* @param value the new schema to attach to
*/
public void setTheSchema(Database value)
{
TheSchema = value;
}
public static void main(String[] args)
{
// TTtable theTable = new TTtable();
TT row1 = new TT();
row1.setSVid("SV010001");
row1.setTTabb("EM");
row1.setTTname("empty");
row1.setDescr("/* EM - Empty table for testing only */");
// theTable.insertRow(row1);
TT row2 = new TT();
row2.setSVid("SV010001");
row2.setTTabb("ON");
row2.setTTname("onerow");
row2.setDescr("/* ON - table containing only one row for testing only */");
// theTable.insertRow(row2);
TT row3 = new TT();
row3.setSVid("SV010001");
row3.setTTabb("DE");
row3.setTTname("DEPT");
row3.setDescr("/* DE - University Department */");
// theTable.insertRow(row3);
TT row4 = new TT();
row4.setSVid("SV010001");
row4.setTTabb("CO");
row4.setTTname("course");
row4.setDescr("/* CO - Course information */");
// theTable.insertRow(row4);
TT row5 = new TT();
row5.setSVid("SV010001");
row5.setTTabb("PR");
row5.setTTname("prereq");
row5.setDescr("/* PQ - Prerequisite */");
// theTable.insertRow(row5);
// for( TT curr = theTable.getFirstRow();
// curr != null;
// curr = theTable.getNextRow(curr)) {
// System.out.println(curr);
// }
}
}