/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package db.providers; import db.*; import beans.EstatisticaProcessoBean; import com.evolute.utils.arrays.Virtual2DArray; import com.evolute.utils.error.ErrorLogger; import com.evolute.utils.sql.Expression; import com.evolute.utils.sql.Field; import com.evolute.utils.sql.Select; import com.evolute.utils.sql.Select2; import com.sun.rave.web.ui.model.Option; import db.data.siprp.outer.AcidentadosData; import db.data.siprp.outer.AnalisesAcidentesData; import db.data.siprp.outer.CausasData; import db.data.siprp.outer.SeccoesData; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * * @author dneves */ public class EstatisticasDataProvider extends GenericDataProvider { 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 EstatisticasDataProvider() throws Exception { super(); } public static synchronized EstatisticasDataProvider getInstance() throws Exception { if ( INSTANCE == null ) { INSTANCE = new EstatisticasDataProvider(); } return INSTANCE; } 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 ); Integer estabelecimentoID = ( Integer ) searchProperties.get( EstatisticasConstants.KEY_ESTABELECIMENTO ); Integer seccaoID = ( Integer ) searchProperties.get( EstatisticasConstants.KEY_SECCAO ); Integer causas = ( Integer ) 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 participado_seguro = ( Boolean ) searchProperties.get( EstatisticasConstants.KEY_PARTICIPADO_SEGURO ); String[] tables; Expression[] joins; Expression where = new Field( AnalisesAcidentesData.APAGADA_FULL ).isEqual( "n" ); Integer[] joinTypes; if ( por != null || nome != null || turno != null ) { tables = new String[] { AnalisesAcidentesData.TABLENAME, AcidentadosData.TABLENAME }; joins = new Expression[] { new Field( AnalisesAcidentesData.ACIDENTADO_ID_FULL ).isEqual( AcidentadosData.ID_FULL ) }; joinTypes = new Integer[] { Select2.JOIN_INNER }; } else { joins = new Expression[] { }; tables = new String[] { AnalisesAcidentesData.TABLENAME }; joinTypes = new Integer[] { }; } if ( por != null ) { where = where.and( new Field( EstatisticasConstants.KEY_POR ).isEqual( por ) ); } if ( nome != null ) { nome = nome.replaceAll( " ", "%" ); where = where.and( new Field( EstatisticasConstants.KEY_NOME ).isILike( "'%" + nome + "%'" ) ); } if ( turno != null ) { where = where.and( new Field( EstatisticasConstants.KEY_TURNO_TRABALHO ).isEqual( turno ) ); } if ( anoOcorrencia != null ) { where = where.and( new Field( EstatisticasConstants.KEY_ANO_OCORRENCIA ).isEqual( anoOcorrencia ) ); } if ( dataOcorrencia != null ) { where = where.and( new Field( EstatisticasConstants.KEY_DATA_OCORRENCIA ).isEqual( 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 ) { where = where.and( new Field( EstatisticasConstants.KEY_HORA_OCORRENCIA ).isEqual( hora ) ); } } if ( horasTrabalhadas != null ) { Integer nrHoras = null; try { nrHoras = Integer.parseInt( horasTrabalhadas ); } catch ( Exception e ) { ErrorLogger.logException( e ); } if ( nrHoras != null ) { where = where.and( new Field( EstatisticasConstants.KEY_HORAS_TRABALHADAS ).isEqual( nrHoras ) ); } } if ( causas != null ) { where = where.and( new Field( EstatisticasConstants.KEY_CAUSAS_ACIDENTE ).isEqual( causas ) ); } if ( estabelecimentoID != null ) { where = where.and( new Field( EstatisticasConstants.KEY_ESTABELECIMENTO ).isEqual( estabelecimentoID ) ); } if ( seccaoID != null ) { where = where.and( new Field( EstatisticasConstants.KEY_SECCAO ).isEqual( seccaoID ) ); } if ( formacao_shst != null ) { where = where.and( new Field( EstatisticasConstants.KEY_FORMACAO_SHST ).isEqual( formacao_shst ? "y" : "n" ) ); } if( participado_seguro != null ) { where = where.and( new Field( EstatisticasConstants.KEY_PARTICIPADO_SEGURO ).isEqual( participado_seguro ? "y" : "n" ) ); } Select select = new Select2( tables, joinTypes, joins, new String[]{ AnalisesAcidentesData.ESTADO_FULL, "COUNT( analises_acidentes.* ) AS count" }, where, new String[]{ AnalisesAcidentesData.ESTADO_FULL }, new String[]{ AnalisesAcidentesData.ESTADO_FULL }, null, null ); List< EstatisticaProcessoBean > stats = new LinkedList< EstatisticaProcessoBean >(); try { stats = getEstatisticasForSQL( select ); } catch ( Exception ex ) { ErrorLogger.logException( ex ); } return stats; } private List< EstatisticaProcessoBean > getEstatisticasForSQL( Select select ) throws Exception { System.out.println( "\nEstatisticasQuery : " + select.toString() + "\n" ); List< EstatisticaProcessoBean > stats = new LinkedList< EstatisticaProcessoBean >(); AnalisesDataProvider adp = AnalisesDataProvider.getInstance(); Virtual2DArray array = getExecuter().executeQuery( select ); if ( array != null ) { for ( int i = 0; i < array.rowCount(); i++ ) { Integer estado = array.get( i, 0 ); Long count = array.get( i, 1 ); String fase = adp.getFase( estado ); if ( fase != null ) { EstatisticaProcessoBean bean = new EstatisticaProcessoBean(); bean.setEstado( estado ); bean.setCount( count ); bean.setFase( fase ); stats.add( bean ); } } } return stats; } public Option[] getSeccoesList( ) throws Exception { Option[] options = null; Select2 query = new Select2( new String[] { SeccoesData.TABLENAME }, new Integer[] { }, new Expression[] { }, new String[] { SeccoesData.ID_FULL, SeccoesData.DESCRICAO_FULL }, new Field( SeccoesData.ACTIVO_FULL ).isEqual( "y" ).and( new Field( SeccoesData.DESCRICAO_FULL ).isDifferent( null ) ), new String[] { SeccoesData.DESCRICAO_FULL }, null, null, null ); Virtual2DArray array = getExecuter().executeQuery( query ); if ( array != null ) { options = new Option[ array.rowCount() + 1 ]; options[ 0 ] = new Option( "", "" ); for ( int i = 0; i < array.rowCount(); i++ ) { Integer seccaoID = array.get( i, 0 ); String seccao = array.get( i, 1 ); options[ (i+1) ] = new Option( seccaoID, seccao ); } } return options; } public SeccoesData loadSeccaoByID( Integer seccaoID ) throws Exception { return getProvider().load( SeccoesData.class, seccaoID, SeccoesData.ID, null ); } public String getCausaByID( Integer causaID ) throws Exception { String causa = ""; CausasData causaData = getProvider().load( CausasData.class, causaID, CausasData.ID, null ); if ( causaData != null ) { causa = causaData.getCausa(); } return causa; } public Option[] getEstabelecimentos( ) throws Exception { Option[] options = null; Expression where = new Field( db.data.siprp_local.outer.EstabelecimentosData.INACTIVO_FULL ).isDifferent( "y" ); Select2 query = new Select2( new String[] { db.data.siprp_local.outer.EstabelecimentosData.TABLENAME }, new Integer[] { }, new Expression[] {}, new String[] { db.data.siprp_local.outer.EstabelecimentosData.ID_FULL, db.data.siprp_local.outer.EstabelecimentosData.NOME_FULL }, where, new String[] { db.data.siprp_local.outer.EstabelecimentosData.NOME_FULL }, null, null, null ); Virtual2DArray array = getLocalExecuter().executeQuery( query ); if ( array != null ) { options = new Option[ array.rowCount() + 1 ]; options[ 0 ] = new Option( "", "" ); for ( int i = 0; i < array.rowCount(); i++ ) { Integer estabelecimentoID = array.get( i, 0 ); String estabelecimentoNome = array.get( i, 1 ); options[ ( i + 1 ) ] = new Option( estabelecimentoID, estabelecimentoNome ); } } return options; } public String getEstabelecimentoByID( Integer estabelecimentoID ) throws Exception { String estabelecimento = ""; db.data.siprp_local.outer.EstabelecimentosData estabelecimentoData = getLocalProvider().load( db.data.siprp_local.outer.EstabelecimentosData.class, estabelecimentoID, db.data.siprp_local.outer.EstabelecimentosData.ID, null ); if ( estabelecimentoData != null ) { estabelecimento = estabelecimentoData.getNome(); } return estabelecimento; } }