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

package web;

import beans.LocationBean;
import ejb.Knowledge;
import ejb.KnowledgeFacade;
import ejb.NewsEntity;
import ejb.NewsEntityFacade;
import ejb.Users;
import ejb.UsersFacade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
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 sinem
 */
@WebServlet(name="PostMessage2", urlPatterns={"/PostMessage2"})
public class PostMessage2 extends HttpServlet {
    @EJB
    private KnowledgeFacade knowledgeFacade;
    @EJB
    private UsersFacade usersFacade;
    @EJB
    private NewsEntityFacade newsEntityFacade;

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


         String userName=null;
        HttpSession session2=request.getSession(true);
        if(session2.getAttribute("userName")!=null && session2.getAttribute("activated") != null &&
                session2.getAttribute("activated").toString().equals("true"))
            userName=session2.getAttribute("userName").toString();
        else {
                response.sendRedirect("jsps/Login.jsp?Error=2");
                return;
        }

        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String body=request.getParameter("body");
        String title=request.getParameter("title");
        String selectedTopic = request.getParameter("topic");

        Users u=usersFacade.find(usersFacade.getIdByUser(userName));

        try{
        if ((title!=null) && (body!=null)) {
            
            Knowledge k = new Knowledge();

            k.setTitle(title);
            k.setDescription(body);
            k.setTopic(findTopicId(selectedTopic));
            k.setCreatedBy(u.getId());
            k.setStatus(Knowledge.Status.newCreated);
            java.util.Date utilDate = new java.util.Date();
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            k.setCreatedIn(sqlDate);
            knowledgeFacade.create(k);
            response.sendRedirect("jsps/confirmation.jsp");
            return;

        }
        }catch(Exception e)
        {
            System.out.println("hata oluştu");
            e.printStackTrace();
            response.sendRedirect("jsps/confirmation.jsp?Error=1");
        }
    }

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



    int findTopicId(String topic)
    {

        int id=-1;
        if(topic.equals("Giriş Sayfası"))
            id=1;
        else if(topic.equals("Ana Sayfa"))
            id=2;
        else if(topic.equals("Tren Takip Sistemi"))
            id=3;
        else if(topic.equals("Haber Bağlantıları"))
            id=4;
        else if(topic.equals("Kayıt Olma"))
            id=5;
        else if(topic.equals("Kişisel Bilgileri Değiştirme"))
            id=6;
        else if(topic.equals("Bulunan Yolları Kaydetme"))
            id=7;
        else if(topic.equals("Kullanım Klavuzlarına Ulaşma"))
            id=8;
        else if(topic.equals("Sorun Bildirme"))
            id=9;
        else if(topic.equals("Diğerleri"))
            id=10;
        else
            System.out.println("ERROR: NO SUCH A TOPIC EXİST!");

        return id;
    }

}
