You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SIPRP/trunk/SIPRPSoft/src/siprp/clientes/ClientesDataProvider.java

200 lines
5.7 KiB

/*
* 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 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.Assignment;
import com.evolute.utils.sql.Delete;
import com.evolute.utils.sql.Field;
import com.evolute.utils.sql.Insert;
import com.evolute.utils.sql.Select;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @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 */
private ClientesDataProvider()
throws Exception
{
DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER /*SingletonConstants.DBMANAGER*/ );
executer = dbm.getSharedExecuter( this );
}
public static ClientesDataProvider 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 Map<Integer,List<IDObject>> 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 );
Map<Integer,List<IDObject>> tipos = new HashMap<Integer,List<IDObject>>();
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 ArrayList<IDObject>() );
}
List tiposGrupo = 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 );
List<Integer> data[] = new List[ 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 ArrayList<Integer>();
}
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 ] = data[ n ].toArray( new Integer[ data[ n ].size() ] );
}
}
return elementos;
}
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 );
}
}