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

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
 */
public class Login 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{
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                }


                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/videokonference?useUnicode=true&characterEncoding=UTF-8","videokonf","Purk118*");

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

                String target = "index.jsp";
                RequestData data = new RequestData();

                while (rs.next()) {

                    if(request.getParameter("login").equals(rs.getString(1))&& 
                       request.getParameter("pass").equals(rs.getString(2))) {
                            if(request.getParameter("login").equals("admin")){
                                target = "superAdmin.jsp";
                                data.setLogin(rs.getString(1));
                            }else{
                            target = "profile.jsp";
                                data.setJmeno(rs.getString(3));
                                data.setPrijmeni(rs.getString(4));
                                data.setMail(rs.getString(5));
                                data.setUri(rs.getString(6));
                                data.setTelefon(rs.getString(7));
                                data.setLogin(rs.getString(1));
                                data.setAdmin(rs.getBoolean(8));
                            }
                    } else {
                            data.setMessage("Nesprávné jméno nebo heslo!");
                      }
                }

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

            //RequestDispatcher dispatcher = request.getRequestDispatcher(target);
            //dispatcher.forward(request, response);
            response.sendRedirect(target);

            } catch (SQLException ex) {
                Logger.getLogger(Login.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>

}
