/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package planosactuacao; import com.evolute.utils.error.ErrorLogger; 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.providers.CreatePlanosDataProvider; import db.providers.UtilizadoresDataProvider; import global.Global; import java.util.Date; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import mail.Mail; import mail.MailNextPhase; import mail.MailNextPhase.SIPRP_TO_RESPONSAVEL_SEGURANCA; import shst.data.outer.HsRelatorioData; import shst.data.provider.PlanoActuacaoDataProvider; import static com.evolute.utils.strings.UnicodeLatin1Map.*; /** * * @author lluis */ public class PlanosActuacao { private List< PlanoActuacao > planosList; private List< Date > planosOnlineList; public PlanosActuacao() { try { System.out.println( "Importar planos de actua" + ccedil + atilde + "o" ); //rdp = new RelatoriosDataProvider(); // Integer estabelecimentoId = null; // if(u.getTipo().intValue() != Global.TECNICO_HS && u.getTipo().intValue() != Global.DIRECTOR_SIPRP) // { // estabelecimentoId = u.getEstabelecimento_id(); // } // // List planosOnlineList = getPlanosOnline(null); // System.out.println("PLANOS ONLINE LIST : " + planosOnlineList.size()); initRelatorios( null, null ); // System.out.println("LIST RELATORIOS : " + listRelatorios.size()); // putPlanosOnline(listRelatorios); } catch ( Exception ex ) { ErrorLogger.logException( ex ); } } 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 > list, Integer estabelecimentoId ) { System.out.println("initRelatorios for estabelecimento id: " + estabelecimentoId ); try { CreatePlanosDataProvider cpdp = CreatePlanosDataProvider.getInstance(); PlanoActuacaoDataProvider pa = PlanoActuacaoDataProvider.getProvider(); if( estabelecimentoId == null ) { System.out.println("A adicionar planos de estabelecimentos novos"); List< Integer > estabelecimentos = cpdp.getEstabelecimentosWithPlanos(); putPlanosOnline( pa.getRelatoriosNotIn( estabelecimentos ) ); estabelecimentos = cpdp.getEstabelecimentosWithPlanos(); System.out.println("A adicionar planos a estabelecimentos anteriores"); for( Integer estabelecimentoID : estabelecimentos ) { try { System.out.println("A importar relatorio para o estabelecimento: " + estabelecimentoID ); List datas = cpdp.getPlanosOnline(estabelecimentoID); if( datas.isEmpty() ) { System.out.println("Nenhuma data de relatorio para o estabelecimento: " + estabelecimentoID ); } else { List< HsRelatorioData > relatorios = pa.getRelatoriosNotIn(datas, estabelecimentoID); if( relatorios.isEmpty() ) { System.out.println("Nenhum relatorio novo para o estabelecimento: " + estabelecimentoID ); } else { putPlanosOnline( relatorios ); } } } catch(Exception e ) { ErrorLogger.logException( e ); } } } else { putPlanosOnline( pa.getRelatoriosNotIn( list, estabelecimentoId ) ); } } catch ( Exception e ) { ErrorLogger.logException( e ); } } private void putPlanosOnline( List< HsRelatorioData > listRelatorios ) { System.out.println("putPlanosOnline (" + listRelatorios == null ? "null" : listRelatorios.size() ); Iterator< HsRelatorioData > iter = listRelatorios.iterator(); while ( iter.hasNext() ) { HsRelatorioData relatorio = iter.next(); System.out.println("\tRELATORIO ID : " + relatorio.getId().toString() ); try { PlanoActuacao p = createPlano( relatorio ); if ( p != null ) { enviarMail( p ); } //showPlano(p); } catch ( Exception ex ) { ErrorLogger.logException( ex ); System.out.println( "ERRO NA CRIACAO DOS PLANOS : " + ex.getMessage() ); } } } private PlanoActuacao createPlano(HsRelatorioData relatorio) throws Exception { System.out.println("CREATE PLANO - RELATORIO ID : " + relatorio.getId().toString()); CreatePlanosDataProvider cpdp = CreatePlanosDataProvider.getInstance(); PlanoActuacao p = new PlanoActuacao(); p.setData_visita( relatorio.toMarcacao_id().getData() ); // p.setData_visita(relatorio.getToHsMarcacoesEstabelecimento().getData()); // if(relatorio.getToHsMarcacoesEstabelecimento().getToMarcacoesTecnicosHst() != null) if ( relatorio.toMarcacao_id().toTecnico_hst() != null ) { // p.setTecnico_hs_id(relatorio.getToHsMarcacoesEstabelecimento().getToMarcacoesTecnicosHst().getId()); // p.setTecnico_hs_nome(relatorio.getToHsMarcacoesEstabelecimento().getToMarcacoesTecnicosHst().getNome()); p.setTecnico_hs_id( relatorio.toMarcacao_id().toTecnico_hst().getId() ); p.setTecnico_hs_nome( relatorio.toMarcacao_id().toTecnico_hst().getNome() ); } // if ( relatorio.getToHsMarcacoesEstabelecimento().getToMarcacoesTecnicosSuperioresHst() != null ) if ( relatorio.toMarcacao_id().toTecnico_superior_hst() != null ) { // p.setTecnico_superior_hs_id( relatorio.getToHsMarcacoesEstabelecimento().getToMarcacoesTecnicosSuperioresHst().getId() ); // p.setTecnico_superior_hs_nome( relatorio.getToHsMarcacoesEstabelecimento().getToMarcacoesTecnicosSuperioresHst().getNome() ); 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.getToHsMarcacoesEstabelecimento().getToEstabelecimentos().getId()); // p.setEmpresa_id(relatorio.getToHsMarcacoesEstabelecimento().getToEstabelecimentos().getToEmpresas().getId()); 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(rdp.getEmpresaNome(p.getEmpresa_id()))); p.setNome_empresa(utils.Utils.unicodeToHTML(cpdp.getEmpresaNome(p.getEmpresa_id()))); } catch(Exception ex) { ErrorLogger.logException( ex ); } // p.setNome_estabelecimento(relatorio.getToHsMarcacoesEstabelecimento().getToEstabelecimentos().getNome()); 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 = rdp.getFullPlano(p, relatorio.getId()); p = cpdp.getFullPlano( p, relatorio.getId() ); if ( p.getAreas() != null ) { if ( p.getAreas().size() > 0 ) { p.setId( cpdp.createPlano( p ) ); System.out.println("CREATE PLANO - PLANO ID : " + p.getId().toString()); if ( p.getAreas() != null ) { createAreas( p.getId(), p.getAreas() ); } } else { p = null; } } else { p = null; } return p; } private void createAreas(Integer plano_id, List areas) throws Exception { CreatePlanosDataProvider cpdp = CreatePlanosDataProvider.getInstance(); for(Area a : areas) { a.setPlano_id( plano_id ); a.setId( cpdp.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()); } } } /** * FIXME : importacao -> r.setPatrimonial( ... ) * * @param area_id * @param riscos * @throws Exception */ private void createRiscos( Integer area_id, List< Risco > riscos ) throws Exception { CreatePlanosDataProvider cpdp = CreatePlanosDataProvider.getInstance(); for ( Risco r : riscos ) { r.setArea_id( area_id ); //r.setId(pdp.createRisco(r)); System.out.println( "CREATE RISCO: " ); if ( r.getValores() != null ) { for ( Valor v : r.getValores() ) { //System.out.println("RISCO -> " + r.getRisco_id().toString() + "VALOR -> " + v.getValor().toString()); r.setValor( v.getValor() ); r.setId( cpdp.createRisco( r ) ); } } if ( r.getMedidas() != null ) { createMedidas( r.getId(), r.getMedidas() ); } } } private void createMedidas(Integer risco_id, List medidas) throws Exception { CreatePlanosDataProvider cpdp = CreatePlanosDataProvider.getInstance(); for(Medida m : medidas) { m.setRisco_id( risco_id ); m.setId( cpdp.createMedida( m ) ); if(m.getPostos() != null) { createPostos(m.getId(), m.getPostos()); } } } private void createPostos(Integer medida_id, List postos) throws Exception { CreatePlanosDataProvider cpdp = CreatePlanosDataProvider.getInstance(); for(PostoTrabalho p : postos) { p.setMedida_id( medida_id ); cpdp.createPostoTrabalho( p ); } } private void enviarMail( PlanoActuacao p ) { 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 ); } } } }