-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
211 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/uk/ac/ebi/eva/submission/config/AppConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/uk/ac/ebi/eva/submission/util/EmailNotificationHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
.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(); | ||
|
||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/uk/ac/ebi/eva/submission/util/HTMLHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/uk/ac/ebi/eva/submission/util/MailSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |