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

package web;

import ejb.NewsFacade;
import ejb.Users;
import ejb.UsersFacade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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="News", urlPatterns={"/News"})
public class News 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");
        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;

        PrintWriter out = response.getWriter();




        out.println(
                    "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"+ \"http://www.w3.org" +
                    "/TR/xhtml1/DTD/xhtml1-strict.dtd\">"+
                     "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"+

                     "<head>"+
                        "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"+
                        "<title>FAQ</title>"+
                        "<link rel=\"stylesheet\" type=\"text/css\" href=\"design/css/faqstyle.css\" />"+
                        "<script src=\"http://www.google.com/jsapi\"></script>"+
                        "<script>google.load(\"jquery\", \"1.2.6\");"+
                        "</script>"+
                        "<script type=\"text/javascript\" src=\"faq.js\"></script>"+
                        "<script type=\"text/javascript\" src=\"forAnswer.js\"></script>"+
                        "</head>"+

                        "<body>"+
                        "<div id=\"genel\">"+
                        "<div id=\"baslik\">"+
                        "<a href=\"jsps/PassengerHome.jsp\"><img src=\"design/Pictures/homesmall.jpg\" width=\"70px\" height=\"70px\" align=\"left\"/></a><font id=\"basliktext\">" +
                        " Haberler</font>"+
                        "</div>"+
                        "<table id=\"anatablo\">"+
                            "<tr><td>"+
                                    "<div id=\"links\">"+
                                    "<p>"+
                                    "<input type=\"button\" value=\"Haber Ekle\"  id=\"linkbutton\" ONCLICK=\"window.location.href='News'\"/>"+
                                    "</p><p>"+
                                    "<input type=\"submit\" value=\"Görev Ekle\" id=\"linkbutton\" ONCLICK=\"window.location.href='AddSchedule'\"/>"+
                                    "</p><p>"+
                                    "<input type=\"button\" value=\"Sorun Giderme\"  id=\"linkbutton\" ONCLICK=\"window.location.href='jsps/KnowledgeDB.jsp'\"/>"+
                                    "</p><p>"+
                                    "<input type=\"button\" value=\"Kişisel Bilgiler\"  id=\"linkbutton\" ONCLICK=\"window.location.href='findperson'\"  />"+
                                    "</p>"+
                                    "</div>"+
                                "</td>"

                    );


        out.println("<td><div id=\"secondcell\">");
        out.println("<p id=\"ortala\"><input type=\"button\" value=\"Yeni Haber Ekle\" ONCLICK=\"window.location.href='AddNews'\"></p>");



        out.println("<div id=\"page-wrap\">");
        List<ejb.News> allNews= newsFacade.findAll();
        int size=allNews.size();

        if(size==0)
        {
            out.println("<p> Şu anda sistemde hiç haber bulunmamaktadır.</p>");
        }

            for(int i=size-1;i>=0;i--)
            {
                ejb.News n=allNews.get(i);
                Users u=usersFacade.find(n.getAddedBy());
                String keys[]=n.getKeywords().split("-");
                out.println(
                    "<dl class=\"faq\">"+
                    "<dt>"+allNews.get(i).getTitle()+"</dt>"+
                    "<dd class=\"answer\">"+
                    "<div> Yazan:"+u.getName()+"<br />Konu: ");
                for(int j=0;j<keys.length-1;j++)
                    out.println(keys[j]+",");
                out.println(keys[keys.length-1]+"<br />");
                out.println("İçerik: "+allNews.get(i).getDescription()+"</div></dd></dl>");
             }

                out.println("</div>");


            out.println("</td></tr></table></div></div></body></html>");

        try {

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

}
