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

package web;

import ejb.NewsFacade;
import ejb.UsersFacade;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.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;
import javax.servlet.http.HttpSession;

/**
 *
 * @author zwergnase
 */
@WebServlet(name="insertNews", urlPatterns={"/insertNews"})
public class insertNews extends HttpServlet {
    @EJB
    private UsersFacade usersFacade;
    @EJB
    private NewsFacade newsFacade;
   
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");

        HttpSession session=request.getSession(true);
        boolean isRegistered=(session.getAttribute("userName")==null ||session.getAttribute("activated")==null||
        session.getAttribute("activated").toString().equals("false")) ? false : true;


try{
        String title=request.getParameter("title");
        String keywords=request.getParameter("keywords");
        String description=request.getParameter("description");
        String validUntil=request.getParameter("valid");
        Long userId=usersFacade.getIdByUser(session.getAttribute("userName").toString());
        java.util.Date utilDate = new java.util.Date();
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

        String splitted[]=validUntil.split("\\.");
        Date d=new Date(Integer.parseInt(splitted[2])-1900,Integer.parseInt(splitted[1])-1,Integer.parseInt(splitted[0]));


        ejb.News n=new ejb.News();
        n.setAddedBy(userId);
        n.setCreatedIn(sqlDate);
        n.setDescription(description);
        n.setKeywords(keywords);
        n.setTitle(title);
        n.setValidUntil(d);
        newsFacade.create(n);

     
            out.println("<html>");
            out.println("<head>");
            out.println("<link href=\"design/css/logout.css\" type=\"text/css\" rel=\"stylesheet\" />");
            out.println("<title>Bilet İptali</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<table id=\"success\"><tr>");
            out.println("<td rowspan=\"2\" ><img src=\"design/Pictures/info.png\" /></td>");
            out.println("<td align=\"center\" id=\"rightcell\"> Haber başarıyla yüklenmiştir. </td></tr>");
            out.println("<tr><td align=\"center\" id=\"rightcell\">Haberlere geri dönmek için <a href=\"News\">tıklayınız!</a> </td></tr></table>");
            out.println("<a href=\"jsps/PassengerHome.jsp\"><img id=\"home\" src=\"design/Pictures/home.png\"></a>");
            out.println("</body>");
            out.println("</html>");
            return;

        }catch(Exception e)
        {
            e.printStackTrace();
        }

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>

}
