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 @@ + + + + + + + + + + + + + + + + + + + + + + +