Commit 3aa42c14 authored by Matija Obreza's avatar Matija Obreza
Browse files

Allow multiple recipients at FaoInstitute (separate with ,) of request

email notification
parent de6f2bfa
...@@ -22,4 +22,6 @@ public interface EMailService { ...@@ -22,4 +22,6 @@ public interface EMailService {
void sendMail(String mailSubject, String mailBody, String[] emailCc, String... emailTo); void sendMail(String mailSubject, String mailBody, String[] emailCc, String... emailTo);
String[] toEmails(String emailAddresses);
} }
...@@ -20,6 +20,8 @@ import java.util.Arrays; ...@@ -20,6 +20,8 @@ import java.util.Arrays;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.service.EMailService; import org.genesys2.server.service.EMailService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -51,6 +53,18 @@ public class EMailServiceImpl implements EMailService { ...@@ -51,6 +53,18 @@ public class EMailServiceImpl implements EMailService {
@Value("${mail.user.from}") @Value("${mail.user.from}")
private String emailFrom; private String emailFrom;
@Override
public String[] toEmails(String emailAddresses) {
if (StringUtils.isBlank(emailAddresses)) {
return null;
}
String[] res = emailAddresses.split(",|;");
for (int i = 0; i < res.length; i++) {
res[i] = StringUtils.trim(res[i]);
}
return res;
}
@Override @Override
public void sendMail(String mailSubject, String mailBody, String... emailTo) { public void sendMail(String mailSubject, String mailBody, String... emailTo) {
sendMail(mailSubject, mailBody, null, emailTo); sendMail(mailSubject, mailBody, null, emailTo);
...@@ -71,6 +85,7 @@ public class EMailServiceImpl implements EMailService { ...@@ -71,6 +85,7 @@ public class EMailServiceImpl implements EMailService {
message.setFrom(emailFrom); message.setFrom(emailFrom);
message.setTo(emailTo); message.setTo(emailTo);
_logger.info("Email to: " + ArrayUtils.toString(emailTo, "<NULL>"));
if (emailCc != null && emailCc.length > 0) { if (emailCc != null && emailCc.length > 0) {
message.setCc(emailCc); message.setCc(emailCc);
} }
......
...@@ -157,7 +157,7 @@ public class RequestServiceImpl implements RequestService { ...@@ -157,7 +157,7 @@ public class RequestServiceImpl implements RequestService {
final String mailBody = contentService.processTemplate(article.getBody(), root); final String mailBody = contentService.processTemplate(article.getBody(), root);
final String mailSubject = "[" + materialRequest.getUuid() + "] " + article.getTitle(); final String mailSubject = "[" + materialRequest.getUuid() + "] " + article.getTitle();
LOG.info(">>>" + mailBody); LOG.debug(">>>" + mailBody);
// send to user // send to user
emailService.sendMail(mailSubject, mailBody, materialRequest.getEmail()); emailService.sendMail(mailSubject, mailBody, materialRequest.getEmail());
...@@ -395,8 +395,8 @@ public class RequestServiceImpl implements RequestService { ...@@ -395,8 +395,8 @@ public class RequestServiceImpl implements RequestService {
final String mailSubject = "[" + materialSubRequest.getInstCode() + "] " + "[" + materialSubRequest.getUuid() + "] " + article.getTitle(); final String mailSubject = "[" + materialSubRequest.getInstCode() + "] " + "[" + materialSubRequest.getUuid() + "] " + article.getTitle();
LOG.info(">>>" + mailBody); LOG.info(">>>" + mailBody);
// send to recipient // send to recipient(s)
emailService.sendMail(mailSubject, mailBody, emailCc, materialSubRequest.getInstEmail()); emailService.sendMail(mailSubject, mailBody, emailCc, emailService.toEmails(materialSubRequest.getInstEmail()));
materialSubRequest.setLastReminderDate(new Date()); materialSubRequest.setLastReminderDate(new Date());
subRequestRepository.save(materialSubRequest); subRequestRepository.save(materialSubRequest);
......
package org.genesys2.server.service.impl;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class EMailServiceTest {
EMailServiceImpl emailService = new EMailServiceImpl();
@Test
public void testToEmailsSplit() {
String[] res = null;
res = emailService.toEmails("");
assertTrue(res == null);
res = emailService.toEmails("test1");
assertTrue(res != null);
assertTrue(res.length == 1);
assertTrue("test1".equals(res[0]));
res = emailService.toEmails("test1,test2");
assertTrue(res != null);
assertTrue(res.length == 2);
assertTrue("test1".equals(res[0]));
assertTrue("test2".equals(res[1]));
res = emailService.toEmails("test1;test2");
assertTrue(res != null);
assertTrue(res.length == 2);
assertTrue("test1".equals(res[0]));
assertTrue("test2".equals(res[1]));
res = emailService.toEmails("test1 ; test2,test3 ");
assertTrue(res != null);
assertTrue(res.length == 3);
assertTrue("test1".equals(res[0]));
assertTrue("test2".equals(res[1]));
assertTrue("test3".equals(res[2]));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment