diff --git a/src/main/java/info/bukova/isspst/mail/MailerWithAttachement.java b/src/main/java/info/bukova/isspst/mail/MailerWithAttachement.java index b148acc3..16ab9a80 100644 --- a/src/main/java/info/bukova/isspst/mail/MailerWithAttachement.java +++ b/src/main/java/info/bukova/isspst/mail/MailerWithAttachement.java @@ -3,9 +3,13 @@ package info.bukova.isspst.mail; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.InputStreamSource; +import org.springframework.mail.MailAuthenticationException; import org.springframework.mail.MailParseException; +import org.springframework.mail.MailSendException; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.scheduling.annotation.Async; @@ -14,6 +18,8 @@ public class MailerWithAttachement implements Mailer { private JavaMailSender sender; private String from; + private boolean overrideFrom = false; + private static final Logger logger = LoggerFactory.getLogger(MailerWithAttachement.class); public MailerWithAttachement(JavaMailSender sender) { this.sender = sender; @@ -27,7 +33,9 @@ public class MailerWithAttachement implements Mailer { try { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); - if (message.getFrom() == null || message.getFrom().isEmpty()) { + message.setReplyTo(message.getFrom()); + + if (message.getFrom() == null || message.getFrom().isEmpty() || overrideFrom) { if (from == null || from.isEmpty()) { message.setFrom("tomcat@is.bukova.info"); } else { @@ -36,9 +44,15 @@ public class MailerWithAttachement implements Mailer { } helper.setFrom(message.getFrom()); + helper.setReplyTo(message.getReplyTo()); helper.setTo(message.getTo()); helper.setSubject(message.getSubject()); - helper.setText(message.getText(), message.isHtml()); + + if (message.getText() != null) { + helper.setText(message.getText(), message.isHtml()); + } else { + helper.setText("", message.isHtml()); + } if (message.getAttachementData() != null) { InputStreamSource source = new ByteArrayResource(message.getAttachementData()); @@ -47,11 +61,22 @@ public class MailerWithAttachement implements Mailer { } catch (MessagingException e) { throw new MailParseException(e); } - sender.send(mimeMessage); + try { + sender.send(mimeMessage); + } catch (MailAuthenticationException e) { + logger.error("Authentication error"); + } catch (MailSendException e) { + logger.error("Mail send error: " + e.getMessage()); + } + } public void setFrom(String from) { this.from = from; } + + public void setOverrideFrom(boolean override) { + this.overrideFrom = override; + } } diff --git a/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java index d338f7dd..0b4765ed 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java @@ -232,6 +232,7 @@ public abstract class RequirementBaseServiceImpl exte } if (message != null) { + message.setFrom(getLoggedInUser().getEmail()); message.setTo(e.getOwnedBy().getEmail()); mailer.send(message); } diff --git a/src/main/java/info/bukova/isspst/ui/mail/MailForm.java b/src/main/java/info/bukova/isspst/ui/mail/MailForm.java index 09c42e91..3251f430 100644 --- a/src/main/java/info/bukova/isspst/ui/mail/MailForm.java +++ b/src/main/java/info/bukova/isspst/ui/mail/MailForm.java @@ -1,15 +1,29 @@ package info.bukova.isspst.ui.mail; +import static ch.lambdaj.Lambda.filter; +import static ch.lambdaj.Lambda.having; +import static ch.lambdaj.Lambda.on; +import static org.hamcrest.Matchers.startsWith; +import info.bukova.isspst.data.Address; +import info.bukova.isspst.data.Member; +import info.bukova.isspst.data.User; 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 info.bukova.isspst.services.addressbook.AdbService; +import info.bukova.isspst.services.users.UserService; +import java.util.ArrayList; +import java.util.List; + +import org.springframework.security.access.AccessDeniedException; 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.bind.annotation.NotifyChange; import org.zkoss.zk.ui.select.annotation.WireVariable; import org.zkoss.zul.Window; @@ -21,10 +35,22 @@ public class MailForm { private ReportDefinition reportDefinition; @WireVariable private GeneratorFactory genFactory; + @WireVariable + private UserService userService; + @WireVariable + private AdbService adbService; private MailMessage message; private String to; + private String cc; + private String bcc; private String attachement; private boolean report; + private List
addressbook; + private List users; + private List
selectedAddresses; + private List selectedUsers; + private int adbType; + private String findAddress; @Init public void init(@ExecutionArgParam("report") Boolean report) { @@ -34,11 +60,37 @@ public class MailForm { if (report) { attachement = "report.pdf"; } + + try { + addressbook = adbService.getAll(); + } catch (AccessDeniedException e) { + addressbook = new ArrayList
(); + } + + users = userService.getAll(); + + selectedAddresses = new ArrayList
(); + selectedUsers = new ArrayList(); + + to = ""; + cc = ""; + bcc = ""; } @Command public void send(@BindingParam("window") Window window) { - message.setTo(to); + message.setTo(to.split(";")); + + if (cc != null && !cc.isEmpty()) { + message.setCc(cc.split(";")); + } + + if (bcc != null && !bcc.isEmpty()) { + message.setCc(bcc.split(";")); + } + + message.setFrom(userService.getCurrent().getEmail()); + if (report) { Generator generator = genFactory.createGenerator(reportDefinition); message.setAttachementData(generator.generate()); @@ -49,6 +101,76 @@ public class MailForm { mailer.send(message); window.detach(); } + + @Command + @NotifyChange("to") + public void addTo() { + to = appendAddress(to, selAddrs()); + } + + @Command + @NotifyChange("cc") + public void addCc() { + cc = appendAddress(cc, selAddrs()); + } + + @Command + @NotifyChange("bcc") + public void addBcc() { + bcc = appendAddress(bcc, selAddrs()); + } + + @Command + @NotifyChange({"selectedAddresses", "selectedUsers"}) + public void find() { + if (adbType == 0) { + List found = filter(having(on(Member.class).getFullName(), startsWith(findAddress)), users); + + if (!found.isEmpty()) { + selectedUsers.clear(); + if (!findAddress.isEmpty() && found.get(0).getEmail() != null && !found.get(0).getEmail().isEmpty()) { + selectedUsers.add(found.get(0)); + } + } + + } else { + List
found = filter(having(on(Address.class).getCompany(), startsWith(findAddress)), addressbook); + + if (!found.isEmpty()) { + selectedAddresses.clear(); + if (!findAddress.isEmpty() && found.get(0).getEmail() != null && !found.get(0).getEmail().isEmpty()) { + selectedAddresses.add(found.get(0)); + } + } + } + } + + private String appendAddress(String field, List addrs) { + for (String addr : addrs) { + if (field.isEmpty()) { + field = addr; + } else { + field += "; " + addr; + } + } + + return field; + } + + private List selAddrs() { + List ret = new ArrayList(); + + if (adbType == 1) { + for (Address addr : selectedAddresses) { + ret.add(addr.getCompany() + " <" + addr.getEmail() + ">"); + } + } else { + for (User u : selectedUsers) { + ret.add(u.getFullName() + " <" + u.getEmail() + ">"); + } + } + return ret; + } public MailMessage getMessage() { return message; @@ -70,4 +192,60 @@ public class MailForm { return attachement; } + public String getCc() { + return cc; + } + + public void setCc(String cc) { + this.cc = cc; + } + + public String getBcc() { + return bcc; + } + + public void setBcc(String bcc) { + this.bcc = bcc; + } + + public List
getAddressbook() { + return addressbook; + } + + public List getUsers() { + return users; + } + + public int getAdbType() { + return adbType; + } + + public void setAdbType(int adbType) { + this.adbType = adbType; + } + + public List
getSelectedAddresses() { + return selectedAddresses; + } + + public void setSelectedAddresses(List
selectedAddresses) { + this.selectedAddresses = selectedAddresses; + } + + public List getSelectedUsers() { + return selectedUsers; + } + + public void setSelectedUsers(List selectedUsers) { + this.selectedUsers = selectedUsers; + } + + public String getFindAddress() { + return findAddress; + } + + public void setFindAddress(String findAddress) { + this.findAddress = findAddress; + } + } diff --git a/src/main/webapp/WEB-INF/locales/zk-label.properties b/src/main/webapp/WEB-INF/locales/zk-label.properties index 8b42b8b6..8525e201 100644 --- a/src/main/webapp/WEB-INF/locales/zk-label.properties +++ b/src/main/webapp/WEB-INF/locales/zk-label.properties @@ -237,9 +237,20 @@ ReportNoOptions=Žádná nestavení MailForm=Odeslání e-mailu MailFor=Komu: +MailCc=Kopie: +MailBcc=Skrytá: MailSubject=Předmět: MailSend=Odeslat MailAttachement=Příloha: +MailAddressBook=Adresář: +MailUsers=Uživatelé +MailSuppliers=Dodavatelé +MailName=Jméno +MailCompany=Firma +MailToButton=Komu -> +MailCcButton=Kopie -> +MailBccButton=Skrytá -> + Error=Chyba ErrorRights=K vykobání této operace nemáte dostatečná oprávnění @@ -258,7 +269,6 @@ false=Ne Information=Informace -Requirements=Požadavky Orders=Objednávky MaterialRequirement=Požadavek na materiál @@ -270,7 +280,6 @@ ApprovedRequirementItems=Schválené položky požadavků CurrentRequirements=Aktuální požadavky ApprovedOrders=Schválené objednávky BussinessTrips=Služební cesty -TravelOrders=Cestovní příkazy Lists=Seznamy Settings=Nastavení Administration=Administrace diff --git a/src/main/webapp/WEB-INF/mail.properties b/src/main/webapp/WEB-INF/mail.properties index 1dbc1730..15149e27 100644 --- a/src/main/webapp/WEB-INF/mail.properties +++ b/src/main/webapp/WEB-INF/mail.properties @@ -1,7 +1,8 @@ -mail.from=kosef.rokos@gmail.com +mail.from=josef.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 +mail.password=XXXXXXXX +mail.overrideFrom=false \ 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 index 7e401b71..ebf1bb71 100644 --- a/src/main/webapp/WEB-INF/spring/mail-services.xml +++ b/src/main/webapp/WEB-INF/spring/mail-services.xml @@ -20,6 +20,7 @@ + diff --git a/src/main/webapp/app/mailForm.zul b/src/main/webapp/app/mailForm.zul index 971c969f..1d4a2178 100644 --- a/src/main/webapp/app/mailForm.zul +++ b/src/main/webapp/app/mailForm.zul @@ -2,30 +2,81 @@ +viewModel="@id('vm') @init('info.bukova.isspst.ui.mail.MailForm')" width="800px" closable="true"> - - - - - - - - - - - - - - - - - -