


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 GetFilmsOnDate extends HttpServlet {

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


        HttpSession session = request.getSession();
        user user = (user) session.getAttribute("user");
        String userName = user.getUserName();

        
        
        String selectedDate = request.getParameter("date");        
        session.setAttribute("selectedDate", selectedDate);
        String unformattedDate = request.getParameter("unformattedDate");
        session.setAttribute("unformattedDate", unformattedDate);
        
        String selectedCinemaId = user.getCineId();
        session.removeAttribute("showTimesResult");
        session.removeAttribute("reservationResult");
        session.removeAttribute("updateResult");
        
        

        
        String url = "";
        String filmResult = "";
        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("Films.query.date");
            sqlStatement2 = dataSQLQueries.getString("Films.query.date2");
            extraCondition = dataSQLQueries.getString("Condition.Date") + data.SQLUtil.addQuotes(selectedDate) + dataSQLQueries.getString("Condition.cinema") + selectedCinemaId;

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

            
            filmResult = HTMLUtil.getFilmSelectHtml(resultSet);
            
            
            resultSet.close();
            statement.close();
            connection.close();
            
        }
        catch(SQLException e)
        {
            filmResult = "Error executing the SQL statement: <br>"
                    + e.getMessage();
        }

        session.setAttribute("filmResult", filmResult);

        
        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) {

        }
    }
 }

