/* * EstatisticasProvider.java * * Created on 3 de Junho de 2004, 18:23 */ package siprp.pesquisas; import java.util.Calendar; import java.util.HashMap; import java.util.Hashtable; import java.util.Vector; import siprp.data.EmpresaData; import siprp.data.Marcacao; import siprp.data.MarcacaoTrabalhadorData; import siprp.data.TrabalhadorData; import com.evolute.utils.Singleton; import com.evolute.utils.arrays.Virtual2DArray; import com.evolute.utils.data.IDObject; import com.evolute.utils.data.MappableObject; import com.evolute.utils.db.DBManager; import com.evolute.utils.db.Executer; import com.evolute.utils.metadb.MetaProvider; 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.evolute.utils.sql.expression.Par; /** * * @author fpalma */ public class PesquisasProvider extends MetaProvider { private static final Object LOCK = new Object(); private static PesquisasProvider instance = null; private final Executer executer; private static final int TIPOS_OCASIONAL[] = new int[]{ 5, //mudanca de posto 6, //mudancas das condicoes -1, //apos 30 dias 4, //iniciativa do medico 3, //pedido do trabalhador 2, //acidente de trabalho 1, //Doenca subita -1, //Cessacao do contrato 10 //Outras razoes }; public static final String DESCRICAO_TIPOS_OCASIONAL[] = new String[]{ "MUDAN\u00C7A DE POSTO DE TRABALHO", "ALTERA\u00C7\u00D5ES NO POSTO DE TRABALHO", "REGRESSO AO TRABALHO AP\u00D3S AUS\u00CANCIA SUPERIOR A 30 DIAS", "INICIATIVA DO M\u00C9DICO", "PEDIDO DO TRABALHADOR", "ACIDENTE DE TRABALHO", "DOEN\u00C7A S\u00DABITA", "POR CESSA\u00C7\u00C3O DO CONTRATO DE TRABALHO", "OUTRAS RAZ\u00D5ES" }; private static final int TIPOS_EXAME[] = new int[]{ 1, //admissao 2, //periodico 3 //ocasional }; private static final int TIPO_OCASIONAL = 3; public static final String DESCRICAO_TIPOS_EXAME[] = new String[]{ "TOTAL DE EXAMES DE ADMISS\u00C3O", "TOTAL DE EXAMES PERI\u00D3DICOS", "TOTAL DE EXAMES OCASIONAIS" }; public static final String PAR_ANO = "PAR_ANO"; public static final String PAR_EMPRESA = "PAR_EMPRESA"; public static final String PAR_ESTABELECIMENTO = "PAR_ESTABELECIMENTO"; public static final String PAR_TIPO_EXAME = "PAR_TIPO_EXAME"; public static final String PAR_RESULTADO = "PAR_RESULTADO"; public static final String PAR_SEXO = "PAR_SEXO"; public static final String PAR_GRUPO_ETARIO = "PAR_GRUPO_ETARIO"; /** Creates a new instance of EstatisticasProvider */ public PesquisasProvider() 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 PesquisasProvider(); } } return instance; } public IDObject[] getAllEmpresas() throws Exception { Select select = new Select( new String[]{ "empresas" }, new String[]{ EmpresaData.ID, EmpresaData.DESIGNACAO_SOCIAL, EmpresaData.DESIGNACAO_SOCIAL_PLAIN }, new Field( EmpresaData.INACTIVO ).isDifferent( "y" ), new String[]{ EmpresaData.DESIGNACAO_SOCIAL_PLAIN }, null ); // Select select = new Select( "SELECT e.id, e.designacao_social FROM empresas e ORDER BY lower( e.designacao_social );" ); Virtual2DArray array = executer.executeQuery( select ); IDObject empresas[] = new IDObject[ array.columnLength() ]; for( int n = 0; n < empresas.length; n++ ) { empresas[ n ] = new MappableObject( (Integer)array.get( n, 0 ), array.get( n, 1 ) ); } return empresas; } public IDObject []getAllEstabelecimentosForEmpresa( Integer empresaID ) throws Exception { return ( ( siprp.FichaDataProvider )siprp.FichaDataProvider.getProvider() ).getAllEstabelecimentosForEmpresa( empresaID ); } public int[] countTrabalhadoresEstabelecimentoDezembro( Integer estabelecimentoID, int ano ) throws Exception { Calendar cal = Calendar.getInstance(); cal.set( Calendar.YEAR, ano ); cal.set( Calendar.MONTH, 11 ); cal.set( Calendar.DAY_OF_MONTH, 30 ); Select select = new Select( new String[]{ "trabalhadores" }, new String[]{ "COUNT(" + TrabalhadorData.ID + ")" }, new Par( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isLessOrEqual( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_ADMISSAO ).isEqual( null ) ) ).and( new Field( "estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( "m" ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ).and( new Par( new Field( TrabalhadorData.DATA_DEMISSAO ).isGreater( cal.getTime() ).or( new Field( TrabalhadorData.DATA_DEMISSAO ).isEqual( null ) ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); int contagem[] = new int[ 2 ]; contagem[ 0 ] = ( ( Number ) array.get( 0, 0 ) ).intValue(); select = new Select( new String[]{ "trabalhadores" }, new String[]{ "COUNT(" + TrabalhadorData.ID + ")" }, new Par( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isLessOrEqual( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_ADMISSAO ).isEqual( null ) ) ).and( new Field( "estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( "f" ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ).and( new Par( new Field( TrabalhadorData.DATA_DEMISSAO ).isGreater( cal.getTime() ).or( new Field( TrabalhadorData.DATA_DEMISSAO ).isEqual( null ) ) ) ) ); array = executer.executeQuery( select ); contagem[ 1 ] = ( ( Number ) array.get( 0, 0 ) ).intValue(); return contagem; } public int[][] countTrabalhadoresEstabelecimentoDezembroPorGrupoEtario( Integer estabelecimentoID, int ano ) throws Exception { Calendar eoy = Calendar.getInstance(); eoy.set( Calendar.YEAR, ano ); eoy.set( Calendar.MONTH, 11 ); eoy.set( Calendar.DAY_OF_MONTH, 30 ); Calendar today = Calendar.getInstance(); today.set( Calendar.YEAR, ano ); today.set( Calendar.MONTH, 11 ); today.set( Calendar.DAY_OF_MONTH, 31 ); today.set( Calendar.HOUR_OF_DAY, 23 ); Calendar age18 = (Calendar) today.clone(); age18.add( Calendar.YEAR, -18 ); Calendar age50 = (Calendar) today.clone(); age50.add( Calendar.YEAR, -50 ); //0-18 Select select = new Select( new String[]{ "trabalhadores" }, new String[]{ "COUNT(" + TrabalhadorData.ID + ")" }, new Par( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isLessOrEqual( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_ADMISSAO ).isEqual( null ) ) ).and( new Field( "estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( "m" ) ).and( new Field( TrabalhadorData.DATA_NASCIMENTO ).isGreater( age18.getTime() ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ).and( new Par( new Field( TrabalhadorData.DATA_DEMISSAO ).isGreater( eoy.getTime() ).or( new Field( TrabalhadorData.DATA_DEMISSAO ).isEqual( null ) ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); int contagem[][] = new int[ 3 ][ 2 ]; contagem[ 0 ][ 0 ] = ( ( Number ) array.get( 0, 0 ) ).intValue(); select = new Select( new String[]{ "trabalhadores" }, new String[]{ "COUNT(" + TrabalhadorData.ID + ")" }, new Par( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isLessOrEqual( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_ADMISSAO ).isEqual( null ) ) ).and( new Field( "estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( "f" ) ).and( new Field( TrabalhadorData.DATA_NASCIMENTO ).isGreater( age18.getTime() ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ).and( new Par( new Field( TrabalhadorData.DATA_DEMISSAO ).isGreater( eoy.getTime() ).or( new Field( TrabalhadorData.DATA_DEMISSAO ).isEqual( null ) ) ) ) ); array = executer.executeQuery( select ); contagem[ 0 ][ 1 ] = ( ( Number ) array.get( 0, 0 ) ).intValue(); //18-49 select = new Select( new String[]{ "trabalhadores" }, new String[]{ "COUNT(" + TrabalhadorData.ID + ")" }, new Par( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isLessOrEqual( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_ADMISSAO ).isEqual( null ) ) ).and( new Field( "estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( "m" ) ).and( new Field( TrabalhadorData.DATA_NASCIMENTO ).isLessOrEqual( age18.getTime() ) ).and( new Field( TrabalhadorData.DATA_NASCIMENTO ).isGreater( age50.getTime() ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ).and( new Par( new Field( TrabalhadorData.DATA_DEMISSAO ).isGreater( eoy.getTime() ).or( new Field( TrabalhadorData.DATA_DEMISSAO ).isEqual( null ) ) ) ) ); array = executer.executeQuery( select ); contagem[ 1 ][ 0 ] = ( ( Number ) array.get( 0, 0 ) ).intValue(); select = new Select( new String[]{ "trabalhadores" }, new String[]{ "COUNT(" + TrabalhadorData.ID + ")" }, new Par( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isLessOrEqual( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_ADMISSAO ).isEqual( null ) ) ).and( new Field( "estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( "f" ) ).and( new Field( TrabalhadorData.DATA_NASCIMENTO ).isLessOrEqual( age18.getTime() ) ).and( new Field( TrabalhadorData.DATA_NASCIMENTO ).isGreater( age50.getTime() ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ).and( new Par( new Field( TrabalhadorData.DATA_DEMISSAO ).isGreater( eoy.getTime() ).or( new Field( TrabalhadorData.DATA_DEMISSAO ).isEqual( null ) ) ) ) ); array = executer.executeQuery( select ); contagem[ 1 ][ 1 ] = ( ( Number ) array.get( 0, 0 ) ).intValue(); //50- select = new Select( new String[]{ "trabalhadores" }, new String[]{ "COUNT(" + TrabalhadorData.ID + ")" }, new Par( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isLessOrEqual( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_ADMISSAO ).isEqual( null ) ) ).and( new Field( "estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( "m" ) ).and( new Field( TrabalhadorData.DATA_NASCIMENTO ).isLessOrEqual( age50.getTime() ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ).and( new Par( new Field( TrabalhadorData.DATA_DEMISSAO ).isGreater( eoy.getTime() ).or( new Field( TrabalhadorData.DATA_DEMISSAO ).isEqual( null ) ) ) ) ); array = executer.executeQuery( select ); contagem[ 2 ][ 0 ] = ( ( Number ) array.get( 0, 0 ) ).intValue(); select = new Select( new String[]{ "trabalhadores" }, new String[]{ "COUNT(" + TrabalhadorData.ID + ")" }, new Par( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isLessOrEqual( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_ADMISSAO ).isEqual( null ) ) ).and( new Field( "estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( "f" ) ).and( new Field( TrabalhadorData.DATA_NASCIMENTO ).isLessOrEqual( age50.getTime() ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ).and( new Par( new Field( TrabalhadorData.DATA_DEMISSAO ).isGreater( eoy.getTime() ).or( new Field( TrabalhadorData.DATA_DEMISSAO ).isEqual( null ) ) ) ) ); array = executer.executeQuery( select ); contagem[ 2 ][ 1 ] = ( ( Number ) array.get( 0, 0 ) ).intValue(); return contagem; } public int[][] countExamesEstabelecimentoDezembroPorGrupoEtario( Integer estabelecimentoID, int ano ) throws Exception { Calendar today = Calendar.getInstance(); today.set( Calendar.YEAR, ano ); today.set( Calendar.MONTH, 11 ); today.set( Calendar.DAY_OF_MONTH, 31 ); today.set( Calendar.HOUR_OF_DAY, 23 ); Calendar age18 = (Calendar) today.clone(); age18.add( Calendar.YEAR, -18 ); Calendar age50 = (Calendar) today.clone(); age50.add( Calendar.YEAR, -50 ); Calendar dates[] = new Calendar[]{ null, age18, age50, null }; String sexos[] = new String[]{ "m", "f" }; int contagem[][] = new int[ 4 ][ 6 ]; for( int t = 0; t < TIPOS_EXAME.length; t++ ) { for( int s = 0; s < sexos.length; s++ ) { for( int d = 0; d < dates.length - 1; d++ ) { Select select = new Select( new String[]{ "exames", "trabalhadores" }, new String[]{ "DISTINCT trabalhadores.id, exames.data" }, new Field( "estabelecimento_id" ).isEqual( estabelecimentoID ).and( new Field( "exames.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( sexos[s] ) ).and( new Field( "YEAR( exames.data )" ).isEqual( new Integer( ano ) ) ).and( dates[ d ] == null ? new Field( "exames.id" ).isDifferent( null ) : new Field( TrabalhadorData.DATA_NASCIMENTO ).isLessOrEqual( dates[ d ].getTime() ) ).and( dates[ d + 1 ] == null ? new Field( "exames.id" ).isDifferent( null ) : new Field( TrabalhadorData.DATA_NASCIMENTO ).isGreater( dates[ d + 1 ].getTime() ) ).and( new Field( "exames.tipo" ).isEqual( new Integer( TIPOS_EXAME[ t ] ) ) ).and( new Field( "exames.inactivo" ).isDifferent( "y" ) ).and( new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ) ); try { Virtual2DArray array = executer.executeQuery( select ); contagem[ t ][ s * 3 + d ] = array.columnLength(); //System.out.println( select + ": " + contagem[ t ][ s * 3 + d ] ); } catch( Exception ex ) { contagem[ t ][ s * 3 + d ] = -1; } } } } return contagem; } public int[][] countExamesOcasionaisEstabelecimentoDezembroPorGrupoEtario( Integer estabelecimentoID, int ano ) throws Exception { Calendar today = Calendar.getInstance(); today.set( Calendar.YEAR, ano ); today.set( Calendar.MONTH, 11 ); today.set( Calendar.DAY_OF_MONTH, 31 ); today.set( Calendar.HOUR_OF_DAY, 23 ); Calendar age18 = (Calendar) today.clone(); age18.add( Calendar.YEAR, -18 ); Calendar age50 = (Calendar) today.clone(); age50.add( Calendar.YEAR, -50 ); Calendar dates[] = new Calendar[]{ null, age18, age50, null }; String sexos[] = new String[]{ "m", "f" }; int contagem[][] = new int[ TIPOS_OCASIONAL.length ][ ( dates.length - 1 ) * sexos.length ]; for( int t = 0; t < TIPOS_OCASIONAL.length; t++ ) { for( int s = 0; s < sexos.length; s++ ) { for( int d = 0; d < dates.length - 1; d++ ) { if( TIPOS_OCASIONAL[t] == -1 ) { contagem[ t ][ s * 3 + d ] = -1; continue; } Select select = new Select( new String[]{ "exames", "trabalhadores" }, new String[]{ "DISTINCT trabalhadores.id, exames.data" }, new Field( "estabelecimento_id" ).isEqual( estabelecimentoID ).and( new Field( "exames.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( sexos[s] ) ).and( new Field( "YEAR( exames.data )" ).isEqual( new Integer( ano ) ) ).and( dates[ d ] == null ? new Field( "exames.id" ).isDifferent( null ) : new Field( TrabalhadorData.DATA_NASCIMENTO ).isLessOrEqual( dates[ d ].getTime() ) ).and( dates[ d + 1 ] == null ? new Field( "exames.id" ).isDifferent( null ) : new Field( TrabalhadorData.DATA_NASCIMENTO ).isGreater( dates[ d + 1 ].getTime() ) ).and( new Field( "exames.tipo" ).isEqual( new Integer( 3 ) ) ).and( new Field( "exames.ocasional" ).isEqual( new Integer( TIPOS_OCASIONAL[ t ] ) ) ).and( new Field( "exames.inactivo" ).isDifferent( "y" ) ).and( new Field( "trabalhadores.inactivo" ).isDifferent( "y" ) ) ); try { Virtual2DArray array = executer.executeQuery( select ); contagem[ t ][ s * 3 + d ] = array.columnLength(); } catch( Exception ex ) { contagem[ t ][ s * 3 + d ] = -1; } } } } return contagem; } public double[] countNumeroMedioTrabalhadoresEmpresa( Integer empresaID, int ano ) throws Exception { double contagens[] = new double[ 2 ];; Select select; String sexos[] = new String[]{ "m", "f" }; for( int n = 0; n < sexos.length; n++ ) { select = new Select( new String[]{ "estabelecimentos", "trabalhadores" }, new String[]{ "COUNT(trabalhadores.id)" }, new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ).and( new Par( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isLess( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_ADMISSAO ).isEqual( null ) ) ) ).and( new Par( new Field( "YEAR(" + TrabalhadorData.DATA_DEMISSAO + ")" ).isGreater( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_DEMISSAO ).isEqual( null ) ) ) ).and( new Field( "empresa_id" ).isEqual( empresaID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( sexos[ n ] ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ) ); Virtual2DArray array = executer.executeQuery( select ); contagens[ n ] = ( ( Number ) array.get( 0, 0 ) ).doubleValue(); } for( int n = 0; n < sexos.length; n++ ) { select = new Select( new String[]{ "estabelecimentos", "trabalhadores" }, new String[]{ "SUM( MONTH( data_demissao ) )" }, new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ).and( new Par( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isLess( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_ADMISSAO ).isEqual( null ) ) ) ).and( new Field( "YEAR(" + TrabalhadorData.DATA_DEMISSAO + ")" ).isEqual( new Integer( ano ) ) ).and( new Field( "empresa_id" ).isEqual( empresaID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( sexos[ n ] ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ) ); System.out.println( "SELECT: " + select ); Virtual2DArray array = executer.executeQuery( select ); if( array.get( 0, 0 ) != null ) { contagens[ n ] += ( ( Number ) array.get( 0, 0 ) ).doubleValue() / 12.0; } } for( int n = 0; n < sexos.length; n++ ) { select = new Select( new String[]{ "estabelecimentos", "trabalhadores" }, new String[]{ "SUM( ( 1 + 12 - MONTH( data_admissao ) ) )" }, new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ).and( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isEqual( new Integer( ano ) ) ).and( new Par( new Field( "YEAR(" + TrabalhadorData.DATA_DEMISSAO + ")" ).isGreater( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_DEMISSAO ).isEqual( null ) ) ) ).and( new Field( "empresa_id" ).isEqual( empresaID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( sexos[ n ] ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ) ); Virtual2DArray array = executer.executeQuery( select ); if( array.get( 0, 0 ) != null ) { contagens[ n ] += ( ( Number ) array.get( 0, 0 ) ).doubleValue() / 12.0; } } for( int n = 0; n < sexos.length; n++ ) { select = new Select( new String[]{ "estabelecimentos", "trabalhadores" }, new String[]{ "SUM( ( 1 + MONTH( data_demissao ) - MONTH( data_admissao ) ) )" }, new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ).and( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isEqual( new Integer( ano ) ) ).and( new Field( "YEAR(" + TrabalhadorData.DATA_DEMISSAO + ")" ).isEqual( new Integer( ano ) ) ).and( new Field( "empresa_id" ).isEqual( empresaID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( sexos[ n ] ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ) ); Virtual2DArray array = executer.executeQuery( select ); if( array.get( 0, 0 ) != null ) { contagens[ n ] += ( ( Number ) array.get( 0, 0 ) ).doubleValue() / 12.0; } } return contagens; } public double[] countNumeroTotalTrabalhadoresEmpresa( Integer empresaID, int ano ) throws Exception { double contagens[] = new double[ 2 ];; Select select; String sexos[] = new String[]{ "m", "f" }; for( int n = 0; n < sexos.length; n++ ) { select = new Select( new String[]{ "estabelecimentos", "trabalhadores" }, new String[]{ "COUNT(trabalhadores.id)" }, new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ).and( new Par( new Field( "YEAR(" + TrabalhadorData.DATA_ADMISSAO + ")" ).isLessOrEqual( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_ADMISSAO ).isEqual( null ) ) ) ).and( new Par( new Field( "YEAR(" + TrabalhadorData.DATA_DEMISSAO + ")" ).isGreaterOrEqual( new Integer( ano ) ).or( new Field( TrabalhadorData.DATA_DEMISSAO ).isEqual( null ) ) ) ).and( new Field( "empresa_id" ).isEqual( empresaID ) ).and( new Field( TrabalhadorData.SEXO ).isEqual( sexos[ n ] ) ).and( new Field( "trabalhadores.inactivo" ).isEqual( "n" ) ) ); Virtual2DArray array = executer.executeQuery( select ); contagens[ n ] = ( ( Number ) array.get( 0, 0 ) ).doubleValue(); } return contagens; } public String []getNomesExames() throws Exception { String nomes[]; Select select = new Select( new String[]{ "tipos_exames_comp" }, new String[]{ "descricao", "ordem" }, null, new String[]{ "ordem" }, null ); Virtual2DArray array = executer.executeQuery( select ); nomes = new String[ array.columnLength() ]; for( int n = 0; n < array.columnLength(); n++ ) { nomes[ n ] = ( String )array.get( n, 0 ); } return nomes; } public int []countExamesComplementaresEmpresa( Integer empresaID, int ano ) throws Exception { // Vector perfis[] = new Vector[ 2 ]; // Hashtable contagens[] = new Hashtable[ 2 ]; // Select select; // Virtual2DArray array; // // for( int p = 0; p < 2; p++ ) // { // Integer pAux = new Integer( p + 1 ); // perfis[ p ] = new Vector(); // select = new Select( new String[]{"estabelecimentos", "trabalhadores", "marcacoes_trabalhador"}, // new String[]{"COUNT(marcacoes_trabalhador.id)", TrabalhadorData.PERFIL }, // 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( "empresa_id" ).isEqual( empresaID ) ).and( // new Field( MarcacaoTrabalhadorData.TIPO ).isEqual( new Integer( MarcacaoTrabalhadorData.TIPO_EXAMES ) ) ).and( // new Field( "YEAR( marcacoes_trabalhador.data ) " ).isEqual( new Integer( ano ) ).and( // new Field( MarcacaoTrabalhadorData.ESTADO ).in( new Integer[]{ new Integer( 1 ), new Integer( 2 ) } ) ) ).and( // new Field( "trabalhadores.perfil" ).isEqual( pAux ) ) ); // array = executer.executeQuery( select ); // // select = // new Select( new String[]{ "prt_elementos_protocolo", "prt_tipos_elementos_protocolo" }, // new String[]{ "DISTINCT prt_tipos_elementos_protocolo.grupo_protocolo_id" }, // new Field( "prt_elementos_protocolo.empresa_id" ).isEqual( empresaID ).and( // new Field( "prt_elementos_protocolo.tipo_elemento_protocolo_id" ).isEqual( new Field( "prt_elementos_protocolo.id" ) ) ).and( // new Field( "pr_elementos_protocolo.perfil" ).isEqual( new Integer( p + 1 ) ) ) ); // array = executer.executeQuery( select ); // for( int ecd = 0; ecd < array.columnLength(); ecd++ ) // { // perfis[ p ].add( ( Integer ) array.get( 0, 0 ) ); // } // } // // // return contagens; int contagens[]; Select select; select = new Select( new String[]{"empresas"}, new String[]{ "perfil_1", "perfil_2" }, new Field( "id" ).isEqual( empresaID ) ); Virtual2DArray array = executer.executeQuery( select ); String p1Str = (String)array.get( 0, 0 ); String p2Str = (String)array.get( 0, 1 ); Number perfil1 = ( p1Str != null && p1Str.length() > 0 )?new Integer( p1Str ):null; Number perfil2 = ( p2Str != null && p2Str.length() > 0 )?new Integer( p2Str ):null; select = new Select( new String[]{ "tipos_exames_comp" }, new String[]{ "id", "ordem" }, null, new String[]{ "ordem" }, null ); array = executer.executeQuery( select ); contagens = new int[ array.columnLength() ]; Hashtable idExames = new Hashtable(); for( int n = 0; n < array.columnLength(); n++ ) { idExames.put( ( Number )array.get( n, 0 ), new Integer( n ) ); } if( perfil1 == null && perfil2 == null ) { return contagens; } else if( perfil1 == null ) { perfil1 = perfil2; } else if( perfil2 == null ) { perfil2 = perfil1; } select = new Select( new String[]{"estabelecimentos", "trabalhadores", "marcacoes_trabalhador"}, new String[]{"COUNT(marcacoes_trabalhador.id)", TrabalhadorData.PERFIL }, 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( "empresa_id" ).isEqual( empresaID ) ).and( new Field( MarcacaoTrabalhadorData.TIPO ).isEqual( new Integer( MarcacaoTrabalhadorData.TIPO_EXAMES ) ) ).and( new Field( "YEAR( marcacoes_trabalhador.data ) " ).isEqual( new Integer( ano ) ).and( new Field( Marcacao.REALIZADA ).isEqual( "y" ) ) ), new String[]{ TrabalhadorData.PERFIL }, new String[]{ TrabalhadorData.PERFIL } ); array = executer.executeQuery( select ); int numeroExames[] = new int[ 2 ]; for( int n = 0; n < array.columnLength(); n++ ) { Integer perfil = (Integer)array.get( n, 1 ); if( perfil == null || perfil.intValue() == 1 ) { numeroExames[0] += ( (Number) array.get( n, 0 ) ).intValue(); } else if( perfil.intValue() == 2 ) { numeroExames[1] += ( (Number) array.get( n, 0 ) ).intValue(); } } Hashtable hash = new Hashtable(); select = new Select( new String[]{ "exames_perfis" }, new String[]{ "tipo", "multiplicador" }, new Field( "perfil" ).isEqual( perfil1 ) ); array = executer.executeQuery( select ); for( int n = 0; n < array.columnLength(); n++ ) { Number tipo = (Number) array.get( n, 0 ); if( tipo == null ) { continue; } Number multiplicador = (Number) array.get( n, 1 ); if( multiplicador == null ) { multiplicador = new Integer( 1 ); } if( idExames.containsKey( tipo ) ) { int pos = ((Integer)idExames.get( tipo )).intValue(); contagens[ pos ] += numeroExames[0] * multiplicador.intValue(); } } select = new Select( new String[]{ "exames_perfis" }, new String[]{ "tipo", "multiplicador" }, new Field( "perfil" ).isEqual( perfil2 ) ); array = executer.executeQuery( select ); for( int n = 0; n < array.columnLength(); n++ ) { Number tipo = (Number) array.get( n, 0 ); if( tipo == null ) { continue; } Number multiplicador = (Number) array.get( n, 1 ); if( multiplicador == null ) { multiplicador = new Integer( 1 ); } if( idExames.containsKey( tipo ) ) { int pos = ((Integer)idExames.get( tipo )).intValue(); contagens[ pos ] += numeroExames[1] * multiplicador.intValue(); } } return contagens; } public HashMap countExamesComplementaresEmpresaNovosProtocolos( Integer empresaId, int ano ) throws Exception { HashMap> perfis = new HashMap>(); HashMap contagens = null; Select select = new Select2( new String[]{ "prt_elementos_protocolo", "prt_tipos_elementos_protocolo", "prt_grupos_protocolo" }, new Integer[]{ Select2.JOIN_INNER, Select2.JOIN_INNER }, new Expression[]{ new Field( "prt_elementos_protocolo.tipo_elemento_protocolo_id" ).isEqual( new Field( "prt_tipos_elementos_protocolo.id" ) ), new Field( "prt_tipos_elementos_protocolo.grupo_protocolo_id" ).isEqual( new Field( "prt_grupos_protocolo.id" ) ), }, new String[]{ "DISTINCT prt_elementos_protocolo.numero_perfil", "prt_grupos_protocolo.descricao" }, new Field( "prt_elementos_protocolo.empresa_id" ).isEqual( empresaId ), null, null, null, null ); Virtual2DArray array = executer.executeQuery( select ); for( int n = 0; n < array.columnLength(); n++ ) { Integer perfil = ( Integer ) array.get( n, 0 ); String tipo = ( String ) array.get( n, 1 ); if( !perfis.containsKey( perfil ) ) { perfis.put( perfil, new Vector() ); } perfis.get( perfil ).add( tipo ); } if( perfis.size() > 0 ) { contagens = new HashMap(); select = new Select2( new String[]{"trabalhadores", "marcacoes_trabalhador", "empresas"}, new Integer[]{ Select2.JOIN_INNER, Select2.JOIN_INNER }, new Expression[]{ new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ), new Field( "trabalhadores.estabelecimentos_id" ).isEqual( new Field( "estabelecimentos.id" ) ), }, new String[]{"COUNT(marcacoes_trabalhador.id)", TrabalhadorData.PERFIL }, new Field( "estabelecimentos.id.empresa_id" ).isEqual( empresaId ).and( new Field( MarcacaoTrabalhadorData.TIPO ).isEqual( new Integer( MarcacaoTrabalhadorData.TIPO_EXAMES ) ) ).and( new Field( "YEAR( marcacoes_trabalhador.data ) " ).isEqual( new Integer( ano ) ).and( new Field( Marcacao.REALIZADA ).isEqual( "y" ) ) ), null, new String[]{ TrabalhadorData.PERFIL }, null, null ); array = executer.executeQuery( select ); for( int n = 0; n < array.columnLength(); n++ ) { Integer perfil = (Integer)array.get( n, 1 ); Integer count = ( ( Number ) array.get( n, 0 ) ).intValue(); if( perfil == null ) { continue; } Vector tipos = perfis.get( perfil ); for( String tipo : tipos ) { if( !contagens.containsKey( tipo ) ) { contagens.put( tipo, count ); } else { contagens.put( tipo, contagens.get( tipo ) + count ); } } } } return contagens; } public int []countExamesComplementaresEstabelecimento( Integer estabelecimentoID, int ano ) throws Exception { int contagens[] = null; Select select; select = new Select( new String[]{"estabelecimentos", "empresas"}, new String[]{ "empresas.perfil_1", "empresas.perfil_2" }, new Field( "estabelecimentos.id" ).isEqual( estabelecimentoID ).and( new Field( "estabelecimentos.empresa_id" ).isEqual( new Field( "empresas.id" ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); String p1Str = (String)array.get( 0, 0 ); String p2Str = (String)array.get( 0, 1 ); Number perfil1 = ( p1Str != null && p1Str.length() > 0 )?new Integer( p1Str ):null; Number perfil2 = ( p2Str != null && p2Str.length() > 0 )?new Integer( p2Str ):null; select = new Select( new String[]{ "tipos_exames_comp" }, new String[]{ "id", "ordem" }, null, new String[]{ "ordem" }, null ); array = executer.executeQuery( select ); contagens = new int[ array.columnLength() ]; Hashtable idExames = new Hashtable(); for( int n = 0; n < array.columnLength(); n++ ) { idExames.put( ( Number )array.get( n, 0 ), new Integer( n ) ); } if( perfil1 == null && perfil2 == null ) { return contagens; } else if( perfil1 == null ) { perfil1 = perfil2; } else if( perfil2 == null ) { perfil2 = perfil1; } select = new Select( new String[]{"trabalhadores", "marcacoes_trabalhador"}, new String[]{"COUNT(marcacoes_trabalhador.id)", TrabalhadorData.PERFIL }, new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( MarcacaoTrabalhadorData.TIPO ).isEqual( new Integer( MarcacaoTrabalhadorData.TIPO_EXAMES ) ) ).and( new Field( "YEAR( marcacoes_trabalhador.data ) " ).isEqual( new Integer( ano ) ).and( new Field( Marcacao.REALIZADA ).isEqual( "y" ) ) ), new String[]{ TrabalhadorData.PERFIL }, new String[]{ TrabalhadorData.PERFIL } ); array = executer.executeQuery( select ); int numeroExames[] = new int[ 2 ]; for( int n = 0; n < array.columnLength(); n++ ) { Integer perfil = (Integer)array.get( n, 1 ); if( perfil == null || perfil.intValue() == 1 ) { numeroExames[0] += ( (Number) array.get( n, 0 ) ).intValue(); } else if( perfil.intValue() == 2 ) { numeroExames[1] += ( (Number) array.get( n, 0 ) ).intValue(); } } Hashtable hash = new Hashtable(); select = new Select( new String[]{ "exames_perfis" }, new String[]{ "tipo", "multiplicador" }, new Field( "perfil" ).isEqual( perfil1 ) ); array = executer.executeQuery( select ); for( int n = 0; n < array.columnLength(); n++ ) { Number tipo = (Number) array.get( n, 0 ); if( tipo == null ) { continue; } Number multiplicador = (Number) array.get( n, 1 ); if( multiplicador == null ) { multiplicador = new Integer( 1 ); } if( idExames.containsKey( tipo ) ) { int pos = ((Integer)idExames.get( tipo )).intValue(); contagens[ pos ] += numeroExames[0] * multiplicador.intValue(); } } select = new Select( new String[]{ "exames_perfis" }, new String[]{ "tipo", "multiplicador" }, new Field( "perfil" ).isEqual( perfil2 ) ); array = executer.executeQuery( select ); for( int n = 0; n < array.columnLength(); n++ ) { Number tipo = (Number) array.get( n, 0 ); if( tipo == null ) { continue; } Number multiplicador = (Number) array.get( n, 1 ); if( multiplicador == null ) { multiplicador = new Integer( 1 ); } if( idExames.containsKey( tipo ) ) { int pos = ((Integer)idExames.get( tipo )).intValue(); contagens[ pos ] += numeroExames[1] * multiplicador.intValue(); } } return contagens; } public HashMap countExamesComplementaresEstabelecimentoNovosProtocolos( Integer estabelecimentoID, int ano ) throws Exception { HashMap> perfis = new HashMap>(); HashMap contagens = null; Select select = new Select2( new String[]{ "estabelecimentos" }, new Integer[]{}, new Expression[]{}, new String[]{ "empresa_id" }, new Field( "id" ).isEqual( estabelecimentoID ), null, null, null, null ); Virtual2DArray array = executer.executeQuery( select ); Integer empresaId = ( Integer ) array.get( 0, 0 ); select = new Select2( new String[]{ "prt_elementos_protocolo", "prt_tipos_elementos_protocolo", "prt_grupos_protocolo" }, new Integer[]{ Select2.JOIN_INNER, Select2.JOIN_INNER }, new Expression[]{ new Field( "prt_elementos_protocolo.tipo_elemento_protocolo_id" ).isEqual( new Field( "prt_tipos_elementos_protocolo.id" ) ), new Field( "prt_tipos_elementos_protocolo.grupo_protocolo_id" ).isEqual( new Field( "prt_grupos_protocolo.id" ) ), }, new String[]{ "DISTINCT prt_elementos_protocolo.numero_perfil", "prt_grupos_protocolo.descricao" }, new Field( "prt_elementos_protocolo.empresa_id" ).isEqual( empresaId ), null, null, null, null ); array = executer.executeQuery( select ); for( int n = 0; n < array.columnLength(); n++ ) { Integer perfil = ( Integer ) array.get( n, 0 ); String tipo = ( String ) array.get( n, 1 ); if( !perfis.containsKey( perfil ) ) { perfis.put( perfil, new Vector() ); } perfis.get( perfil ).add( tipo ); } if( perfis.size() > 0 ) { contagens = new HashMap(); select = new Select2( new String[]{"trabalhadores", "marcacoes_trabalhador"}, new Integer[]{ Select2.JOIN_INNER }, new Expression[]{ new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ) }, new String[]{"COUNT(marcacoes_trabalhador.id)", TrabalhadorData.PERFIL }, new Field( "trabalhadores.estabelecimento_id" ).isEqual( estabelecimentoID ).and( new Field( MarcacaoTrabalhadorData.TIPO ).isEqual( new Integer( MarcacaoTrabalhadorData.TIPO_EXAMES ) ) ).and( new Field( "YEAR( marcacoes_trabalhador.data ) " ).isEqual( new Integer( ano ) ).and( new Field( Marcacao.REALIZADA ).isEqual( "y" ) ) ), null, new String[]{ TrabalhadorData.PERFIL }, null, null ); array = executer.executeQuery( select ); for( int n = 0; n < array.columnLength(); n++ ) { Integer perfil = (Integer)array.get( n, 1 ); Integer count = ( ( Number ) array.get( n, 0 ) ).intValue(); if( perfil == null ) { continue; } Vector tipos = perfis.get( perfil ); for( String tipo : tipos ) { if( !contagens.containsKey( tipo ) ) { contagens.put( tipo, count ); } else { contagens.put( tipo, contagens.get( tipo ) + count ); } } } } return contagens; } }