import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

import HTML.*;

/*
    This servlet is the base class for all of our servlets
    with visual responses.  It is an abstract class that
    defines a single abstract method called buildClientArea.
*/

public abstract class CatalogServlet extends HttpServlet {

  // Method Name: init()
  // Purpose: This is the default init() method.
  public void init(ServletConfig config)
    throws ServletException {

    super.init(config);
  }

  // Method Name: buildTopTable()
  // Purpose: This method builds a table that acts as a
  // container for the LogoCell, SearchCell and BasketCell
  // Objects.  All classes that inhereit from this class will
  // have this table included.
  private HTMLTable buildTopTable(HttpServletRequest request) {

    HTMLTable table = new HTMLTable();
    table.setWidth(100);

    HTMLTableRow row = new HTMLTableRow();

    // Create and add the LogoCell
    LogoCell logo_cell = new LogoCell();
    logo_cell.setAlignment(HTMLObject.CENTER);
    row.addObject(logo_cell);

    // Create and add the SearchCell
    SearchCell search_cell = new SearchCell();
    search_cell.setAlignment(HTMLObject.CENTER);
    row.addObject(search_cell);

    // Create and add the BasketCell
    row.addObject(new BasketCell(request));
    table.addObject(row);

    return table;
  }

  // Method Name: buildBottomTable()
  // Purpose: This method builds a table that acts as a
  // container for the NavigationTable and Client Area
  // Objects.  All classes that inhereit from this class will
  // have this table included.
  private HTMLTable
    buildBottomTable(HttpServletRequest request)
    throws Exception {

    HTMLTable table = new HTMLTable();
    table.setVerticalAlign(HTMLObject.TOP);
    table.setHeight(500);
    HTMLTableRow row = new HTMLTableRow();

    // Create and add the NavigationTable
    HTMLTableCell cell = new HTMLTableCell(HTMLTableCell.DATA);
    cell.setVerticalAlign(HTMLObject.TOP);
    cell.setHorizontalAlign(HTMLObject.LEFT);
    cell.addObject(new NavigationTable(getServletContext()));

    // Call the child class'es buildClientArea method, which
    // returns an HTMLTable object to be added to the
    // bottom table.
    cell.addObject(buildClientArea(request));
    row.addObject(cell);

    table.addObject(row);

    return table;
  }

  // Method Name: buildClientArea()
  // Purpose: This is the abstract method that must be
  // implemented by all children.  It represents the client
  // area in the bottom table.
  public abstract HTMLTable
    buildClientArea(HttpServletRequest request)
    throws Exception;

  // Method Name: processRequest()
  // Purpose: This method defines the framework for all child
  // classes.
  public final HTMLDocument
    processRequest(HttpServletRequest request)
    throws Exception {

    // Create the HTMLDocument
    HTMLDocument doc =
      new HTMLDocument("Sams Online Video Store");

    HTMLTable table = new HTMLTable();
    HTMLTableRow row = new HTMLTableRow();
    HTMLTableCell cell = new HTMLTableCell(HTMLTableCell.DATA);

    // build the Top Table
    cell.addObject(buildTopTable(request));
    row.addObject(cell);
    table.addObject(row);

    // build the Bottom Table
    row = new HTMLTableRow();
    cell = new HTMLTableCell(HTMLTableCell.DATA);
    cell.addObject(buildBottomTable(request));
    row.addObject(cell);
    table.addObject(row);
    
    doc.addObject(table);

    return doc;
  }

  // Method Name: doGet()
  // Purpose: This is the doGet() method for all children.
  // This method further defines the catalog servlet's
  // framework.
  public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    try {

      // Send the request through the processRequest()
      // method, which returns an HTMLDocument.  The
      // returned HTMLDocument.toHTML() method is then
      // called returning a String representing the HTML
      // text.
      out.println(processRequest(request).toHTML());
    }
    catch (Exception e) {

      throw new ServletException(e.getMessage());
    }
    out.close();
  }
}
