import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

import HTML.*;

/*
    This servlet represents the final step in the order
    process.  It takes a request, empties the contents of
    the shopping basket and responds with a simple
    thank you message.  This is where you would add the
    appropriate functionality to handle your orders.
    This could be sending a message to fulfillment or just
    logging it in a database for later processing.  This
    servlet inherits its doGet() method from CatalogServlet.
*/

public class ProcessOrderServlet extends CatalogServlet {

  // Method Name: init()
  // Purpose: This is the default init() method.
  public void init(ServletConfig config)
    throws ServletException {

    super.init(config);
  }

  // Method Name: emptyBasket()
  // Purpose: This method gets a reference to the basket
  // Vector and calls the removeAllElements method, emptying
  // the entire contents of the shopping basket.
  private void emptyBasket(HttpServletRequest request) {

    // Get/Create a reference to the HttpSession object.
    HttpSession session = request.getSession(true);

    if ( session != null ) {

      // Get a reference to the basket Vector.
      Vector basket = (Vector)session.getValue("basket");

      // If basket is null, create one
      if ( basket != null ) {

        // Empty the basket.
        basket.removeAllElements();
      }
    }
  }

  // Method Name: buildClientArea()
  // Purpose: This method implements its parents abstract
  // method.  It represents the client area of the browser
  // window.
  public HTMLTable buildClientArea(HttpServletRequest request)
    throws Exception {

    // empty the shopping basket, so future requests will
    // begin with an empty basket
    emptyBasket(request);
    
    // We are now getting ready to checkout.
    HttpSession session = request.getSession(true);
    Vector basket = null;

    // Create a table container for the client area.
    HTMLTable table = new HTMLTable();
    // Set a fixed width.
    table.setWidthByPixel(400);
    table.setAlignment(HTMLObject.CENTER);

    HTMLTableRow row = new HTMLTableRow();
    HTMLTableCell cell = new HTMLTableCell(HTMLTableCell.DATA);
    // Create and add a simple thank you message
    cell.addObject(new HTMLHeading("Thank you for shopping" +
      " with Sams!", HTMLHeading.H3));

    row.addObject(cell);
    table.addObject(row);

    return table;
  }

  //Get Servlet information
  public String getServletInfo() {

    return "ProcessOrderServlet Information";
  }
}
