
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.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Pavel Jandera
 */
public class Register 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(Register.class.getName()).log(Level.SEVERE, null, ex);
            }
            Connection conn;
            try {
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/videokonference?useUnicode=true&characterEncoding=UTF-8","videokonf","Purk118*");

                st = conn.createStatement();
                rs = st.executeQuery("SELECT * FROM uzivatele");
                RequestData data = new RequestData();


                data.setLogin(request.getParameter("login"));
                data.setHeslo(request.getParameter("heslo"));
                data.setJmeno(request.getParameter("jmeno"));
                data.setPrijmeni(request.getParameter("prijmeni"));
                data.setMail(request.getParameter("mail"));
                data.setUri(request.getParameter("uri"));
                data.setTelefon(request.getParameter("telefon"));

                try{
                if(data.getLogin().equals("Zadaný login již existuje")){
                    throw new SQLException();}
                    st.executeUpdate("INSERT INTO uzivatele(login, heslo, jmeno, prijmeni, mail, uri, telefon) VALUES ('"
                                   +data.getLogin()+"','"+data.getHeslo()+"','"+data.getJmeno()+"','"+data.getPrijmeni()+"','"+data.getMail()+"','"+data.getUri()+"','"+data.getTelefon()+"')");
                    st.executeUpdate("INSERT INTO prirazeni(uzivatel, skupina) VALUES ('"
                                        +data.getLogin()+"', 1)");
                
                }
                catch(SQLException ex) {
                    data.setLogin("Zadaný login již existuje");
                    request.getSession().setAttribute("RequestData", data);

                    response.sendRedirect("register.jsp");
                    return;
                }

                data.setMessage("Registrace proběhla úspěšně. Můžete se přihlásit.");
                request.getSession().setAttribute("RequestData", data);

                //RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
                //dispatcher.forward(request, response);
                response.sendRedirect("index.jsp");

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



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

}
