/* * LocaisRealizacaoDataProvider.java * * Created on February 22, 2007, 10:44 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package siprp.medicina.locais_realizacao; import com.evolute.utils.Singleton; import com.evolute.utils.arrays.Virtual2DArray; import com.evolute.utils.db.DBManager; import com.evolute.utils.db.Executer; import com.evolute.utils.sql.Assignment; 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.Update; import com.evolute.utils.tables.ColumnizedMappable; import java.util.Date; /** * * @author fpalma */ public class LocaisRealizacaoDataProvider { private static final Object LOCK = new Object(); private static LocaisRealizacaoDataProvider instance = null; private Executer EXECUTER; /** Creates a new instance of LocaisRealizacaoDataProvider */ public LocaisRealizacaoDataProvider() throws Exception { DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER ); EXECUTER = dbm.getSharedExecuter( this ); } public static LocaisRealizacaoDataProvider getProvider() throws Exception { synchronized( LOCK ) { if( instance == null ) { instance = new LocaisRealizacaoDataProvider(); } } return instance; } public ColumnizedMappable[] getEmpresasComMarcacoes( Date data ) throws Exception { Select select = new Select2( new String[]{ "marcacoes_trabalhador", "trabalhadores", "estabelecimentos", "empresas" }, new Integer[]{ Select2.JOIN_INNER, Select2.JOIN_INNER, Select2.JOIN_INNER }, new Expression[]{ new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ), new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ), new Field( "estabelecimentos.empresa_id" ).isEqual( new Field( "empresas.id" ) ) }, new String[]{ "DISTINCT empresas.id", "empresas.designacao_social", "empresas.designacao_social_plain" }, new Field( "marcacoes_trabalhador.data" ).isEqual( data ), new String[]{ "empresas.designacao_social_plain" }, null, null, null ); Virtual2DArray array = EXECUTER.executeQuery( select ); ColumnizedMappable empresas[] = new ColumnizedMappable[ array.columnLength() ]; for( int n = 0; n < empresas.length; n++ ) { Integer id = ( Integer ) array.get( n, 0 ); String designacao = ( String ) array.get( n, 1 ); empresas[ n ] = new ColumnizedMappable( id, designacao ); } return empresas; } public ColumnizedMappable[] getEstabelecimentosComMarcacoesByEmpresa( Integer empresaID, Date data ) throws Exception { Select select = new Select2( new String[]{ "marcacoes_trabalhador", "trabalhadores", "estabelecimentos" }, new Integer[]{ Select2.JOIN_INNER, Select2.JOIN_INNER }, new Expression[]{ new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ), new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ) }, new String[]{ "DISTINCT estabelecimentos.id", "estabelecimentos.nome", "estabelecimentos.nome_plain" }, new Field( "marcacoes_trabalhador.data" ).isEqual( data ).and( new Field( "estabelecimentos.empresa_id" ).isEqual( empresaID ) ), new String[]{ "estabelecimentos.nome_plain" }, null, null, null ); Virtual2DArray array = EXECUTER.executeQuery( select ); ColumnizedMappable estabelecimentos[] = new ColumnizedMappable[ array.columnLength() ]; for( int n = 0; n < estabelecimentos.length; n++ ) { Integer id = ( Integer ) array.get( n, 0 ); String nome = ( String ) array.get( n, 1 ); estabelecimentos[ n ] = new ColumnizedMappable( id, nome ); } return estabelecimentos; } public int getNumeroMarcacoesByEstabelecimentoAndTipo( Integer estabelecimentoID, int tipo, Date data ) throws Exception { Select select = new Select2( new String[]{ "marcacoes_trabalhador", "trabalhadores" }, new Integer[]{ Select2.JOIN_INNER }, new Expression[]{ new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ) }, new String[]{ "COUNT( * )" }, new Field( "marcacoes_trabalhador.data" ).isEqual( data ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( "marcacoes_trabalhador.tipo" ).isEqual( tipo ) ), null, null, null, null ); Virtual2DArray array = EXECUTER.executeQuery( select ); if( array.columnLength() == 0 || array.get( 0, 0 ) == null ) { return 0; } else { return ( ( Number ) array.get( 0, 0 ) ).intValue(); } } public Integer[] getPrestadoresIDByEstabelecimentoAndTipo( Integer estabelecimentoID, int tipo, Date data ) throws Exception { Select select = new Select2( new String[]{ "marcacoes_trabalhador", "trabalhadores" }, new Integer[]{ Select2.JOIN_INNER }, new Expression[]{ new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ) }, new String[]{ "DISTINCT prestador_id" }, new Field( "marcacoes_trabalhador.data" ).isEqual( data ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( "marcacoes_trabalhador.tipo" ).isEqual( tipo ) ), null, null, null, null ); 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 ); } return ids; } public void setPrestadorIDForEstabelecimentoAndTipo( Integer estabelecimentoID, int tipo, Date data, Integer prestadorID ) throws Exception { Select select = new Select2( new String[]{ "marcacoes_trabalhador", "trabalhadores" }, new Integer[]{ Select2.JOIN_INNER }, new Expression[]{ new Field( "marcacoes_trabalhador.trabalhador_id" ).isEqual( new Field( "trabalhadores.id" ) ) }, new String[]{ "marcacoes_trabalhador.id" }, new Field( "marcacoes_trabalhador.data" ).isEqual( data ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( estabelecimentoID ) ).and( new Field( "marcacoes_trabalhador.tipo" ).isEqual( tipo ) ), null, null, null, null ); Virtual2DArray array = EXECUTER.executeQuery( select ); System.out.println( select + "\n\n\n" + array.columnLength() ); for( int n = 0; n < array.columnLength(); n++ ) { Integer id = ( Integer ) array.get( n, 0 ); Update update = new Update( "marcacoes_trabalhador", new Assignment[]{ new Assignment( "prestador_id", prestadorID ) }, new Field( "id" ).isEqual( id ) ); EXECUTER.executeQuery( update ); System.out.println( update ); } } }