//ShowByLocation
//Shows all the complaints by location
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class ShowByLocation extends HttpServlet
{
	// define a few things we'll need
	private HttpSession session = null;
	private Connection conn = null;
	private Statement stmt = null;
	Configure cfg = new Configure();

	public void init (ServletConfig config)
		throws ServletException
	{
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection (cfg.dbPath, cfg.dbUsername, cfg.dbPassword);
		}

		catch (SQLException sqe)
		{
			sqe.printStackTrace();
		}

		catch (ClassNotFoundException ce)
		{
			System.out.println(ce);
		}
		System.out.println("Init Successful");
	}
	
	// This shows a form where you can enter a location
	public void doGet (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
		session = req.getSession (true);
		PrintWriter out = res.getWriter();
		res.setContentType ("text/html");
		String location = req.getParameter ("location");
		
		try
		{
			Statement stmt = conn.createStatement();
			out.println("<html><head><title>Show complaints by location</title></head><body bgcolor=#ffffff><h1>Show complaints by location</h1>");
			if (location == null)
			{
			ResultSet rs = stmt.executeQuery ("SELECT distinct location from COMPLAINT");
		
			out.println("<form action=\""+cfg.ServletDir+"ShowByLocation\" method=\"GET\">"+
			"Select location: <select name=\"location\">");

			while (rs.next())
			{
				out.println("<option value=\""+rs.getString(1)+"\">"+rs.getString(1));
			}
		
			out.println("</select><input type=\"submit\"></form></body></html>");
			}
			else
			{
			ResultSet rs = stmt.executeQuery ("SELECT complaint_id,description from COMPLAINT where location='"+location+"'");
			
			out.println("<table>");
			while (rs.next())
			{
			out.println("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td></tr>");
			}
			out.println("</table></body></html>");
			}
		}
		catch(Exception e)
		{
			out.println(e);
		}
	}

	// Frankly i dont know what this does. but what the heck
	public void doDelete (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
	}

	public void doPost (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
		doGet(req, res);
	}
}
