// !!!!!!! Dies anpassen  !!!!!!!!!
package aufgaben.seyrich;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Vector;
import java.sql.SQLException;
import pizzasvc.db.*;

public class Aufg1Servlet extends HttpServlet {
    public final static String html_head="<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n<meta name=\"Author\" content=\"Sebastian Seyrich <sebastian.seyrich@student.uni/tuebingen.de>\">\n <meta name=\"KeyWords\" content=\"CORBA, LDAP,SERVLETS, Mastermind, Pizzaservice,OS/390, VIERGEWINNT\">\n<title>Client/Server-Praktikum - Antwortservlet Pizzaservice</title>\n<link rel=stylesheet type=\"text/css\" href=\"http://homepages.uni-tuebingen.de/student/sebastian.seyrich/seyrich.css\">\n</head>";
    public final static String html_headerrow = "<a class=\"linkNormal\" NAME=\"oben\"></a>\n<center><table CELLPADDING=5 WIDTH=\"96%\" HEIGHT=\"96%\" ><tr class=\"upperRowColor\">\n<td>&nbsp;<br>&nbsp;</td>\n<td VALIGN=TOP><a class=\"linkNormal\" NAME=\"oben\"></a><div ALIGN=right><font class=\"defaultFont\">\n<h1>Client/Server-Praktikum WS 2001/2002</h1></div><div align=right>Dozent: Spruth</div></font></td></tr>";
    public final static String html_linkcell = "<tr VALIGN=TOP><td class=\"leftColumnColor\"><blockquote><a href=\"http://homepages.uni-tuebingen.de/student/sebastian.seyrich/cs/index.html\" class=\"linkNormal\">Anfang</a>\n<br><a class=\"linkNormal\" href=\"http://homepages.uni-tuebingen.de/student/sebastian.seyrich/cs/aufgaben.html#aufgabe1\">Aufgabenstellung</a>\n<br><a class=\"linkNormal\" href=\"http://homepages.uni-tuebingen.de/student/sebastian.seyrich/cs/abgaben.html#blatt4\">Abgabebeschreibung</a>\n</blockquote>\n</td>";
    private final static String html_lowerborder = "<tr class=\"leftColumnColor\">\n<td>&nbsp;<br>&nbsp;</td>\n<td VALIGN=BOTTOM><div align=right><font class=\"defaultFont\"><font class=\"font1\">\n<b>&copy; <a class=\"linkNormal\"  href=\"mailto:Sebastian Seyrich<webmaster@transeuropalauf.de>?subject=C/S-Praktikum\">Sebastian Seyrich</a> \nund <a class=\"linkNormal\" href=\"mailto:Judith Bentele<judith.bentele@student.uni-tuebingen.de>?subject=C/S-Praktikum\">Judith Bentele</a> \nJanuar 2002</b><br>Erste Erstellung: 14. Januar 2002 - automatisch generierte Seiten ohne Datum\n</font></font></div>\n</td></tr></table>\n</center>";

    private pCategories   pCatList;  // Product Categories List Object
    private pDescriptions pDescList; // Product Descriptions List Object

    // Wird einmal ausgeführt, wenn die Klasse von Server instaziiert wird.
    public void init(ServletConfig config)
    throws ServletException
    {
	super.init(config);

	// Erzeuge Product Category List Objekt
	pCatList = new pCategories();
	// Erzeuge Product Descriptions List Object
	pDescList = new pDescriptions();

	pCatList.init();    // initalisieren
	pCatList.select();  // Produkt Kategorien aus der Datenbank laden
	pDescList.init();   // initialisieren
    }
    

    // Wird von einer HTML Seite mit dem POST HTTP-Kommando Aufgerufen
    public synchronized void doPost(HttpServletRequest request, 
		                    HttpServletResponse response)
    throws ServletException, IOException
    {
	int           pCategoryID;
	PrintWriter   pwriter;
	pCategory     pCat = null;
	Vector        pList;
        pDescription  pDescr;


	PrintWriter out = response.getWriter();

	// ----------------- HERE STARTS YOUR TASK ------------------------------------
	// Hole ProductCategory.id aus Aufg1.html
	pCategoryID = Integer.parseInt(request.getParameter("pCategory"));
	// Hole pCategory Objekt mit der ensprechender pCategoryID aus 
	// der Datenbank
	pCat = pCatList.getPCategoryByID(pCategoryID);
	// Uebermittle notwendige Daten fuer die Produktsuche an pDescriptions pDescList
	pDescList.setCategory(pCat);
	try{pDescList.select();}catch(Exception e){};
        // Ergebnisse der Abfrage als Vektor holen
	pList = pDescList.getPDescr();
	
	// ------------------- Antwort Generieren ---------------------------------
	// Caching ausschalten

	// ContentType setzen
	response.setContentType("text/html");

	// HTML Seite erzeugen
	// Wenn Ihr noch nie HTML Code konfrontiert wurdet
	// könnt Ihr eine HTML Seite mit Netscape öffnen, und mit "View, Page Source"
	// sehen wie die Seite "programmiert" ist.

	// Head Part
	out.println("<html>");
	out.println(html_head);


	//<html><body><h1>Sie haben gewählt:</h1><br><h3>"+pCat.getDescr()+"</h3>");
	// Body Part
	out.println("<body>");
	out.println(html_headerrow);

	out.println(html_linkcell);
	out.println("<td class=\"innerCellColor\"><div align=\"Justify\"><font class=\"defaultFont\">");

	out.println("<blockquote>");
	out.println("<h1>Sie haben "+pCat.getDescr()+" gewählt:</h1>");
	out.println("<br><div align=\"right\"><a class=\"linkNormal\" href=\"http://quak.informatik.uni-tuebingen.de/~cspuser/seyrich/Aufg1.html\"><font class=\"font1\">[zurück zur Auswahl]</font></a></div></font><br><br>");
	out.println("<table CELLPADDING=4 >");
	for(int i=0;i<pList.size();i++){	
	    // Jedes Product aus dem Vector pList holen und anzeigen
	    // Typecast nach pDescription haben wir nicht vergessen
	    pDescr = (pDescription)pList.elementAt(i);
	    // Erzeugte Zeile rausschreiben
	    out.println("<tr class=\"tablecsColor\"><td>&nbsp;"+pDescr.getTitle()+"</td><td>&nbsp;"+pDescr.getDescr()+"</td><td>&nbsp;"+pDescr.getPrice()+"</td></tr>");  

	}	
	out.println("</table></blockquote>");
	out.println("<div align=\"right\"><a class=\"linkNormal\" href=\"#oben\"><font class=\"font1\">[nach oben]</font></a></div></font><br>");
	out.println("<div align=\"right\"><a class=\"linkNormal\" href=\"http://quak.informatik.uni-tuebingen.de/~cspuser/seyrich/Aufg1.html\"><font class=\"font1\">[zurück zur Auswahl]</font></a></div></font></blockquote><br></td>");
	out.println("</td></tr>");
	out.println(html_lowerborder);
    	out.println("</body></html>");
	out.close();
	// -------------------- HERE ENDS YOUR TASK -------------------------------------
    }




}
