forked from Coded/SIPRP
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.
168 lines
5.4 KiB
168 lines
5.4 KiB
/*
|
|
* To change this template, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
|
|
package db.providers;
|
|
|
|
import com.evolute.utils.arrays.Virtual2DArray;
|
|
import com.evolute.utils.db.DBException;
|
|
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 db.data.siprp.outer.EmpresasData;
|
|
import db.data.siprp.outer.UtilizadoresData;
|
|
import db.entidades.Utilizador;
|
|
import utils.Global;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
*
|
|
* @author lluis
|
|
*/
|
|
public class UtilizadoresDataProvider extends GenericDataProvider
|
|
{
|
|
private static UtilizadoresDataProvider INSTANCE = null;
|
|
|
|
private UtilizadoresDataProvider() throws Exception
|
|
{
|
|
super();
|
|
|
|
}
|
|
|
|
public static synchronized UtilizadoresDataProvider getInstance() throws Exception
|
|
{
|
|
if ( INSTANCE == null )
|
|
{
|
|
INSTANCE = new UtilizadoresDataProvider();
|
|
}
|
|
return INSTANCE;
|
|
}
|
|
|
|
|
|
public Utilizador getUtilizador( String login ) throws Exception
|
|
{
|
|
Utilizador user = null;
|
|
|
|
Select2 query = new Select2(
|
|
new String[] { "utilizadores" },
|
|
new Integer[] {},
|
|
new Expression[] {},
|
|
new String[] { "utilizadores.id" },
|
|
new Field( "utilizadores.apagado" ).isEqual( "n" ).and(
|
|
new Field( "LOWER( utilizadores.login )" ).isEqual( login.toLowerCase() )
|
|
),
|
|
new String[] {},
|
|
null, null, null
|
|
);
|
|
Virtual2DArray result = getExecuter().executeQuery( query );
|
|
if ( result != null && result.rowCount( ) > 0 )
|
|
{
|
|
Integer userID = result.get( 0, 0 );
|
|
user = getUtilizador( userID );
|
|
}
|
|
return user;
|
|
}
|
|
|
|
public Utilizador getUtilizador( Integer userID ) throws Exception
|
|
{
|
|
Utilizador user = null;
|
|
UtilizadoresData userData = getProvider().load( UtilizadoresData.class, userID );
|
|
user = copyFrom( userData );
|
|
return user;
|
|
}
|
|
|
|
private Utilizador copyFrom( UtilizadoresData userData )
|
|
{
|
|
Utilizador user = null;
|
|
if ( userData != null )
|
|
{
|
|
user = new Utilizador();
|
|
user.setId( userData.getId() );
|
|
user.setLogin( userData.getLogin() );
|
|
user.setPassword( userData.getPassword() );
|
|
user.setData_password( userData.getData_password() );
|
|
user.setEmail( userData.getEmail() );
|
|
user.setEmpresa_id( userData.getEmpresa_id() );
|
|
user.setEstabelecimento_id( userData.getEstabelecimento_id() );
|
|
user.setAdministrador( userData.getAdministrador() );
|
|
user.setTipo( userData.getTipo() );
|
|
user.setNumero_cedula( userData.getNumero_cedula() );
|
|
user.setCap( userData.getCap() );
|
|
user.setNome( userData.getNome() );
|
|
user.setMedico_id( userData.getMedico_id() );
|
|
user.setFuncionario_hst_id( userData.getFuncionario_hst_id() );
|
|
user.setActivo( userData.getActivo() );
|
|
user.setResponsavel_loja( userData.getResponsavel_loja() );
|
|
user.setGestor_geral( userData.getGestor_geral() );
|
|
user.setApagado( userData.getApagado() );
|
|
}
|
|
return user;
|
|
}
|
|
|
|
public List< Utilizador > getUtilizadoresListByTipo( Integer tipo, String responsavel_loja, Integer estabelecimento_id )
|
|
throws Exception
|
|
{
|
|
List< Utilizador > list = new LinkedList< Utilizador >();
|
|
int type = tipo;
|
|
|
|
Expression where = new Field( UtilizadoresData.ACTIVO_FULL ).isEqual( "y" );
|
|
where = where.and( new Field( UtilizadoresData.APAGADO_FULL ).isEqual( "n" ) );
|
|
where = where.and( new Field( UtilizadoresData.TIPO_FULL ).isEqual( type ) );
|
|
if ( type == Global.RESPONSAVEL_SEGURANCA || type == Global.DIRECTOR_LOJA )
|
|
{
|
|
where = where.and( new Field( UtilizadoresData.ESTABELECIMENTO_ID_FULL ).isEqual( estabelecimento_id ) );
|
|
}
|
|
if ( type == Global.RESPONSAVEL_SEGURANCA && "y".equals( responsavel_loja ) )
|
|
{
|
|
where = where.and( new Field( UtilizadoresData.RESPONSAVEL_LOJA_FULL ).isEqual( "y" ) );
|
|
}
|
|
|
|
if ( type == Global.DIRECTOR_LOJA || type == Global.TECNICO_HS || type == Global.DIRECTOR_NACIONAL_SEGURANCA
|
|
|| ( type == Global.RESPONSAVEL_SEGURANCA && "y".equals( responsavel_loja ) ) )
|
|
{
|
|
System.out.println( "\nUtilizadoresDataProvider . getUtilizadoresListByTipo( " + tipo + ", " + responsavel_loja + ", " + estabelecimento_id + " ) : " );
|
|
System.out.println( "\tSQL : SELECT * FROM utilizadores WHERE " + where.toString() );
|
|
|
|
List< UtilizadoresData > listData = getProvider().listLoad(
|
|
UtilizadoresData.class, where, new String[] { UtilizadoresData.NOME }, null );
|
|
for ( UtilizadoresData userData : listData )
|
|
{
|
|
list.add( copyFrom( userData ) );
|
|
}
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
public List< Utilizador > getUtilizadoresList() throws Exception
|
|
{
|
|
List< Utilizador > list = new LinkedList< Utilizador >();
|
|
|
|
Expression where = new Field( UtilizadoresData.ACTIVO_FULL ).isEqual( "y" ).and(
|
|
new Field( UtilizadoresData.APAGADO_FULL ).isEqual( "n" ) );
|
|
|
|
List< UtilizadoresData > listData = getProvider().listLoad( UtilizadoresData.class, where, new String[] { UtilizadoresData.NOME }, null );
|
|
for ( UtilizadoresData userData : listData )
|
|
{
|
|
list.add( copyFrom( userData ) );
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public Boolean isSiprp( Utilizador utilizador ) throws DBException
|
|
{
|
|
Select select = new Select2( EmpresasData.TABLENAME, new Field( EmpresasData.DESIGNACAO_SOCIAL_FULL ).isILike( "siprp%" )
|
|
.and( new Field( EmpresasData.ID_FULL ).isEqual( utilizador.getEmpresa_id() ) ), "1" );
|
|
|
|
Select outer = new Select( "select exists( " + select.toString() + " )" );
|
|
|
|
Virtual2DArray array = getExecuter().executeQuery( outer );
|
|
|
|
return array.rowCount() > 0 ? ( Boolean ) array.get( 0, 1 ) : Boolean.FALSE;
|
|
}
|
|
|
|
}
|