/*
 * DBTest.java
 *
 * Created on 2 de agosto de 2004, 11:29
 */

package com.fenalco.servlets.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Hashtable;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fenalco.entities.User;
import com.fenalco.utilities.database.SQLUtilities;

/**
 *
 * @author  andres
 * @version
 */
public class DBTest extends HttpServlet {
    
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
    }
    
    /** Destroys the servlet.
     */
    public void destroy() {
        
    }
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        /* TODO output your page here
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet</title>");
        out.println("</head>");
        out.println("<body>");
         
        out.println("</body>");
        out.println("</html>");
         */
        out.close();
    }
    
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        String mensaje = "";
        PrintWriter out = response.getWriter();
        
        User u = new User( "josefa" );
        
        out.println( u.getCompanyName() );
        out.println( u.getEmail() );
        out.println( u.getFirstName() );
        out.println( u.getLastName() );
        out.println( u.getLogin() );
        out.println( u.getNuritLogin() );
        out.println( u.getNuritPassword() );
        out.println( u.getPassword() );
        out.println( u.getPhoneNumber() );
        out.println( u.getPositionId() );
        out.println( u.getRoleName() );
        

/*        
        Transaction t = new Transaction();
        
        s.delete( t );
        
        mensaje = t.execute() + "";
*/
        
        out.println( mensaje );
        out.close();
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        String name = request.getParameter( "nombre" );
        String description = request.getParameter( "descripcion" );
        name = URLEncoder.encode( name, "UTF-8" );
        description = URLEncoder.encode( description, "UTF-8" );
        String sql = "INSERT INTO rol VALUES(5, \'" + name + "\', \'" + description + "\', \'ww.xxx.org\');";
        String mensaje = "";
        PrintWriter out = response.getWriter();
        
//        System.out.println( "probando....." + name );
//        System.out.println( "probando....." + description );

        try
        {
//            SQLUtilities.execute(sql);
            mensaje = "OK";
            
            sql = "SELECT * FROM rol;";
            Vector res = SQLUtilities.query(sql);
            
            for( int i = 0; i < res.size(); i++ )
            {
                Hashtable datos = ( Hashtable )res.elementAt( i );
                String n = ( String )datos.get( "rol_nombre" );
                n = URLDecoder.decode( n , "UTF-8" );
                String d = ( String )datos.get( "rol_descripcion" );
                d = URLDecoder.decode( d , "UTF-8" );
                out.println( "nombre = " + n + "\tDescripción = " + n );
            }
        }
        catch( Exception e )
        {
            mensaje = "En la mala --> " + e;
        }
        out.println( mensaje );
        out.close();
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    
}
