package siprp.pagina;
import com.evolute.utils.arrays.*;
import com.evolute.utils.strings.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.lang.reflect.Array;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.velocity.*;
import org.apache.velocity.app.*;
public class doGetTrabalhador extends siprpServlet
{
    
    /** Creates a new instance of doGetTrabalhador */
	public doGetTrabalhador(HttpServletRequest req, HttpServletResponse res) throws IOException
	{
		ServletOutputStream out = res.getOutputStream();
		Connection con = null ;
		Statement stmt = null ;
		ResultSet2DArray rs;
		StringBuffer dbQuery, sBuffer;
		String userRole, empresaId, estabelecimentoId, trabalhadorId, temp;
		HttpSession session = req.getSession(false);
		Vector names = new Vector();
		Vector values = new Vector();
		try
		{
			userRole = (String)session.getAttribute(super.sessionUserRole);
			empresaId = (String)session.getAttribute(super.sessionEmpresaId);
			estabelecimentoId = (String)session.getAttribute(super.sessionEstabelecimentoId);
			trabalhadorId = (String)session.getAttribute(super.sessionTrabalhadorId);   
			Class.forName(super.bdDriver);
			con = DriverManager.getConnection( super.bdUrl, super.bdUsername, super.bdPassword );
			if ( ( userRole.equals ( super.superUserRole ) || userRole.equals ( empresaId ) )
				&& super.verificaEstabelecimento(con, empresaId, estabelecimentoId)
				&& super.verificaTrabalhador(con, estabelecimentoId,  trabalhadorId) )
			{
				dbQuery = new StringBuffer();
				dbQuery.append( " SELECT *  FROM trabalhadores  where id='"+trabalhadorId+"'");
				stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
				rs = new ResultSet2DArray( stmt.executeQuery( dbQuery.toString()) );
				//		int index=0;
				//                int max = super.trabalhadorDescFields.length;
				HashMap hmFuncionario = new HashMap();
				if( rs.columnLength() > 0 )
				{
					String tmp;
					hmFuncionario.put( "sexo", rs.get( 0, "sexo" ) );
					hmFuncionario.put( "categoria", rs.get( 0, "categoria" ) );
					hmFuncionario.put( "data_nascimento", rs.get( 0, "data_nascimento" ) );
					hmFuncionario.put( "nome", rs.get( 0, "nome" ) );
					hmFuncionario.put( "numero", rs.get( 0, "numero_mecanografico" ) );
					hmFuncionario.put( "ultimo_exame", rs.get( 0, "ultimo_exame" ) );
					hmFuncionario.put( "resultado", rs.get( 0, "resultado" ) );
					tmp = ( String ) rs.get( 0, "realizado" );
					if( tmp != null )
					{
						if( tmp.equals( "n" ) )
						{
							hmFuncionario.put( "realizado", "não" );
						}
						else if( tmp.equals( "s" ) || tmp.equals( "y" ) )
						{
							hmFuncionario.put( "realizado", "sim" );
						}
					}
					hmFuncionario.put( "proximo_exame", rs.get( 0, "proximo_exame" ) );
					hmFuncionario.put( "ultima_consulta", rs.get( 0, "ultima_consulta" ) );
					tmp = ( String ) rs.get( 0, "realizada" );
					if( tmp != null )
					{
						if( tmp.equals( "n" ) )
						{
							hmFuncionario.put( "realizada", "n\u00E3o" );
						}
						else if( tmp.equals( "s" ) || tmp.equals( "y" ) )
						{
							hmFuncionario.put( "realizada", "sim" );
						}
					}
					hmFuncionario.put( "proxima_consulta", rs.get( 0, "proxima_consulta" ) );
				}
				//                while ( index < max )
				//                {
				//                    dbQuery = new StringBuffer();
				//                    dbQuery.append( " SELECT "+super.trabalhadorDescFields[index][1]+" FROM trabalhadores  where id='"+trabalhadorId+"'");
				//                    stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
				//                    rs = new ResultSet2DArray( stmt.executeQuery( dbQuery.toString()) );
				//                    names.add(trabalhadorDescFields[index][0]);
				//                
				//                    if (rs.get(0,0) == null )
				//                    {
				//                        values.add(super.bdNullString);
				//                    }
				//                    else
				//                    {
				////                        values.add(rs.get(0,0).toString());
				//			temp=rs.get(0,0).toString();
				//                        
				//                        if ( temp.equals("n") )
				//                        {
				//                            temp="n\u00E3o";
				//                        }
				//                        else if ( temp.equals("y") )
				//                        {
				//                            temp="sim";
				//                        }
				//                        values.add(temp);                    }
				//                
				//                    ++index;
				//                
				//                }
				stmt.close();
				sBuffer = new StringBuffer();
				sBuffer.append(""+super.nomeEmpresa(con,empresaId)+"
"
				+super.nomeEstabelecimento(con, estabelecimentoId)+"" );
				HashMap hmValues = new HashMap();
				hmValues.put( "empresa_nome", session.getAttribute( sessionCompanyName ) );
				hmValues.put( "empresa_id", session.getAttribute( sessionEmpresaId ) );
				hmValues.put( "estabelecimento_nome", super.nomeEstabelecimento( con, estabelecimentoId ) );
				/*Dados para marcacao de consulta/exame*/
				session.setAttribute( "session_estabelecimento_nome", hmValues.get( "estabelecimento_nome" ) );
				session.setAttribute( "session_funcionario_nome", hmFuncionario.get( "nome" ) );
				session.setAttribute( "session_funcionario_numero", hmFuncionario.get( "numero" ) );
				hmValues.put( "estabelecimento_id", estabelecimentoId );
				hmValues.put( "userRole", userRole );
				hmValues.put( "userName", session.getAttribute( sessionUser ) );
				hmValues.put( msgTemplate , sBuffer.toString() ) ;
				hmValues.put( templateUserRole, userRole);
				hmValues.put( templateQuery, super.queryStringTrabalhador );
				hmValues.put( "funcionario", hmFuncionario );
				//hmValues.put( templateVector1,names);
				//hmValues.put( templateVector2,values);
				//hmValues.put( templateVector3,null);
				out.println( mergeTemplate( hmValues, super.authenticatedUserTemplate));  
				//		out.println( mergeTemplate( sBuffer.toString(), userRole, super.queryStringTrabalhador, names, values, null, super.authenticatedUserTemplate) ); 
			}
			else
			{
				out.println( mergeTemplate( super.msgLinkFormatError , userRole, super.errorTemplate) );
			}
			con.close();
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
			out.println( mergeTemplate( super.msgErroBd , super.errorTemplate) );
		}
		catch ( Exception e )
		{
			e.printStackTrace();
			out.println( mergeTemplate( super.msgGenericError , super.errorTemplate) );
		}
	}
}