
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;





public class getReservationShow extends HttpServlet {

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



        HttpSession session = request.getSession();
        String selectedShow = request.getParameter("showTime");
        session.setAttribute("selectedShow", selectedShow);
        user user = (user) session.getAttribute("user");
        String userName = user.getUserName();

        
        String url = "";
        String reservationResult = "";
        String sqlStatement = "";
        String sqlStatement2 = "";
        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("Reservation.query.show");
            sqlStatement2 = dataSQLQueries.getString("Reservation.query.show2");

            extraCondition = dataSQLQueries.getString("Condition.show") + selectedShow;
            ResultSet resultSet = statement.executeQuery(sqlStatement + extraCondition + sqlStatement2);


            
            reservationResult = HTMLUtil.getReservationSelectHtml(resultSet);


            resultSet.close();
            statement.close();
            connection.close();


        }
        catch(SQLException e)
        {
            reservationResult = "Error executing the SQL statement: <br>"
                    + e.getMessage();
        }

        

        String varShowResult = (String) session.getAttribute("showTimesResult");
        String selectedShowValue = "value=\"" + selectedShow + "\"";
        String selected = "selected=\"selected\"";
        varShowResult = varShowResult.replaceAll(selected, "");
        varShowResult = varShowResult.replaceAll(selectedShowValue, selectedShowValue + selected);
        session.setAttribute("showTimesResult", varShowResult);

        session.setAttribute("reservationResult", reservationResult);

        
        url = "/web/MenuC.jsp";
        
        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) {

        }
    }
 }

