import java.io.*; import java.util.*; public class TableGenerator implements Serializable { protected PrintWriter file; Database theSchema; CodeGenerator theGenerator; public TableGenerator(CodeGenerator generator) { theGenerator = generator; theSchema = theGenerator.getTheSchema(); } protected void OpenFile(String filename) { try { file = new PrintWriter( new FileWriter(filename) ); theGenerator.RegisterFile(filename); } catch(IOException e) { System.err.println("GENJAVA-F-BADOUTPUT: " + filename + " - " + e.getMessage()); System.exit(-1); } } public void Generate() { String filename = new String(); for( TT TTcurr = theSchema.TTtab.getFirstRow(); TTcurr != null; TTcurr = theSchema.TTtab.getNextRow(TTcurr)) { filename = new String(Genjava.directory); filename += Genjava.filesep; filename += TTcurr.getTTabb(); filename += "table.java"; OpenFile( filename ); file.println("import java.util.*;"); file.println("import java.text.DecimalFormat;"); file.println("import java.io.*;"); file.println(); file.println("/**"); file.println(" * This class contains the collection of " + TTcurr.getTTabb() + " objects in a table."); file.println(" * @author Genjava v1.0"); file.println(" */"); file.println("public class " + TTcurr.getTTabb() + "table implements Serializable"); file.println("{"); file.println(" private String Abbrev;"); file.println(" private String Name;"); file.println(" public Vector Rows;"); file.println(" private int Rowcount;"); file.println(" private int Maxver;"); file.println(" private Database TheSchema;"); file.println(); file.println(" /**"); file.println(" * " + TTcurr.getTTabb() + "table constructor"); file.println(" * Initializes table name, abbreviation, row count and allocates"); file.println(" * space for the row objects in the table"); file.println(" * @param mySchema schema to which this table belongs"); file.println(" */"); file.println(" public " + TTcurr.getTTabb() + "table(Database mySchema)"); file.println(" {"); file.println(" Abbrev = \"" + TTcurr.getTTabb() + "\";"); file.println(" Name = \"" + TTcurr.getTTname() + "\";"); file.println(" Rowcount = 0;"); file.println(); file.println(" Rows = new Vector(1);"); file.println(" TheSchema = mySchema;"); file.println(" }"); file.println(); file.println(" /** "); file.println(" * Gets the name of the table"); file.println(" * @return a String containing the table name"); file.println(" */"); file.println(" public String getName()"); file.println(" {"); file.println(" return Name;"); file.println(" }"); file.println(); file.println(" /** "); file.println(" * Gets the abbreviation for the table"); file.println(" * @return a String containing the table name"); file.println(" */"); file.println(" public String getAbbrev()"); file.println(" {"); file.println(" return Abbrev;"); file.println(" }"); file.println(); file.println(" /** "); file.println(" * Gets the number of rows in table"); file.println(" * @return an integer containing the number of rows"); file.println(" */"); file.println(); file.println(" public int getRowcount()"); file.println(" {"); file.println(" return Rowcount;"); file.println(" }"); file.println(); file.println(" /** "); file.println(" * Get the first row in the table"); file.println(" * @return a " + TTcurr.getTTabb() + " which is that row"); file.println(" */"); file.println(" public " + TTcurr.getTTabb() + " getFirstRow()"); file.println(" {"); file.println(" if( Rows.isEmpty() )"); file.println(" return null;"); file.println(" return (" + TTcurr.getTTabb() + ")Rows.firstElement();"); file.println(" }"); file.println(" /** "); file.println(" * Get the last row in the table"); file.println(" * @return a " + TTcurr.getTTabb() + " which is that row"); file.println(" */"); file.println(" public " + TTcurr.getTTabb() + " getLastRow()"); file.println(" {"); file.println(" if( Rows.isEmpty() )"); file.println(" return null;"); file.println(" return (" + TTcurr.getTTabb() + ")Rows.lastElement();"); file.println(" }"); file.println(); file.println(" /** "); file.println(" * Given a row in the table, returns the next row"); file.println(" * @param thisrow the current row"); file.println(" * @return the next row, null if the row does not exist or if"); file.println(" * the current row is the last row."); file.println(" */"); file.println(" public " + TTcurr.getTTabb() + " getNextRow(" + TTcurr.getTTabb() + " thisrow)"); file.println(" {"); file.println(" if( Rows.isEmpty() )"); file.println(" return null;"); file.println(); file.println(" if( Rows.indexOf( thisrow ) == ( Rows.size() - 1 ) )"); file.println(" return null;"); file.println(); file.println(" return (" + TTcurr.getTTabb() + ")Rows.elementAt( Rows.indexOf( thisrow ) + 1 );"); file.println(" }"); file.println(); file.println(" /** "); file.println(" * Given a row in the table, returns the previous row"); file.println(" * @param thisrow the current row"); file.println(" * @return the next row, null if the row does not exist or if"); file.println(" * the current row is the first row."); file.println(" */"); file.println(" public " + TTcurr.getTTabb() + " getPrevRow(" + TTcurr.getTTabb() + " thisrow)"); file.println(" {"); file.println(" if( Rows.isEmpty() )"); file.println(" return null;"); file.println(); file.println(" if( Rows.indexOf( thisrow ) == 0 )"); file.println(" return null;"); file.println(); file.println(" return (" + TTcurr.getTTabb() + ")Rows.elementAt( Rows.indexOf( thisrow ) - 1 );"); file.println(" }"); file.println(); file.println(" /** "); file.println(" * Inserts a new row in the table."); file.println(" * Expects that the new row's linkRow method"); file.println(" * will be called to link the new row to its sibling rows in turn "); file.println(" * @param newRow the row to add"); file.println(" */"); file.println(" public boolean insertRow(" + TTcurr.getTTabb() + " newRow)"); file.println(" {"); file.println(" int i;"); file.println(); file.println(" newRow.setTable(this);"); file.println(); file.println(" if( newRow.getPkid() == null ) {"); file.println(" String keystr;"); file.println(" DecimalFormat twoDigits = new DecimalFormat( \"00\" );"); file.println(" DecimalFormat fourDigits = new DecimalFormat( \"0000\" );"); file.println(); file.println(" keystr = getAbbrev();"); file.println(" keystr += twoDigits.format(1);"); file.println(" keystr += fourDigits.format(Rowcount + 1);"); file.println(); file.println(); file.println(" Key newRowKey = new Key(keystr, TheSchema);"); file.println(" newRow.setPkid(newRowKey);"); file.println(" }"); file.println(); file.println(); file.println(" for( i = 0; i < Rows.size(); ++i ) {"); file.println(" " + TTcurr.getTTabb() + " currRow = (" + TTcurr.getTTabb() + ")Rows.elementAt(i);"); file.println(" if( currRow.getPkid().getValue() == newRow.getPkid().getValue() ) { "); file.println(" System.out.println(\"Warning: Duplicate key value \" + newRow.getPkid() + "); file.println(" \" found: row will not be inserted.\");"); file.println(" return false;"); file.println(" }"); file.println(); file.println(" if( currRow.getPkid().getValue() > newRow.getPkid().getValue() )"); file.println(" break;"); file.println(" }"); file.println(); file.println(); file.println(" Rows.insertElementAt(newRow, i);"); file.println(" Rowcount++;"); file.println(" if( newRow.getPkid().getVersion() > Maxver )"); file.println(" Maxver = newRow.getPkid().getVersion();"); file.println(); file.println(" /* added by AV-NT for logging */"); file.println(" if (GlobalVarsBean.getHcg_log()==1)"); file.println(" insert_row_log(newRow);"); file.println(" return true;"); file.println(" }"); file.println(); file.println(" /* added by AV-NT for logging */"); file.println(" /**"); file.println(" * This method is used to log the call to insert row"); file.println(" * @param newRow the new row added"); file.println(" */"); file.println(" public void insert_row_log(" + TTcurr.getTTabb() + " newRow)"); file.println(" {"); file.println(" Pr_log.logwait();"); file.println(" Pr_log.logstr(newRow.toString());"); file.println(" }"); file.println(" /** "); file.println(" * Deletes a row from the table."); file.println(" * Expects that the old row's unlinkRow method"); file.println(" * will be called to unlink the old row from its sibling rows in turn "); file.println(" * @param newRow the row to add"); file.println(" */"); file.println(" public boolean removeRow(RC oldRow)"); file.println(" {"); file.println(" Rows.removeElement(oldRow);"); file.println(" /* added by AV-NT for logging */"); file.println(" if (GlobalVarsBean.getHcg_log()==1)"); file.println(" delete_row_log(oldRow);"); file.println(" /* */"); file.println(" return true;"); file.println(" }"); file.println(" /* added by AV-NT for logging */"); file.println(" /**"); file.println(" * This method is used to log the call to delete row"); file.println(" * @param newRow the row deleted"); file.println(" */"); file.println(" public void delete_row_log(RC oldRow)"); file.println(" {"); file.println(" Pr_log.logwait();"); file.println(" Pr_log.logstr(\"DL \"+oldRow.getPkid());"); file.println(" }"); file.println(); file.println(" /** "); file.println(" * Writes the contents of the table row by row to a given output stream."); file.println(" * @param output the output stream being "); file.println(" * written to"); file.println(" * @exception IOException if an error writing is encountered"); file.println(" */"); file.println(" public void dumpTable(BufferedWriter output) throws IOException"); file.println(" {"); file.println(" for( " + TTcurr.getTTabb() + " curr = getFirstRow();"); file.println(" curr != null;"); file.println(" curr = getNextRow(curr)) {"); file.println(" curr.dumpRow(output);"); file.println(" }"); file.println(" }"); file.println(); file.println(" /** "); file.println(" * @return the Database object this table belongs to"); file.println(" */"); file.println(" public Database getTheSchema()"); file.println(" {"); file.println(" return TheSchema;"); file.println(" }"); file.println(); file.println(" /** "); file.println(" * Attaches the table to a particular database schema"); file.println(" * @param value the new schema to attach to"); file.println(" */"); file.println(" public void setTheSchema(Database value)"); file.println(" {"); file.println(" TheSchema = value;"); file.println(" }"); file.println("}"); file.close(); } // end for } // end Generate }