import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.rmi.*;

public class RMIInventoryServlet extends HttpServlet {

  private Inventory rmiInventoryServer = null;
  private String rmiServerIP = "localhost";

  // Setup static connection to Security Manager.
  static {
    if(System.getSecurityManager() == null) {
      System.setSecurityManager(new RMISecurityManager());
    }
  }

  public void init(ServletConfig config) throws ServletException {

    // Always pass the ServletConfig object to the super class
    super.init(config);

    // Get a handle to the Inventory RMI Server.
    try {
      rmiInventoryServer = (Inventory)Naming.lookup("rmi://" +
        rmiServerIP + ":2001/Inventory");
    } catch (Exception ex) {
      System.out.println("Caught exception = " + ex);
      ex.printStackTrace();
    }
 
  }

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

    doPost(request, response); 
  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>RMI Inventory</title></head>");
    out.println("<body>");

    // Get all the parameter names
    Enumeration parameters = request.getParameterNames();
    String param = null;

    // Interate over the names, getting the parameters
    while(parameters.hasMoreElements()) {

      param = (String)parameters.nextElement();

      if(param.equals("Query Product Type") ) {
        String productType = request.getParameter(param);
        String title = request.getParameter("Title");
        String artist = request.getParameter("Artist-Author");
        out.println("<B>looking up product type: " + productType + 
          "</B></BR>");

        // Only checking for Product Type of CD.
        if(productType.equals("CD") || productType.equals("cd")) {

          // rmi client stuff
          // Create a CD to lookup w/ bogus values for price, quantity.
          CD lookupCD = new CD(title, artist, -1, -1);

          // Write information to HTML client.
          String cd = "<B>CD information: Title=" + lookupCD.getTitle() + 
            ", Artist=" + lookupCD.getArtist() + "</B><BR>";
          out.println(cd);

          System.out.println("Looking for cd Title=" + 
          lookupCD.getTitle() + ", Artist=" + lookupCD.getArtist() +
            " in Inventory");

          // Execute method on RMI object.
          int qty = rmiInventoryServer.getQuantityInInventory(lookupCD);

          if(qty > 0) {
            // Debug for WebServer console.
            System.out.println("Found CD .");
            System.out.println("\t quantity in Inventory = " + qty);

            // Write HTML to client.
            out.println("<B> Quantity in Inventory is: " + 
              qty + "</B><BR>");
          }
        } else {
          // Write error to HTML client.
          out.println("<B> Inventory does not exist for requested " +
            productType + ".</B><BR>");
        }
      }
    }

    out.println("</body></html>");
    out.close();
  }

  //Get Servlet information
  
  public String getServletInfo() {
    return "RMIInventoryServlet";
  }
}
