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

package web;

import ejb.SeferFacade;
import ejb.TrainsFacade;
import ejb.UsersFacade;
import ejb.Schedule;
import ejb.ScheduleFacade;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author e1502608
 */
@WebServlet(name="AddSingleSchedule", urlPatterns={"/AddSingleSchedule"})
public class AddSingleSchedule extends HttpServlet {
    @EJB
    private SeferFacade seferFacade;
    @EJB
    private UsersFacade usersFacade;
    @EJB
    private ScheduleFacade scheduleFacade;


   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        Long seferId=Long.parseLong(request.getParameter("sid").toString());
        Long machId=Long.parseLong(request.getParameter("mid").toString());
        String dt=request.getParameter("date");

        java.sql.Date sqlDate;
        if(dt==null || dt.equals(""))
        {
          java.util.Date utilDate=new java.util.Date();
           sqlDate= new java.sql.Date(utilDate.getTime());
        }
        else
        {
            String[] splitted=dt.split("/");
            java.util.Date utilDate=new java.util.Date(Integer.parseInt(splitted[2])-1900,
                    Integer.parseInt(splitted[1])-1,
                    Integer.parseInt(splitted[0])
                    );
            sqlDate = new java.sql.Date(utilDate.getTime());
        }

        // TODO implement errors
        if(usersFacade.find(machId)==null)
        {
              response.sendRedirect("AddSchedule?Error=1");
              return;

        }
        if(seferFacade.find(seferId)==null)
        {
             response.sendRedirect("AddSchedule?Error=2");
             return;
        }


        Schedule s = new Schedule();
        s.setMachinstId(machId);
        s.setSeferId(seferId);
        s.setScheduleDate(sqlDate);
        scheduleFacade.create(s);
        response.sendRedirect("AddSchedule");
        return;

       
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AddSingleSchedule</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet AddSingleSchedule at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */

    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
