/* * EstatisticaDataProvider.java * * Created on 16 de Dezembro de 2004, 12:50 */ package siprp.estatistica; import java.text.*; import java.util.*; import com.evolute.utils.*; import com.evolute.utils.arrays.*; import com.evolute.utils.data.*; import com.evolute.utils.db.*; import com.evolute.utils.db.keyretrievers.*; import com.evolute.utils.metadb.*; import com.evolute.utils.sql.*; import com.evolute.utils.sql.expression.*; import siprp.data.*; /** * * @author fpalma */ public class EstatisticaDataProvider extends MetaProvider { private static final Object LOCK = new Object(); private static EstatisticaDataProvider instance = null; private final Executer executer; public static final DateFormat DF = DateFormat.getDateInstance( DateFormat.SHORT ); /** Creates a new instance of EstatisticaDataProvider */ public EstatisticaDataProvider() throws Exception { DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER /*SingletonConstants.DBMANAGER*/ ); executer = dbm.getSharedExecuter( this ); } public static MetaProvider getProvider() throws Exception { synchronized( LOCK ) { if( instance == null ) { instance = new EstatisticaDataProvider(); } } return instance; } public String[][] getMarcacoesPeriodo( Date dataInicio, Date dataFim ) throws Exception { String [][]result = null; Select select = new Select( new String[]{ "marcacoes_trabalhador" }, new String[]{ MarcacaoTrabalhadorData.DATA, "trabalhador_id", MarcacaoTrabalhadorData.REALIZADA, MarcacaoTrabalhadorData.TIPO }, new Field( "data" ).isGreaterOrEqual( dataInicio ).and( new Field( "data" ).isLessOrEqual( dataFim ) ), new String[]{ "data" }, null ); Virtual2DArray array = executer.executeQuery( select ); result = new String[ array.columnLength() ][ 5 ]; for( int n = 0; n < result.length; n++ ) { Date data = ( Date ) array.get( n, 0 ); int trabalhadorID = ( ( Number ) array.get( n, 1 ) ).intValue(); boolean realizada = "y".equals( array.get( n, 2 ) ); int tipo = ( ( Number ) array.get( n, 3 ) ).intValue(); String nomeEstabEmp[] = getNomeEstabelecimentoEmpresaForTrabalhador( trabalhadorID ); result[ n ][ 0 ] = nomeEstabEmp[ 2 ]; result[ n ][ 1 ] = nomeEstabEmp[ 0 ]; result[ n ][ 2 ] = nomeEstabEmp[ 1 ]; result[ n ][ 3 ] = DF.format( data ); result[ n ][ 4 ] = ( tipo == Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES ? "(Exame)" : "(Consulta)" ); } return result; } public String[] getNomeEstabelecimentoEmpresaForTrabalhador( int trabalhadorID ) throws Exception { String data[] = new String[ 3 ]; Select select = new Select( new String[]{ "trabalhadores", "estabelecimentos", "empresas"}, new String[]{ "trabalhadores.nome", "estabelecimentos.nome", "empresas.designacao_social" }, new Field( "trabalhadores.id" ).isEqual( new Integer( trabalhadorID ) ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ) ).and( new Field( "estabelecimentos.empresa_id" ).isEqual( new Field( "empresas.id" ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() == 1 ) { data[ 0 ] = ( String ) array.get( 0, 0 ); data[ 1 ] = ( String ) array.get( 0, 1 ); data[ 2 ] = ( String ) array.get( 0, 2 ); } return data; } public int countExamesPeriodo( Date dataInicio, Date dataFim ) throws Exception { Select select = new Select( new String[]{ "marcacoes_trabalhador" }, new String[]{ "count(*)" }, new Field( "data" ).isGreaterOrEqual( dataInicio ).and( new Field( "data" ).isLessOrEqual( dataFim ) ).and( new Field( "tipo" ).isEqual( new Integer( Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() == 0 || array.get( 0, 0 ) == null ) { return 0; } return ( ( Number ) array.get( 0, 0 ) ).intValue(); } public int countConsultasPeriodo( Date dataInicio, Date dataFim ) throws Exception { Select select = new Select( new String[]{ "marcacoes_trabalhador" }, new String[]{ "count(*)" }, new Field( "data" ).isGreaterOrEqual( dataInicio ).and( new Field( "data" ).isLessOrEqual( dataFim ) ).and( new Field( "tipo" ).isEqual( new Integer( Marcacao.TIPO_MARCACAO_TRABALHADOR_CONSULTA ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() == 0 || array.get( 0, 0 ) == null ) { return 0; } return ( ( Number ) array.get( 0, 0 ) ).intValue(); } public int countExamesPeriodoForEmpresa( Date dataInicio, Date dataFim, Integer empresaID ) throws Exception { Select select = new Select( new String[]{ "marcacoes_trabalhador", "trabalhadores", "estabelecimentos" }, new String[]{ "count(*)" }, new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ) ).and( new Field( "estabelecimentos.empresa_id" ).isEqual( empresaID ) ).and( new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( new Field( "data" ).isLessOrEqual( dataFim ) ).and( new Field( "tipo" ).isEqual( new Integer( Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() == 0 || array.get( 0, 0 ) == null ) { return 0; } return ( ( Number ) array.get( 0, 0 ) ).intValue(); } public int countConsultasPeriodoForEmpresa( Date dataInicio, Date dataFim, Integer empresaID ) throws Exception { Select select = new Select( new String[]{ "marcacoes_trabalhador", "trabalhadores", "estabelecimentos" }, new String[]{ "count(*)" }, new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ) ).and( new Field( "estabelecimentos.empresa_id" ).isEqual( empresaID ) ).and( new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( new Field( "data" ).isLessOrEqual( dataFim ) ).and( new Field( "tipo" ).isEqual( new Integer( Marcacao.TIPO_MARCACAO_TRABALHADOR_CONSULTA ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() == 0 || array.get( 0, 0 ) == null ) { return 0; } return ( ( Number ) array.get( 0, 0 ) ).intValue(); } public String[][] getMarcacoesPeriodoForEstabelecimento( Date dataInicio, Date dataFim, Integer estabelecimentoID ) throws Exception { String [][]result = null; Select select = new Select( new String[]{ "marcacoes_trabalhador", "trabalhadores" }, new String[]{ MarcacaoTrabalhadorData.DATA, "trabalhador_id", MarcacaoTrabalhadorData.REALIZADA, MarcacaoTrabalhadorData.TIPO }, new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( new Field( "data" ).isLessOrEqual( dataFim ) ), new String[]{ "data" }, null ); Virtual2DArray array = executer.executeQuery( select ); result = new String[ array.columnLength() ][ 5 ]; for( int n = 0; n < result.length; n++ ) { Date data = ( Date ) array.get( n, 0 ); int trabalhadorID = ( ( Number ) array.get( n, 1 ) ).intValue(); boolean realizada = "y".equals( array.get( n, 2 ) ); int tipo = ( ( Number ) array.get( n, 3 ) ).intValue(); String nomeEstabEmp[] = getNomeEstabelecimentoEmpresaForTrabalhador( trabalhadorID ); result[ n ][ 0 ] = nomeEstabEmp[ 2 ]; result[ n ][ 1 ] = nomeEstabEmp[ 0 ]; result[ n ][ 2 ] = nomeEstabEmp[ 1 ]; result[ n ][ 3 ] = DF.format( data ); result[ n ][ 4 ] = ( tipo == Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES ? "(Exame)" : "(Consulta)" ); } return result; } public int countExamesPeriodoForEstabelecimento( Date dataInicio, Date dataFim, Integer estabelecimentoID ) throws Exception { Select select = new Select( new String[]{ "marcacoes_trabalhador", "trabalhadores" }, new String[]{ "count(*)" }, new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( new Field( "data" ).isLessOrEqual( dataFim ) ).and( new Field( "tipo" ).isEqual( new Integer( Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() == 0 || array.get( 0, 0 ) == null ) { return 0; } return ( ( Number ) array.get( 0, 0 ) ).intValue(); } public int countConsultasPeriodoForEstabelecimento( Date dataInicio, Date dataFim, Integer estabelecimentoID ) throws Exception { Select select = new Select( new String[]{ "marcacoes_trabalhador", "trabalhadores" }, new String[]{ "count(*)" }, new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( new Field( "data" ).isLessOrEqual( dataFim ) ).and( new Field( "tipo" ).isEqual( new Integer( Marcacao.TIPO_MARCACAO_TRABALHADOR_CONSULTA ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() == 0 || array.get( 0, 0 ) == null ) { return 0; } return ( ( Number ) array.get( 0, 0 ) ).intValue(); } public String[][] getTrabalhadoresSemExamesOuConsultas( Date dataInicio, Date dataFim, boolean exames ) throws Exception { // Select subSelect = // new Select( new String[]{ "marcacoes_trabalhador" }, // new String[]{ "trabalhador_id" }, // new Field( "tipo" ).isEqual( new Integer( Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES ) ).and( // new Field( "realizada" ).isEqual( "y" ) ) ); Select select = new Select( new String[]{ "empresas", "estabelecimentos", "trabalhadores LEFT OUTER JOIN marcacoes_trabalhador ON " + "( trabalhadores.id = marcacoes_trabalhador.trabalhador_id AND marcacoes_trabalhador.tipo = " + (exames ? Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES : Marcacao.TIPO_MARCACAO_TRABALHADOR_CONSULTA) + " AND marcacoes_trabalhador.realizada = 'y' )" }, new String[]{ "empresas.designacao_social", "estabelecimentos.nome", "trabalhadores.nome", "empresas.designacao_social_plain", "estabelecimentos.nome_plain", "trabalhadores.nome_plain" }, new Field( "estabelecimentos.empresa_id" ).isEqual( new Field( "empresas.id" ) ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ) ).and( new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( null ) ).and( new Field( "estabelecimentos.inactivo" ).isDifferent( "y" ) ).and( new Field( "trabalhadores.inactivo" ).isDifferent( "y" ).or( new Field( "marcacoes_trabalhador.realizada" ).isDifferent( "y" ) ) ).and( new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( new Field( "data" ).isLessOrEqual( dataFim ) ), new String[]{ "empresas.designacao_social_plain", "estabelecimentos.nome_plain", "trabalhadores.nome_plain" }, null ); Virtual2DArray array = executer.executeQuery( select ); String data[][] = new String[ array.columnLength() ][ 3 ]; for( int r = 0; r < array.columnLength(); r++ ) { for( int c = 0; c < 3; c++ ) { data[ r ][ c ] = (String) array.get( r, c ); } } return data; } public int countTrabalhadoresSemExamesOuConsultas( Date dataInicio, Date dataFim, boolean exames ) throws Exception { Select select = new Select( new String[]{ "trabalhadores" }, new String[]{ "COUNT(*)" }, new Field( "inactivo" ).isDifferent( "y" ) ); int totalCount = 0; Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() > 0 && array.rowLength() > 0 ) { Number n = (Number) array.get( 0, 0 ); if( n != null ) { totalCount = n.intValue(); } } // select = // new Select( new String[]{ "marcacoes_trabalhador" }, // new String[]{ "COUNT(*)" }, // new Field( "tipo" ).isEqual( new Integer( Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES ) ).and( // new Field( "realizada" ).isEqual( "y" ) ).and( // new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ) ); // int subCount = 0; // array = executer.executeQuery( select ); // if( array.columnLength() > 0 && array.rowLength() > 0 ) // { // Number n = (Number) array.get( 0, 0 ); // if( n != null ) // { // subCount = n.intValue(); // } // } return totalCount - countTrabalhadoresComExamesOuConsultasPeriodo( dataInicio, dataFim, exames ); } public String[][] getTrabalhadoresSemExamesOuConsultasEstabelecimento( Integer estabelecimentoID, Date dataInicio, Date dataFim, boolean exames ) throws Exception { Select select = new Select( new String[]{ "marcacoes_trabalhador" }, new String[]{ "trabalhador_id" }, new Field( "tipo" ).isEqual( new Integer( exames ? Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES : Marcacao.TIPO_MARCACAO_TRABALHADOR_CONSULTA ) ).and( new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( new Field( "data" ).isLessOrEqual( dataFim ) ).and( new Field( "realizada" ).isEqual( "y" ) ) ); Virtual2DArray array = executer.executeQuery( select ); Integer ids[] = new Integer[ array.columnLength() ]; for( int n = 0; n < ids.length; n++ ) { ids[ n ] = ( Integer ) array.get( n, 0 ); } if( ids.length == 0 ) { ids = new Integer[]{ new Integer( -1 ) }; } select = new Select( new String[]{ "estabelecimentos", "trabalhadores" }, new String[]{ "estabelecimentos.nome", "trabalhadores.nome" }, new Field( "estabelecimentos.id" ).isEqual( estabelecimentoID ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ) ).and( new Field( "estabelecimentos.inactivo" ).isDifferent( "y" ) ).and( new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ).and( new Field( "trabalhadores.id" ).notIn( ids ) ), new String[]{ "trabalhadores.nome" }, null ); // Select select = // new Select( new String[]{ "estabelecimentos", // "trabalhadores LEFT OUTER JOIN marcacoes_trabalhador ON " // + "( trabalhadores.id = marcacoes_trabalhador.trabalhador_id AND marcacoes_trabalhador.tipo = " // + (exames ? Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES : Marcacao.TIPO_MARCACAO_TRABALHADOR_CONSULTA) + " AND marcacoes_trabalhador.realizada = 'y' )" }, // new String[]{ "estabelecimentos.nome", "trabalhadores.nome" }, // new Field( "estabelecimentos.id" ).isEqual( estabelecimentoID ).and( // new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ) ).and( // new Field( "estabelecimentos.inactivo" ).isDifferent( "y" ) ).and( // new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( null ).or( // new Field( "marcacoes_trabalhador.realizada" ).isDifferent( "y" ) ) ).and( // new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( // new Field( "data" ).isLessOrEqual( dataFim ) ).and( // new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ), // new String[]{ "trabalhadores.nome" }, null ); array = executer.executeQuery( select ); String data[][] = new String[ array.columnLength() ][ 2 ]; for( int r = 0; r < array.columnLength(); r++ ) { for( int c = 0; c < 2; c++ ) { data[ r ][ c ] = (String) array.get( r, c ); } } return data; } public Object[][] getCountTrabalhadoresSemExamesOuConsultasPeriodoForAllEmpresas( Date dataInicio, Date dataFim, boolean exames ) throws Exception { Select select = new Select( new String[]{ "empresas" }, new String[]{ "id", "designacao_social", "designacao_social_plain" }, new Field( "inactivo" ).isDifferent( "y" ), new String[]{ "designacao_social_plain" }, null ); Virtual2DArray array = executer.executeQuery( select ); Object data[][] = new Object[ array.columnLength() ][ 3 ]; for( int n = 0; n < data.length; n++ ) { data[ n ][ 0 ] = array.get( n, 1 ); Integer empresaID = (Integer)array.get( n, 0 ); data[ n ][ 1 ] = new Integer( countTrabalhadoresSemExamesOuConsultasEmpresa( empresaID, dataInicio, dataFim, exames ) ); data[ n ][ 2 ] = empresaID; } return data; } public int countTrabalhadoresSemExamesOuConsultasEmpresa( Integer empresaID, Date dataInicio, Date dataFim, boolean exames ) throws Exception { Select select = new Select( new String[]{ "trabalhadores", "estabelecimentos" }, new String[]{ "COUNT(*)" }, new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ).and( new Field( "estabelecimentos.empresa_id" ).isEqual( empresaID ) ).and( new Field( "estabelecimentos.inactivo" ).isDifferent( "y" ) ).and( new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ) ); int totalCount = 0; Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() > 0 && array.rowLength() > 0 ) { Number n = (Number) array.get( 0, 0 ); if( n != null ) { totalCount = n.intValue(); } } // select = // new Select( new String[]{ "marcacoes_trabalhador", "trabalhadores", "estabelecimentos" }, // new String[]{ "COUNT(*)" }, // new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( // new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ) ).and( // new Field( "estabelecimentos.empresa_id" ).isEqual( empresaID ) ).and( // new Field( "tipo" ).isEqual( new Integer( Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES ) ) ).and( // new Field( "realizada" ).isEqual( "y" ) ).and( // new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ) ); // int subCount = 0; // array = executer.executeQuery( select ); // if( array.columnLength() > 0 && array.rowLength() > 0 ) // { // Number n = (Number) array.get( 0, 0 ); // if( n != null ) // { // subCount = n.intValue(); // } // } return totalCount - countTrabalhadoresComExamesOuConsultasPeriodoForEmpresa( dataInicio, dataFim, empresaID, exames ); } public int countTrabalhadoresSemExamesOuConsultasEstabelecimento( Integer estabelecimentoID, Date dataInicio, Date dataFim, boolean exames ) throws Exception { Select select = new Select( new String[]{ "trabalhadores" }, new String[]{ "COUNT(*)" }, new Field( "trabalhadores.estabelecimento_id" ).isEqual( estabelecimentoID ).and( new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ) ); int totalCount = 0; Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() > 0 && array.rowLength() > 0 ) { Number n = (Number) array.get( 0, 0 ); if( n != null ) { totalCount = n.intValue(); } } // select = // new Select( new String[]{ "marcacoes_trabalhador", "trabalhadores" }, // new String[]{ "COUNT(*)" }, // new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( // new Field( "trabalhadores.estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( // new Field( "tipo" ).isEqual( new Integer( Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES ) ) ).and( // new Field( "realizada" ).isEqual( "y" ) ).and( // new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ) ); // int subCount = 0; // array = executer.executeQuery( select ); // if( array.columnLength() > 0 && array.rowLength() > 0 ) // { // Number n = (Number) array.get( 0, 0 ); // if( n != null ) // { // subCount = n.intValue(); // } // } return totalCount - countTrabalhadoresComExamesOuConsultasPeriodoForEstabelecimento( dataInicio, dataFim, estabelecimentoID, exames ); } public String[][]getTrabalhadoresComExamesOuConsultasPeriodo( Date dataInicio, Date dataFim, boolean exames ) throws Exception { Select select = new Select( new String[]{ "empresas", "estabelecimentos", "trabalhadores", "marcacoes_trabalhador" }, new String[]{ "empresas.designacao_social", "estabelecimentos.nome", "trabalhadores.nome", "empresas.designacao_social_plain", "estabelecimentos.nome_plain", "trabalhadores.nome_plain" }, new Field( "estabelecimentos.empresa_id" ).isEqual( new Field( "empresas.id" ) ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ) ).and( new Field( "estabelecimentos.inactivo" ).isDifferent( "y" ) ).and( new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ).and( new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ) ).and( new Field( "marcacoes_trabalhador.tipo" ).isEqual( new Integer( exames ? Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES : Marcacao.TIPO_MARCACAO_TRABALHADOR_CONSULTA ) ) ).and( new Field( "marcacoes_trabalhador.realizada" ).isEqual( "y" ) ).and( new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( new Field( "data" ).isLessOrEqual( dataFim ) ), new String[]{ "empresas.designacao_social_plain", "estabelecimentos.nome_plain", "trabalhadores.nome_plain" }, null ); Virtual2DArray array = executer.executeQuery( select ); String data[][] = new String[ array.columnLength() ][ 3 ]; for( int r = 0; r < array.columnLength(); r++ ) { for( int c = 0; c < 3; c++ ) { data[ r ][ c ] = (String) array.get( r, c ); } } return data; } public int countTrabalhadoresComExamesOuConsultasPeriodo( Date dataInicio, Date dataFim, boolean exames ) throws Exception { Select select = new Select( new String[]{ "marcacoes_trabalhador", "trabalhadores" }, new String[]{ "COUNT(*)" }, new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( new Field( "tipo" ).isEqual( new Integer( exames ? Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES : Marcacao.TIPO_MARCACAO_TRABALHADOR_CONSULTA ) ) ).and( new Field( "realizada" ).isEqual( "y" ) ).and( new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ).and( new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( new Field( "data" ).isLessOrEqual( dataFim ) ) ); int count = 0; Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() > 0 && array.rowLength() > 0 ) { Number n = (Number) array.get( 0, 0 ); if( n != null ) { count = n.intValue(); } } return count; } public String[][]getTrabalhadoresComExamesOuConsultasPeriodoForEstabelecimento( Date dataInicio, Date dataFim, Integer estabelecimentoID, boolean exames ) throws Exception { Select select = new Select( new String[]{ "estabelecimentos", "trabalhadores", "marcacoes_trabalhador" }, new String[]{ "estabelecimentos.nome", "trabalhadores.nome" }, new Field( "estabelecimentos.id" ).isEqual( estabelecimentoID ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ) ).and( new Field( "estabelecimentos.inactivo" ).isDifferent( "y" ) ).and( new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ).and( new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ) ).and( new Field( "marcacoes_trabalhador.tipo" ).isEqual( new Integer( exames ? Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES : Marcacao.TIPO_MARCACAO_TRABALHADOR_CONSULTA ) ) ).and( new Field( "marcacoes_trabalhador.realizada" ).isEqual( "y" ) ).and( new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( new Field( "data" ).isLessOrEqual( dataFim ) ), new String[]{ "estabelecimentos.nome", "trabalhadores.nome" }, null ); Virtual2DArray array = executer.executeQuery( select ); String data[][] = new String[ array.columnLength() ][ 2 ]; for( int r = 0; r < array.columnLength(); r++ ) { for( int c = 0; c < 2; c++ ) { data[ r ][ c ] = (String) array.get( r, c ); } } return data; } public Object[][] getCountTrabalhadoresComExamesOuConsultasPeriodoForAllEmpresas( Date dataInicio, Date dataFim, boolean exames ) throws Exception { Select select = new Select( new String[]{ "empresas" }, new String[]{ "id", "designacao_social", "designacao_social_plain" }, new Field( "inactivo" ).isDifferent( "y" ), new String[]{ "designacao_social_plain" }, null ); Virtual2DArray array = executer.executeQuery( select ); Object data[][] = new Object[ array.columnLength() ][ 3 ]; for( int n = 0; n < data.length; n++ ) { data[ n ][ 0 ] = array.get( n, 1 ); Integer empresaID = (Integer)array.get( n, 0 ); data[ n ][ 1 ] = new Integer( countTrabalhadoresComExamesOuConsultasPeriodoForEmpresa( dataInicio, dataFim, empresaID, exames ) ); data[ n ][ 2 ] = empresaID; } return data; } public int countTrabalhadoresComExamesOuConsultasPeriodoForEmpresa( Date dataInicio, Date dataFim, Integer empresaID, boolean exames ) throws Exception { Select select = new Select( new String[]{ "marcacoes_trabalhador", "trabalhadores", "estabelecimentos" }, new String[]{ "COUNT(*)" }, new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ) ).and( new Field( "estabelecimentos.empresa_id" ).isEqual( empresaID ) ).and( new Field( "tipo" ).isEqual( new Integer( exames ? Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES : Marcacao.TIPO_MARCACAO_TRABALHADOR_CONSULTA ) ) ).and( new Field( "realizada" ).isEqual( "y" ) ).and( new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ).and( new Field( "estabelecimentos.inactivo" ).isDifferent( "y" ) ).and( new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( new Field( "data" ).isLessOrEqual( dataFim ) ) ); int count = 0; Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() > 0 && array.rowLength() > 0 ) { Number n = (Number) array.get( 0, 0 ); if( n != null ) { count = n.intValue(); } } return count; } public int countTrabalhadoresComExamesOuConsultasPeriodoForEstabelecimento( Date dataInicio, Date dataFim, Integer estabelecimentoID, boolean exames ) throws Exception { Select select = new Select( new String[]{ "marcacoes_trabalhador", "trabalhadores" }, new String[]{ "COUNT(*)" }, new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( "tipo" ).isEqual( new Integer( exames ? Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES : Marcacao.TIPO_MARCACAO_TRABALHADOR_CONSULTA ) ) ).and( new Field( "realizada" ).isEqual( "y" ) ).and( new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ).and( new Field( "data" ).isGreaterOrEqual( dataInicio ) ).and( new Field( "data" ).isLessOrEqual( dataFim ) ) ); int count = 0; Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() > 0 && array.rowLength() > 0 ) { Number n = (Number) array.get( 0, 0 ); if( n != null ) { count = n.intValue(); } } return count; } public String [][]getDadosTrabalhadoresPeriodo( Date dataInicio, Date dataFim ) throws Exception { Select select = new Select( new String[]{ "empresas", "estabelecimentos", "trabalhadores" }, new String[]{ "empresas.designacao_social", "estabelecimentos.nome", "trabalhadores.nome", "trabalhadores.id", "empresas.designacao_social_plain", "estabelecimentos.nome_plain", "trabalhadores.nome_plain" }, new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ).and( new Field( "estabelecimentos.empresa_id" ).isEqual( new Field( "empresas.id" ) ) ).and( new Field( "estabelecimentos.inactivo" ).isDifferent( "y" ) ).and( new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ), new String[]{ "empresas.designacao_social_plain", "estabelecimentos.nome_plain", "trabalhadores.nome_plain" }, null ); Virtual2DArray array = executer.executeQuery( select ); String data[][] = new String[ array.columnLength() ][ 7 ]; for( int n = 0; n < array.columnLength(); n++ ) { data[ n ][ 0 ] = ( String ) array.get( n, 0 ); data[ n ][ 1 ] = ( String ) array.get( n, 1 ); data[ n ][ 2 ] = ( String ) array.get( n, 2 ); Integer id = new Integer( ( (Number) array.get( n, 3 ) ).intValue() ); Select exameSelect = new Select( new String[]{ "trabalhadores", "marcacoes_trabalhador" }, new String[]{ "data", "realizada" }, new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( new Field( "trabalhadores.id" ).isEqual( id ) ).and( new Field( "marcacoes_trabalhador.data" ).isLessOrEqual( dataFim ) ).and( new Field( "marcacoes_trabalhador.tipo" ).isEqual( new Integer( Marcacao.TIPO_MARCACAO_TRABALHADOR_EXAMES ) ) ), new String[]{ "data desc" }, null ); Virtual2DArray exameArray = executer.executeQuery( exameSelect ); if( exameArray.columnLength() > 0 ) { Date dataExame = ( Date ) exameArray.get( 0, 0 ); String estado = ( String ) exameArray.get( 0, 1 ); data[ n ][ 3 ] = DF.format( dataExame ); data[ n ][ 4 ] = "y".equals( estado ) ? "Realizado" : "Faltou"; } else { data[ n ][ 3 ] = "--"; data[ n ][ 4 ] = "--"; } Select consultaSelect = new Select( new String[]{ "trabalhadores", "marcacoes_trabalhador" }, new String[]{ "data", "realizada" }, new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( new Field( "trabalhadores.id" ).isEqual( id ) ).and( new Field( "marcacoes_trabalhador.data" ).isLessOrEqual( dataFim ) ).and( new Field( "marcacoes_trabalhador.tipo" ).isEqual( new Integer( Marcacao.TIPO_MARCACAO_TRABALHADOR_CONSULTA ) ) ), new String[]{ "data desc" }, null ); Virtual2DArray consultaArray = executer.executeQuery( consultaSelect ); if( consultaArray.columnLength() > 0 ) { Date dataConsulta = ( Date ) consultaArray.get( 0, 0 ); String estado = ( String ) consultaArray.get( 0, 1 ); data[ n ][ 5 ] = DF.format( dataConsulta ); data[ n ][ 6 ] = "y".equals( estado ) ? "Realizada" : "Faltou"; } else { data[ n ][ 5 ] = "--"; data[ n ][ 6 ] = "--"; } } return data; } public String[][]getDadosHigieneSeguranca() throws Exception { Select select = new Select( new String[]{ "empresas", "estabelecimentos" }, new String[]{ "empresas.designacao_social", "estabelecimentos.nome", "estabelecimentos.id", "empresas.designacao_social_plain", "estabelecimentos.nome_plain" }, new Field( "estabelecimentos.empresa_id" ).isEqual( new Field( "empresas.id" ) ).and( new Field( "empresas.inactivo" ).isDifferent( "y" ) ).and( new Field( "estabelecimentos.inactivo" ).isDifferent( "y" ) ), new String[]{ "empresas.designacao_social_plain", "estabelecimentos.nome_plain" }, null ); Virtual2DArray array = executer.executeQuery( select ); String data[][] = new String[ array.columnLength() ][ 6 ]; for( int n = 0; n < array.columnLength(); n++ ) { data[ n ][ 0 ] = ( String ) array.get( n, 0 ); data[ n ][ 1 ] = ( String ) array.get( n, 1 ); Integer id = new Integer( ( (Number) array.get( n, 2 ) ).intValue() ); Select ultimaSelect = new Select( new String[]{ "marcacoes_estabelecimento" }, new String[]{ "data", "realizada", "data_relatorio" }, new Field( "marcacoes_estabelecimento.estabelecimento_id" ).isEqual( id ).and( new Field( "marcacoes_estabelecimento.data" ).isLessOrEqual( new Date() ) ), new String[]{ "data desc" }, null ); Virtual2DArray ultimaArray = executer.executeQuery( ultimaSelect ); if( ultimaArray.columnLength() > 0 ) { Date dataAud = ( Date ) ultimaArray.get( 0, 0 ); String estado = ( String ) ultimaArray.get( 0, 1 ); Date dataRel = ( Date ) ultimaArray.get( 0, 2 ); data[ n ][ 2 ] = DF.format( dataAud ); data[ n ][ 3 ] = "y".equals( estado ) ? "Realizada" : "Não Realizada"; data[ n ][ 4 ] = dataRel != null ? DF.format( dataRel ) : "--"; } else { data[ n ][ 2 ] = "--"; data[ n ][ 3 ] = "--"; data[ n ][ 4 ] = "--"; } Select proximaSelect = new Select( new String[]{ "marcacoes_estabelecimento" }, new String[]{ "data" }, new Field( "marcacoes_estabelecimento.estabelecimento_id" ).isEqual( id ).and( new Field( "marcacoes_estabelecimento.data" ).isGreaterOrEqual( new Date() ) ), new String[]{ "data desc" }, null ); Virtual2DArray proximaArray = executer.executeQuery( proximaSelect ); if( proximaArray.columnLength() > 0 ) { Date dataProx = ( Date ) proximaArray.get( 0, 0 ); data[ n ][ 5 ] = DF.format( dataProx ); } else { data[ n ][ 5 ] = "--"; } } return data; } }