/*
 * 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.util.List;
import java.util.Vector;
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="findperson", urlPatterns={"/findperson"})
public class findperson extends HttpServlet {
    @EJB
    private NewsFacade newsFacade;
    @EJB
    private UsersFacade usersFacade;

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

        HttpSession session=request.getSession(true);
        Long creationTime = session.getCreationTime();
        String userName=session.getAttribute("userName").toString();
        Long userId=usersFacade.getIdByUser(userName);
        Users u= usersFacade.find(userId);
        session.setAttribute("user", u);

        ejb.News newsArray[]=new ejb.News[3];


            if(u.getLocation()==null || u.getLocation().equals("-"))
            {

                List<ejb.News> news=newsFacade.findAll();
                int size=news.size();
                for(int i=0;i<(size < 3 ? size : 3);i++)
                {
                    System.out.println("newssiez: "+(size-1-i));
                    newsArray[i]=(ejb.News)newsFacade.find(news.get(size-1-i).getId());
                }
                session.setAttribute("news", newsArray);
            }
            else
            {
                    System.out.println(u.getLocation());
                    String keywords[]={u.getLocation()};
                    Vector<Long> news= newsFacade.getByKeyword(keywords);
                    int newssize=news.size();
                    for(int i=0;i<(newssize<3 ? newssize : 3);i++)
                    {
                        newsArray[i]=(ejb.News)newsFacade.find(news.get(newssize-1-i));
                    }
                    session.setAttribute("news", newsArray);
            }



        response.sendRedirect("./jsps/ShowUserInfo.jsp");

    } 

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

}
