package siprp.data.provider; import java.util.ArrayList; import java.util.Date; import java.util.LinkedList; import java.util.List; import shst.data.inner.HsRelatorioMedida; 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.HsNormalizacaoEmpresaData; import siprp.data.outer.HsNormalizacaoEstabelecimentoData; import siprp.data.outer.HsPostoData; import siprp.data.outer.HsPostoRiscoData; import siprp.data.outer.HsRelatorioData; import siprp.data.outer.HsRelatorioMedidaData; import siprp.data.outer.HsRelatorioPostoData; import siprp.data.outer.HsRelatorioPostoMedidaData; import siprp.data.outer.HsRelatorioPostoRiscoData; 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.entity.evo.EvoDataProvider; import com.evolute.entity.evo.EvoJoinObject; 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.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 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 ) 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 = (EstabelecimentosData) 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 { if( empresa != null ) { 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 } ); } else { result = ENTITY_PROVIDER.listLoad( HsLegislacaoData.class, new Object[]{ null }, new String[] { HsLegislacaoData.DELETED_DATE_FULL }, new String[]{ HsLegislacaoData.DESCRIPTION_FULL } ); } } catch ( Exception e ) { ErrorLogger.logException( e ); } return result == null ? new LinkedList() : result; } public List getNormalizacaoForEstabelecimento( boolean portuguesa, EstabelecimentosData estabelecimento ) { List result = null; try { if( estabelecimento != null ) { Expression where = new Field( HsNormalizacaoData.DELETED_DATE_FULL ).isEqual( null ); where = where.and( new Field( HsNormalizacaoEstabelecimentoData.ESTABELECIMENTO_ID_FULL ).isEqual( estabelecimento.getId() ) ); where = where.and( new Field( HsNormalizacaoData.PORTUGUESA_FULL ).isEqual( portuguesa ) ); List fks = new LinkedList(); fks.add( new ForeignKey( HsNormalizacaoData.class, HsNormalizacaoData.ID_FULL, HsNormalizacaoEstabelecimentoData.class, HsNormalizacaoEstabelecimentoData.NORMALIZACAO_ID_FULL ) ); result = ENTITY_PROVIDER.listLoad( HsNormalizacaoData.class, fks, where, new String[] { HsNormalizacaoData.DESCRICAO_FULL } ); } else { result = ENTITY_PROVIDER.listLoad( HsNormalizacaoData.class, new Object[]{ null, portuguesa }, new String[]{ HsNormalizacaoData.DELETED_DATE_FULL, HsNormalizacaoData.PORTUGUESA_FULL }, new String[] { HsNormalizacaoData.DESCRICAO_FULL } ); } } catch ( Exception e ) { ErrorLogger.logException( e ); } return result == null ? new LinkedList() : result; } public List getNormalizacaoForEmpresa( boolean portuguesa, EmpresasData empresa ) { List result = null; try { if( empresa != null ) { Expression where = new Field( HsNormalizacaoData.DELETED_DATE_FULL ).isEqual( null ); where = where.and( new Field( HsNormalizacaoEmpresaData.EMPRESA_ID_FULL ).isEqual( empresa.getId() ) ); where = where.and( new Field( HsNormalizacaoData.PORTUGUESA_FULL ).isEqual( portuguesa ) ); List fks = new LinkedList(); fks.add( new ForeignKey( HsNormalizacaoData.class, HsNormalizacaoData.ID_FULL, HsNormalizacaoEmpresaData.class, HsNormalizacaoEmpresaData.NORMALIZACAO_ID_FULL ) ); result = ENTITY_PROVIDER.listLoad( HsNormalizacaoData.class, fks, where, new String[] { HsNormalizacaoData.DESCRICAO_FULL } ); } else { result = ENTITY_PROVIDER.listLoad( HsNormalizacaoData.class, new Object[]{ null, portuguesa }, new String[]{ HsNormalizacaoData.DELETED_DATE_FULL, HsNormalizacaoData.PORTUGUESA_FULL }, new String[] { HsNormalizacaoData.DESCRICAO_FULL } ); } } catch ( Exception e ) { ErrorLogger.logException( e ); } return result == null ? new LinkedList() : result; } public List getNormalizacao( boolean portuguesa ) { return getNormalizacaoForEmpresa( portuguesa, (EmpresasData) null ); } 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 (HsRiscoData) ENTITY_PROVIDER.load( HsRiscoData.class, id ); } //FIXME : a query esta errada !!! :s /* SELECT * FROM hs_relatorio_posto_medida INNER JOIN hs_relatorio_medida ON ( hs_relatorio_posto_medida.medida_id = hs_relatorio_medida.id ) INNER JOIN hs_relatorio_risco ON ( hs_relatorio_medida.risco_id = hs_relatorio_risco.id ) INNER JOIN hs_relatorio_posto_risco ON ( hs_relatorio_risco.id = hs_relatorio_posto_risco.risco_id ) WHERE hs_relatorio_posto_medida.posto_id = 6515 AND hs_relatorio_medida.deleted_date IS NULL AND hs_relatorio_risco.deleted_date IS NULL ORDER BY hs_relatorio_risco.description, hs_relatorio_medida.description, hs_relatorio_medida.requesitos_legais */ public List getRelatorioMedidasForPosto( HsRelatorioPostoData posto ) throws Exception { List result = null; List fks = new LinkedList(); fks.add( new ForeignKey( HsRelatorioPostoMedidaData.class, HsRelatorioPostoMedidaData.MEDIDA_ID_FULL, HsRelatorioMedidaData.class, HsRelatorioMedidaData.ID_FULL ) ); fks.add( new ForeignKey( HsRelatorioMedidaData.class, HsRelatorioMedidaData.RISCO_ID_FULL, HsRelatorioRiscoData.class, HsRelatorioRiscoData.ID_FULL ) ); fks.add( new ForeignKey( HsRelatorioRiscoData.class, HsRelatorioRiscoData.ID_FULL, HsRelatorioPostoRiscoData.class, HsRelatorioPostoRiscoData.RISCO_ID_FULL ) ); Expression where = new Field( HsRelatorioPostoMedidaData.POSTO_ID_FULL ).isEqual( posto.getId() ).and( new Field( HsRelatorioMedidaData.DELETED_DATE_FULL ).isEqual( null ).and( new Field( HsRelatorioRiscoData.DELETED_DATE_FULL ).isEqual( null ) ) ); result = ((EvoDataProvider)ENTITY_PROVIDER).listLoad( fks, where, HsRelatorioRiscoData.DESCRIPTION_FULL, HsRelatorioMedida.DESCRIPTION_FULL, HsRelatorioMedida.REQUESITOS_LEGAIS_FULL ); return result; } }