From 2501ef1b93f6559e91a0060a5242a649e798ad9a Mon Sep 17 00:00:00 2001 From: Diogo Neves Date: Tue, 27 Sep 2011 11:15:06 +0000 Subject: [PATCH] fixed data ocorrencia verification git-svn-id: https://svn.coded.pt/svn/SIPRP@1738 bb69d46d-e84e-40c8-a05a-06db0d633741 --- .../AnaliseAcidenteTrabalho.java | 218 +++++++++--------- .../src/java/app.properties | 6 + 2 files changed, 118 insertions(+), 106 deletions(-) diff --git a/trunk/AnaliseAcidentesTrabalho/src/java/analiseacidentestrabalho/AnaliseAcidenteTrabalho.java b/trunk/AnaliseAcidentesTrabalho/src/java/analiseacidentestrabalho/AnaliseAcidenteTrabalho.java index b08afa37..6cdf4115 100644 --- a/trunk/AnaliseAcidentesTrabalho/src/java/analiseacidentestrabalho/AnaliseAcidenteTrabalho.java +++ b/trunk/AnaliseAcidentesTrabalho/src/java/analiseacidentestrabalho/AnaliseAcidenteTrabalho.java @@ -76,6 +76,7 @@ import javax.servlet.http.HttpServletRequest; import org.apache.commons.validator.EmailValidator; import pagestate.AnaliseAcidenteTrabalhoState; import pdf.PdfGenerator; +import shst.companydataloaders.SIPRPPropertiesLoader; import utils.Utils; /** @@ -11189,9 +11190,10 @@ public class AnaliseAcidenteTrabalho extends AbstractPageBean { System.out.println( "BUT GRAVAR" ); - if ( getSessionBean1().getCurrentAnalise() == null ) //new analise + AnaliseAcidente analise = getSessionBean1().getCurrentAnalise(); + if ( analise == null ) //new analise { - if ( validateDataOcorrencia() ) + if ( validateDataOcorrencia( analise ) ) { if ( validationDepartmentSectionOk() ) { @@ -11240,11 +11242,10 @@ public class AnaliseAcidenteTrabalho extends AbstractPageBean } else // edit analise { - if ( validateDataOcorrencia() ) + if ( validateDataOcorrencia( analise ) ) { if ( fieldsLenOk() ) { - AnaliseAcidente a = fillAnaliseFields(); Acidentado ac = null; ArrayList recs = null; @@ -12826,9 +12827,9 @@ public class AnaliseAcidenteTrabalho extends AbstractPageBean return a; } - private boolean validateDataOcorrencia() + private boolean validateDataOcorrencia( AnaliseAcidente analise ) { - Date d = getCalDataOcorrencia(); + Date d = analise == null ? getCalDataOcorrencia() : analise.getData_acidente(); if ( d == null ) { getSessionBean1().setMsg( "Falta data da ocorrĂȘncia!" ); @@ -13213,22 +13214,22 @@ public class AnaliseAcidenteTrabalho extends AbstractPageBean return false; } + AnaliseAcidente analise = getSessionBean1().getCurrentAnalise(); int estado = 0; int estado_assinatura = 0; - if ( getSessionBean1().getCurrentAnalise() == null ) + if ( analise == null ) { estado = Global.ESTADO_SEG; } else { - AnaliseAcidente a = getSessionBean1().getCurrentAnalise(); - estado = a.getEstado().intValue(); - estado_assinatura = a.getEstado_assinatura().intValue(); + estado = analise.getEstado().intValue(); + estado_assinatura = analise.getEstado_assinatura().intValue(); } if ( estado == Global.ESTADO_SEG ) { - if ( ! validateDataOcorrencia() ) + if ( ! validateDataOcorrencia( analise ) ) { return false; } @@ -15231,109 +15232,114 @@ public class AnaliseAcidenteTrabalho extends AbstractPageBean // t.close(); // System.out.println("Email Enviado !!!!" ); // } + private void sendEmailWithPdf( AnaliseAcidente a, String emailTo, String emailFrom, String assunto, String texto_email ) throws Exception { - System.out.println( "\nSEND EMAIL WITH PDF" ); + boolean isDebug = SIPRPPropertiesLoader.getInstance().findProperty( "debug", false ); + System.out.println( "\nAnaliseAcidenteTrabalho . sendEmailWithPdf() : debug ? " + 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() ) ); + 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() ) ); - UtilizadoresDataProvider udp = UtilizadoresDataProvider.getInstance(); + UtilizadoresDataProvider udp = UtilizadoresDataProvider.getInstance(); - Utilizador ut = udp.getUtilizador( a.getTecnico_saude_id() ); - Utilizador um = udp.getUtilizador( a.getMedico_id() ); -// AnalisesDataProvider adp = new AnalisesDataProvider(); - AnalisesDataProvider adp = AnalisesDataProvider.getInstance(); + Utilizador ut = udp.getUtilizador( a.getTecnico_saude_id() ); + Utilizador um = udp.getUtilizador( a.getMedico_id() ); + // AnalisesDataProvider adp = new AnalisesDataProvider(); + AnalisesDataProvider adp = AnalisesDataProvider.getInstance(); - Acidentado ac = adp.getAcidentado( a.getAcidentado_id() ); - Estabelecimento e = new Estabelecimento(); - e.setId( a.getEstabelecimento_id() ); - e.setNome( adp.getEstabelecimentoNome( a.getEstabelecimento_id() ) ); - Seccao s = adp.getSeccao( a.getSeccao_id() ); - Causa c = adp.getCausa( a.getCausas() ); - ArrayList recList = adp.getRecomendacoesByAnalise( a.getId() ); - Recomendacao[] recArray = new Recomendacao[ recList.size() ]; - ListIterator iter = recList.listIterator(); - int i = 0; - while ( iter.hasNext() ) - { - Recomendacao rec = ( Recomendacao ) iter.next(); - recArray[i] = rec; - i++; + Acidentado ac = adp.getAcidentado( a.getAcidentado_id() ); + Estabelecimento e = new Estabelecimento(); + e.setId( a.getEstabelecimento_id() ); + e.setNome( adp.getEstabelecimentoNome( a.getEstabelecimento_id() ) ); + Seccao s = adp.getSeccao( a.getSeccao_id() ); + Causa c = adp.getCausa( a.getCausas() ); + ArrayList recList = adp.getRecomendacoesByAnalise( a.getId() ); + Recomendacao[] recArray = new Recomendacao[ recList.size() ]; + ListIterator iter = recList.listIterator(); + int i = 0; + while ( iter.hasNext() ) + { + Recomendacao rec = ( Recomendacao ) iter.next(); + recArray[i] = rec; + i++; + } + //Recomendacao recArray[] = (Recomendacao)recList.toArray(); + ArrayList medList = adp.getMedidasByAnalise( a.getId() ); + //Medida medArray[] = (Medida)medList.toArray(); + Medida[] medArray = new Medida[ medList.size() ]; + iter = medList.listIterator(); + i = 0; + while ( iter.hasNext() ) + { + Medida med = ( Medida ) iter.next(); + medArray[i] = med; + i++; + } + + PdfGenerator pdf = new PdfGenerator( Global.IMAGE_FOLDER, e, ut, um, a, ac, s, recArray, medArray, c ); + + ByteArrayDataSource ds = new ByteArrayDataSource( pdf.generatePdf(), "application/pdf" ); + 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_username ); + props.put( "mail.smtp.password", mail_password ); + + // 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 ); + + MimeBodyPart pdfPart = new MimeBodyPart(); + //pdfPart.setContent(ds,"application/pdf"); + pdfPart.setDataHandler( new DataHandler( ds ) ); + pdfPart.setHeader( "Content-Transfer-Encoding", "base64" ); + pdfPart.setHeader( "Content-Disposition", "attachment" ); + String pdfname = "Analise" + a.getAnalise_nr(); + pdfPart.setFileName( pdfname ); + multipart.addBodyPart( pdfPart, 1 ); + + msg.setContent( multipart ); + + Transport t; + t = session.getTransport( "smtp" ); + + t.connect( SMTP_HOST, SMTP_PORT, mail_username, mail_password ); + t.sendMessage( msg, msg.getAllRecipients() ); + + t.close(); + System.out.println( "Email Pdf Enviado !!!! " + emailTo ); } - //Recomendacao recArray[] = (Recomendacao)recList.toArray(); - ArrayList medList = adp.getMedidasByAnalise( a.getId() ); - //Medida medArray[] = (Medida)medList.toArray(); - Medida[] medArray = new Medida[ medList.size() ]; - iter = medList.listIterator(); - i = 0; - while ( iter.hasNext() ) - { - Medida med = ( Medida ) iter.next(); - medArray[i] = med; - i++; - } - - PdfGenerator pdf = new PdfGenerator( Global.IMAGE_FOLDER, e, ut, um, a, ac, s, recArray, medArray, c ); - - ByteArrayDataSource ds = new ByteArrayDataSource( pdf.generatePdf(), "application/pdf" ); - 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_username ); - props.put( "mail.smtp.password", mail_password ); - -// 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 ); - - MimeBodyPart pdfPart = new MimeBodyPart(); - //pdfPart.setContent(ds,"application/pdf"); - pdfPart.setDataHandler( new DataHandler( ds ) ); - pdfPart.setHeader( "Content-Transfer-Encoding", "base64" ); - pdfPart.setHeader( "Content-Disposition", "attachment" ); - String pdfname = "Analise" + a.getAnalise_nr(); - pdfPart.setFileName( pdfname ); - multipart.addBodyPart( pdfPart, 1 ); - - msg.setContent( multipart ); - - Transport t; - t = session.getTransport( "smtp" ); - - t.connect( SMTP_HOST, SMTP_PORT, mail_username, mail_password ); - t.sendMessage( msg, msg.getAllRecipients() ); - - t.close(); - System.out.println( "Email Pdf Enviado !!!! " + emailTo ); } public String butImprimir_action() diff --git a/trunk/AnaliseAcidentesTrabalho/src/java/app.properties b/trunk/AnaliseAcidentesTrabalho/src/java/app.properties index c29c6ec8..e086e95b 100644 --- a/trunk/AnaliseAcidentesTrabalho/src/java/app.properties +++ b/trunk/AnaliseAcidentesTrabalho/src/java/app.properties @@ -12,6 +12,8 @@ database.siprp_local = siprp_local_3 username.siprp_local = postgres #password.siprp_local = +debug = false + # tests localhost #server.siprp = localhost @@ -25,6 +27,8 @@ username.siprp_local = postgres #database.siprp_local = siprp_local_3 #username.siprp_local = postgres #password.siprp_local = Typein +# +#debug = true # tests ws2 @@ -39,3 +43,5 @@ username.siprp_local = postgres #database.siprp_local = siprp_local_3 #username.siprp_local = postgres #password.siprp_local = Typein +# +#debug = true \ No newline at end of file