diff --git a/trunk/PlanosActuacao/src/importer/PlanosActuacaoImporter.java b/trunk/PlanosActuacao/src/importer/PlanosActuacaoImporter.java new file mode 100644 index 00000000..e74ea1b8 --- /dev/null +++ b/trunk/PlanosActuacao/src/importer/PlanosActuacaoImporter.java @@ -0,0 +1,465 @@ +package importer; + +import com.evolute.utils.error.ErrorLogger; +import db.data.siprp.outer.PlanoValoresQualitativosData; +import db.entidades.Area; +import db.entidades.Medida; +import db.entidades.PlanoActuacao; +import db.entidades.PostoTrabalho; +import db.entidades.Risco; +import db.entidades.Utilizador; +import db.entidades.Valor; +import db.entidades.ValorQualitativo; +import db.providers.EvoBaseProvider; +import db.providers.UtilizadoresDataProvider; +import java.io.File; +import java.io.FileInputStream; +import utils.Global; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import mail.Mail; +import mail.MailNextPhase; +import mail.MailNextPhase.SIPRP_TO_RESPONSAVEL_SEGURANCA; +import shst.companydataloaders.SIPRPPropertiesLoader; +import shst.data.outer.HsRelatorioData; +import shst.data.provider.PlanoActuacaoDataProvider; +import utils.PlanosActuacaoLogger; + + +public class PlanosActuacaoImporter +{ + + public static void main( final String[] args ) + { + try + { + System.out.println( "\nInit: properties ..." ); + readProperties( args ); + + System.out.println( "\nInit: db ..." ); + EvoBaseProvider.getInstance(); + + System.out.println( "\nInit: PA logger ..." ); + PlanosActuacaoLogger.init(); + + System.out.println( "\nInit: A importar Planos de Actua\u00E7\u00E3o ..." ); + PlanosActuacaoImporter.getInstace().init(); + } + catch ( Exception e ) + { + e.printStackTrace( System.out ); + } + } + + private static void readProperties( String[] args ) throws Exception + { + args = new String[]{ "/home/fpalma/workspace/PlanosActuacao/properties/0-Importer/app.properties" }; + if ( args != null && args.length > 0 ) + { + String filepath = args[ 0 ]; + File f = new File( filepath ); + if ( f.isFile() ) + { + Properties props = new Properties(); + props.load( new FileInputStream( f ) ); + Iterator< Object > it = props.keySet().iterator(); + while ( it.hasNext() ) + { + Object key = it.next(); + Object value = props.get( key ); + System.setProperty( ( String ) key, ( String ) value ); + } + } + else + { + throw new IllegalArgumentException( "USAGE: java PlanosActuacaoImporter " ); + } + } + else + { +// throw new IllegalArgumentException( "USAGE: java PlanosActuacaoImporter " ); + System.out.println( "\tNo properties file given. Trying to use project properties ..." ); + readProjectProperties(); + } + } + + private static void readProjectProperties() throws Exception + { + SIPRPPropertiesLoader.forceEncryptedPropertiesFile = false; + SIPRPPropertiesLoader.getInstance().load(); + } + + + + private void debug( String debugMessage ) + { + System.out.println( debugMessage ); + } + + + private static PlanosActuacaoImporter INSTANCE = null; + + private PlanosActuacaoImporter() + { + + } + + public static synchronized PlanosActuacaoImporter getInstace() + { + if ( INSTANCE == null ) + { + INSTANCE = new PlanosActuacaoImporter(); + } + return INSTANCE; + } + + public void init() + { + initRelatorios( null, null ); + } + +// private List< PlanoActuacao > planosList; +// private List< Date > planosOnlineList; + +// public PlanosActuacao( Utilizador u ) +// { +// try +// { +// //rdp = new RelatoriosDataProvider(); +// Integer estabelecimentoId = null; +// if(u.getTipo().intValue() != Global.TECNICO_HS && u.getTipo().intValue() != Global.DIRECTOR_SIPRP) +// { +// estabelecimentoId = u.getEstabelecimento_id(); +// } +// List< Date > planosOnlineList = getPlanosOnline( estabelecimentoId ); +// System.out.println("PLANOS ONLINE LIST : " + planosOnlineList.size()); +// initRelatorios( planosOnlineList, estabelecimentoId ); +// } +// catch ( Exception ex ) +// { +// ErrorLogger.logException( ex ); +// } +// } + +// private List< Date > getPlanosOnline( Integer estabelecimentoId ) +// { +// List< Date > list = null; +// try +// { +// if ( estabelecimentoId != null ) +// { +// CreatePlanosDataProvider cpdp = CreatePlanosDataProvider.getInstance(); +// list = cpdp.getPlanosOnline( estabelecimentoId ); +// } +// } +// catch ( Exception ex ) +// { +// ErrorLogger.logException( ex ); +// } +// return list == null ? new LinkedList< Date >() : list; +// } + + private void initRelatorios( List< Date > datesList, Integer fromEstabelecimentoID ) + { + debug("\ninitRelatorios( " + datesList + ", " + fromEstabelecimentoID + " ) : "); + try + { + PlanosActuacaoImporterProvider provider = PlanosActuacaoImporterProvider.getInstance(); + PlanoActuacaoDataProvider planosProvider = PlanoActuacaoDataProvider.getProvider(); + + debug( "\n\tA adicionar novos valores qualitativos" ); + List valoresQualitativos = provider.getValoresQualitativos(); + putValoresQualitativosOnline( valoresQualitativos ); + debug( "\n\tTerminou adicao de novos valores qualitativos" ); + if ( fromEstabelecimentoID == null ) + { + debug( "\n\tA adicionar novos planos ..." ); + + List< Integer > estabelecimentos = provider.getEstabelecimentosWithPlanos(); + putPlanosOnline( planosProvider.getRelatoriosNotIn( estabelecimentos ) ); + + estabelecimentos = provider.getEstabelecimentosWithPlanos(); + debug("\n\tA adicionar planos a estabelecimentos anteriores"); + for ( Integer estabelecimentoID : estabelecimentos ) + { + try + { + debug( "\n\t\tA importar relatorio para o estabelecimento: " + estabelecimentoID ); + List< Date > datas = provider.getPlanosOnline( estabelecimentoID ); + if ( datas.isEmpty() ) + { + debug("\n\t\t\tNenhuma data de relatorio para o estabelecimento: " + estabelecimentoID); + } + else + { + List< HsRelatorioData > relatorios = planosProvider.getRelatoriosNotIn( datas, estabelecimentoID ); + if ( relatorios.isEmpty() ) + { + debug("\n\t\t\tNenhum relatorio novo para o estabelecimento: " + estabelecimentoID); + } + else + { + putPlanosOnline( relatorios ); + } + } + } + catch ( Exception e ) + { + ErrorLogger.logException( e ); + } + } + } + else + { + putPlanosOnline(planosProvider.getRelatoriosNotIn(datesList, fromEstabelecimentoID)); + } + } + catch ( Exception e ) + { + System.out.println( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " + e.getMessage() ); + e.printStackTrace(); + ErrorLogger.logException( e ); + } + } + + private void putPlanosOnline( List< HsRelatorioData > listRelatorios ) + { + debug("\n\t\t\tputPlanosOnline (" + listRelatorios == null ? "[null]" : "" + listRelatorios.size()); + for ( HsRelatorioData relatorio : listRelatorios ) + { + debug("\t\t\t\tRelatorioID : " + relatorio.getId()); + try + { + PlanoActuacao p = createPlano( relatorio ); + if ( p != null ) + { + enviarMail( p ); + } + } + catch ( Exception ex ) + { + debug("\nERRO NA CRIACAO DOS PLANOS : " + ex.getMessage()); + ErrorLogger.logException( ex ); + } + } + } + + private PlanoActuacao createPlano( HsRelatorioData relatorio ) throws Exception + { + debug("\n\t\t\t\t\tCREATE PLANO - RELATORIO ID : " + relatorio.getId()); + + PlanosActuacaoImporterProvider provider = PlanosActuacaoImporterProvider.getInstance(); + + PlanoActuacao p = new PlanoActuacao(); + p.setData_visita( relatorio.toMarcacao_id().getData() ); + + if ( relatorio.toMarcacao_id().toTecnico_hst() != null ) + { + p.setTecnico_hs_id( relatorio.toMarcacao_id().toTecnico_hst().getId() ); + p.setTecnico_hs_nome( relatorio.toMarcacao_id().toTecnico_hst().getNome() ); + } + + if ( relatorio.toMarcacao_id().toTecnico_superior_hst() != null ) + { + p.setTecnico_superior_hs_id( relatorio.toMarcacao_id().toTecnico_superior_hst().getId() ); + p.setTecnico_superior_hs_nome( relatorio.toMarcacao_id().toTecnico_superior_hst().getNome() ); + } + + p.setFase( new Integer( Global.FASE_SEGURANCA_PREENCHIMENTO ) ); + + p.setEstabelecimento_id( relatorio.toMarcacao_id().toEstabelecimento_id().getId() ); + p.setEmpresa_id( relatorio.toMarcacao_id().toEstabelecimento_id().toEmpresa_id().getId() ); + try + { + p.setNome_empresa( utils.Utils.unicodeToHTML( provider.getEmpresaNome( p.getEmpresa_id() ) ) ); + } + catch ( Exception ex ) + { + ErrorLogger.logException( ex ); + } + p.setNome_estabelecimento( relatorio.toMarcacao_id().toEstabelecimento_id().getNome() ); + p.setData_relatorio( relatorio.getData() ); + p.setData_disponibilizacao( relatorio.getIs_submetido() ); + p.setHs_relatorio_id( relatorio.getId() ); + + p = provider.getFullPlano( p, relatorio.getId() ); + if ( p.getAreas() != null ) + { + if ( p.getAreas().size() > 0 ) + { + p.setId( provider.createPlano( p ) ); + debug("\n\t\t\t\t\t\tCREATE PLANO - PLANO ID : " + p.getId()); + if ( p.getAreas() != null ) + { + createAreas( p.getId(), p.getAreas() ); + } + } + else + { + p = null; + } + } + else + { + p = null; + } + + return p; + } + + private void createAreas( Integer plano_id, List< Area > areas ) throws Exception + { + PlanosActuacaoImporterProvider provider = PlanosActuacaoImporterProvider.getInstance(); + for ( Area a : areas ) + { + a.setPlano_id( plano_id ); + a.setId( provider.createArea( a ) ); + System.out.println( "AREA =: " + a.getId().toString() ); + if ( a.getRiscos() != null ) + { + System.out.println( "AREA RISCOS: " + a.getRiscos().size() ); + createRiscos( a.getId(), a.getRiscos() ); + } + } + } + + private void createRiscos( Integer area_id, List< Risco > riscos ) throws Exception + { + PlanosActuacaoImporterProvider provider = PlanosActuacaoImporterProvider.getInstance(); + for ( Risco r : riscos ) + { + r.setArea_id( area_id ); + System.out.println( "CREATE RISCO: " ); + if ( r.getValores() != null ) + { + for ( Valor v : r.getValores() ) + { + r.setValorQuantitativo( v.getValorQuantitativo() ); + r.setValorQualitativoID( v.getValorQualitativoID() ); + r.setId( provider.createRisco( r ) ); + } + } + if ( r.getMedidas() != null ) + { + createMedidas( r.getId(), r.getMedidas() ); + } + } + } + + private void createMedidas( Integer risco_id, List< Medida > medidas ) throws Exception + { + PlanosActuacaoImporterProvider provider = PlanosActuacaoImporterProvider.getInstance(); + for ( Medida m : medidas ) + { + m.setRisco_id( risco_id ); + m.setId( provider.createMedida( m ) ); + if ( m.getPostos() != null ) + { + createPostos( m.getId(), m.getPostos() ); + } + } + } + + private void createPostos( Integer medida_id, List< PostoTrabalho > postos ) throws Exception + { + PlanosActuacaoImporterProvider provider = PlanosActuacaoImporterProvider.getInstance(); + for ( PostoTrabalho p : postos ) + { + p.setMedida_id( medida_id ); + provider.createPostoTrabalho( p ); + } + } + + private void putValoresQualitativosOnline( List valoresQualitativos ) + throws Exception + { + System.out.println( "putValoresQualitativosOnline: " + valoresQualitativos.size() ); + List valoresOnline = PlanosActuacaoImporterProvider.getInstance().getValoresQualitativosOnline(); + Set valoresOnlineSet = new HashSet(); + Map valoresOnlineIdsSet = new HashMap(); + valoresOnlineSet.addAll( valoresOnline ); + for( PlanoValoresQualitativosData valorOnline : valoresOnlineSet ) + { + valoresOnlineIdsSet.put( valorOnline.getId(), valorOnline ); + } + for( ValorQualitativo valorQualitativo : valoresQualitativos ) + { + if( valoresOnlineIdsSet.containsKey( valorQualitativo.getId() ) ) + { + PlanosActuacaoImporterProvider.getInstance().updateValorQualitativo( valorQualitativo ); + valoresOnlineSet.remove( valoresOnlineIdsSet.get( valorQualitativo.getId() ) ); + } + else + { + PlanosActuacaoImporterProvider.getInstance().createValorQualitativo( valorQualitativo ); + } + } + for( PlanoValoresQualitativosData valorOnline : valoresOnlineSet ) + { + PlanosActuacaoImporterProvider.getInstance().deleteValorQualitativo( valorOnline.getId() ); + } + } + + private void enviarMail( PlanoActuacao p ) + { + Boolean isDebug = SIPRPPropertiesLoader.getInstance().findProperty( "debug", false ); + + if ( ! isDebug ) + { + MailNextPhase nextPhase = null; + + int fase = p.getFase().intValue(); + int tipo_utilizador = 0; + switch ( fase ) + { + case Global.FASE_SEGURANCA_PREENCHIMENTO : // SIPRP para RS + tipo_utilizador = Global.RESPONSAVEL_SEGURANCA; + nextPhase = new SIPRP_TO_RESPONSAVEL_SEGURANCA( p ); + break; + } + + if ( nextPhase != null ) + { + String responsavel_loja = "n"; + if ( tipo_utilizador == Global.RESPONSAVEL_SEGURANCA ) + { + responsavel_loja = "y"; + } + try + { + Mail mail = new Mail(); + + UtilizadoresDataProvider udp = UtilizadoresDataProvider.getInstance(); + List< Utilizador > list = udp.getUtilizadoresListByTipo( tipo_utilizador, responsavel_loja, p.getEstabelecimento_id() ); + ListIterator iter = list.listIterator(); + while ( iter.hasNext() ) + { + Utilizador u = ( Utilizador ) iter.next(); + System.out.println( "\t\tUSER MAIL NEXT FASE : " + u.getLogin() + " ; " + u.getEmail() ); + try + { + mail.send( u.getEmail(), Mail.ENDERECO_ENVIO, nextPhase ); + System.out.println( "\t\t\tEMAIL ENVIADO !!" ); + } + catch ( Exception ex1 ) + { + ErrorLogger.logException( ex1 ); + System.out.println("\t\t\tMAIL ERROR : " + ex1.getMessage()); + } + } + } + catch ( Exception ex ) + { + ErrorLogger.logException( ex ); + } + } + } + } +} diff --git a/trunk/PlanosActuacao/src/importer/PlanosActuacaoImporterProvider.java b/trunk/PlanosActuacao/src/importer/PlanosActuacaoImporterProvider.java new file mode 100644 index 00000000..ade56417 --- /dev/null +++ b/trunk/PlanosActuacao/src/importer/PlanosActuacaoImporterProvider.java @@ -0,0 +1,659 @@ +package importer; + +import db.providers.*; +import com.evolute.utils.arrays.Virtual2DArray; +import com.evolute.utils.error.ErrorLogger; +import com.evolute.utils.sql.Expression; +import com.evolute.utils.sql.Field; +import com.evolute.utils.sql.Select2; +import db.DBConstants.DB; +import db.data.siprp.outer.PlanoAreasData; +import db.data.siprp.outer.PlanoMedidasData; +import db.data.siprp.outer.PlanoPostosTrabalhoData; +import db.data.siprp.outer.PlanoRiscosData; +import db.data.siprp.outer.PlanosActuacaoData; +import db.data.siprp.outer.PlanoValoresQualitativosData; +import db.data.siprp_local.outer.EmpresasData; +import db.entidades.Area; +import db.entidades.Medida; +import db.entidades.PlanoActuacao; +import db.entidades.PostoTrabalho; +import db.entidades.Risco; +import db.entidades.Valor; +import db.entidades.ValorQualitativo; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; +import utils.Utils; + + +public class PlanosActuacaoImporterProvider extends GenericDataProvider +{ + private static PlanosActuacaoImporterProvider INSTANCE = null; + + private PlanosActuacaoImporterProvider() throws Exception + { + super(); + } + + protected static synchronized PlanosActuacaoImporterProvider getInstance() throws Exception + { + if ( INSTANCE == null ) + { + INSTANCE = new PlanosActuacaoImporterProvider(); + } + return INSTANCE; + } + + public List< Date > getPlanosOnline( Integer estabelecimentoID ) throws Exception + { + System.out.println( "\n\t\tPlanosActuacaoImporterProvider . getPlanosOnline( " + estabelecimentoID + " ) : " ); + + List< Date > list = new LinkedList< Date >(); + + Expression where = new Field( PlanosActuacaoData.DELETED_DATE_FULL ).isEqual( null ); + if ( estabelecimentoID != null ) + { + where = where.and( new Field( PlanosActuacaoData.ESTABELECIMENTO_ID_FULL ).isEqual( estabelecimentoID ) ); + } + + Select2 query = new Select2( PlanosActuacaoData.TABLENAME, + where, + new String[] { PlanosActuacaoData.DATA_VISITA_FULL }, + new String[] {} ); + Virtual2DArray array = getExecuter().executeQuery( query ); + for ( int i = 0; i < array.columnLength(); i++ ) + { + Date date = array.get( i, 0 ); + System.out.println( "\n\t\t\tData-Visita Plano : " + date ); + list.add( date ); + } + return list; + } + + public List< Integer > getEstabelecimentosWithPlanos() throws Exception + { + List< Integer > result = new LinkedList< Integer >(); + + Select2 query = new Select2( + PlanosActuacaoData.TABLENAME, + new Field( PlanosActuacaoData.DELETED_DATE_FULL ).isEqual( null ), + new String[] { "DISTINCT " + PlanosActuacaoData.ESTABELECIMENTO_ID_FULL }, + new String[] {} + ); + Virtual2DArray array = getExecuter().executeQuery( query ); + for ( int i = 0; i < array.columnLength(); i++ ) + { + Integer estabelecimentoID = array.get( i, 0 ); + result.add( estabelecimentoID ); + } + return result; + } + + + + public PlanoActuacao getFullPlano( PlanoActuacao plano, Integer relatorioId ) + { + try + { + plano = getAreasByPlano( plano, relatorioId ); + } + catch ( Exception ex ) + { + ErrorLogger.logException( ex ); + } + return plano; + } + + private PlanoActuacao getAreasByPlano( PlanoActuacao plano, Integer relatorioID ) throws Exception + { + String sql = "select hs_relatorio_posto.area_id as area, hs_relatorio_area.description as descricao, hs_relatorio_area.ordem as ordem, bool_or( coalesce( hs_relatorio_posto.is_principal, false ) ) as is_principal " + + " from hs_relatorio_posto " + + " inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id " + + " inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id " + + " inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id " + + " inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id " + + " inner join hs_relatorio_area on hs_relatorio_area.id = hs_relatorio_posto.area_id " + + " where hs_relatorio.id = " + relatorioID + " " + + " group by area, descricao, ordem" + + " order by area "; + System.out.println( "\n\t\t\t\t\t\tprovider . getAreasByPlano( " + plano.getId() + ", " + relatorioID + " ) : " ); + System.out.println( "\n\t\t\t\t\t\t\t\tSQL : " + sql ); + + Statement st = createLocalStatement(); + ResultSet rs = st.executeQuery( sql ); + List< Area > areas = new LinkedList< Area >(); + if ( rs.isBeforeFirst() ) + { + rs.first(); + do + { + Area a = new Area(); + a.setId( new Integer( rs.getInt( "area" ) ) ); + a.setArea_id( a.getId() ); + a.setIs_principal( new Boolean( rs.getBoolean( "is_principal" ) ) ); + Integer ordem = ( Integer ) rs.getObject( "ordem" ); + if( ordem != null ) + { + a.setOrdem( ordem ); + } + if ( existemMedidasByArea( a.getId() ) ) + { + a.setDescricao( Utils.unicodeToHTML( rs.getString( "descricao") ) ); + try + { + a.setRiscos( getRiscosByArea( a ) ); + } + catch ( Exception ex ) + { + System.out.println( "\nERRO RISCOS BY AREA !!!!" ); + ErrorLogger.logException( ex ); + a.setRiscos( null ); + } + areas.add( a ); + } + } + while ( rs.next() ); + plano.setAreas( areas ); + } + return plano; + } + + private List< Risco > getRiscosByArea( Area a ) throws Exception + { + String sql = "select distinct hs_relatorio_risco.id, hs_relatorio_risco.description as descricao, hs_relatorio_risco.is_patrimonial as is_patrimonial " + + " from hs_relatorio_posto " + + " inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id " + + " inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id " + + " inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id " + + " inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id " + + " inner join hs_relatorio_area on hs_relatorio_area.id = area_id " + + " where area_id = " + a.getId() + " " + + " order by hs_relatorio_risco.id "; + System.out.println( "\t\t\t\t\t\t\tprovider . getRiscosByArea( " + a.getId() + " ) : " ); + System.out.println( "\t\t\t\t\t\t\t\t\tSQL : " + sql ); + + Statement st = createLocalStatement(); + ResultSet rs = st.executeQuery( sql ); + List< Risco > riscos = new LinkedList< Risco >(); + if ( rs.isBeforeFirst() ) + { + rs.first(); + do + { + Risco r = new Risco(); + r.setId( new Integer( rs.getInt( "id" ) ) ); + if ( existemMedidasByRisco( r.getId() ) ) + { + r.setDescricao( Utils.unicodeToHTML( rs.getString( "descricao" ) ) ); + r.setIsPatrimonial( rs.getBoolean( "is_patrimonial" ) ); + + r.setValores( getValoresByRisco( r, a ) ); + r.setRisco_id( new Integer( rs.getInt( "id" ) ) ); + r.setMedidas( getMedidasByRisco( r, a ) ); + riscos.add( r ); + } + } + while ( rs.next() ); + } + return riscos; + } + + private List< Valor > getValoresByRisco( Risco r, Area a ) throws Exception + { + String sql = "select subquery.valor, subquery.valor_qualitativo_id from " + + " (select distinct hs_relatorio_posto_risco.risco_id, hs_relatorio_posto_risco.valor_qualitativo_id, hs_relatorio_risco_valor_qualitativo.description, " + + " case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null " + + " when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade " + + " else null " + + " end as valor " + + " from hs_relatorio_posto " + + " inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id " + + " inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id " + + " inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id " + + " inner join hs_relatorio_posto_risco on (hs_relatorio_posto_risco.posto_id = hs_relatorio_posto.id and hs_relatorio_posto_risco.risco_id = hs_relatorio_risco.id) " + + " inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id " + + " inner join hs_relatorio_area on hs_relatorio_area.id = area_id " + + " left outer join hs_relatorio_risco_valor_qualitativo on ( hs_relatorio_risco_valor_qualitativo.id = hs_relatorio_posto_risco.valor_qualitativo_id ) " + + " where hs_relatorio_posto_risco.risco_id = " + r.getId() + " and area_id = " + a.getId() + ") subquery " + + " order by subquery.valor"; + + System.out.println( "\t\t\t\t\t\t\t\tprovider . getValoresByRisco( " + r.getId() + ", " + a.getId() + " ) : " ); + System.out.println( "\t\t\t\t\t\t\t\t\t\tSQL : " + sql ); + + Statement st = createLocalStatement(); + ResultSet rs = st.executeQuery( sql ); + List< Valor > valores = new LinkedList< Valor >(); + if ( rs.isBeforeFirst() ) + { + rs.first(); + do + { + Object oValor = rs.getObject( "valor" ); + Object valorQualitativoID = rs.getObject( "valor_qualitativo_id" ); + + Valor v = new Valor(); + v.setValorQuantitativo( oValor == null ? null : ( Integer ) oValor ); + v.setValorQualitativoID( valorQualitativoID == null ? null : ( Integer ) valorQualitativoID ); + valores.add( v ); + } + while ( rs.next() ); + } + return valores; + } + +// private List getMedidasByValor(Valor v, Risco r, Area a) throws Exception +// { +// Statement st = dblocal.createStatement(); +// String sql = "select distinct medida_id, valor, descricao from "; +// sql += "( select distinct hs_relatorio_posto_medida.medida_id, hs_relatorio_posto_risco.risco_id, hs_relatorio_medida.description as descricao, "; +// sql += "case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null "; +// sql += "when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade "; +// sql += "else hs_relatorio_posto_risco.valor_qualitativo_id "; +// sql += "end as valor "; +// sql += "from hs_relatorio_posto "; +// sql += "inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id "; +// sql += "inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id "; +// sql += "inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id "; +// sql += "inner join hs_relatorio_posto_risco on (hs_relatorio_posto_risco.posto_id = hs_relatorio_posto.id and hs_relatorio_posto_risco.risco_id = hs_relatorio_risco.id) "; +// sql += "inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id "; +// sql += "inner join hs_relatorio_area on hs_relatorio_area.id = area_id "; +// sql += "where hs_relatorio_posto_risco.risco_id = " + r.getId() + " and area_id = " + a.getId() + " and hs_relatorio_posto_medida.is_plano_actuacao = true" + ") subquery "; +// //sql += "where hs_relatorio_posto_risco.risco_id = " + r.getId() + " and area_id = " + a.getId() + ") subquery "; +// //sql += "where valor = " + v.getValorQuantitativo() + " "; +// sql += "order by subquery.medida_id, valor"; +// System.out.println("MEDIDAS BY VALOR SQL : " + sql); +// ResultSet rs = st.executeQuery(sql); +// List medidas = new ArrayList(); +// if(rs.isBeforeFirst()) +// { +// rs.first(); +// do +// { +// Medida m = new Medida(); +// //int valor = rs.getInt("valor"); +// //m.setValorQuantitativo(new Integer(valor)); +// m.setId(new Integer( rs.getInt("medida_id") )); +// m.setDescricao(Utils.unicodeToHTML(rs.getString("descricao"))); +// m.setPostos(getPostosByMedidaAndValor(m, v, a)); +// medidas.add(m); +// }while(rs.next()); +// } +// return medidas; +// } + + private List getMedidasByRisco( Risco r, Area a ) throws Exception + { + String sql = "select distinct medida_id, valor, descricao from " + + " ( select distinct hs_relatorio_posto_medida.medida_id, hs_relatorio_posto_risco.risco_id, hs_relatorio_medida.description as descricao, " + + " case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null " + + " when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade " + + " else hs_relatorio_posto_risco.valor_qualitativo_id " + + " end as valor " + + " from hs_relatorio_posto " + + " inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id " + + " inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id " + + " inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id " + + " inner join hs_relatorio_posto_risco on (hs_relatorio_posto_risco.posto_id = hs_relatorio_posto.id and hs_relatorio_posto_risco.risco_id = hs_relatorio_risco.id) " + + " inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id " + + " inner join hs_relatorio_area on hs_relatorio_area.id = area_id " + + " where hs_relatorio_posto_risco.risco_id = " + r.getId() + " and area_id = " + a.getId() + " and hs_relatorio_posto_medida.is_plano_actuacao = true" + ") subquery " + //+ " where hs_relatorio_posto_risco.risco_id = " + r.getId() + " and area_id = " + a.getId() + ") subquery " + //+ " where valor = " + v.getValorQuantitativo() + " " + + " order by subquery.medida_id, valor"; + System.out.println( "\t\t\t\t\t\t\t\t\tprovider . getMedidasByRisco( " + r.getId() + ", " + a.getId() + " ) : " ); + System.out.println( "\t\t\t\t\t\t\t\t\t\t\tSQL : " + sql ); + + Statement st = createLocalStatement(); + ResultSet rs = st.executeQuery( sql ); + List< Medida > medidas = new LinkedList< Medida >(); + if ( rs.isBeforeFirst() ) + { + rs.first(); + do + { + Medida m = new Medida(); + m.setId( new Integer( rs.getInt("medida_id") ) ); + m.setMedida_id( m.getId() ); + m.setDescricao( Utils.unicodeToHTML( rs.getString( "descricao" ) ) ); + //m.setPostos(getPostosByMedidaAndValor(m, v, a)); + m.setPostos( getPostosByMedida( r, m, a ) ); + medidas.add( m ); + } + while ( rs.next() ); + } + return medidas; + } + +// private List getPostosByMedidaAndValor(Medida m, Valor v, Area a) throws Exception +// { +// Statement st = dblocal.createStatement(); +// String sql = "select subquery.posto_id, valor, descricao from "; +// sql += "(select hs_relatorio_posto_medida.posto_id, hs_relatorio_posto.description as descricao, "; +// sql += "case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null "; +// sql += "when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade "; +// sql += "else hs_relatorio_posto_risco.valor_qualitativo_id "; +// sql += "end as valor "; +// sql += "from hs_relatorio_posto_medida "; +// sql += "inner join hs_relatorio_posto_risco on hs_relatorio_posto_risco.posto_id = hs_relatorio_posto_medida.posto_id "; +// sql += "inner join hs_relatorio_posto on hs_relatorio_posto.id = hs_relatorio_posto_medida.posto_id "; +// sql += "inner join hs_relatorio_area on hs_relatorio_area.id = area_id "; +// sql += "where medida_id = " + m.getId() + " and area_id = " + a.getId() + ") subquery "; +// //sql += "where valor = " + v.getValorQuantitativo(); +// System.out.println("POSTOS BY MEDIDA SQL : " + sql); +// ResultSet rs = st.executeQuery(sql); +// List postos = new ArrayList(); +// if(rs.isBeforeFirst()) +// { +// rs.first(); +// do +// { +// PostoTrabalho p = new PostoTrabalho(); +// p.setId(new Integer( rs.getInt("posto_id") )); +// p.setDescricao(Utils.unicodeToHTML(rs.getString("descricao"))); +// postos.add(p); +// }while(rs.next()); +// } +// return postos; +// } + + private List getPostosByMedida(Risco r, Medida m, Area a) throws Exception + { + String sql = "select subquery.posto_id, valor, descricao, is_principal from "; + sql += "(select hs_relatorio_posto_medida.posto_id, hs_relatorio_posto.description as descricao, "; + sql += "case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null "; + sql += "when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade "; + sql += "else hs_relatorio_posto_risco.valor_qualitativo_id "; + sql += "end as valor, coalesce( hs_relatorio_posto.is_principal, false ) as is_principal "; + sql += "from hs_relatorio_posto_medida "; + sql += "inner join hs_relatorio_posto_risco on hs_relatorio_posto_risco.posto_id = hs_relatorio_posto_medida.posto_id "; + sql += "inner join hs_relatorio_posto on hs_relatorio_posto.id = hs_relatorio_posto_medida.posto_id "; + sql += "inner join hs_relatorio_area on hs_relatorio_area.id = area_id "; + sql += "where medida_id = " + m.getId() + " and area_id = " + a.getId() + " and risco_id = " + r.getId() + ") subquery "; + //sql += "where valor = " + v.getValorQuantitativo(); + System.out.println("POSTOS BY MEDIDA SQL : " + sql); + Statement st = createLocalStatement(); + ResultSet rs = st.executeQuery( sql ); + List< PostoTrabalho > postos = new LinkedList< PostoTrabalho >(); + if ( rs.isBeforeFirst() ) + { + rs.first(); + do + { + PostoTrabalho p = new PostoTrabalho(); + p.setId( new Integer( rs.getInt("posto_id") ) ); + p.setPosto_id( p.getId() ); + p.setDescricao( Utils.unicodeToHTML( rs.getString( "descricao" ) ) ); + p.setIs_principal( new Boolean( rs.getBoolean( "is_principal" ) ) ); + postos.add( p ); + } + while ( rs.next() ); + } + return postos; + } + + public List getValoresQualitativos() + throws Exception + { + String sql = "select id, description from hs_relatorio_risco_valor_qualitativo;"; + System.out.println("VALORES QUALITATIVOS SQL : " + sql); + Statement st = createLocalStatement(); + ResultSet rs = st.executeQuery( sql ); + List< ValorQualitativo > valores = new LinkedList< ValorQualitativo >(); + if ( rs.isBeforeFirst() ) + { + rs.first(); + do + { + ValorQualitativo v = new ValorQualitativo(); + v.setId( new Integer( rs.getInt("id") ) ); + v.setDescricao( Utils.unicodeToHTML( rs.getString( "description" ) ) ); + valores.add( v ); + } + while ( rs.next() ); + } + return valores; + } + + public List getValoresQualitativosOnline() throws Exception + { + return getProvider().listLoad( PlanoValoresQualitativosData.class, PlanoValoresQualitativosData.ID_FULL ); + } + + private boolean existemMedidasByArea( Integer areaID ) throws Exception + { + String sql = "select distinct medida_id, valor, descricao " + + " from ( select distinct hs_relatorio_posto_medida.medida_id, hs_relatorio_posto_risco.risco_id, hs_relatorio_medida.description as descricao, " + + " case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null " + + " when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade else hs_relatorio_posto_risco.valor_qualitativo_id end as valor from hs_relatorio_posto " + + " inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id " + + " inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id " + + " inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id " + + " inner join hs_relatorio_posto_risco on (hs_relatorio_posto_risco.posto_id = hs_relatorio_posto.id and hs_relatorio_posto_risco.risco_id = hs_relatorio_risco.id) " + + " inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id " + + " inner join hs_relatorio_area on hs_relatorio_area.id = area_id " + + " where area_id = " + areaID + " and hs_relatorio_posto_medida.is_plano_actuacao = true) subquery " + + " order by subquery.medida_id, valor"; + + System.out.println( "\t\t\t\t\t\t\t\t\tprovider . existemMedidasByArea( " + areaID + " ) : " ); + System.out.println( "\t\t\t\t\t\t\t\t\t\t\tSQL : " + sql ); + + Statement st = createLocalStatement(); + ResultSet rs = st.executeQuery( sql ); + return rs.isBeforeFirst(); + } + + private boolean existemMedidasByRisco(Integer risco_id) throws Exception + { + String sql = "select distinct medida_id " + + " from ( select distinct hs_relatorio_posto_medida.medida_id, hs_relatorio_posto_risco.risco_id, hs_relatorio_medida.description as descricao, " + + " case when hs_relatorio_posto_risco.valor_qualitativo_id isnull and hs_relatorio_posto_risco.probabilidade isnull and hs_relatorio_posto_risco.severidade isnull then null " + + " when hs_relatorio_posto_risco.valor_qualitativo_id isnull then hs_relatorio_posto_risco.probabilidade * hs_relatorio_posto_risco.severidade else hs_relatorio_posto_risco.valor_qualitativo_id end as valor from hs_relatorio_posto " + + " inner join hs_relatorio_posto_medida on hs_relatorio_posto_medida.posto_id = hs_relatorio_posto.id " + + " inner join hs_relatorio_medida on hs_relatorio_medida.id = hs_relatorio_posto_medida.medida_id " + + " inner join hs_relatorio_risco on hs_relatorio_risco.id = hs_relatorio_medida.risco_id " + + " inner join hs_relatorio_posto_risco on (hs_relatorio_posto_risco.posto_id = hs_relatorio_posto.id and hs_relatorio_posto_risco.risco_id = hs_relatorio_risco.id) " + + " inner join hs_relatorio on hs_relatorio.id = hs_relatorio_risco.relatorio_id " + + " inner join hs_relatorio_area on hs_relatorio_area.id = area_id " + + " where hs_relatorio_posto_risco.risco_id = " + risco_id + " and hs_relatorio_posto_medida.is_plano_actuacao = true) subquery " + + " order by medida_id"; + + System.out.println("EXISTEM MEDIDAS BY RISCO SQL : " + sql); + Statement st = createLocalStatement(); + ResultSet rs = st.executeQuery( sql ); + return rs.isBeforeFirst(); + } + + + public Integer createPlano( PlanoActuacao p ) throws Exception + { + Integer newPlanoID = null; + Date todayDate = new Date(); + + boolean concluidoPorDesactivacao = p.getConcluidoPorDesactivacao() == null ? Boolean.FALSE : p.getConcluidoPorDesactivacao(); + Date dataDesactivacao = p.getDataDesactivacao(); + if ( dataDesactivacao == null && concluidoPorDesactivacao ) + { + dataDesactivacao = todayDate; + } + + PlanosActuacaoData planoData = new PlanosActuacaoData(); + planoData.setEstabelecimento_id( p.getEstabelecimento_id() ); + planoData.setFase( p.getFase() ); + planoData.setData_visita( p.getData_visita() ); + planoData.setNome_estabelecimento( p.getNome_estabelecimento() ); + + planoData.setValidacao_director_loja( false ); + planoData.setValidacao_dns( false ); + planoData.setValidacao_hs( false ); + + planoData.setParecer_dns( null ); + planoData.setObs_correcao( null ); + planoData.setData_validacao_dir_loja( null ); + planoData.setUser_dir_loja( null ); + planoData.setData_validacao_dns( null ); + planoData.setUser_dns( null ); + planoData.setData_validacao_hs( null ); + planoData.setUser_hs( null ); + + planoData.setFase_antes_correcao( null ); + planoData.setCorrecao( "n" ); + planoData.setObservacoes_dl( null ); + planoData.setObservacoes_dns( null ); + planoData.setVerificacao_siprp( null ); + + planoData.setData_controlo( todayDate ); + planoData.setData_email_controlo( null ); + + planoData.setEmpresa_id( p.getEmpresa_id() ); + planoData.setNome_empresa( p.getNome_empresa() ); + + planoData.setTecnico_hs_id( p.getTecnico_hs_id() ); + planoData.setTecnico_hs_nome( p.getTecnico_hs_nome() ); + + planoData.setData_relatorio( p.getData_relatorio() ); + planoData.setDeleted_date( null ); + + planoData.setConcluido_por_desactivacao( concluidoPorDesactivacao ); + + planoData.setTecnico_superior_hs_id( p.getTecnico_superior_hs_id() ); + planoData.setTecnico_superior_hs_nome( p.getTecnico_superior_hs_nome() ); + + planoData.setData_desactivacao( dataDesactivacao ); + planoData.setData_disponibilizacao( p.getData_disponibilizacao() ); + planoData.setData_validacao_seg( null ); + planoData.setUser_seg( null ); + + planoData.setHs_relatorio_id( p.getHs_relatorio_id() ); + + System.out.println( "\nCREATE PLANO : " ); + planoData.setProvider( EvoBaseProvider.getInstance().getProvider() ); + planoData.save(); + newPlanoID = planoData.getId(); + System.out.println( "\n\tplanoID : " + newPlanoID ); + + return newPlanoID; + } + + public Integer createArea( Area a ) throws Exception + { + Integer newAreaID = null; + + PlanoAreasData areaData = new PlanoAreasData(); + areaData.setArea_id( a.getArea_id() ); + areaData.setPlano_id( a.getPlano_id() ); + areaData.setDescricao( Utils.parseToInsert( a.getDescricao() ) ); + areaData.setIs_principal( a.getIs_principal() ); + areaData.setOrdem( a.getOrdem() ); + System.out.println( "\nCREATE AREA : " ); + areaData.setProvider( EvoBaseProvider.getInstance().getProvider( DB.SIPRP ) ); + areaData.save(); + newAreaID = areaData.getId(); + System.out.println( "\n\tareaID : " + newAreaID ); + + return newAreaID; + } + + public Integer createRisco( Risco r ) throws Exception + { + Integer newRiscoID = null; + + PlanoRiscosData riscoData = new PlanoRiscosData(); + riscoData.setDescricao( Utils.parseToInsert( r.getDescricao() ) ); + riscoData.setActivo( r.getActivo() == null ? "y" : r.getActivo() ); + riscoData.setArea_id( r.getArea_id() ); + riscoData.setValor( r.getValorQuantitativo() ); + riscoData.setRisco_id( r.getId() ); + riscoData.setIs_patrimonial( r.getIsPatrimonial() ); + riscoData.setValor_qualitativo_id( r.getValorQualitativoID() ); + + System.out.println( "\nCREATE RISCO : " ); + riscoData.setProvider( EvoBaseProvider.getInstance().getProvider( DB.SIPRP ) ); + riscoData.save(); + newRiscoID = riscoData.getId(); + System.out.println( "\n\triscoID : " + newRiscoID ); + + return newRiscoID; + } + + public Integer createMedida( Medida m ) throws Exception + { + Integer newMedidaID = null; + + PlanoMedidasData medidaData = new PlanoMedidasData(); + medidaData.setMedida_id( m.getMedida_id() ); + medidaData.setRisco_id( m.getRisco_id() ); + medidaData.setDescricao( Utils.parseToInsert( m.getDescricao() ) ); + + System.out.println( "\nCREATE MEDIDA : " ); + medidaData.setProvider( EvoBaseProvider.getInstance().getProvider( DB.SIPRP ) ); + medidaData.save(); + newMedidaID = medidaData.getId(); + System.out.println( "\n\tmedidaID : " + newMedidaID ); + + return newMedidaID; + } + + public void createPostoTrabalho( PostoTrabalho p ) throws Exception + { + PlanoPostosTrabalhoData postoData = new PlanoPostosTrabalhoData(); + postoData.setPosto_id( p.getPosto_id() ); + postoData.setMedida_id( p.getMedida_id() ); + postoData.setDescricao( Utils.parseToInsert( p.getDescricao() ) ); + postoData.setIs_principal( p.getIs_principal() ); + System.out.println( "\nCREATE POSTO TRABALHO : " ); + postoData.setProvider( EvoBaseProvider.getInstance().getProvider( DB.SIPRP ) ); + postoData.save(); + } + + public void createValorQualitativo( ValorQualitativo v ) throws Exception + { + PlanoValoresQualitativosData valor = new PlanoValoresQualitativosData(); + valor.setId( v.getId() ); + valor.setDescricao( Utils.parseToInsert( v.getDescricao() ) ); + + System.out.println( "\nCREATE VALOR QUALITATIVO : " ); + valor.setProvider( EvoBaseProvider.getInstance().getProvider( DB.SIPRP ) ); + valor.save(); + } + + public void updateValorQualitativo( ValorQualitativo v ) throws Exception + { + PlanoValoresQualitativosData valor = getProvider().load( PlanoValoresQualitativosData.class, v.getId() ); + valor.setDescricao( Utils.parseToInsert( v.getDescricao() ) ); + + System.out.println( "\nUPDATE VALOR QUALITATIVO : " ); + valor.setProvider( EvoBaseProvider.getInstance().getProvider( DB.SIPRP ) ); + valor.save(); + } + + public void deleteValorQualitativo( Integer vId ) throws Exception + { + PlanoValoresQualitativosData valor = getProvider().load( PlanoValoresQualitativosData.class, vId ); + System.out.println( "\nDELETE VALOR QUALITATIVO : " ); + valor.setDeleted_stamp( new java.sql.Timestamp( System.currentTimeMillis() ) ); + valor.save(); + } + + //==================== + public String getEmpresaNome( Integer empresaID ) throws Exception + { + String nome = null; + + EmpresasData empresaData = getLocalProvider().load( EmpresasData.class, empresaID, EmpresasData.ID ); + if ( empresaData != null ) + { + nome = Utils.unicodeToHTML( empresaData.getDesignacao_social() ); + } + else + { + ErrorLogger.logException( new Exception( "CreatePlanosDataProvider . getEmpresaNome( " + empresaID + " ) : is Null !" + + "\n\tempresaData = " + (empresaData == null ? "null" : "not null") + ", nome = " + nome ) ); + } + return nome; + } + + +}