package edu.memphis.books.web;

import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class ListBooksServlet
 */
public class EditAuthor extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 boolean check = true;
	 String authorName = "";
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(check){
			if(request.getParameter("deleteCheck") == null){
				Connection con = null;
				ServletContext context = getServletContext();
				String url = context.getInitParameter("dbUrl"); 
				String db = context.getInitParameter("dbName");
				String user = context.getInitParameter("dbUser");
				String pass = context.getInitParameter("dbPasswd"); 
				ResultSet rs;
				PrintWriter out = response.getWriter();
				try {					
					con = DriverManager.getConnection(url+db, user, pass);
					Statement st = con.createStatement(); 
					rs = st.executeQuery("SELECT * FROM Authors" + " order by LastName, FirstName");
					while (rs.next()) {
						String lastName = rs.getString("LastName"); 
						String firstName = rs.getString("FirstName"); 
						String dOB = rs.getString("YearBorn");
						String iD = rs.getString("AuthorID");
						DataBaseModel.authors.add(lastName + "," + firstName + "," + dOB + "," + iD);
					}
					con.close();
				}catch(SQLException e){ 
					out.println("<pre>"); 
					out.println("SQLException information"); 
					while (e != null) { 
						out.println ("Error msg: " + e.getMessage()); 
						out.println ("SQLSTATE: " + e.getSQLState()); 
						out.println ("Error code: " + e.getErrorCode()); 
						e.printStackTrace(); 
						e = e.getNextException(); // For drivers that support chained exceptions
				}		
			}
		}
	}
		check = false;
		RequestDispatcher view = request.getRequestDispatcher("edit.jsp");
		view.forward(request, response);
}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connection con = null;
		ServletContext context = getServletContext();
		String url = context.getInitParameter("dbUrl"); 
		String db = context.getInitParameter("dbName");
		String user = context.getInitParameter("dbUser");
		String pass = context.getInitParameter("dbPasswd"); 
		PrintWriter out = response.getWriter();
		try{
			con = DriverManager.getConnection(url+db, user, pass);
			Statement statement = con.createStatement();
			statement.executeUpdate("UPDATE Authors SET " + "FirstName =\"" + request.getParameter("firstName") + "\", LastName = \"" + request.getParameter("lastName") + "\", YearBorn = \"" + request.getParameter("dOB") + "\" WHERE AuthorID = \"" + request.getParameter("iD") + "\"");
			ResultSet rs;
			out = response.getWriter();				
			con = DriverManager.getConnection(url+db, user, pass);
			Statement st = con.createStatement(); 
			rs = st.executeQuery("SELECT * FROM Authors" + " order by LastName, FirstName");
			DataBaseModel.authors.clear();
		while (rs.next()) {
				String lastName = rs.getString("LastName"); 
				String firstName = rs.getString("FirstName"); 
				String dOB = rs.getString("YearBorn");
				String iD = rs.getString("AuthorID");
				DataBaseModel.authors.add(lastName + "," + firstName + "," + dOB + "," + iD);		
			}
		RequestDispatcher view = request.getRequestDispatcher("edit.jsp");
		view.forward(request, response);
		con.close();	
		}catch(SQLException e){ 
			out.println("<pre>"); 
			out.println("SQLException information"); 
			while (e != null) { 
				out.println ("Error msg: " + e.getMessage()); 
				out.println ("SQLSTATE: " + e.getSQLState()); 
				out.println ("Error code: " + e.getErrorCode()); 
				e.printStackTrace(); 
				e = e.getNextException(); // For drivers that support chained exceptions
				out.println("You have entered some incorrect information you will now be redirected back to the start page have a nice day!");
				response.setHeader("Refresh", "10; EditAuthor.do");
			}
		}
	}
}
		

