
package com.videokonference.servlets;

import com.videokonference.bean.RequestData;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Pavel Jandera
 */
public class Edit extends HttpServlet {
   
    /** 
     * 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 {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Statement st = null;
        ResultSet rs = null;

        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Edit.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/videokonference?useUnicode=true&characterEncoding=UTF-8","videokonf","Purk118*");
                st = conn.createStatement();
                RequestData d = new RequestData();
                d = (RequestData)request.getSession().getAttribute("RequestData");
               
                st.executeUpdate("UPDATE uzivatele SET jmeno='"+request.getParameter("jmeno")+"', prijmeni='"+
                                request.getParameter("prijmeni")+"', mail='"+request.getParameter("mail")+"', uri='"+
                                request.getParameter("uri")+"', telefon='"+request.getParameter("telefon")+"' WHERE login='"+d.getLogin()+"'");


                rs = st.executeQuery("SELECT * FROM uzivatele WHERE login='"+d.getLogin()+"'");
                rs.next();

                d.setJmeno(rs.getString("jmeno"));
                d.setPrijmeni(rs.getString("prijmeni"));
                d.setMail(rs.getString("mail"));
                d.setUri(rs.getString("uri"));
                d.setTelefon(rs.getString("telefon"));

                request.getSession().setAttribute("RequestData", d);

            } catch (SQLException ex) {
                Logger.getLogger(Edit.class.getName()).log(Level.SEVERE, null, ex);
            }

                response.sendRedirect("profile.jsp");

        } finally { 
            out.close();

            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException sqlEx) { // ignore }
                    rs = null;
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException sqlEx) { // ignore }
                    st = null;
                }
            }
        }
    } 

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

}
