import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.math.*;
import java.io.*;
import java.util.*;
import Configure;

public class AnalystRequests extends HttpServlet{
    //System.out.println( " before get" );
    public void doGet (HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
	try{
	    String fromanalystquerypage=null;
	    String  fromanalystresultpage=null;
	    
	    
	    HttpSession session = request.getSession(true);
	    String name = (String)session.getValue(session.getId());
	    
	    System.out.println("asdffffffffffffffffffffffffffffffffff"+ "::"+ name);
	    System.out.println(session.getId());
	    
	    fromanalystquerypage=request.getParameter("fromanalystquerypage");
	    fromanalystresultpage=request.getParameter("fromanalystresultpage");
	    
	    Enumeration all_parameters=request.getParameterNames();
	    System.out.println("all the parameters are");
	    
	    while(all_parameters.hasMoreElements()){
		System.out.println(all_parameters.nextElement());
	    }
	    System.out.println("some");
	    if(name==null){
		AnalystPrintCode.alreadyLoggedout(request,response);
	    }
	    
	    if(!(fromanalystquerypage==null)){
		String own_query=null;
		String logout=null;
		String location_button=null;
		String worker_loadbutton=null;
		String total_button=null;
		own_query=request.getParameter("ownquery");
		logout=request.getParameter("logout");
		location_button=request.getParameter("locationbutton");
		worker_loadbutton=request.getParameter("workerloadbutton");
		total_button=request.getParameter("totalbutton");
		
		if(!(own_query==null)){
		    System.out.println("aaaaaaaaaaaa");
		    AnalystPrintCode.showResults(request,response);
		}
		else if (!(location_button==null)){
		    AnalystPrintCode.byLocation(request,response);
		}
		else if (!(logout==null)){
		    session.invalidate();
		    AnalystPrintCode.pressedLogout(request,response,name);
		}
		else if(!(worker_loadbutton==null)){
		    AnalystPrintCode.workLoad(request,response);
		}
		else if(!(total_button==null)){
		    AnalystPrintCode.total(request,response);
		}
	    }

	    else if(!(fromanalystresultpage==null)){
		System.out.println("calling Analyst.makeOneMoreQueryCode");
		String onemorequery=null;
		String logout=null;
		onemorequery=request.getParameter("onemorequery");
		logout=request.getParameter("logout");
		
		if(!(onemorequery==null)){
		    Analyst.printAnalystLoginCode(request,response);
		}
		else if (!(logout==null)){
		    session.invalidate();
		    AnalystPrintCode.pressedLogout(request,response,name); //weite code for invalidate..
		}
	    }
	}catch(IOException e){ System.out.println("IO exception caught");}
    }
}



class AnalystPrintCode{
    public static void showResults(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
	try{
	    PrintWriter  out;
	    HttpSession session;
	    String  entered_query=request.getParameter("query");
	    String TITLE= "ANLALYST";
	    response.setContentType("text/html"); 
	    System.out.println( " 12" );
	    out = response.getWriter();
	    Statement stmt=null;
	    ResultSet rs;
	    ResultSetMetaData rsmd;
	    stmt=MakeConnection.toDatabase();
	    rs = stmt.executeQuery(entered_query);
	    rsmd=rs.getMetaData();
	    session=request.getSession(true);
		
	    out.println("<HTML><HEAD><TITLE>");
	    out.println(TITLE);
	    out.println("</TITLE></HEAD><BODY>");
	    out.println("<h3>query results</br><h3>");

	    out.println("<FORM ACTION=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/AnalystRequests\" method =\"GET\">");
	    out.println("<TABLE CELLPADDING=4 BORDER=2>");
		
		
		
	    //start of print of headings of cloumns
	    EasyPrint.printHeadings(rsmd,out);
		 
	    while(rs.next())
		{
		    EasyPrint.printColumns(rs,rsmd,out);   
		}
		
	    EasyPrint.printButton("submit","onemorequery","More Queries",out);
	    EasyPrint.printButton("hidden","fromanalystresultpage","fromanalystresultpage",out);
	    EasyPrint.printButton("submit","logout","logout",out);
		    
		    
	    out.println("</TABLE></FORM></BODY></HTML>");
		
	    out.close();
	    System.out.println("done with Analyst.java");
	}catch (Exception sqle){ 
	    System.out.println( "exception in get " );
	    System.out.println(sqle);
	}
    }


    

    public static void alreadyLoggedout(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
	try{
	    PrintWriter  out;
	    String TITLE= "ANLALYST";
	    response.setContentType("text/html"); 
	    out = response.getWriter();
	    out.println("<HTML><HEAD><TITLE>");
	    out.println(TITLE);
	    out.println("</TITLE></HEAD><BODY>");
	    out.println("<B>Sorry </BLINK><B>");
	    out.println("<h3>you have already logged out</br><h3>");
	    out.println("</TABLE></FORM></BODY></HTML>");
	    out.close();
	    
	}catch (Exception sqle){System.out.println( "exception in get111111111 " );
	System.out.println(sqle);
	}
    }




    public static void pressedLogout(HttpServletRequest request, HttpServletResponse response ,String name) throws ServletException,IOException{
	try{
	    PrintWriter  out;
	    String TITLE= "LOGOUT";
	    response.setContentType("text/html"); 
	    out = response.getWriter();
	    out.println("<HTML><HEAD><TITLE>");
	    out.println(TITLE);
	    out.println("</TITLE></HEAD><BODY>");
	    out.println("<B>Dear"+"    "+ name+ "   " + "<B>");
	    out.println("<h3>you have  logged out Succesfully</br><h3>");
	    out.println("</TABLE></FORM></BODY></HTML>");
	    out.close();
	    
	}catch (Exception sqle){System.out.println( "exception in get111111111 " );
	System.out.println(sqle);
	}
    }



    public static void byLocation(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
	try{
	    PrintWriter  out;
	    HttpSession session;
	    String  entered_location=request.getParameter("getlocation");
	    String TITLE= "ANLALYST";
	    response.setContentType("text/html"); 
	    System.out.println( " 12" );
	    out = response.getWriter();
	    Statement stmt=null;
	    ResultSet rs;
	    ResultSetMetaData rsmd;
	    String query="select complaint_id ,title, location,description from complaint where location='"+entered_location+"'" ;
	    stmt=MakeConnection.toDatabase();
	    rs = stmt.executeQuery(query);
	    rsmd=rs.getMetaData();
	    session=request.getSession(true);
		
	    out.println("<HTML><HEAD><TITLE>");
	    out.println(TITLE);
	    out.println("</TITLE></HEAD><BODY>");
	    out.println("<h3>view complaints by location </br><h3>");
	    
	    out.println("<FORM ACTION=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/AnalystRequests\" method =\"GET\">");
	    out.println("<TABLE CELLPADDING=4 BORDER=2>");
		
	    //start of print of headings of cloumns
	    EasyPrint.printHeadings(rsmd,out);
		 
	    while(rs.next())
		{
		    EasyPrint.printColumns(rs,rsmd,out);   
		}
		
	    EasyPrint.printButton("submit","onemorequery","More Queries",out);
	    EasyPrint.printButton("hidden","fromanalystresultpage","fromanalystresultpage",out);
	    EasyPrint.printButton("submit","logout","logout",out);
		    
		    
		
	    out.println("</TABLE></FORM></BODY></HTML>");
		
	    out.close();
	    System.out.println("done with Analyst.java");
	}catch (Exception sqle){ 
	    System.out.println( "exception in get " );
	    System.out.println(sqle);
	}
    }

    public static void total(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
	try{
	    PrintWriter  out;
	    HttpSession session;
	    String  entered_location=request.getParameter("getlocation");
	    String TITLE= "ANLALYST";
	    response.setContentType("text/html"); 
	    System.out.println( " 12" );
	    out = response.getWriter();
	    Statement stmt=null;
	    Statement stmt1=null;
	    Statement stmt2=null;
	    ResultSet rs;
	    ResultSet rs1;
	    ResultSet rs2;
	    ResultSetMetaData rsmd=null;
	    ResultSetMetaData rsmd1=null;
	    ResultSetMetaData rsmd2=null;
	    String query="select count(complaint_id) from complaint";
	    String query1="select count(complaint_id) from complaint where end_date is not null";
	    String query2="select count(complaint_id) from complaint where end_date is null";
	    
	    session=request.getSession(true);
		
	    out.println("<HTML><HEAD><TITLE>");
	    out.println(TITLE);
	    out.println("</TITLE></HEAD><BODY>");
	    out.println("<h3>complaints summary</br><h3>");
		
	    out.println("<FORM ACTION=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/AnalystRequests\" method =\"GET\">");
	    out.println("<TABLE CELLPADDING=4 BORDER=2>");
	    
	    stmt=MakeConnection.toDatabase();
	    rs = stmt.executeQuery(query);
	    rsmd=rs.getMetaData();
	    out.print("<tr><td>TOTAL COMPLAINTS</td><td>COMPLAINTS RESOLVED");
	    out.print("</td><td>COMPLAINTS BEING RESOLVED</td></tr><tr>"); 
	    
	    while(rs.next())
		{
		    out.println("<td>" + rs.getString(1) + "</td>");
		}
	    stmt1=MakeConnection.toDatabase();
	    rs1 = stmt.executeQuery(query1);
	    rsmd1=rs1.getMetaData();	    	 	 		 
	    
	    while(rs1.next()) 
		{
		    out.println("<td>" + rs1.getString(1) + "</td>");
		}
	    stmt2=MakeConnection.toDatabase();
	    rs2 = stmt.executeQuery(query2);
	    rsmd2=rs2.getMetaData();	    	 	 		 
	    
	    while(rs2.next())
		{
		    out.println("<td>" + rs2.getString(1) + "</td>");
		}
		
	    out.print("</tr>");
	    EasyPrint.printButton("submit","onemorequery","More Queries",out);
	    EasyPrint.printButton("hidden","fromanalystresultpage","fromanalystresultpage",out);
	    EasyPrint.printButton("submit","logout","logout",out);
		    
		    
		
	    out.println("</TABLE></FORM></BODY></HTML>");
		
	    out.close();
	    System.out.println("done with Analyst.java");
	}catch (Exception sqle){ 
	    System.out.println( "exception in get " );
	    System.out.println(sqle);
	}
    }
    


    public static void workLoad(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
	try{
	    PrintWriter  out;
	    HttpSession session;
	    String  entered_location=request.getParameter("getlocation");
	    String TITLE= "worker load";
	    response.setContentType("text/html"); 
	    System.out.println( " 12" );
	    out = response.getWriter();
	    
	    Statement stmt=null;
	    ResultSet rs;
	    ResultSetMetaData rsmd;
	    String query="select worker_id ,count(worker_id) from resolve group by worker_id order by count(worker_id) desc";
	    stmt=MakeConnection.toDatabase();
	    rs = stmt.executeQuery(query);
	    rsmd=rs.getMetaData();
	    session=request.getSession(true);
		
	    out.println("<HTML><HEAD><TITLE>");
	    out.println(TITLE);
	    out.println("</TITLE></HEAD><BODY>");
	    out.println("<h3>Work load per Worker</br><h3>");
	    
	    out.println("<FORM ACTION=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/AnalystRequests\" method =\"GET\">");
	    out.println("<TABLE CELLPADDING=4 BORDER=2>");
	    
	    //start of print of headings of cloumns
	    EasyPrint.printHeadings(rsmd,out);
	    
	    while(rs.next())
		{
		    EasyPrint.printColumns(rs,rsmd,out);   
		}
	    
	    EasyPrint.printButton("submit","onemorequery","More Queries",out);
	    EasyPrint.printButton("hidden","fromanalystresultpage","fromanalystresultpage",out);
	    EasyPrint.printButton("submit","logout","logout",out);
	    
	    
	    
	    out.println("</TABLE></FORM></BODY></HTML>");
	    
	    out.close();
	    System.out.println("done with Analyst.java");
	}catch (Exception sqle){ 
	    System.out.println( "exception in get " );
	    System.out.println(sqle);
	}
    }
}

















