package com.evolute.siprp.pagina; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.evolute.utils.arrays.ResultSet2DArray; public class doGetTrabalhador extends siprpServlet { private static final long serialVersionUID = 1L; private ResultSet2DArray executeQuery( Connection con, String query ) throws Exception { Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY ); ResultSet2DArray rs = new ResultSet2DArray( stmt.executeQuery( query ) ); rs.getObjects(); stmt.close(); return rs; } 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; String empresaId; String estabelecimentoId; String trabalhadorId; HttpSession session = req.getSession( false ); try { userRole = ( String ) session.getAttribute( sessionUserRole ); empresaId = ( String ) session.getAttribute( sessionEmpresaId ); estabelecimentoId = ( String ) session.getAttribute( sessionEstabelecimentoId ); trabalhadorId = ( String ) session.getAttribute( sessionTrabalhadorId ); Class.forName( bdDriver ); con = DriverManager.getConnection( bdLocalUrl, bdLocalUsername, bdLocalPassword ); if ( ( userRole.equals( superUserRole ) || userRole.equals( empresaId ) ) && verificaEstabelecimento( con, empresaId, estabelecimentoId ) && verificaTrabalhador( con, estabelecimentoId, trabalhadorId ) ) { dbQuery = new StringBuffer(); dbQuery.append( " SELECT sexo, categoria, data_nascimento, nome, numero_mecanografico " + "FROM trabalhadores where id='" + trabalhadorId + "'" ); stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY ); rs = new ResultSet2DArray( stmt.executeQuery( dbQuery.toString() ) ); Map< String, Object > hmFuncionario = new HashMap< String, Object >(); if ( rs.columnLength() > 0 ) { 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" ) ); stmt.close(); /** dados de exames **/ String queryUltimo = "select id, data, estado " + "from marcacoes_trabalhador " + "where deleted_stamp is null and tipo = 0 and estado = 2 and trabalhador_id = " + trabalhadorId + " order by data desc " + "limit 1"; rs = executeQuery( con, queryUltimo ); java.util.Date dataUltimoExameRealizado = null; String realizado = ""; if ( rs.columnLength() > 0 ) { dataUltimoExameRealizado = ( java.util.Date ) rs.get( 0, 1 ); realizado = "Sim"; } java.util.Date dataProximoExame = null; String queryProximo = "select id, data, estado " + "from marcacoes_trabalhador " + "where deleted_stamp is null and tipo = 0 and estado != 2 and data >= now() and trabalhador_id = " + trabalhadorId + " " + "order by data desc " + "limit 1"; rs = executeQuery( con, queryProximo ); if ( rs.columnLength() > 0 ) { dataProximoExame = ( java.util.Date ) rs.get( 0, 1 ); } hmFuncionario.put( "ultimo_exame", dataUltimoExameRealizado == null ? "" : dataUltimoExameRealizado ); hmFuncionario.put( "realizado", realizado ); hmFuncionario.put( "proximo_exame", dataProximoExame == null ? "" : dataProximoExame ); /** eof dados exames **/ String query = "SELECT MAX(id) FROM exames WHERE trabalhador_id = " + trabalhadorId + " AND inactivo <> 'y'"; stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY ); rs = new ResultSet2DArray( stmt.executeQuery( query ) ); Integer fichaId = null; if ( rs.columnLength() > 0 ) { fichaId = ( Integer ) rs.get( 0, 0 ); } stmt.close(); if ( fichaId != null ) { query = "SELECT data, resultado FROM exames WHERE id = " + fichaId; stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY ); rs = new ResultSet2DArray( stmt.executeQuery( query ) ); Integer resultadoFicha = ( Integer ) rs.get( 0, 1 ); stmt.close(); if ( resultadoFicha != null ) { switch ( resultadoFicha.intValue() ) { case 1 : hmFuncionario.put( "resultado", "Apto" ); break; case 2 : hmFuncionario.put( "resultado", "Apto Condicionalmente" ); break; case 3 : hmFuncionario.put( "resultado", "Inapto Temporariamente" ); break; case 4 : hmFuncionario.put( "resultado", "Inapto Definitivamente" ); break; default : hmFuncionario.put( "resultado", null ); } } else { hmFuncionario.put( "resultado", null ); } } else { hmFuncionario.put( "resultado", null ); } /** consulta dados **/ String queryUltima = "select id, data, estado " + "from marcacoes_trabalhador " + "where deleted_stamp is null and tipo = 1 and estado = 2 and trabalhador_id = " + trabalhadorId + " " + "order by data desc " + "limit 1"; rs = executeQuery( con, queryUltima ); java.util.Date dataUltimaConsultaRealizada = null; String consultaRealizada = ""; if ( rs.columnLength() > 0 ) { dataUltimaConsultaRealizada = ( java.util.Date ) rs.get( 0, 1 ); consultaRealizada = "Sim"; } String queryProxima = "select id, data, estado " + "from marcacoes_trabalhador " + "where deleted_stamp is null and tipo = 1 and estado != 2 and data >= now() and trabalhador_id = " + trabalhadorId + " " + "order by data desc " + "limit 1"; rs = executeQuery( con, queryProxima ); java.util.Date dataProximaConsulta = null; if ( rs.columnLength() > 0 ) { dataProximaConsulta = ( java.util.Date ) rs.get( 0, 1 ); } hmFuncionario.put( "ultima_consulta", dataUltimaConsultaRealizada == null ? "" : dataUltimaConsultaRealizada ); hmFuncionario.put( "realizada", consultaRealizada ); hmFuncionario.put( "proxima_consulta", dataProximaConsulta == null ? "" : dataProximaConsulta ); /** eof consulta dados **/ } sBuffer = new StringBuffer(); sBuffer.append( "" + nomeEmpresa( con, empresaId ) + "

" + nomeEstabelecimento( con, estabelecimentoId ) + "" ); Map< String, Object > hmValues = new HashMap< String, Object >(); hmValues.put( "empresa_nome", session.getAttribute( sessionCompanyName ) ); hmValues.put( "empresa_id", session.getAttribute( sessionEmpresaId ) ); hmValues.put( "estabelecimento_nome", 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, queryStringTrabalhador ); hmValues.put( "funcionario", hmFuncionario ); out.println( mergeTemplate( hmValues, authenticatedUserTemplate ) ); } else { out.println( mergeTemplate( msgLinkFormatError, userRole, errorTemplate ) ); } con.close(); } catch ( SQLException e ) { e.printStackTrace(); out.println( mergeTemplate( msgErroBd, errorTemplate ) ); } catch ( Exception e ) { e.printStackTrace(); out.println( mergeTemplate( msgGenericError, errorTemplate ) ); } } }