import java.util.*; import java.text.DecimalFormat; import java.io.*; /** * This class contains the collection of TE objects in a table. * @author Genjava v1.0 */ public class TEtable implements Serializable { private String Abbrev; private String Name; public Vector Rows; private int Rowcount; private int Maxver; private Database TheSchema; /** * TEtable 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 TEtable(Database mySchema) { Abbrev = "TE"; Name = "Teacher"; 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 TE which is that row */ public TE getFirstRow() { if( Rows.isEmpty() ) return null; return (TE)Rows.firstElement(); } /** * Get the last row in the table * @return a TE which is that row */ public TE getLastRow() { if( Rows.isEmpty() ) return null; return (TE)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 TE getNextRow(TE thisrow) { if( Rows.isEmpty() ) return null; if( Rows.indexOf( thisrow ) == ( Rows.size() - 1 ) ) return null; return (TE)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 TE getPrevRow(TE thisrow) { if( Rows.isEmpty() ) return null; if( Rows.indexOf( thisrow ) == 0 ) return null; return (TE)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 boolean insertRow(TE 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 ) { TE currRow = (TE)Rows.elementAt(i); if( currRow.getPkid().getValue() == newRow.getPkid().getValue() ) { System.out.println("Warning: Duplicate key value " + newRow.getPkid() + " found: row will not be inserted."); return false; } if( currRow.getPkid().getValue() > newRow.getPkid().getValue() ) break; } Rows.insertElementAt(newRow, i); Rowcount++; if( newRow.getPkid().getVersion() > Maxver ) Maxver = newRow.getPkid().getVersion(); return true; } /** * 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 boolean removeRow(RC oldRow) { Rows.removeElement(oldRow); return true; } /** * 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( TE 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; } }