//NewUser.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.lang.*;

public class NewUser 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);
			stmt = conn.createStatement();
		}

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

		catch (ClassNotFoundException ce)
		{
			ce.getMessage();
		}
		System.out.println("Init Successful");
	}
	
	public void doPost (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
	}

	// Frankly i dont know what this does. but what the heck
	public void doDelete (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
	
	}
	
	// Gets the stuff from the NewUser form and updates database
	public void doGet (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
		session = req.getSession (true);
		PrintWriter out = res.getWriter();
		String username = req.getParameter ("username");
		String password1 = req.getParameter ("password1");
		String password2 = req.getParameter ("password2");
		String name = req.getParameter ("name");
		String type = req.getParameter ("type");
		String job = req.getParameter ("job");
		String roomno = req.getParameter ("roomno");
		String building = req.getParameter ("building");
		String contactinfo = req.getParameter ("contactinfo");
		String designation = req.getParameter ("designation");
		String company = req.getParameter ("company");
		String ability = req.getParameter ("ability");
		
		session.putValue (session.getId(), username);
		session = null;
		
		res.setContentType ("text/html");

		try
		{
			//Check whether a user of the same name is present
			ResultSet rs = stmt.executeQuery ("SELECT passwd from LOGIN WHERE name='"+username+"'");
			boolean Error = false;
			while (rs.next())
				Error = true;
			
			if (Error)
				out.println ("Please select another username");
			
			//Check if the passwords match
			if (!password1.equals(password2))
			{
				Error=true;
				out.println ("Passwords donot match. Try Again.");
			}
			if (contactinfo == null)
			{
				Error=true;
				out.println ("Contact Info is compulsory");
			}
			if (!Error)
			{
				stmt = conn.createStatement();
				stmt.executeQuery("insert into LOGIN values ('" + username + "','" +
					password1 + "','" + type + "')");
				if (type.equals("Complaintee"))
				{
					stmt.executeQuery ("insert into COMPLAINTEE values ('" + username + "','" +
						name + "','" + job + "','" + contactinfo + "','" + roomno + "','" +
						building + "')");
				}
				else
				{
System.out.println("insert into WORKER values ('" + username + "','" +
						name + "','" + designation + "','" + company + "','" +
						contactinfo + "','" + roomno + "','" + building + "')");
					
					stmt.executeQuery ("insert into WORKER values ('" + username + "','" +
						name + "','" + designation + "','" + company + "','" +
						contactinfo + "','" + roomno + "','" + building + "')");
System.out.println("insert into WORKER_TYPE values ('" + ability + "','"
					+ username+"')");
					stmt.executeQuery ("insert into WORKER_TYPE values ('" + ability + "','"
					+ username+"')");
					
				}
System.out.println(".");
				out.println("<html><body><h1>Congratulations! You have been added.</h1></body></html>");
			}
		}
		catch (Exception e)
		{
			out.println(e);
		}
	}
}
