
package A10Park;

import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class A10_View extends HttpServlet {

    private Properties connectionProperties = new Properties();
    private Driver     driver               = null;
    private String     driverName           = null;
    private String     jdbcURL              = null;
    

    public void init(ServletConfig cfg) throws ServletException {
        super.init(cfg);
        {
        	String user, pw;
	      ServletContext ctx = getServletContext();
         driverName = ctx.getInitParameter("gb.DBdriver");
         jdbcURL = ctx.getInitParameter("gb.DBconn_string");
         user = ctx.getInitParameter("gb.DBusername");
         if( user != null ) {
         	connectionProperties.put("user", user);
         }
         pw = ctx.getInitParameter("gb.DBpassword");
         if( pw != null ) {
         	connectionProperties.put("password", pw);
         }
         try {
         	driver = (Driver)Class.forName(driverName).newInstance();
        	}
         catch( Exception e ) {
         	throw new ServletException("Unable to load driver: " + e.getMessage());
         }
       }
   }

  //public void doPost(HttpServletRequest request, HttpServletResponse response)
    //throws IOException, ServletException
  //{    
      public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
      ResultSet results;
      Statement stmt;
		Connection conn = null;

		int startRow = 0;

      try {
      	int rows, count;
	      String errorMessage = null;        
         conn = DriverManager.getConnection(jdbcURL, connectionProperties);
         stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);


			if(request.getParameter("username")!=null)
			{
				String username = request.getParameter("username");
				out.println(username);

   	     	String[] intdays=new String[5];
            results = stmt.executeQuery("SELECT * FROM calendar WHERE username='" + username + "'");
            //results = stmt.executeQuery("SELECT * FROM calendar");
			
				results.last();
				int numberOfRows = results.getRow();
				out.println("Result set has " + numberOfRows + " rows<br />");
				if(numberOfRows == 1)
				{
				results.beforeFirst();            
            results.next();
            	String schedule = results.getString("sched");
            	String[] days = schedule.split("\\;");
            	String aday;

		        String days_str[] = 
      			{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};            	
					out.println("<table border=\"1\">");
        			out.println("<tr><td>Day </td>");
					out.println("<td>9:00</td><td>9:30</td><td>10:00</td><td>10:30</td><td>11:00</td><td>11:30</td><td>12:00</td><td>12:30</td><td>1:00</td><td>1:30</td><td>2:00</td><td>2:30</td><td>3:00</td><td>3:30</td><td>4:00<td>4:30</td></tr>");            	
           	for(int i=0; i<5; i++) {
            		aday = days[i].substring(2);
            		aday = aday.replaceAll(",","");
            		intdays[i] = aday;
            	   out.println("<tr><td>" + days_str[i] + "</td>");



            		for (int j =0; j<16; j++) {
       					String nextChar = String.valueOf(aday.charAt(j));
       					if (nextChar.equals("1")) {
          					out.println("<td bgcolor='red'>" +  "</td>");   
        					}else {  
        						out.println("<td bgcolor='blue'>" +  "</td>");   
       					}
      				} 
 			
 						out.println("</tr>");
 					}	

					out.println("</table>");
            	out.print("Username: " + results.getString("username") + " <br />");
            	//out.print(results.getString("sched") + "<br />");
            	out.print("<p>");
            	ParserTimes a = new ParserTimes();
           
            	for(int i=0;i<5;i++)
            	{
            	//out.print(intdays[i] + "<br>");
            	out.print(a.formatFreeTimes(intdays[i]) + "<br>");
          		}
        
      }
      }
  }
   catch( SQLException e ) {
   	out.println("A database error occurred: " + e.getMessage() + "startRow=" + startRow);
   }
   finally {
   	if( conn != null ) {
      	try { conn.close(); }
         catch( SQLException e ) { }
	
			out.println("</body></html>");
      }
   }
        
  }
}