/* * ClientesDataProvider.java * * Created on January 27, 2006, 12:50 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package siprp.clientes; 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.metadb.*; import com.evolute.utils.sql.*; import com.evolute.utils.strings.*; /** * * @author fpalma */ public class ClientesDataProvider extends MetaProvider { private static final Object LOCK = new Object(); private static ClientesDataProvider instance = null; private final Executer executer; private Integer etiquetaID; /** Creates a new instance of ClientesDataProvider */ public ClientesDataProvider() 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 ClientesDataProvider(); } } return instance; } public IDObject[] getAllGruposProtocoloReais() throws Exception { return getAllGruposProtocoloPorTipo( true ); } public IDObject[] getAllGruposProtocoloFalsos() throws Exception { return getAllGruposProtocoloPorTipo( false ); } public IDObject[] getAllGruposProtocoloPorTipo( boolean real ) throws Exception { Select select = new Select( new String[]{ "prt_grupos_protocolo" }, new String[]{ "id", "descricao", "ordem" }, new Field( "grupo_real" ).isEqual( real ? "y" : "n" ), new String[]{ "ordem" }, null ); Virtual2DArray array = executer.executeQuery( select ); IDObject grupos[] = new IDObject[ array.columnLength() ]; for( int n = 0; n < grupos.length; n++ ) { grupos[ n ] = new MappableObject( ( Integer ) array.get( n, 0 ), ( String ) array.get( n, 1 ) ); } return grupos; } public Hashtable getAllTiposElementosProtocoloByGrupo() throws Exception { Select select = new Select( new String[]{ "prt_tipos_elementos_protocolo" }, new String[]{ "id", "descricao", "ordem", "grupo_protocolo_id" }, null, new String[]{ "grupo_protocolo_id", "ordem" }, null ); Virtual2DArray array = executer.executeQuery( select ); Hashtable tipos = new Hashtable(); for( int n = 0; n < array.columnLength(); n++ ) { IDObject tipo = new MappableObject( ( Integer ) array.get( n, 0 ), ( String ) array.get( n, 1 ) ); Integer grupoID = ( Integer ) array.get( n, 3 ); if( !tipos.containsKey( grupoID ) ) { tipos.put( grupoID, new Vector() ); } Vector tiposGrupo = ( Vector ) tipos.get( grupoID ); tiposGrupo.add( tipo ); } return tipos; } public void setElementosProtocoloForEmpresa( Integer empresaID, Integer elementos[][] ) throws Exception { // executer.executeQuery( Begin.BEGIN ); try { Delete delete = new Delete( "prt_elementos_protocolo", new Field( "empresa_id" ).isEqual( empresaID ) ); executer.executeQuery( delete ); for( int p = 0; p < elementos.length; p++ ) { Integer numeroPerfil = new Integer( p + 1 ); for( int e = 0; e < elementos[ p ].length; e++ ) { Insert insert = new Insert( "prt_elementos_protocolo", new Assignment[]{ new Assignment( new Field( "empresa_id" ), empresaID ), new Assignment( new Field( "tipo_elemento_protocolo_id" ), elementos[ p ][ e ] ), new Assignment( new Field( "numero_perfil" ), numeroPerfil ) } ); executer.executeQuery( insert, null ); } } // executer.executeQuery( Commit.COMMIT ); } catch( Exception ex ) { // executer.executeQuery( Rollback.ROLLBACK ); throw ex; } } public Integer[][] getElementosProtocoloForEmpresa( Integer empresaID, int numeroPerfis ) throws Exception { Select select = new Select( new String[]{ "prt_elementos_protocolo" }, new String[]{ "tipo_elemento_protocolo_id", "numero_perfil" }, new Field( "empresa_id" ).isEqual( empresaID ) , new String[]{ "numero_perfil" }, null ); Virtual2DArray array = executer.executeQuery( select ); Vector data[] = new Vector[ numeroPerfis ]; for( int n = 0; n < array.columnLength(); n++ ) { int perfil = ( ( Integer ) array.get( n, 1 ) ).intValue() - 1; Integer tipo = ( Integer ) array.get( n, 0 ); if( data[ perfil ] == null ) { data[ perfil ] = new Vector(); } data[ perfil ].add( tipo ); } Integer elementos[][] = new Integer[ numeroPerfis ][]; for( int n = 0; n < numeroPerfis; n++ ) { if( data[ n ] == null ) { elementos[ n ] = new Integer[ 0 ]; } else { elementos[ n ] = ( Integer [] ) data[ n ].toArray( new Integer[ data[ n ].size() ] ); } } return elementos; } public Integer getEtiquetaID() throws Exception { if( etiquetaID == null ) { Select select = new Select( new String[]{ "etiquetas" }, new String[]{ "max(id)" }, null ); Virtual2DArray array = executer.executeQuery( select ); if( array.columnLength() == 0 || array.get( 0, 0 ) == null ) { return null; } etiquetaID = ( Integer ) array.get( 0, 0 ); } return etiquetaID; } public Integer getEmpresaIDByTrabalhadorID( Integer trabalhadorID ) throws Exception { Select select = new Select( new String[]{ "trabalhadores", "estabelecimentos" }, new String[]{ "estabelecimentos.empresa_id" }, new Field( "trabalhadores.id" ).isEqual( trabalhadorID ).and( new Field( "trabalhadores.estabelecimento_id" ).isEqual( new Field( "estabelecimentos.id" ) ) ) ); Virtual2DArray array = executer.executeQuery( select ); return ( Integer ) array.get( 0, 0 ); } }