package siprp.data.provider; import java.util.ArrayList; import java.util.Date; import java.util.LinkedList; import java.util.List; import siprp.data.outer.EmailPlanoDeActuacaoData; import siprp.data.outer.EmpresasData; import siprp.data.outer.EstabelecimentosData; import siprp.data.outer.HsEmailEstabelecimentoData; import siprp.data.outer.HsEquipamentoData; import siprp.data.outer.HsLegislacaoCategoriaData; import siprp.data.outer.HsLegislacaoData; import siprp.data.outer.HsLegislacaoEmpresaData; import siprp.data.outer.HsNormalizacaoData; import siprp.data.outer.HsPostoData; import siprp.data.outer.HsPostoRiscoData; import siprp.data.outer.HsRelatorioData; import siprp.data.outer.HsRelatorioRiscoData; import siprp.data.outer.HsRelatorioRiscoValorQualitativoData; import siprp.data.outer.HsRiscoData; import siprp.data.outer.HsRiscoTemaData; import siprp.data.outer.MarcacoesEstabelecimentoData; import com.evolute.entity.ForeignKey; import com.evolute.entity.ProviderInterface; import com.evolute.utils.Singleton; import com.evolute.utils.arrays.Virtual2DArray; import com.evolute.utils.dataedition.persistence.Persistent; import com.evolute.utils.db.DBManager; import com.evolute.utils.db.Executer; import com.evolute.utils.error.ErrorLogger; import com.evolute.utils.sql.Expression; import com.evolute.utils.sql.Field; import com.evolute.utils.sql.Select2; public class PlanoActuacaoDataProvider { private static PlanoActuacaoDataProvider INSTANCE = null; private ProviderInterface< Persistent< ? >, Exception > ENTITY_PROVIDER; private Executer executer; private static final Integer ID_COMPANHIA_HIPERMERCADOS = 32; private PlanoActuacaoDataProvider() throws Exception { DBManager manager = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER ); executer = manager.getSharedExecuter( this ); ENTITY_PROVIDER = ( ProviderInterface< Persistent< ? >, Exception > ) Singleton.getInstance( Singleton.DEFAULT_EVO_DATA_PROVIDER ); } public static synchronized PlanoActuacaoDataProvider getProvider() throws Exception { if ( INSTANCE == null ) { INSTANCE = new PlanoActuacaoDataProvider(); } return INSTANCE; } public EstabelecimentosData getEstabelecimentoByID( Integer id ) { EstabelecimentosData result = null; try { result = ENTITY_PROVIDER.load( EstabelecimentosData.class, id ); } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } public void addEmailToEstabelecimento( String email, EstabelecimentosData estabelecimento ) { try { EmailPlanoDeActuacaoData emailObject = new EmailPlanoDeActuacaoData(); emailObject.setDescription( email ); emailObject.setToEstabelecimento_id( estabelecimento ); emailObject.save(); } catch ( Exception e ) { ErrorLogger.logException( e ); } } public List< HsRiscoTemaData > getAllRiscoTemas() { List< HsRiscoTemaData > result = null; try { result = ENTITY_PROVIDER.listLoad( HsRiscoTemaData.class, new Object[] { null }, new String[] { HsRiscoTemaData.DELETED_DATE }, new String[] { HsRiscoTemaData.DESCRIPTION } ); } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } public HsRelatorioData criarRelatorioForVisita( MarcacoesEstabelecimentoData visita ) { HsRelatorioData result = null; try { if( visita != null ) { result = new HsRelatorioData(); result.setToMarcacao_id( visita ); result.setData( new Date() ); result.save(); } } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } public HsPostoRiscoData createValoresFor( HsRiscoData risco, HsPostoData posto ) { HsPostoRiscoData result = null; try { if( risco != null && posto != null ) { result = new HsPostoRiscoData(); result.setToPosto_id( posto ); result.setToRisco_id( risco ); result.setIs_plano_actuacao( "n" ); result.save(); } } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } public List< HsRelatorioRiscoValorQualitativoData > getAllValoresQualitativos() { List< HsRelatorioRiscoValorQualitativoData > result = null; try { result = ENTITY_PROVIDER.listLoad( HsRelatorioRiscoValorQualitativoData.class ); } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } /** * Get * @param dates * @param estabelecimentoID * @return */ public List< HsRelatorioData > getRelatoriosNotIn( List< Date > dates, Integer estabelecimentoID ) { List< HsRelatorioData > result = new LinkedList< HsRelatorioData >(); dates = dates != null ? dates : new LinkedList< Date >(); try { Expression where = new Field( HsRelatorioData.IS_SUBMETIDO ).isDifferent( null ).and( new Field( HsRelatorioData.DELETED_DATE ).isEqual( null ) ); List< HsRelatorioData > all = ENTITY_PROVIDER.listLoad( HsRelatorioData.class, where, null, null ); for( HsRelatorioData relatorio : all ) { MarcacoesEstabelecimentoData visita = relatorio.toMarcacao_id(); if( visita != null ) { if ( visita.toEstabelecimento_id() != null && visita.toEstabelecimento_id().toEmpresa_id().getId().equals( ID_COMPANHIA_HIPERMERCADOS ) ) { if( estabelecimentoID == null || estabelecimentoID != null && estabelecimentoID.equals( visita.getEstabelecimento_id() ) ) { Date dateVisita = visita.getData(); if( dateVisita != null && !dates.contains( dateVisita ) ) { result.add( relatorio ); } } } } } } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } public List< HsRelatorioData > getRelatoriosNotIn( List< Integer > knownEstabelecimentosIDs ) { List< HsRelatorioData > result = new LinkedList< HsRelatorioData >(); knownEstabelecimentosIDs = knownEstabelecimentosIDs != null ? knownEstabelecimentosIDs : new LinkedList< Integer >(); try { Expression where = new Field( HsRelatorioData.IS_SUBMETIDO ).isDifferent( null ).and( new Field( HsRelatorioData.DELETED_DATE ).isEqual( null ) ); List< HsRelatorioData > all = ENTITY_PROVIDER.listLoad( HsRelatorioData.class, where, null, null ); for( HsRelatorioData relatorio : all ) { MarcacoesEstabelecimentoData visita = relatorio.toMarcacao_id(); if( visita != null ) { if( visita.toEstabelecimento_id() != null && visita.toEstabelecimento_id().toEmpresa_id().getId().equals( ID_COMPANHIA_HIPERMERCADOS ) ) { Integer estabelecimentoID = visita.toEstabelecimento_id().getId(); if( ! knownEstabelecimentosIDs.contains( estabelecimentoID ) ) { result.add( relatorio ); } } } } } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } public List< HsLegislacaoData > getAllLegislacaoGeral() { List< HsLegislacaoData > result = null; try { result = ENTITY_PROVIDER.listLoad( HsLegislacaoData.class, new Object[] { null, null }, new String[] { HsLegislacaoData.DELETED_DATE, HsLegislacaoData.CATEGORIA_ID }, new String[] { HsLegislacaoData.DESCRIPTION } ); } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } public List< HsLegislacaoCategoriaData > getAllLegislacaoCategorias() { List< HsLegislacaoCategoriaData > result = null; try { result = ENTITY_PROVIDER.listLoad( HsLegislacaoCategoriaData.class, new Object[] { null }, new String[] { HsLegislacaoCategoriaData.DELETED_DATE }, new String[] { HsLegislacaoCategoriaData.DESCRIPTION } ); } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } public List< HsLegislacaoData > getAllLegislacaoForEmpresa( EmpresasData empresa ) { List< HsLegislacaoData > result = null; try { Expression where = new Field( HsLegislacaoData.DELETED_DATE_FULL ).isEqual( null ); where = where.and( new Field( HsLegislacaoEmpresaData.EMPRESA_ID_FULL ).isEqual( empresa.getId() ) ); List fks = new LinkedList(); fks.add( new ForeignKey( HsLegislacaoData.class, HsLegislacaoData.ID_FULL, HsLegislacaoEmpresaData.class, HsLegislacaoEmpresaData.LEGISLACAO_ID_FULL ) ); result = ENTITY_PROVIDER.listLoad( HsLegislacaoData.class, fks, where, new String[] { HsLegislacaoData.DESCRIPTION_FULL } ); } catch ( Exception e ) { ErrorLogger.logException( e ); } return result == null ? new LinkedList() : result; } public List< HsNormalizacaoData > getNormalizacao( boolean portuguesa ) { List< HsNormalizacaoData > result = null; try { result = ENTITY_PROVIDER.listLoad( HsNormalizacaoData.class, new Object[] { null, portuguesa }, new String[] { HsNormalizacaoData.DELETED_DATE, HsNormalizacaoData.PORTUGUESA }, new String[] {} ); } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } public List< HsEquipamentoData > getAllEquipamentos() { List< HsEquipamentoData > result = null; try { result = ENTITY_PROVIDER.listLoad( HsEquipamentoData.class, new Object[] { null }, new String[] { HsEquipamentoData.DELETED_DATE }, new String[] { HsEquipamentoData.TIPO } ); } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } public List< HsEmailEstabelecimentoData > getEmailsForEstabelecimento( EstabelecimentosData estabelecimento ) { List< HsEmailEstabelecimentoData > result = null; try { if( estabelecimento != null ) { result = ENTITY_PROVIDER.listLoad( HsEmailEstabelecimentoData.class, new Object[] { estabelecimento.getId() }, new String[] { HsEmailEstabelecimentoData.ESTABELECIMENTO_ID }, null ); } } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } public List< HsRiscoData > findHsRiscosFor( HsRelatorioRiscoData relatorioRisco ) { List< HsRiscoData > result = null; try { Select2 query = new Select2( new String[] { HsRiscoData.TABLENAME }, new Integer[] { }, new Expression[] { }, new String[] { HsRiscoData.ID_FULL }, new Field( HsRiscoData.DESCRIPTION_FULL ).isILike( relatorioRisco.getDescription() ), null, null, null, null ); Virtual2DArray array = executer.executeQuery( query ); result = new ArrayList< HsRiscoData >(); for ( int i = 0; i < array.columnLength(); i++ ) { Integer id = array.get( i, 0 ); result.add( loadHsRiscoDataByID( id ) ); } } catch ( Exception e ) { ErrorLogger.logException( e ); } return result; } public HsRiscoData loadHsRiscoDataByID( Integer id ) throws Exception { return ENTITY_PROVIDER.load( HsRiscoData.class, id ); } }