-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EVA-3529 Notify submission update to user through email #10
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package uk.ac.ebi.eva.submission.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.JavaMailSenderImpl; | ||
|
||
@Configuration | ||
public class AppConfig { | ||
|
||
@Bean | ||
public JavaMailSender javaMailService() { | ||
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); | ||
javaMailSender.setHost("smtp.ebi.ac.uk"); | ||
javaMailSender.setPort(25); | ||
return javaMailSender; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,16 @@ | |
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import uk.ac.ebi.eva.submission.exception.SubmissionDoesNotExistException; | ||
import uk.ac.ebi.eva.submission.entity.Submission; | ||
import uk.ac.ebi.eva.submission.entity.SubmissionAccount; | ||
import uk.ac.ebi.eva.submission.exception.SubmissionDoesNotExistException; | ||
import uk.ac.ebi.eva.submission.model.SubmissionStatus; | ||
import uk.ac.ebi.eva.submission.repository.SubmissionAccountRepository; | ||
import uk.ac.ebi.eva.submission.repository.SubmissionDetailsRepository; | ||
import uk.ac.ebi.eva.submission.repository.SubmissionRepository; | ||
import uk.ac.ebi.eva.submission.util.EmailNotificationHelper; | ||
import uk.ac.ebi.eva.submission.util.HTMLHelper; | ||
import uk.ac.ebi.eva.submission.util.MailSender; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
@@ -25,17 +28,24 @@ public class SubmissionService { | |
|
||
private final GlobusDirectoryProvisioner globusDirectoryProvisioner; | ||
|
||
private final MailSender mailSender; | ||
|
||
@Value("${globus.uploadHttpDomain}") | ||
private String uploadHttpDomain; | ||
|
||
private EmailNotificationHelper emailHelper; | ||
|
||
public SubmissionService(SubmissionRepository submissionRepository, | ||
SubmissionAccountRepository submissionAccountRepository, | ||
SubmissionDetailsRepository submissionDetailsRepository, | ||
GlobusDirectoryProvisioner globusDirectoryProvisioner) { | ||
GlobusDirectoryProvisioner globusDirectoryProvisioner, | ||
MailSender mailSender, EmailNotificationHelper emailHelper) { | ||
this.submissionRepository = submissionRepository; | ||
this.submissionAccountRepository = submissionAccountRepository; | ||
this.submissionDetailsRepository = submissionDetailsRepository; | ||
this.globusDirectoryProvisioner = globusDirectoryProvisioner; | ||
this.mailSender = mailSender; | ||
this.emailHelper = emailHelper; | ||
} | ||
|
||
public Submission initiateSubmission(SubmissionAccount submissionAccount) { | ||
|
@@ -45,7 +55,7 @@ public Submission initiateSubmission(SubmissionAccount submissionAccount) { | |
|
||
Optional<SubmissionAccount> optSubmissionAccount = submissionAccountRepository.findById(submissionAccount.getId()); | ||
// if the user account is not present or if its primary email has changed, save/update the user account | ||
if (!optSubmissionAccount.isPresent() || optSubmissionAccount.get().getPrimaryEmail() != submissionAccount.getPrimaryEmail()) { | ||
if (!optSubmissionAccount.isPresent() || !optSubmissionAccount.get().getPrimaryEmail().equals(submissionAccount.getPrimaryEmail())) { | ||
submissionAccountRepository.save(submissionAccount); | ||
} | ||
|
||
|
@@ -85,9 +95,17 @@ public boolean checkUserHasAccessToSubmission(SubmissionAccount account, String | |
Optional<Submission> optSubmission = submissionRepository.findById(submissionId); | ||
if (optSubmission.isPresent()) { | ||
SubmissionAccount submissionAccount = optSubmission.get().getSubmissionAccount(); | ||
return submissionAccount.getId() == account.getId(); | ||
return submissionAccount.getId().equals(account.getId()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this fixes EVA-3536 as well 👍 |
||
} else { | ||
throw new SubmissionDoesNotExistException("Given submission with id " + submissionId + " does not exist"); | ||
} | ||
} | ||
|
||
public void sendMailNotificationForStatusUpdate(SubmissionAccount submissionAccount, String submissionId, | ||
SubmissionStatus submissionStatus, Boolean success) { | ||
String sendTo = submissionAccount.getPrimaryEmail(); | ||
String subject = emailHelper.getSubjectForSubmissionStatusUpdate(submissionStatus, success); | ||
String body = emailHelper.getTextForSubmissionStatusUpdate(submissionAccount, submissionId, submissionStatus, success); | ||
mailSender.sendEmail(sendTo, subject, body); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,58 @@ | ||||||
package uk.ac.ebi.eva.submission.util; | ||||||
|
||||||
import org.springframework.stereotype.Component; | ||||||
import uk.ac.ebi.eva.submission.entity.SubmissionAccount; | ||||||
import uk.ac.ebi.eva.submission.model.SubmissionStatus; | ||||||
|
||||||
@Component | ||||||
public class EmailNotificationHelper { | ||||||
private static final String EVA_HELPDESK_EMAIL = "[email protected]"; | ||||||
|
||||||
public String getSubjectForSubmissionStatusUpdate(SubmissionStatus submissionStatus, Boolean success) { | ||||||
String result = (success == Boolean.TRUE) ? "SUCCESS" : "FAILED"; | ||||||
return String.format("EVA Submission Update: %s %s", submissionStatus, result); | ||||||
} | ||||||
|
||||||
public String getTextForSubmissionStatusUpdate(SubmissionAccount submissionAccount, String submissionId, | ||||||
SubmissionStatus submissionStatus, Boolean success) { | ||||||
String result; | ||||||
String resultColor; | ||||||
if (success) { | ||||||
result = "SUCCESS"; | ||||||
resultColor = "green"; | ||||||
} else { | ||||||
result = "FAILED"; | ||||||
resultColor = "red"; | ||||||
} | ||||||
|
||||||
String notificationText = new HTMLHelper() | ||||||
.addText("Dear " + submissionAccount.getFirstName() + ",") | ||||||
.addGap(1) | ||||||
.addText("Here is the update for your submission: ") | ||||||
.addGap(1) | ||||||
.addText("submission id: " + submissionId) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||
.addLineBreak() | ||||||
.addText("Submission Status: " + submissionStatus) | ||||||
.addLineBreak() | ||||||
.addText("Result: ") | ||||||
.addBoldTextWithColor(result, resultColor) | ||||||
.addGap(2) | ||||||
.build(); | ||||||
|
||||||
notificationText += getNotificationFooter(); | ||||||
|
||||||
return notificationText; | ||||||
} | ||||||
|
||||||
public String getNotificationFooter() { | ||||||
return new HTMLHelper() | ||||||
.addTextWithSize("Please don't reply to this email.", 10) | ||||||
.addLineBreak() | ||||||
.addTextWithSize("For any issues/support please contact us at ", 10) | ||||||
.addEmailLinkWithSize(EVA_HELPDESK_EMAIL, EVA_HELPDESK_EMAIL, 10) | ||||||
.addLineBreak() | ||||||
.addTextWithSize("European Variation Archive: EMBL-EBI", 10) | ||||||
.build(); | ||||||
|
||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package uk.ac.ebi.eva.submission.util; | ||
|
||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class HTMLHelper { | ||
private final StringBuilder htmlBuilder; | ||
|
||
public HTMLHelper() { | ||
htmlBuilder = new StringBuilder(); | ||
} | ||
|
||
public HTMLHelper addLineBreak() { | ||
htmlBuilder.append("<br />"); | ||
return this; | ||
} | ||
|
||
public HTMLHelper addGap(int count) { | ||
htmlBuilder.append(IntStream.range(0, count+1).boxed().map(i -> "<br />").collect(Collectors.joining(""))); | ||
return this; | ||
} | ||
|
||
public HTMLHelper addText(String text) { | ||
htmlBuilder.append(text); | ||
return this; | ||
} | ||
|
||
public HTMLHelper addTextWithSize(String text, int size) { | ||
htmlBuilder.append("<span style=\"font-size:" + size + "px;\">" + text + "</span>"); | ||
return this; | ||
} | ||
|
||
public HTMLHelper addTextWithColor(String text, String color) { | ||
htmlBuilder.append("<span style=\"color:" + color + ";\">" + text + "</span>"); | ||
return this; | ||
} | ||
|
||
public HTMLHelper addBoldText(String text) { | ||
htmlBuilder.append("<b>" + text + "</b>"); | ||
return this; | ||
} | ||
|
||
public HTMLHelper addLink(String url, String text) { | ||
htmlBuilder.append("<a href=\"" + url + "\">" + text + "</a>"); | ||
return this; | ||
} | ||
|
||
public HTMLHelper addEmailLink(String email, String text) { | ||
htmlBuilder.append("<a href=\"mailto:" + email + "\">" + text + "</a>"); | ||
return this; | ||
} | ||
|
||
public HTMLHelper addEmailLinkWithSize(String email, String text, int size) { | ||
htmlBuilder.append("<span style=\"font-size:" + size + "px;\"> <a href=\"mailto:" + email + "\">" + text + "</a> </span>"); | ||
return this; | ||
} | ||
|
||
public HTMLHelper addBoldTextWithColor(String text, String color) { | ||
htmlBuilder.append("<b><span style=\"color:" + color + ";\">" + text + "</span></b>"); | ||
return this; | ||
} | ||
|
||
public String build() { | ||
return htmlBuilder.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package uk.ac.ebi.eva.submission.util; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.MimeMessageHelper; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.mail.MessagingException; | ||
import javax.mail.internet.MimeMessage; | ||
|
||
@Component | ||
public class MailSender { | ||
private final Logger logger = LoggerFactory.getLogger(MailSender.class); | ||
private final JavaMailSender javaMailSender; | ||
private final String DEFAULT_SENDER = "[email protected]"; | ||
|
||
@Autowired | ||
MailSender(JavaMailSender javaMailSender) { | ||
this.javaMailSender = javaMailSender; | ||
} | ||
|
||
public void sendEmail(String to, String subject, String body) { | ||
sendEmail(DEFAULT_SENDER, to, subject, body); | ||
} | ||
|
||
public void sendEmail(String from, String to, String subject, String body) { | ||
MimeMessage message = javaMailSender.createMimeMessage(); | ||
MimeMessageHelper helper = new MimeMessageHelper(message); | ||
try { | ||
helper.setFrom(from); | ||
helper.setTo(to); | ||
helper.setSubject(subject); | ||
helper.setText(body, true); | ||
javaMailSender.send(message); | ||
} catch (MessagingException e) { | ||
logger.error("Error sending mail: " + e); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, is there a reason to use a
Boolean
object here instead of a primitive?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really and frankly not needed. Have changed to primitive to be consistent