Vylepšený dialog pro odeslání e-mailu. Adresa "Od" se nastavuje podle
přihlášeného uživatele. Přidáno logování chyb při odesílání e-mailu.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -232,6 +232,7 @@ public abstract class RequirementBaseServiceImpl<T extends RequirementBase> exte
|
||||
}
|
||||
|
||||
if (message != null) {
|
||||
message.setFrom(getLoggedInUser().getEmail());
|
||||
message.setTo(e.getOwnedBy().getEmail());
|
||||
mailer.send(message);
|
||||
}
|
||||
|
||||
@@ -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<Address> addressbook;
|
||||
private List<User> users;
|
||||
private List<Address> selectedAddresses;
|
||||
private List<User> 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<Address>();
|
||||
}
|
||||
|
||||
users = userService.getAll();
|
||||
|
||||
selectedAddresses = new ArrayList<Address>();
|
||||
selectedUsers = new ArrayList<User>();
|
||||
|
||||
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<User> 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<Address> 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<String> addrs) {
|
||||
for (String addr : addrs) {
|
||||
if (field.isEmpty()) {
|
||||
field = addr;
|
||||
} else {
|
||||
field += "; " + addr;
|
||||
}
|
||||
}
|
||||
|
||||
return field;
|
||||
}
|
||||
|
||||
private List<String> selAddrs() {
|
||||
List<String> ret = new ArrayList<String>();
|
||||
|
||||
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<Address> getAddressbook() {
|
||||
return addressbook;
|
||||
}
|
||||
|
||||
public List<User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public int getAdbType() {
|
||||
return adbType;
|
||||
}
|
||||
|
||||
public void setAdbType(int adbType) {
|
||||
this.adbType = adbType;
|
||||
}
|
||||
|
||||
public List<Address> getSelectedAddresses() {
|
||||
return selectedAddresses;
|
||||
}
|
||||
|
||||
public void setSelectedAddresses(List<Address> selectedAddresses) {
|
||||
this.selectedAddresses = selectedAddresses;
|
||||
}
|
||||
|
||||
public List<User> getSelectedUsers() {
|
||||
return selectedUsers;
|
||||
}
|
||||
|
||||
public void setSelectedUsers(List<User> selectedUsers) {
|
||||
this.selectedUsers = selectedUsers;
|
||||
}
|
||||
|
||||
public String getFindAddress() {
|
||||
return findAddress;
|
||||
}
|
||||
|
||||
public void setFindAddress(String findAddress) {
|
||||
this.findAddress = findAddress;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user