From 1283fe627dcbf27d4302778cb71663c2984d8385 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Wed, 2 Jul 2014 16:21:24 +0200 Subject: [PATCH] =?UTF-8?q?Formul=C3=A1=C5=99=20a=20t=C5=99=C3=ADdy=20pro?= =?UTF-8?q?=20odes=C3=ADl=C3=A1n=C3=AD=20e-mail=C5=AF.=20Implementov=C3=A1?= =?UTF-8?q?no=20odes=C3=ADl=C3=A1n=C3=AD=20PDF=20sestav=20mailem.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 ++ .../info/bukova/isspst/mail/MailMessage.java | 49 +++++++++++++ .../java/info/bukova/isspst/mail/Mailer.java | 7 ++ .../isspst/mail/MailerWithAttachement.java | 55 ++++++++++++++ .../info/bukova/isspst/mail/SimpleMailer.java | 31 ++++++++ .../info/bukova/isspst/ui/mail/MailForm.java | 73 +++++++++++++++++++ .../bukova/isspst/ui/reporting/ReportVM.java | 14 ++++ .../WEB-INF/locales/zk-label.properties | 6 ++ src/main/webapp/WEB-INF/mail.properties | 7 ++ .../webapp/WEB-INF/spring/mail-services.xml | 44 +++++++++++ .../webapp/WEB-INF/spring/root-context.xml | 3 + src/main/webapp/app/mailForm.zul | 31 ++++++++ src/main/webapp/app/reporting/report.zul | 2 +- 13 files changed, 328 insertions(+), 1 deletion(-) create mode 100644 src/main/java/info/bukova/isspst/mail/MailMessage.java create mode 100644 src/main/java/info/bukova/isspst/mail/Mailer.java create mode 100644 src/main/java/info/bukova/isspst/mail/MailerWithAttachement.java create mode 100644 src/main/java/info/bukova/isspst/mail/SimpleMailer.java create mode 100644 src/main/java/info/bukova/isspst/ui/mail/MailForm.java create mode 100644 src/main/webapp/WEB-INF/mail.properties create mode 100644 src/main/webapp/WEB-INF/spring/mail-services.xml create mode 100644 src/main/webapp/app/mailForm.zul diff --git a/pom.xml b/pom.xml index 4ee4cc5d..fe4a45d7 100644 --- a/pom.xml +++ b/pom.xml @@ -308,6 +308,13 @@ DynamicJasper-core-fonts 1.0 + + + + javax.mail + mail + 1.4.3 + diff --git a/src/main/java/info/bukova/isspst/mail/MailMessage.java b/src/main/java/info/bukova/isspst/mail/MailMessage.java new file mode 100644 index 00000000..43df9fe4 --- /dev/null +++ b/src/main/java/info/bukova/isspst/mail/MailMessage.java @@ -0,0 +1,49 @@ +package info.bukova.isspst.mail; + +import org.springframework.mail.SimpleMailMessage; + +public class MailMessage extends SimpleMailMessage { + + /** + * + */ + private static final long serialVersionUID = -3240551712942170018L; + + private boolean html; + private String attachementName; + private byte[] attachementData; + private String contentType; + + public boolean isHtml() { + return html; + } + + public void setHtml(boolean html) { + this.html = html; + } + + public byte[] getAttachementData() { + return attachementData; + } + + public void setAttachementData(byte[] attachementData) { + this.attachementData = attachementData; + } + + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public String getAttachementName() { + return attachementName; + } + + public void setAttachementName(String attachementName) { + this.attachementName = attachementName; + } + +} diff --git a/src/main/java/info/bukova/isspst/mail/Mailer.java b/src/main/java/info/bukova/isspst/mail/Mailer.java new file mode 100644 index 00000000..d01e5daf --- /dev/null +++ b/src/main/java/info/bukova/isspst/mail/Mailer.java @@ -0,0 +1,7 @@ +package info.bukova.isspst.mail; + +public interface Mailer { + + public void send(MailMessage message); + +} diff --git a/src/main/java/info/bukova/isspst/mail/MailerWithAttachement.java b/src/main/java/info/bukova/isspst/mail/MailerWithAttachement.java new file mode 100644 index 00000000..5dcde5ba --- /dev/null +++ b/src/main/java/info/bukova/isspst/mail/MailerWithAttachement.java @@ -0,0 +1,55 @@ +package info.bukova.isspst.mail; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; + +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.InputStreamSource; +import org.springframework.mail.MailParseException; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; + +public class MailerWithAttachement implements Mailer { + + private JavaMailSender sender; + private String from; + + public MailerWithAttachement(JavaMailSender sender) { + this.sender = sender; + } + + @Override + public void send(MailMessage message) { + MimeMessage mimeMessage = sender.createMimeMessage(); + + try { + MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); + + if (message.getFrom() == null || message.getFrom().isEmpty()) { + if (from == null || from.isEmpty()) { + message.setFrom("tomcat@is.bukova.info"); + } else { + message.setFrom(from); + } + } + + helper.setFrom(message.getFrom()); + helper.setTo(message.getTo()); + helper.setSubject(message.getSubject()); + helper.setText(message.getText(), message.isHtml()); + + if (message.getAttachementData() != null) { + InputStreamSource source = new ByteArrayResource(message.getAttachementData()); + helper.addAttachment(message.getAttachementName(), source, message.getContentType()); + } + } catch (MessagingException e) { + throw new MailParseException(e); + } + sender.send(mimeMessage); + } + + public void setFrom(String from) { + this.from = from; + } + +} diff --git a/src/main/java/info/bukova/isspst/mail/SimpleMailer.java b/src/main/java/info/bukova/isspst/mail/SimpleMailer.java new file mode 100644 index 00000000..0d905017 --- /dev/null +++ b/src/main/java/info/bukova/isspst/mail/SimpleMailer.java @@ -0,0 +1,31 @@ +package info.bukova.isspst.mail; + +import org.springframework.mail.javamail.JavaMailSender; + +public class SimpleMailer implements Mailer { + + private JavaMailSender sender; + private String from; + + public SimpleMailer(JavaMailSender sender) { + this.sender = sender; + } + + @Override + public void send(MailMessage message) { + if (message.getFrom() == null || message.getFrom().isEmpty()) { + if (from == null || from.isEmpty()) { + message.setFrom("tomcat@is.bukova.info"); + } else { + message.setFrom(from); + } + } + + sender.send(message); + } + + public void setFrom(String from) { + this.from = from; + } + +} diff --git a/src/main/java/info/bukova/isspst/ui/mail/MailForm.java b/src/main/java/info/bukova/isspst/ui/mail/MailForm.java new file mode 100644 index 00000000..09c42e91 --- /dev/null +++ b/src/main/java/info/bukova/isspst/ui/mail/MailForm.java @@ -0,0 +1,73 @@ +package info.bukova.isspst.ui.mail; + +import info.bukova.isspst.mail.MailMessage; +import info.bukova.isspst.mail.Mailer; +import info.bukova.isspst.reporting.Generator; +import info.bukova.isspst.reporting.GeneratorFactory; +import info.bukova.isspst.reporting.ReportDefinition; + +import org.zkoss.bind.annotation.BindingParam; +import org.zkoss.bind.annotation.Command; +import org.zkoss.bind.annotation.ExecutionArgParam; +import org.zkoss.bind.annotation.Init; +import org.zkoss.zk.ui.select.annotation.WireVariable; +import org.zkoss.zul.Window; + +public class MailForm { + + @WireVariable + private Mailer mailer; + @WireVariable + private ReportDefinition reportDefinition; + @WireVariable + private GeneratorFactory genFactory; + private MailMessage message; + private String to; + private String attachement; + private boolean report; + + @Init + public void init(@ExecutionArgParam("report") Boolean report) { + message = new MailMessage(); + message.setHtml(true); + this.report = report; + if (report) { + attachement = "report.pdf"; + } + } + + @Command + public void send(@BindingParam("window") Window window) { + message.setTo(to); + if (report) { + Generator generator = genFactory.createGenerator(reportDefinition); + message.setAttachementData(generator.generate()); + message.setAttachementName("report.pdf"); + message.setContentType("application/pdf"); + } + + mailer.send(message); + window.detach(); + } + + public MailMessage getMessage() { + return message; + } + + public void setMessage(MailMessage message) { + this.message = message; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + public String getAttachement() { + return attachement; + } + +} diff --git a/src/main/java/info/bukova/isspst/ui/reporting/ReportVM.java b/src/main/java/info/bukova/isspst/ui/reporting/ReportVM.java index 5fda96d7..dd0f4dcd 100644 --- a/src/main/java/info/bukova/isspst/ui/reporting/ReportVM.java +++ b/src/main/java/info/bukova/isspst/ui/reporting/ReportVM.java @@ -1,6 +1,12 @@ package info.bukova.isspst.ui.reporting; +import java.util.HashMap; +import java.util.Map; + +import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.Init; +import org.zkoss.zk.ui.Executions; +import org.zkoss.zul.Window; public class ReportVM { @@ -8,5 +14,13 @@ public class ReportVM { public void init() { } + + @Command + public void send() { + Map params = new HashMap(); + params.put("report", true); + Window mailForm = (Window) Executions.createComponents("/app/mailForm.zul", null, params); + mailForm.doModal(); + } } diff --git a/src/main/webapp/WEB-INF/locales/zk-label.properties b/src/main/webapp/WEB-INF/locales/zk-label.properties index 9aeb2b18..8a66b098 100644 --- a/src/main/webapp/WEB-INF/locales/zk-label.properties +++ b/src/main/webapp/WEB-INF/locales/zk-label.properties @@ -134,6 +134,12 @@ ReportTitle=Nadpis sestavy: ReportOptions=Volby sestavy ReportNoOptions=Žádná nestavení +MailForm=Odeslání e-mailu +MailFor=Komu: +MailSubject=Předmět: +MailSend=Odeslat +MailAttachement=Příloha: + Error=Chyba ErrorRights=K vykobání této operace nemáte dostatečná oprávnění diff --git a/src/main/webapp/WEB-INF/mail.properties b/src/main/webapp/WEB-INF/mail.properties new file mode 100644 index 00000000..1dbc1730 --- /dev/null +++ b/src/main/webapp/WEB-INF/mail.properties @@ -0,0 +1,7 @@ +mail.from=kosef.rokos@gmail.com +mail.host=smtp.gmail.com +mail.port=587 +mail.useauth=true +mail.usessl=true +mail.username=josef.rokos@gmail.com +mail.password=XXXXXX \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/spring/mail-services.xml b/src/main/webapp/WEB-INF/spring/mail-services.xml new file mode 100644 index 00000000..4e58ad9a --- /dev/null +++ b/src/main/webapp/WEB-INF/spring/mail-services.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + ${mail.useauth} + ${mail.usessl} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/webapp/WEB-INF/spring/root-context.xml b/src/main/webapp/WEB-INF/spring/root-context.xml index d4b47b1c..9074fc57 100644 --- a/src/main/webapp/WEB-INF/spring/root-context.xml +++ b/src/main/webapp/WEB-INF/spring/root-context.xml @@ -16,6 +16,7 @@ /WEB-INF/jdbc.properties /WEB-INF/ldap.properties + /WEB-INF/mail.properties @@ -75,6 +76,8 @@ + + diff --git a/src/main/webapp/app/mailForm.zul b/src/main/webapp/app/mailForm.zul new file mode 100644 index 00000000..971c969f --- /dev/null +++ b/src/main/webapp/app/mailForm.zul @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + +