Commit 09d80345 authored by Prayas Jain's avatar Prayas Jain

Added Work Anniversary Service and backend code

parent 39bd924f
package com.nisum.myteam.schedular;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import javax.mail.MessagingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.nisum.myteam.exception.handler.MyTeamException;
import com.nisum.myteam.model.Mail;
import com.nisum.myteam.model.dao.Employee;
import com.nisum.myteam.service.IEmployeeService;
import com.nisum.myteam.service.IMailService;
import com.nisum.myteam.utils.MyTeamDateUtils;
@Component
public class WorkAnniversaryScheduler {
private static final Logger logger = LoggerFactory.getLogger(LeaveNotificationScheduler.class);
@Autowired
private IMailService mailService;
@Autowired
private IEmployeeService empService;
@Autowired
private Environment environment;
@Scheduled(cron = "${email.workAnniversary.notification.cron}")
private void sendWorkAnniversaryMailToEmployees () throws MessagingException, IOException {
logger.info("Work Anniversary notification::");
Mail mail = new Mail();
mail.setFrom(environment.getProperty("email.workAnniversary.notification.from"));
mail.setSubject(environment.getProperty("email.workAnniversary.notification.subject"));
try {
List<Employee> activeEmpList = empService.getActiveEmployees();
logger.info("The active Employees count::" + activeEmpList.size());
if (activeEmpList != null && activeEmpList.size() > 0) {
LocalDate currentDate = LocalDate.now();
for (Employee activeEmployee : activeEmpList) {
LocalDate empDateOfJoining = MyTeamDateUtils.convertUtilDateToLocalDate(activeEmployee.getDateOfJoining());
logger.info("Date Of Joining::" + empDateOfJoining);
if((empDateOfJoining.getDayOfMonth() == currentDate.getDayOfMonth()) &&
(empDateOfJoining.getMonthValue() == currentDate.getMonthValue())) {
logger.info("Mail Notification is sending to:" + activeEmployee.getEmployeeName() + "::" + activeEmployee.getEmailId());
mail.setTo(activeEmployee.getEmailId());
mail.setCc(new String[]{"hrops@nisum.com"});
// mail.setTo("pjain@nisum.com");
// mail.setCc(new String[]{"vksingh@nisum.com"});
mailService.sendWorkAnniversaryNotification(mail);
}
}
}
} catch (MyTeamException e) {
e.printStackTrace();
}
}
}
......@@ -20,6 +20,8 @@ public interface IMailService {
public void sendLeaveNotification(Mail mail)throws MessagingException, IOException;
public void sendProjectNotification(Mail mail)throws MessagingException, IOException;
public void sendWorkAnniversaryNotification(Mail mail)throws MessagingException, IOException;
}
......@@ -7,8 +7,6 @@ import com.nisum.myteam.model.EmailDomain;
import com.nisum.myteam.model.Mail;
import com.nisum.myteam.service.IMailService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource;
......@@ -19,10 +17,17 @@ import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.thymeleaf.spring4.SpringTemplateEngine;
import javax.activation.DataHandler;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Date;
/**
......@@ -36,11 +41,12 @@ public class MailService implements IMailService {
@Autowired
JavaMailSender emailSender;
@Autowired
ServletContext servletContext;
@Autowired
ResourceLoader resourceLoader;
@Autowired
private SpringTemplateEngine templateEngine;
@Autowired
private Environment environment;
......@@ -140,6 +146,34 @@ public class MailService implements IMailService {
emailSender.send(message);
}
public void sendWorkAnniversaryNotification(Mail mail) throws IOException, MessagingException {
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
MimeMultipart multipart = new MimeMultipart("related");
MimeBodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<img src=\"cid:image\">";
messageBodyPart.setContent(htmlText, "text/html");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
InputStream targetStream = servletContext.getResourceAsStream("/WEB-INF/images/workAnniversaryTemp.png");
ByteArrayDataSource ds = new ByteArrayDataSource(targetStream, "image/png");
messageBodyPart.setDataHandler(new DataHandler(ds));
messageBodyPart.setHeader("Content-ID", "<image>");
messageBodyPart.setDisposition(MimeBodyPart.INLINE);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
helper.setTo(mail.getTo());
helper.setSubject(mail.getSubject());
helper.setFrom(mail.getFrom());
helper.setCc(mail.getCc());
//helper.setBcc(mail.getBcc());
emailSender.send(message);
}
}
......@@ -58,10 +58,16 @@ email.leave.notification.subject=Employee Leave Email Notification
spring.thymeleaf.view-names:thymeleaf/*
email.project.notification.template.file.path=email/projectMailTemplate.html
email.project.notification.from=mytime.nisum@gmail.com
email.project.notification.subject=Project EndDate Email Notification
email.project.notification.cron=00 00 15 * * 1-5
#email.project.notification.template.file.path=email/projectMailTemplate.html
#email.project.notification.from=mytime.nisum@gmail.com
#email.project.notification.subject=Project EndDate Email Notification
#email.project.notification.cron=00 00 15 * * 1-5
#email.workAnniversary.notification.from=mytime.nisum@gmail.com
#email.workAnniversary.notification.subject=Happy Work Anniversary
#email.workAnniversary.notification.cron=00 00 06 * * 1-7
email.holidays.list.2019=14-01-2019,21-03-2019,19-04-2019,05-06-2019,12-08-2019,15-08-2019,02-09-2019,02-10-2019,08-10-2019,25-12-2019
message=this is from development
......@@ -65,6 +65,11 @@ email.project.notification.from=mytime.nisum@gmail.com
email.project.notification.subject=Project EndDate Email Notification
email.project.notification.cron=00 00 15 * * 1-5
email.workAnniversary.notification.from=mytime.nisum@gmail.com
email.workAnniversary.notification.subject=Happy Work Anniversary
email.workAnniversary.notification.cron=00 00 06 * * 1-7
email.holidays.list.2019=14-01-2019,21-03-2019,19-04-2019,05-06-2019,12-08-2019,15-08-2019,02-09-2019,02-10-2019,08-10-2019,25-12-2019
message=this is from production
\ No newline at end of file
......@@ -59,11 +59,17 @@ email.leave.notification.shift5.cron=00 30 23 * * 1-5
spring.thymeleaf.view-names:thymeleaf/*
email.project.notification.template.file.path=email/projectMailTemplate.html
email.project.notification.from=mytime.nisum@gmail.com
email.project.notification.subject=Project EndDate Email Notification
email.project.notification.cron=00 00 15 * * 1-5
spring.profiles.active=production
email.workAnniversary.notification.from=mytime.nisum@gmail.com
email.workAnniversary.notification.subject=Happy Work Anniversary
email.workAnniversary.notification.cron=00 00 06 * * 1-7
spring.profiles.active=development
message=this is from default configuration
\ No newline at end of file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Work Anniversary Notification</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div style="margin: 0; padding: 0; text-align: center">
<img src="../../webapp/WEB-INF/images/Happy-work-anniversary.png" alt="Work Anniversary" />
</div>
</body>
</html>
\ No newline at end of file
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