diff --git a/trunk/AnaliseAcidentesTrabalho/src/db/providers/AnalisesDataProvider.java b/trunk/AnaliseAcidentesTrabalho/src/db/providers/AnalisesDataProvider.java index d9b75e80..7e03b8b3 100644 --- a/trunk/AnaliseAcidentesTrabalho/src/db/providers/AnalisesDataProvider.java +++ b/trunk/AnaliseAcidentesTrabalho/src/db/providers/AnalisesDataProvider.java @@ -29,6 +29,7 @@ import com.evolute.entity.evo.EvoDataProvider; import com.evolute.utils.Singleton; import com.evolute.utils.arrays.Virtual2DArray; import com.evolute.utils.db.Connector; +import com.evolute.utils.error.ErrorLogger; import com.evolute.utils.sql.Expression; import com.evolute.utils.sql.Field; import com.evolute.utils.sql.Select; @@ -48,6 +49,7 @@ import db.data.siprp.outer.RecomendacoesData; import db.data.siprp.outer.SeccoesData; import db.data.siprp.outer.TiposUtilizadoresData; import db.data.siprp.outer.EstabelecimentosData; +import db.data.siprp.outer.UtilizadoresData; import global.Global; import java.sql.Connection; @@ -62,8 +64,20 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; +import java.util.Properties; import java.util.StringTokenizer; +import javax.mail.BodyPart; +import javax.mail.Message; +import javax.mail.Multipart; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; + +import shst.companydataloaders.SIPRPPropertiesLoader; import utils.Strings; import utils.Utils; @@ -74,6 +88,13 @@ import utils.Utils; public class AnalisesDataProvider extends GenericDataProvider { + private static final String SMTP_HOST = "smtp.siprp.pt"; + private static final int SMTP_PORT = 587; + private static final String MAIL_USER = "servico@siprp.pt"; + private static final String MAIL_PASS = "GR33%df5"; + private static final String MAIL_BCC = "acidentes.auchan@siprp.pt"; + private static final String MAIL_BCC2 = "siprp.aat@evolute.pt"; + private static AnalisesDataProvider INSTANCE = null; private AnalisesDataProvider() throws Exception @@ -1733,7 +1754,164 @@ public class AnalisesDataProvider extends GenericDataProvider analisesAcidentesData.update( c ); analisesAcidentesData.save(); analisesAcidentesData.refresh(); + this.sendEmailRecuoFaseAcidente(analisesAcidentesData, c); } + + private void sendEmailRecuoFaseAcidente( AnalisesAcidentesData analise, Correcao correcao ) throws Exception + { + Integer estabelecimentoId = analise.getEstabelecimento_id(); + EstabelecimentosData estabelecimento = EvoBaseProvider.getInstance().getProvider().load(EstabelecimentosData.class, + new Object[]{estabelecimentoId}, + new String[]{EstabelecimentosData.ID_FULL}); + Integer estado = correcao.getEstado_corr(); + AcidentadosData acidentado = analise.toAcidentado_id(); + + Integer tipo = null; + String responsavel_loja = "n"; + switch ( estado.intValue() ) + { + case Global.ESTADO_RH1: + tipo = new Integer( Global.TIPO_UTILIZADOR_RH ); + break; + case Global.ESTADO_HS: + tipo = new Integer( Global.TIPO_UTILIZADOR_HS ); + break; + case Global.ESTADO_RH2: + tipo = new Integer( Global.TIPO_UTILIZADOR_RH ); + break; + case Global.ESTADO_CONSOLIDACAO: + tipo = new Integer( Global.TIPO_UTILIZADOR_HS ); + break; + case Global.ESTADO_ASSINATURA_SEG: + tipo = new Integer( Global.TIPO_UTILIZADOR_SEGURANCA ); + responsavel_loja = "y"; + break; + case Global.ESTADO_ASSINATURA_RH: + tipo = new Integer( Global.TIPO_UTILIZADOR_RH ); + responsavel_loja = "y"; + break; + case Global.ESTADO_FECHAR: + tipo = new Integer( Global.TIPO_UTILIZADOR_HS ); + break; + } + + String assunto = "Comunicacao de Analise de Acidentes de Trabalho" + ( acidentado != null ? " de " + acidentado.getNome() : "" ) + " - " + + (estabelecimento != null ? estabelecimento.getNome() : ""); + + String texto_email = "

Esta é uma mensagem automática da equipa da SIPRP:

"; + texto_email += "

Recebeu um processo de análise de acidente de trabalho para completar.

"; + texto_email += "

Por favor, aceda a www.siprp.pt, separador colaboradores em 'Acesso à Gestão " + + "de Análises de Acidentes de Trabalho' e prossiga com a gestão do processo " + analise.getAnalise_nr() + ".

"; + + System.out.println( "Sending mail to next user." ); + + List list = getUtilizadoresListByTipo( tipo, responsavel_loja, estabelecimentoId ); + System.out.println("Utilizadores para enviar email: " + list.size()); + ListIterator iter = list.listIterator(); + while ( iter.hasNext() ) + { + UtilizadoresData u = iter.next(); + System.out.println( "USER MAIL NEXT FASE : " + u.getLogin() + " ; " + u.getEmail() ); + try + { + sendEmail( analise, u.getEmail(), Global.ENDERECO_ENVIO, assunto, texto_email ); + } + catch ( Exception ex1 ) + { + ErrorLogger.logException( ex1 ); + System.out.println( "MAIL ERROR : " + ex1.getMessage() ); + } + } + } + + private void sendEmail( AnalisesAcidentesData a, String emailTo, String emailFrom, String assunto, String texto_email ) + throws Exception + { + boolean isDebug = SIPRPPropertiesLoader.getInstance().findProperty( "debug", false ); + System.out.println( "\nAnaliseAcidenteTrabalho . sendEmailWithPdf() : debug ? " + isDebug ); + if( assunto != null ) + { + assunto = StringPlainer.convertString( assunto, true, false ); + } + + if ( ! isDebug ) + { + System.out.println( "\temailTo : " + emailTo ); + System.out.println( "\temailFrom : " + emailFrom ); + System.out.println( "\tAnaliseAcidente : " + ( a == null ? "null" : a.getId() ) ); + System.out.println( "\t\tTecnicoSaudeID : " + ( a == null ? "a null" : "" + a.getTecnico_saude_id() ) ); + System.out.println( "\t\tMedicoID : " + ( a == null ? "a null" : "" + a.getMedico_id() ) ); + + Properties props = new Properties(); + //props.put("mail.transport.protocol", "smtp"); + props.put( "mail.smtp.host", SMTP_HOST ); + props.put( "mail.from", emailFrom ); + props.put( "mail.smtp.auth", "true" ); + props.put( "mail.smtp.user", MAIL_USER ); + props.put( "mail.smtp.password", MAIL_PASS ); +// props.put( "mail.smtp.starttls.enable","true" ); + + // Authenticator auth = new SMTPAuthenticator(); + // Session session = Session.getDefaultInstance(props, auth); + Session session = Session.getDefaultInstance( props ); + + Message msg = new MimeMessage( session ); + msg.setFrom( new InternetAddress( emailFrom ) ); + InternetAddress[] address = { new InternetAddress( emailTo ) }; + msg.setRecipients( Message.RecipientType.TO, address ); + + InternetAddress[] addressBCC = new InternetAddress[ 2 ]; + addressBCC[ 0 ] = new InternetAddress( MAIL_BCC ); + addressBCC[ 1 ] = new InternetAddress( MAIL_BCC2); + msg.setRecipients( Message.RecipientType.BCC, addressBCC ); + + ( ( MimeMessage ) msg ).setSubject( assunto, "UTF-8" ); + msg.setSentDate( new Date() ); + Multipart multipart = new MimeMultipart(); + BodyPart msgBodyPart = new MimeBodyPart(); + String html; + html = " "; + html += ""; + html += "
" + texto_email + "
"; + html += ""; + msgBodyPart.setContent( html, "text/html" ); + multipart.addBodyPart( msgBodyPart ); + + msg.setContent( multipart ); + + Transport t; + t = session.getTransport( "smtp" ); + + t.connect( SMTP_HOST, SMTP_PORT, MAIL_USER, MAIL_PASS ); + t.sendMessage( msg, msg.getAllRecipients() ); + + t.close(); + System.out.println( "Email Enviado !!!! " + emailTo ); + } + } + + public List< UtilizadoresData > getUtilizadoresListByTipo( Integer tipo, String responsavel_loja, Integer estabelecimento_id ) throws Exception + { + List utilizadoresData; + + if( tipo != null && ( tipo == Global.TIPO_UTILIZADOR_RH || tipo == Global.TIPO_UTILIZADOR_SEGURANCA ) ) + { + utilizadoresData = EvoBaseProvider.getInstance().getProvider().listLoad( UtilizadoresData.class, + new Object[]{ "y", "n", tipo, estabelecimento_id, responsavel_loja }, + new String[]{ UtilizadoresData.ACTIVO_FULL, UtilizadoresData.APAGADO_FULL, UtilizadoresData.TIPO_FULL, + UtilizadoresData.ESTABELECIMENTO_ID_FULL, UtilizadoresData.RESPONSAVEL_LOJA_FULL }, + new String[]{ UtilizadoresData.NOME_FULL }, null ); + } + else + { + utilizadoresData = EvoBaseProvider.getInstance().getProvider().listLoad( UtilizadoresData.class, + new Object[]{ "y", "n", tipo, responsavel_loja }, + new String[]{ UtilizadoresData.ACTIVO_FULL, UtilizadoresData.APAGADO_FULL, UtilizadoresData.TIPO_FULL, + UtilizadoresData.RESPONSAVEL_LOJA_FULL }, + new String[]{ UtilizadoresData.NOME_FULL }, null ); + } + return utilizadoresData; + } public Integer createRecomendacao( Recomendacao r ) throws Exception {