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

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



        HttpSession session = request.getSession();
        String selectedReservationId = request.getParameter("reservationId");
        //String reservationResult = (String) session.getAttribute("reservationResult");
        

        String url = "";        
        String sqlStatement = "";
        String sqlStatement1 = "";
        String sqlStatement2 = "";
        String extraCondition = "";
        String delivreDate = data.DateUtil.currentDate_YYYYMMDD();
        String delivreTime = TimeDateUtil.currentTime_HHMM();
        String delivreDateTime = delivreDate + delivreTime;
        String updateResult = "";



        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("Query.updateDelivreTime");
            sqlStatement1 = dataSQLQueries.getString("Update.delivreTime.part1") + data.SQLUtil.addQuotes(delivreDateTime);
            sqlStatement2 = dataSQLQueries.getString("Update.delivreTime.part2");

            extraCondition = dataSQLQueries.getString("Condition.reservationId") + selectedReservationId;
            
            
            int i = statement.executeUpdate(sqlStatement + sqlStatement1 + sqlStatement2 + extraCondition);
            if (i != 1)
                updateResult = "<b>Update failed</b>";
            else
                updateResult = "<b>Update successfull </b><br>";
            

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


        }
        catch(SQLException e)
        {
            updateResult = "<b>Error executing the SQL statement: <br>"
                    + e.getMessage() + "</b><br>";
            url = "/web/MenuC.jsp";
        }

        String selectedShow = (String) session.getAttribute("selectedShow");
        url = "/getReservationShow?showTime="+ selectedShow; 
        session.setAttribute("updateResult", updateResult);
        
        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) {

        }
    }
 }

