/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



package web;

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



/**
 *
 * @author Administrator
 *
 */
public class ValidateUser extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException{

        String selectedUser = request.getParameter("login");
        String selectedPwd = request.getParameter("password");

        HttpSession session = request.getSession();
        

        String url = "";
        String sqlStatement = "";
        String extraCondition = "";


        try
        {
            ResourceBundle config = ResourceBundle.getBundle("config");
            // get a DB connection
            String dbURL = config.getString("dbURL");
            String username = config.getString("username");
            String password = config.getString("password");

            Connection connection = DriverManager.getConnection(dbURL, username, password);
            Statement statement = connection.createStatement();


            ResourceBundle dataSQLQueries = ResourceBundle.getBundle("web.SQLQueries");

            sqlStatement = dataSQLQueries.getString("User.query");
            extraCondition = dataSQLQueries.getString("Condition.login") + data.SQLUtil.addQuotes(selectedUser) + dataSQLQueries.getString("Condition.pwd") + data.SQLUtil.addQuotes(selectedPwd);

            ResultSet resultSet = statement.executeQuery(sqlStatement + extraCondition);

           	if(resultSet.next())
            {
              //save info to session then go to redirect
              user user = new user(resultSet.getString(1),resultSet.getString(2),resultSet.getString(3),resultSet.getString(4),resultSet.getString(5),resultSet.getString(6));
              session.setAttribute("user", user);
              session.setAttribute("userName", user.getUserName());
              session.setAttribute("cinemaName", user.getCinemaName());
              //String msg= "correct";
              url = "/RedirectUser";
            }
            else
            {

              //go back to login.jsp with error msg
              String msg= "You entered wrong login and/or password. Please try again";
              url = "/web/Login.jsp?error="+ msg;
            }

            resultSet.close();
            statement.close();
            connection.close();
			// forward to the view
			RequestDispatcher dispatcher =
			getServletContext().getRequestDispatcher(url);
			dispatcher.forward(request, response);

        }
        catch(SQLException e)
        {
            url = "/web/Login.jsp?error="+ e.getMessage();
			RequestDispatcher dispatcher =
			getServletContext().getRequestDispatcher(url);
			dispatcher.forward(request, response);

        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {

        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException  {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {

        }
    }
 }

