Skip to content
Snippets Groups Projects

Thymeleaf

Merged Michal Badin requested to merge mailing into develop
2 files
+ 70
5
Compare changes
  • Side-by-side
  • Inline
Files
2
package cz.muni.pa165.service;
import cz.muni.pa165.exceptions.EmailException;
import cz.muni.pa165.generated.model.*;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Service
public class NotificationService {
private static final String EMAIL = "formula.team.management@gmail.com";
private static final Logger log = LoggerFactory.getLogger(NotificationService.class);
private final JavaMailSender emailSender;
private final TemplateEngine templateEngine;
@Autowired
public NotificationService(JavaMailSender emailSender) {
public NotificationService(JavaMailSender emailSender, TemplateEngine templateEngine) {
this.emailSender = emailSender;
this.templateEngine = templateEngine;
}
public ConfirmationDto notify(NotificationDto notificationDto) {
@@ -25,80 +38,73 @@ public class NotificationService {
public ConfirmationDto notifyNewComponent(CarComponentNotificationDto carComponentNotificationDto) {
var c = carComponentNotificationDto.getCarComponent();
var subject = "New component: " + c.getInformation();
var text = "New component was added to Formula team management core.\n"
+ "\nType: " + c.getComponentType()
+ "\nInformation: " + c.getInformation()
+ "\nWeight: " + c.getWeight();
return sendEmail(subject, text, carComponentNotificationDto.getReceivers());
Context ctx = new Context();
ctx.setVariable("id", c.getId());
ctx.setVariable("type", c.getComponentType());
ctx.setVariable("info", c.getInformation());
ctx.setVariable("weight", c.getWeight());
var htmlContent = templateEngine.process("new-component-email", ctx);
return sendEmail(subject, htmlContent, carComponentNotificationDto.getReceivers());
}
public ConfirmationDto notifyNewApplication(ApplicationNotificationDto applicationNotificationDto) {
var a = applicationNotificationDto.getApplication();
var subject = "New application: " + getFullName(a);
var text = "New application was received.\n"
+ "\nId: " + a.getId()
+ "\nName: " + a.getName()
+ "\nSurname: " + a.getSurname()
+ "\nBirthday: " + a.getBirthday()
+ "\nEmail: " + a.getEmail()
+ "\nFilling out date: " + a.getFillingOutDate();
Context ctx = new Context();
ctx.setVariable("id", a.getId());
ctx.setVariable("name", a.getName());
ctx.setVariable("surname", a.getSurname());
ctx.setVariable("birthday", a.getBirthday().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
ctx.setVariable("email", a.getEmail());
ctx.setVariable("fillingOutDate", a.getFillingOutDate().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
return sendEmail(subject, text, applicationNotificationDto.getReceivers());
var htmlContent = templateEngine.process("new-application-email", ctx);
return sendEmail(subject, htmlContent, applicationNotificationDto.getReceivers());
}
public ConfirmationDto notifyApplicationStatusChange(ApplicationNotificationDto applicationNotificationDto) {
var a = applicationNotificationDto.getApplication();
var subject = "Application for Formula Driver Position";
var text = "";
var htmlContent = "";
Context ctx = new Context();
ctx.setVariable("fullName", getFullName(a));
ctx.setVariable("today", LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
if (a.getStatus() == ApplicationStatus.ACCEPTED) {
text = "Dear " + getFullName(a) + ",\n" +
"\n" +
"I am pleased to inform you that your application for the position of Formula Driver has been accepted. We are excited to have you join our team and look forward to working with you.\n" +
"\n" +
"Your experience, skills, and passion for racing make you an excellent fit for our team. We believe that you will be a valuable asset to our organization and we are eager to see what you can accomplish on the track.\n" +
"\n" +
"As a next step, we will contact you shortly to discuss the details of your employment contract and to schedule your training and orientation. In the meantime, please do not hesitate to contact us if you have any questions or concerns.\n" +
"\n" +
"Thank you for your interest in our team and for taking the time to apply. We look forward to a long and successful partnership with you.\n" +
"\n" +
"Best regards,\n" +
"\n" +
"Formula Team Management";
htmlContent = templateEngine.process("accepted-email", ctx);
} else {
text = "Dear " + getFullName(a) + ",\n" +
"\n" +
"I regret to inform you that your application for the Formula Driver position has been declined. While we appreciate your interest in the role, we have decided to pursue other candidates whose skills and experience more closely match our requirements.\n" +
"\n" +
"I want to personally thank you for taking the time to apply for the position and for your passion for motorsports. We would like to keep your resume on file in case any suitable openings arise in the future.\n" +
"\n" +
"If you have any questions or would like feedback on your application, please feel free to reach out to us.\n" +
"\n" +
"Thank you again for considering Formula Team Management, and we wish you all the best in your future endeavors.\n" +
"\n" +
"Best regards,\n" +
"\n" +
"Formula Team Management";
htmlContent = templateEngine.process("rejected-email", ctx);
}
return sendEmail(subject, text, applicationNotificationDto.getReceivers());
return sendEmail(subject, htmlContent, applicationNotificationDto.getReceivers());
}
private ConfirmationDto sendEmail(String subject, String content, List<String> receivers) {
var message = new SimpleMailMessage();
message.setFrom(EMAIL);
message.setTo(receivers.toArray(String[]::new));
message.setSubject(subject);
message.setText(content);
emailSender.send(message);
return new ConfirmationDto()
.sender(EMAIL)
.receivers(receivers)
.subject(subject)
.message(content);
private ConfirmationDto sendEmail(String subject, String text, List<String> receivers) {
MimeMessage message = emailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true, StandardCharsets.UTF_8.name());
helper.setFrom(EMAIL);
helper.setTo(receivers.toArray(String[]::new));
helper.setSubject(subject);
helper.setText(text, true);
emailSender.send(message);
return new ConfirmationDto()
.sender(EMAIL)
.receivers(receivers)
.subject(subject)
.message(text);
} catch (MessagingException e) {
throw new EmailException("Failed to send email", e);
}
}
private String getFullName(ApplicationDto applicationDto) {
Loading