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

package web;

import ejb.Schedule;
import ejb.ScheduleFacade;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
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="AddFileSchedule", urlPatterns={"/AddFileSchedule"})
public class AddFileSchedule extends HttpServlet {
    @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 {
        FileInputStream fis=null;
        DataInputStream dis=null;
        BufferedReader br=null;

try{
       String fileName=request.getParameter("fileName");
       fis =new FileInputStream(fileName);
       dis=new DataInputStream(fis);
       br=new BufferedReader(new InputStreamReader(dis));

        String line;
        String [] splitted;




        while((line=br.readLine())!=null)
        {
            Schedule s=new Schedule();
            splitted=line.split(";");
            s.setMachinstId(Long.parseLong(splitted[0]));
            s.setSeferId(Long.parseLong(splitted[1]));

             String[] splitteddate=splitted[2].split("/");
            java.util.Date utilDate=new java.util.Date(Integer.parseInt(splitteddate[2])-1900,
                    Integer.parseInt(splitteddate[1])-1,
                    Integer.parseInt(splitteddate[0])
                    );
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            s.setScheduleDate(sqlDate);
            scheduleFacade.create(s);
            response.sendRedirect("AddSchedule");

        }



}catch (Exception e){}
finally
{
    br.close();
    dis.close();
    fis.close();

}






        

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AddFileSchedule</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet AddFileSchedule at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <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>

}
