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

package web;

import ejb.Knowledge;
import ejb.KnowledgeFacade;
import ejb.Users;
import ejb.UsersFacade;
import java.io.IOException;
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;
import javax.servlet.http.HttpSession;

/**
 *
 * @author zwergnase
 */
@WebServlet(name="AddAnswer", urlPatterns={"/AddAnswer"})
public class AddAnswer extends HttpServlet {
    @EJB
    private UsersFacade usersFacade;
    @EJB
    private KnowledgeFacade knowledgeFacade;
   




    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;



        String postIdstr=request.getParameter("postId").toString();
        System.out.println(postIdstr);

        Long postId=Long.parseLong(postIdstr);
        Knowledge post=knowledgeFacade.find(postId);
        String cevapText=request.getParameter("adminCevap").toString();
        Long adminId=usersFacade.getIdByUser(session.getAttribute("userName").toString());




        Knowledge cevap=new Knowledge();
        cevap.setAnswerTo(postId);
        cevap.setCreatedBy(adminId);
        cevap.setDescription(cevapText);
        java.util.Date utilDate = new java.util.Date();
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        cevap.setCreatedIn(sqlDate);
        knowledgeFacade.create(cevap);


        out.println("<html>");
        out.println("<head>");
        out.println("<title>Konu içeriği</title>");
        out.println("</head>");
        out.println("<body>");
        out.println(
        "<form name=\"return\" method=\"post\" action=\"ShowTopicContent\">"+
        "<input type=\"hidden\" name=\"topic\" value=\""+request.getParameter("topic")+"\""+
        "<input type=\"hidden\" name=\"section\" value=\""+request.getParameter("section")+"\""+
        "</form>"+
        "<script language=\"JavaScript\" type=\"text/javascript\">"+
        "document.return.submit()"+
        "</script>");

        
        
    } 

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

}
