/*
 * DatabaseBuilderServlet.java
 *
 * Created on 8 de Março de 2001, 14:17
 */

package com.newtradebr.servlets;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author  Christian Rauh
 * @version 0.1
 */
public class DatabaseBuilderServlet extends DatabaseHttpServlet {
    
    // Variables used to create the table
    
    protected String title = "DatabaseBuilderServlet";
    protected String tableName = "ExampleTable";
    protected String fields[] = {"name VARCHAR(50)", "email VARCHAR(50)", "date DATETIME"};
    
    private void printHeader(PrintWriter p) {
        p.println("<TITLE>" + title + "</TITLE>");
        p.println("<H1>" + title + "</H1>");
        p.println("<H2>Database Builder</H2>");
    }
    
    private void createTable()
    throws Exception, SQLException {
        
        // Only proceed if we have at least one field to add to the table
        if (fields.length == 0) {
            
            throw new Exception("There must be at least one field on the table to create it.");
            
        } else {
            
            Statement stmt = dbConnection.createStatement();
            
            // Destroy the table if it already exists
            
            try {
                String dropTable = "DROP TABLE " + tableName;
                stmt.executeUpdate(dropTable);
            } catch (SQLException sqle) {
            }
            
            // Mount the query that creates the table
            
            StringBuffer createTable = new StringBuffer("CREATE TABLE " + tableName + " (");
            for (int i=0; i<fields.length-1 ; i++) {
                createTable.append(fields[i] + ", ");
            }
            
            // We can add this field because there is at least one field to add
            // since otherwiser we would have thrown an exception previously
            
            createTable.append(fields[fields.length-1] + ")");
            
            System.out.println("Query: " + createTable);
            
            // Execute the query creating the table
            
            stmt.executeUpdate(createTable.toString());
        }
    }
    
    public void doWork(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        printHeader(out);
        
        try {
            
            out.println("Creating table '" + tableName + "'...");
            createTable();
            out.println("OK<BR>");
            
        } catch (Exception e) {
            out.println("<P><B>Error creating table!<B>");
            out.println("<P>Exception caught:<BR>" + e);
            out.close();
            return;
        }
        
        out.println("<P><B>Finished.</B>");
        out.close();
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
        // Call the method that does the work.
        doWork(request, response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
        // Call the method that does the work.
        doWork(request, response);
    }
    
}