//ComplainteeRate.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class ComplainteeRate extends HttpServlet
{
	// define a few things we'll need
	private HttpSession session = null;
	private Connection conn = null;
	private Statement stmt = null;
	private 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);
			stmt = conn.createStatement();
		}

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

		catch (ClassNotFoundException ce)
		{
			ce.getMessage();
		}
		System.out.println("ComplainteeRate initialised");
	}
	
	public void doGet (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
		session = req.getSession (true);
		PrintWriter out = res.getWriter();
		String username = req.getParameter ("username");
		String password = req.getParameter ("password");
		String complaint_id = req.getParameter ("complaint_id");
		
		session.putValue (session.getId(), username);
		session = null;
		
		res.setContentType ("text/html");

		try
		{
			ResultSet rs = stmt.executeQuery ("SELECT passwd from LOGIN WHERE name='"+username+"'");
			String temp="";
			while(rs.next())
			temp=rs.getString(1);	

			if (password.equals (temp))
			{
				out.println("<html><head><title>Rate the complaint "+complaint_id+
					"</title></head><body bgcolor=\"#ffffff\">"+
					"<form action=\""+cfg.ServletDir+"ComplainteeRate\" method=\"POST\">"+
					"Rate: <select name=\"rate\"><option value=1>Poor<option value=2>Moderate<option value=3>Great</select><br>"+
					"Feedback: <input type=\"TEXT\" size=20 name=\"feedback\"><br>"+
					"<input type=\"hidden\" name=\"username\" value=\""+username+"\">"+
					"<input type=\"hidden\" name=\"password\" value=\""+password+"\">"+
					"<input type=\"hidden\" name=\"complaint_id\" value=\""+complaint_id+"\">"+
					"<input type=\"submit\" value=\"Give Feedback\">"+
					"</Form></body></html>");
			}
			else
			{
				out.println("Sorry. You are not authorised for this operation or have been logged out.");
				//Authentication failure
			}
		}
		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
	{
	
	}
	
	// This is going toupdate the database
	public void doPost (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
		session = req.getSession (true);
		PrintWriter out = res.getWriter();
		String username = req.getParameter ("username");
		String password = req.getParameter ("password");
		String complaint_id = req.getParameter ("complaint_id");
		String rate = req.getParameter ("rate");
		String feedback = req.getParameter ("feedback");
		
//		session.putValue (session.getId(), username);
//		session = null;
		res.setContentType ("text/html");

		try
		{
			ResultSet rs = stmt.executeQuery ("SELECT passwd from LOGIN WHERE name='"+username+"'");
			String temp="";
			while(rs.next())
				temp=rs.getString(1);
			if (password.equals (temp))
			{
System.out.println("1");
				stmt.executeQuery ("update COMPLAINT set feedback='" + feedback +
					"', rating='" + rate + "' where complaint_id='"+complaint_id+"'");
System.out.println("2");
				out.println("<html><body><h2>Thankyou for your feed back</h2>"+
				"<form action=\"" + cfg.ServletDir + "Complaintee\" method=\"POST\">"+
				"<input type=\"hidden\" name=\"username\" value=\""+username+"\">"+
				"<input type=\"hidden\" name=\"password\" value=\""+password+"\">"+
				"<input type=\"submit\" value=\"Return to main page\"></form></body></html>");
			}
		}
		catch (Exception e)
		{
			out.println(e);
		}
	}
}
