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

package web;

import ejb.NewsEntityFacade;
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;

/**
 *
 * @author sinem
 */
@WebServlet(name="AfterPost", urlPatterns={"/AfterPost"})
public class AfterPost extends HttpServlet {
    @EJB
    private UsersFacade usersFacade;
    @EJB
    private NewsEntityFacade newsEntityFacade;


   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String queryStr = request.getQueryString();
        queryStr = queryStr.substring(queryStr.indexOf("=")+1);
        Users u = usersFacade.find(usersFacade.getIdByUser(queryStr));
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            
         Long max = newsEntityFacade.getMaxId(queryStr);
         out.println("<p>"+max+"</p>");

      /*   String oldlog=u.getLog();
        if(oldlog!=null)
        {
            String[] str=oldlog.split("\\|");

            if(str.length==11) {
            oldlog=oldlog.substring(oldlog.indexOf("|")+1);
            oldlog=oldlog.substring(oldlog.indexOf("|"));}

        }
        java.util.Date utilDate = new java.util.Date();
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
         if(oldlog!=null)
        {
        u.setLog(oldlog+"|4$"+max+"$"+sqlDate);}
         else
              u.setLog("|4$"+max+"$"+sqlDate);

        */
        usersFacade.edit(u);



        response.sendRedirect("ListNews");
        } 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>

}
