From d66c1e77d131ff8a9f258f430ec4d22c9a8326f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Sim=C3=A3o?= Date: Fri, 27 Mar 2009 18:42:11 +0000 Subject: [PATCH] git-svn-id: https://svn.coded.pt/svn/SIPRP@992 bb69d46d-e84e-40c8-a05a-06db0d633741 --- .../higiene/relatorio/PanelRelatorio.java | 64 ++++++++-- .../print/RelatorioPrintDataProvider.java | 2 +- .../medicina/processo/mail/MailDialog.java | 110 +++++++++++------- .../medicina/processo/mail/MailSender.java | 34 +++++- .../processo/ui/ProcessoDadosPanel.java | 2 +- 5 files changed, 151 insertions(+), 61 deletions(-) diff --git a/trunk/SIPRPSoft/src/siprp/higiene/relatorio/PanelRelatorio.java b/trunk/SIPRPSoft/src/siprp/higiene/relatorio/PanelRelatorio.java index 3698e81e..9ab5744d 100644 --- a/trunk/SIPRPSoft/src/siprp/higiene/relatorio/PanelRelatorio.java +++ b/trunk/SIPRPSoft/src/siprp/higiene/relatorio/PanelRelatorio.java @@ -24,6 +24,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.EventObject; import java.util.List; +import java.util.Vector; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; @@ -39,11 +40,13 @@ import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; +import javax.swing.text.AbstractDocument.LeafElement; import leaf.data.Validator; import leaf.ui.LeafButton; import leaf.ui.LeafDialog; import leaf.ui.LeafIconButton; +import leaf.ui.LeafLogic; import leaf.ui.LeafTextAreaEditor; import siprp.database.cayenne.objects.HsEmailEstabelecimento; import siprp.database.cayenne.objects.HsRelatorio; @@ -349,6 +352,7 @@ public class PanelRelatorio extends JPanel implements CaretListener, ChangeListe { try { + setCursor(new Cursor(Cursor.WAIT_CURSOR)); if( relatorio != null ) { relatorio.setIsSubmetido( new Date() ); @@ -357,10 +361,15 @@ public class PanelRelatorio extends JPanel implements CaretListener, ChangeListe refresh(); setRelatorio( relatorio, false ); } - } catch( Exception e ) + } + catch( Exception e ) { LeafDialog.error(e); } + finally + { + setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + } } private boolean confirmDelete( File file ) @@ -433,9 +442,9 @@ public class PanelRelatorio extends JPanel implements CaretListener, ChangeListe JFrame frame = null; while( comp != null ) { - if( comp.getParent() instanceof JFrame ) + if( comp.getParent() instanceof RelatorioHigieneSegurancaWindow ) { - frame = (JFrame) comp.getParent(); + frame = (RelatorioHigieneSegurancaWindow) comp.getParent(); break; } comp = comp.getParent(); @@ -443,11 +452,8 @@ public class PanelRelatorio extends JPanel implements CaretListener, ChangeListe return frame; } - private void sendMail( HsRelatorio relatorio ) + private void sendMail( HsRelatorio relatorio ) throws Exception { - try - { - List rels = relatorio.getToHsMarcacoesEstabelecimento().getToEstabelecimentos().getHsEmailEstabelecimento(); String to = ""; for( HsEmailEstabelecimento rel : rels ) @@ -469,7 +475,7 @@ public class PanelRelatorio extends JPanel implements CaretListener, ChangeListe "Junto enviamos o relatório de avaliação de riscos laborais e " + "respectivo plano de actuação da auditoria realizada no dia " + dataVisitaString + - "ao vosso estabelecimento de(o) " + + " ao vosso estabelecimento de(o) " + nomeEstabelecimento + ". Aconselhamos que tenham em conta as não conformidades indicadas " + "nestes documentos e que preencham o plano de actuação por forma a " + @@ -503,13 +509,49 @@ public class PanelRelatorio extends JPanel implements CaretListener, ChangeListe "
" + "
" + "Esta é uma mensagem gerada automaticamente pelo nosso sistema, por favor não responda."; - new MailDialog( getFrame(), to, "", subject, body ); + new MailDialog( getFrame(), /*FIXME to */ "tiagosimao@gmail.com", "", subject, body, getRelatoriosTemp(relatorio) ); + } + } + + private Vector getRelatoriosTemp(HsRelatorio relatorio) + { + Vector result = new Vector(); + try + { + byte[] relatorioPDF = new RelatorioPDFCreator().createPDF( relatorio.getId() ); + byte[] planoPDF = new PlanoActuacaoPDFCreator().createPDF( relatorio.getId(), false ); + if( relatorioPDF != null ) + { + String data = relatorio.getToHsMarcacoesEstabelecimento().getDataRelatorio() == null ? "" : relatorio.getData().toString(); + String estabelecimento = relatorio.getToHsMarcacoesEstabelecimento().getToEstabelecimentos().getNomePlain(); + File relat = File.createTempFile("Relatorio - " + estabelecimento + " - " + data, ".pdf"); + if( relat != null ) + { + FileOutputStream fos = new FileOutputStream(relat); + fos.write(relatorioPDF); + fos.close(); + result.add( relat ); + } + } + if( planoPDF != null ) + { + String data = relatorio.getToHsMarcacoesEstabelecimento().getDataRelatorio() == null ? "" : relatorio.getData().toString(); + String estabelecimento = relatorio.getToHsMarcacoesEstabelecimento().getToEstabelecimentos().getNomePlain(); + File plan = File.createTempFile("Plano de actuacao - " + estabelecimento + " - " + data, ".pdf"); + if( plan != null ) + { + FileOutputStream fos = new FileOutputStream(plan); + fos.write(planoPDF); + fos.close(); + result.add( plan ); + } } } - catch( Exception ex ) + catch ( Exception e ) { - ex.printStackTrace(); + LeafDialog.error( e ); } + return result; } private void save() diff --git a/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RelatorioPrintDataProvider.java b/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RelatorioPrintDataProvider.java index e55c4099..8b66f6bd 100644 --- a/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RelatorioPrintDataProvider.java +++ b/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RelatorioPrintDataProvider.java @@ -120,7 +120,7 @@ public class RelatorioPrintDataProvider String funcaoAcompanhante = ( String ) array.get( 0, 8 ); String textoFuncionarios = ( String ) array.get( 0, 9 ); String morada = ( String ) array.get( 0, 10 ); - String tipoAvaliacao = ( ( Boolean ) array.get( 0, 11 ) ).booleanValue() ? "INICIAL" : "PERI\u00d3DICA"; + String tipoAvaliacao = ( array.get( 0, 11 ) != null && ( ( Boolean ) array.get( 0, 11 ) ).booleanValue() ) ? "INICIAL" : "PERI\u00d3DICA"; EmpresaToPrint empresa = new EmpresaToPrint( nomeEmpresa, "http://www.evolute.pt:13080/SIPRPImages/image?id=" + logoId, nomeEstabelecimento, actividadeEmpresa, diff --git a/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailDialog.java b/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailDialog.java index 5769bac1..55410d74 100644 --- a/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailDialog.java +++ b/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailDialog.java @@ -12,11 +12,14 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; +import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; +import java.util.LinkedList; +import java.util.List; import java.util.Vector; import javax.swing.AbstractAction; @@ -61,7 +64,7 @@ public class MailDialog extends CustomJDialog protected JTextField bccText; protected JTextField subjectText; protected BaseTable attachmentsTable; - protected VectorTableModel attachmentsModel; + protected VectorTableModel attachmentsModel; protected JTextPane bodyPane; protected Action removeAttachmentAction; @@ -109,7 +112,7 @@ public class MailDialog extends CustomJDialog System.exit( 0 ); } - public MailDialog( JFrame owner, String to, String bcc, String subject, String message ) + public MailDialog( JFrame owner, String to, String bcc, String subject, String message, Vector files ) throws Exception { super( owner, true ); @@ -119,6 +122,7 @@ public class MailDialog extends CustomJDialog setBcc( bcc ); setSubject( subject ); setMessage( message ); + attach( files ); setModal( true ); setSize( 1024, 768 ); setVisible( true ); @@ -194,10 +198,7 @@ public class MailDialog extends CustomJDialog if( filename != null ) { String dir = fd.getDirectory(); - String full = filename + " (" + dir + ")"; - Vector values = attachmentsModel.getValues(); - values.add( full ); - attachmentsModel.setValues( values ); + attach( filename, dir ); } } }; @@ -242,10 +243,10 @@ public class MailDialog extends CustomJDialog bccText.setEditable( false ); JLabel assuntoLabel = new JLabel( "Assunto:" ); subjectText = new JTextField(); - attachmentsModel = new VectorTableModel( new String[]{ "" } ); + attachmentsModel = new VectorTableModel( new String[]{ "" } ); attachmentsTable = new BaseTable( attachmentsModel ); - JScrollPane attachmentsScroll = new JScrollPane( attachmentsTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, - JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); +// JScrollPane attachmentsScroll = new JScrollPane( attachmentsTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, +// JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); attachmentsTable.getSelectionModel().addListSelectionListener( new ListSelectionListener(){ @Override @@ -529,47 +530,52 @@ public class MailDialog extends CustomJDialog String subject = subjectText.getText(); String body = bodyPane.getText(); String attachmentList[] = new String[ attachmentsTable.getRowCount() ]; - byte attachments[][] = new byte[ attachmentsTable.getRowCount() ][]; - Vector values = attachmentsModel.getValues(); + List attachments = new LinkedList(); + Vector values = attachmentsModel.getValues(); for( int n = 0; n < attachmentList.length; n++ ) { - String str = ( String ) values.elementAt( n ); + String str = values.elementAt( n ); String name = str.substring( 0, str.indexOf( "(") - 1 ); String path = str.substring( str.indexOf( "(") + 1, str.indexOf( ")") ); - try +// try +// { + File file = new File(path + name); + if( file != null && file.exists() ) { - FileInputStream fis = new FileInputStream( path + name ); - Vector bytes = new Vector(); - int available = 0; - int total = 0; - while( ( available = fis.available() ) > 0 ) - { - byte b[] = new byte[ available ]; - fis.read( b ); - bytes.add( b ); - total += available; - } - attachments[ n ] = new byte[ total ]; - int pos = 0; - for( byte[] chunk : bytes ) - { - System.arraycopy( chunk, 0, attachments[ n ], pos, chunk.length ); - pos += chunk.length; - } - } - catch( FileNotFoundException fnfex ) - { - JOptionPane.showMessageDialog( owner, - "O ficheiro " + path + name + " n\u00e3o existe.", - "Ficheiro inexistente", - JOptionPane.ERROR_MESSAGE ); - return; - } - catch( IOException ioex ) - { - DialogException.showExceptionMessage( ioex, "Erro a ler ficheiro " + path + name, true ); - return; + attachments.add(file); } +// FileInputStream fis = new FileInputStream( path + name ); +// Vector bytes = new Vector(); +// int available = 0; +// int total = 0; +// while( ( available = fis.available() ) > 0 ) +// { +// byte b[] = new byte[ available ]; +// fis.read( b ); +// bytes.add( b ); +// total += available; +// } +// attachments[ n ] = new byte[ total ]; +// int pos = 0; +// for( byte[] chunk : bytes ) +// { +// System.arraycopy( chunk, 0, attachments[ n ], pos, chunk.length ); +// pos += chunk.length; +// } +// } +// catch( FileNotFoundException fnfex ) +// { +// JOptionPane.showMessageDialog( owner, +// "O ficheiro " + path + name + " n\u00e3o existe.", +// "Ficheiro inexistente", +// JOptionPane.ERROR_MESSAGE ); +// return; +// } +// catch( IOException ioex ) +// { +// DialogException.showExceptionMessage( ioex, "Erro a ler ficheiro " + path + name, true ); +// return; +// } } MailSender sender = new MailSender(); try @@ -593,4 +599,20 @@ public class MailDialog extends CustomJDialog } ); } + + private void attach( Vector files ) + { + for (File file : files ) + { + attach( file.getName(), file.getParent() + File.separator ); + } + } + + public void attach( String filename, String dir ) + { + String full = filename + " (" + dir + ")"; + Vector values = attachmentsModel.getValues(); + values.add( full ); + attachmentsModel.setValues( values ); + } } diff --git a/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailSender.java b/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailSender.java index 5533815c..fb5ec3aa 100644 --- a/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailSender.java +++ b/trunk/SIPRPSoft/src/siprp/medicina/processo/mail/MailSender.java @@ -1,16 +1,24 @@ package siprp.medicina.processo.mail; +import java.io.File; import java.util.Date; +import java.util.List; import java.util.Properties; +import javax.activation.DataHandler; +import javax.activation.DataSource; +import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.Authenticator; import javax.mail.Message; +import javax.mail.Multipart; import javax.mail.PasswordAuthentication; 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; public class MailSender { @@ -24,7 +32,7 @@ public class MailSender } - public void send( String to, String bcc, String subject, String body, String attachmentNames[], byte attachments[][] ) throws Exception + public void send( String to, String bcc, String subject, String body, String attachmentNames[], List attachments ) throws Exception { Properties props = System.getProperties(); props.put( "mail.smtp.host", mailServer ); @@ -33,15 +41,33 @@ public class MailSender Authenticator auth = new SMTPAuthenticator(); Session session = Session.getInstance( props, auth ); - Message msg = new MimeMessage( session ); + MimeMessage msg = new MimeMessage( session ); msg.setFrom( new InternetAddress( from ) ); msg.setReplyTo( new Address[]{ new InternetAddress(from)} ); msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to, true ) ); msg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse( bcc, true ) ); msg.setSubject( subject ); - msg.setContent( body, "text/html" ); - + + MimeBodyPart bodyPart = new MimeBodyPart(); + bodyPart.setContent( body,"text/html" ); + + Multipart multipart = new MimeMultipart(); + multipart.addBodyPart(bodyPart); + + if( attachmentNames != null && !attachments.isEmpty() ) + { + for(int i = 0; i < attachmentNames.length; ++i ) + { +// String attachName = attachmentNames[i]; + MimeBodyPart attachPart = new MimeBodyPart(); + DataSource dataSource = new FileDataSource(attachments.get(i)); + attachPart.setDataHandler( new DataHandler(dataSource)); + attachPart.setFileName(attachments.get(i).getName()); + multipart.addBodyPart(attachPart, i + 1); + } + } + msg.setContent(multipart); msg.setHeader( "X-Mailer", "Evolute Mailer" ); msg.setSentDate( new Date() ); Transport.send( msg ); diff --git a/trunk/SIPRPSoft/src/siprp/medicina/processo/ui/ProcessoDadosPanel.java b/trunk/SIPRPSoft/src/siprp/medicina/processo/ui/ProcessoDadosPanel.java index 9cd01003..e60f0718 100755 --- a/trunk/SIPRPSoft/src/siprp/medicina/processo/ui/ProcessoDadosPanel.java +++ b/trunk/SIPRPSoft/src/siprp/medicina/processo/ui/ProcessoDadosPanel.java @@ -656,7 +656,7 @@ public class ProcessoDadosPanel extends JPanel to = estabelecimentoEmail; try { - MailDialog mailDialog = new MailDialog( parentWindow, to, bcc, subject, body ); + MailDialog mailDialog = new MailDialog( parentWindow, to, bcc, subject, body, null ); result = new String[2]; result[0] = mailDialog.getSubject(); result[1] = mailDialog.getMessage();