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

package web;


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 zwergnase
 */
@WebServlet(name="UpdateUserInfo", urlPatterns={"/UpdateUserInfo"})
public class UpdateUserInfo extends HttpServlet {
    @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 {
        PrintWriter out = response.getWriter();
         response.setContentType("text/html;charset=UTF-8");
         request.setCharacterEncoding("UTF-8");
try{

        String userName=request.getParameter("userName").toString();
        String password2=request.getParameter("password").toString();
        String email = request.getParameter("email").toString();
        String location = request.getParameter("location").toString();
        String name= request.getParameter("name").toString();
        String surname= request.getParameter("surname").toString();
        String phone= request.getParameter("phone").toString();
        String birthday=request.getParameter("birthday").toString();
        String preferred=request.getParameter("preferred").toString();

        Long userId= usersFacade.getIdByUser(userName);
        Users U=usersFacade.find(userId);
        U.setPassword(password2);
        U.setEmail(email);
        U.setLocation(location);
        U.setName(name);
        U.setSurname(surname);
        U.setTelephone(phone);
        U.setBirthday(birthday);
        U.setPreferredCity(preferred);
        usersFacade.edit(U);
       
        
//TODO: kontroller yapılmalı!! error verilmeli



            /* TODO output your page here*/

            out.println("<html>");
            out.println("<head>");
            out.println("<link href=\"design/css/logout.css\" type=\"text/css\" rel=\"stylesheet\" />");
            out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
            out.println("<title>Bilgi Güncelleme</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<table id=\"success\"><tr>");
            out.println("<td><img src=\"design/Pictures/info.png\" /></td>");
            out.println("<td align=\"center\" id=\"rightcell\"> Bilgileriniz basariyla güncellenmistir!<br />" +
                    "Bilgilerinize geri dönmek için <a href='findperson'>tiklayiniz.</a> </td></tr></table>");
            out.println("<a href=\"jsps/PassengerHome.jsp\"><img id=\"home\" src=\"design/Pictures/home.png\"></a>");
            out.println("</body>");
            out.println("</html>");
            
        } catch(Exception E)
        {
            E.printStackTrace();
            out.println("<html>");
            out.println("<head>");
            out.println("<link href=\"design/css/logout.css\" type=\"text/css\" rel=\"stylesheet\" />");
            out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
            out.println("<title>Bilgi Güncelleme</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<table id=\"success\"><tr>");
            out.println("<td><img src=\"design/Pictures/info.png\" /></td>");
            out.println("<td align=\"center\" id=\"rightcell\"> Veritabaninda olusan bir sorun nedeniyle bilgileriniz güncellenememistir! </td></tr></table>");
            out.println("<a href=\"jsps/PassengerHome.jsp\"><img id=\"home\" src=\"design/Pictures/home.png\"></a>");
            out.println("</body>");
            out.println("</html>");
        }



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>

}
