/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package db; import beans.EstatisticaProcessoBean; import com.evolute.utils.error.ErrorLogger; import java.sql.ResultSet; import java.sql.Statement; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author dneves */ public class EstatisticasDataProvider { private static final SimpleDateFormat D_F = new SimpleDateFormat( "yyyy-MM-dd" ); private static final SimpleDateFormat T_F = new SimpleDateFormat( "HH:mm:ss" ); private static EstatisticasDataProvider INSTANCE = null; private Db db = null; private EstatisticasDataProvider() { db = new Db(); } public static synchronized EstatisticasDataProvider getInstance() { if ( INSTANCE == null ) { INSTANCE = new EstatisticasDataProvider(); } return INSTANCE; } private Statement createStatement() { if ( db == null ) { db = new Db(); } return db.createStatement(); } public List< EstatisticaProcessoBean > getEstatisticas( Map< String, Object > searchProperties ) { String por = ( String ) searchProperties.get( EstatisticasConstants.KEY_POR ); String nome = ( String ) searchProperties.get( EstatisticasConstants.KEY_NOME ); String anoOcorrencia = ( String ) searchProperties.get( EstatisticasConstants.KEY_ANO_OCORRENCIA ); Date dataOcorrencia = ( Date ) searchProperties.get( EstatisticasConstants.KEY_DATA_OCORRENCIA ); String horaOcorrencia = ( String ) searchProperties.get( EstatisticasConstants.KEY_HORA_OCORRENCIA ); String horasTrabalhadas = ( String ) searchProperties.get( EstatisticasConstants.KEY_HORAS_TRABALHADAS ); String departamento = ( String ) searchProperties.get( EstatisticasConstants.KEY_DEPARTAMENTO ); String seccao = ( String ) searchProperties.get( EstatisticasConstants.KEY_SECCAO ); String causas = ( String ) searchProperties.get( EstatisticasConstants.KEY_CAUSAS_ACIDENTE ); String turno = ( String ) searchProperties.get( EstatisticasConstants.KEY_TURNO_TRABALHO ); Boolean formacao_shst = ( Boolean ) searchProperties.get( EstatisticasConstants.KEY_FORMACAO_SHST ); Boolean postoAcidentado = ( Boolean ) searchProperties.get( EstatisticasConstants.KEY_POSTO_ACIDENTADO ); Boolean outrosAcidentados = ( Boolean ) searchProperties.get( EstatisticasConstants.KEY_OUTROS_ACIDENTADOS ); String tableNames = "analises_acidentes"; String joinConditions = ""; String whereConditions = " analises_acidentes.apagada = 'n' "; if ( por != null || nome != null || turno != null ) { tableNames += ", acidentados"; joinConditions += " analises_acidentes.acidentado_id = acidentados.id "; } if ( departamento != null ) { tableNames += ", departamentos"; joinConditions += ( "".equals( joinConditions ) ? "" : " AND " ) + " analises_acidentes.departamento_id = departamentos.id "; } if ( seccao != null ) { tableNames += ", seccoes"; joinConditions += ( "".equals( joinConditions ) ? "" : " AND " ) + " analises_acidentes.seccao_id = seccoes.id "; } if ( por != null ) { whereConditions += " AND " + EstatisticasConstants.KEY_POR + " = '" + por + "' "; } if ( nome != null ) { nome = nome.replaceAll( " ", "%" ); whereConditions += " AND " + EstatisticasConstants.KEY_NOME + " ILIKE '%" + nome + "%' "; } if ( turno != null ) { whereConditions += " AND " + EstatisticasConstants.KEY_TURNO_TRABALHO + " = '" + turno + "' "; } if ( anoOcorrencia != null ) { whereConditions += " AND " + EstatisticasConstants.KEY_ANO_OCORRENCIA + " = '" + anoOcorrencia + "' "; } if ( dataOcorrencia != null ) { whereConditions += " AND " + EstatisticasConstants.KEY_DATA_OCORRENCIA + " = '" + D_F.format( dataOcorrencia ) + "' "; } if ( horaOcorrencia != null ) { String hora = null; try { Date dateOcorrencia = T_F.parse( horaOcorrencia ); hora = T_F.format( dateOcorrencia ); } catch ( ParseException ex ) { ErrorLogger.logException( ex ); } if ( hora != null ) { whereConditions += " AND " + EstatisticasConstants.KEY_HORA_OCORRENCIA + " = '" + hora + "' " ; } } if ( horasTrabalhadas != null ) { Integer nrHoras = null; try { nrHoras = Integer.parseInt( horasTrabalhadas ); } catch ( Exception e ) { ErrorLogger.logException( e ); } if ( nrHoras != null ) { whereConditions += " AND " + EstatisticasConstants.KEY_HORAS_TRABALHADAS + " = " + nrHoras + " "; } } if ( causas != null ) { causas = causas.replaceAll( " ", "%" ); whereConditions += " AND " + EstatisticasConstants.KEY_CAUSAS_ACIDENTE + " ILIKE '%" + causas + "%' "; } if ( departamento != null ) { departamento = departamento.replaceAll( " ", "%" ); whereConditions += " AND " + EstatisticasConstants.KEY_DEPARTAMENTO + " ILIKE '%" + departamento + "%' "; } if ( seccao != null ) { seccao = seccao.replaceAll( " ", "%" ); whereConditions += " AND " + EstatisticasConstants.KEY_SECCAO + " ILIKE '%" + seccao + "%' "; } if ( formacao_shst != null ) { whereConditions += " AND " + EstatisticasConstants.KEY_FORMACAO_SHST + " = '" + (formacao_shst ? "t" : "f") + "' "; } String sql = "SELECT analises_acidentes.estado, COUNT( analises_acidentes.* ) AS count " + "FROM " + tableNames + " " + "WHERE " + ( "".equals( joinConditions ) ? "" : joinConditions + " AND " ) + whereConditions + " " + "GROUP BY analises_acidentes.estado " + "ORDER BY analises_acidentes.estado"; List< EstatisticaProcessoBean > stats = new LinkedList< EstatisticaProcessoBean >(); try { stats = getEstatisticasForSQL(sql); } catch ( Exception ex ) { ErrorLogger.logException( ex ); } return stats; } private List< EstatisticaProcessoBean > getEstatisticasForSQL( String sql ) throws Exception { // System.out.println( "\nSQL -> " + sql ); List< EstatisticaProcessoBean > stats = new LinkedList< EstatisticaProcessoBean >(); Statement st = createStatement(); ResultSet rs = st.executeQuery( sql ); AnalisesDataProvider adp = new AnalisesDataProvider(); // System.out.println( "\nListing ..." ); while ( rs.next() ) { Integer estado = rs.getInt( "estado" ); Long count = rs.getLong( "count" ); String fase = adp.getFase( estado ); // System.out.println( "\tEstado : " + estado + " => " + fase + " : " + count ); if ( fase != null ) { EstatisticaProcessoBean bean = new EstatisticaProcessoBean(); bean.setEstado( estado ); bean.setFase( fase ); bean.setCount( count ); stats.add( bean ); } } // System.out.println( "Listing DONE." ); return stats; } }