optional attachment implemented

pull/12/head
parent 8b293a9f10
commit 44e23a6d93

@ -26,7 +26,6 @@ public class MailController {
private static final Logger logger = LoggerFactory.getLogger(MailController.class); private static final Logger logger = LoggerFactory.getLogger(MailController.class);
@PostMapping @PostMapping
public ResponseEntity<String> sendEmail(@ModelAttribute MailModel emailModel) { public ResponseEntity<String> sendEmail(@ModelAttribute MailModel emailModel) {
String msg; String msg;

@ -2,11 +2,16 @@ package entgra.mailsender.DAO.Impl;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import entgra.mailsender.Controller.MailController;
import entgra.mailsender.DAO.MailDAO; import entgra.mailsender.DAO.MailDAO;
import entgra.mailsender.DTO.MailModel; import entgra.mailsender.DTO.MailModel;
import entgra.mailsender.exception.DatabaseAccessException; import entgra.mailsender.exception.DatabaseAccessException;
import entgra.mailsender.exception.FileConversionException; import entgra.mailsender.exception.FileConversionException;
import entgra.mailsender.exception.MailProcessingException; import entgra.mailsender.exception.MailProcessingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -14,20 +19,19 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.sql.*; import java.sql.*;//remove the star
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Logger;
@Component @Component
public class MailDAOImpl implements MailDAO { public class MailDAOImpl implements MailDAO {
Logger logger = Logger.getLogger(String.valueOf(MailDAOImpl.class)); private static final Logger logger = LoggerFactory.getLogger(MailDAOImpl.class);
private DataSource dataSource; private DataSource dataSource;
public MailDAOImpl(DataSource dataSource){ public MailDAOImpl(DataSource dataSource) {
this.dataSource = dataSource; this.dataSource = dataSource;
} }
@ -43,11 +47,22 @@ public class MailDAOImpl implements MailDAO {
stmt.setString(1, mailModel.getEmailAddress()); stmt.setString(1, mailModel.getEmailAddress());
stmt.setString(2, mailModel.getMsgTemplate()); stmt.setString(2, mailModel.getMsgTemplate());
stmt.setInt(3, mailModel.getPriority()); stmt.setInt(3, mailModel.getPriority());
stmt.setBytes(4, mailModel.getAttachment().getBytes());
// Set attachment only if it is present
if (mailModel.getAttachment() != null) {
stmt.setBytes(4, mailModel.getAttachment().getBytes());
} else {
stmt.setNull(4, Types.BLOB); // Assuming attachment column is of type BLOB
}
stmt.setString(5, parametersJson); stmt.setString(5, parametersJson);
stmt.setTimestamp(6, current_time); stmt.setTimestamp(6, current_time);
String filename = mailModel.getAttachment().getOriginalFilename(); if (mailModel.getAttachment()!=null) {
stmt.setString(7, filename); String filename = mailModel.getAttachment().getOriginalFilename();
stmt.setString(7, filename);
} else {
stmt.setNull(7,Types.VARCHAR);
}
stmt.setDate(8, mailModel.getExpiry_at()); stmt.setDate(8, mailModel.getExpiry_at());
stmt.executeUpdate(); stmt.executeUpdate();
ResultSet rs = stmt.getGeneratedKeys(); ResultSet rs = stmt.getGeneratedKeys();
@ -69,7 +84,7 @@ public class MailDAOImpl implements MailDAO {
@Override @Override
public List<MailModel> getUnsentMessages() { public List<MailModel> getUnsentMessages() {
String sql = "SELECT * FROM EMAIL WHERE EMAIL_ID NOT IN (SELECT EMAIL_ID FROM SENTEMAIL)"; String sql = "SELECT * FROM EMAIL WHERE EMAIL_ID NOT IN (SELECT EMAIL_ID FROM SENTEMAIL)";//use expiry date and use left join for get the id
List<MailModel> unsentMails = new ArrayList<>(); List<MailModel> unsentMails = new ArrayList<>();
@ -111,12 +126,14 @@ public class MailDAOImpl implements MailDAO {
List<MailModel.Parameter> parameters = parseJsonParameters(jsonData); List<MailModel.Parameter> parameters = parseJsonParameters(jsonData);
mailModel.setParameters(parameters); mailModel.setParameters(parameters);
String filename = rs.getString("FILENAME"); if (rs.getString("FILENAME") != null) {
mailModel.setFilename(filename); String filename = rs.getString("FILENAME");
mailModel.setFilename(filename);
Blob blob = rs.getBlob("ATTACHMENT"); Blob blob = rs.getBlob("ATTACHMENT");
File file = convertBlobToFile(blob, filename); File file = convertBlobToFile(blob, filename);
mailModel.setFile(file); mailModel.setFile(file);
}
} }
return mailModel; return mailModel;
} catch (IOException e) { } catch (IOException e) {

@ -1,10 +1,13 @@
package entgra.mailsender.Service.Impl; package entgra.mailsender.Service.Impl;
import entgra.mailsender.Controller.MailController;
import entgra.mailsender.DAO.MailDAO; import entgra.mailsender.DAO.MailDAO;
import entgra.mailsender.DTO.MailModel; import entgra.mailsender.DTO.MailModel;
import entgra.mailsender.Service.MailQueueService; import entgra.mailsender.Service.MailQueueService;
import entgra.mailsender.util.PriorityQueueHolder; import entgra.mailsender.util.PriorityQueueHolder;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -12,12 +15,11 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Logger;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class MailQueueServiceImpl implements MailQueueService { public class MailQueueServiceImpl implements MailQueueService {
Logger logger = Logger.getLogger(String.valueOf(MailQueueServiceImpl.class)); private static final Logger logger = LoggerFactory.getLogger(MailQueueServiceImpl.class);
@Autowired @Autowired
MailDAO mailDAO; MailDAO mailDAO;

@ -1,5 +1,6 @@
package entgra.mailsender.Service.Impl; package entgra.mailsender.Service.Impl;
import entgra.mailsender.Controller.MailController;
import entgra.mailsender.DAO.MailDAO; import entgra.mailsender.DAO.MailDAO;
import entgra.mailsender.DTO.MailModel; import entgra.mailsender.DTO.MailModel;
import entgra.mailsender.Service.MailQueueService; import entgra.mailsender.Service.MailQueueService;
@ -9,6 +10,8 @@ import entgra.mailsender.exception.MailException;
import entgra.mailsender.exception.QueueException; import entgra.mailsender.exception.QueueException;
import entgra.mailsender.util.PriorityQueueHolder; import entgra.mailsender.util.PriorityQueueHolder;
import jakarta.annotation.PreDestroy; import jakarta.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
@ -17,12 +20,11 @@ import org.springframework.stereotype.Service;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Logger;
@Service @Service
public class MailServiceImpl implements MailService { public class MailServiceImpl implements MailService {
Logger logger = Logger.getLogger(String.valueOf(MailServiceImpl.class)); private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
private volatile boolean shutdownRequested = false; private volatile boolean shutdownRequested = false;
@Autowired @Autowired
@ -43,20 +45,29 @@ public class MailServiceImpl implements MailService {
public void sendEmail(MailModel emailModel) throws MailException, SQLException { public void sendEmail(MailModel emailModel) throws MailException, SQLException {
if (shutdownRequested) { if (shutdownRequested) {
String msg = "Server is shutting down. SMS requests will not be accepted."; String msg = "Server is shutting down. SMS requests will not be accepted.";
logger.warning(msg); logger.info(msg);
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
if (isValidEmailAddress(emailModel.getEmailAddress())) { if (isValidEmailAddress(emailModel.getEmailAddress())) {
String msg = "Invalid Email address"; String msg = "Invalid Email address";
logger.warning(msg); logger.warn(msg);
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
if (emailModel.getMsgTemplate().isBlank()) {
String msg = "Empty message template";
logger.warn(msg);
throw new BadRequestException(msg);
}
int emailid = mailDAO.addMail(emailModel); // save the mail details in the database and get the auto generated id int emailid = mailDAO.addMail(emailModel); // save the mail details in the database and get the auto generated id
emailModel.setEmailId(emailid); emailModel.setEmailId(emailid); //uuid
emailModel.setFilename(emailModel.getAttachment().getOriginalFilename()); if (emailModel.getAttachment() != null) {
emailModel.setFilename(emailModel.getAttachment().getOriginalFilename());
}
//add to the priority queue //add to the priority queue
List<MailModel> mailModels = new ArrayList<>(); List<MailModel> mailModels = new ArrayList<>();
mailModels.add(emailModel); mailModels.add(emailModel);
@ -76,7 +87,7 @@ public class MailServiceImpl implements MailService {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setTo(prioritizedMail.getEmailAddress()); mimeMessageHelper.setTo(prioritizedMail.getEmailAddress());
mimeMessageHelper.setSubject("Bill details for March "); mimeMessageHelper.setSubject("Bill details for March ");//input as a param key
StringBuilder emailBody = new StringBuilder(); StringBuilder emailBody = new StringBuilder();
emailBody.append(prioritizedMail.getMsgTemplate()).append("\n"); emailBody.append(prioritizedMail.getMsgTemplate()).append("\n");
@ -92,7 +103,9 @@ public class MailServiceImpl implements MailService {
mimeMessageHelper.setText(emailBody.toString(), false); mimeMessageHelper.setText(emailBody.toString(), false);
// Append attachments to email body // Append attachments to email body
mimeMessageHelper.addAttachment(prioritizedMail.getFilename(), prioritizedMail.getAttachment()); if (prioritizedMail.getAttachment() != null) {
mimeMessageHelper.addAttachment(prioritizedMail.getFilename(), prioritizedMail.getAttachment());
}
//Store the sent mail to the table //Store the sent mail to the table
mailDAO.addToSentMail(prioritizedMail.getEmailId()); mailDAO.addToSentMail(prioritizedMail.getEmailId());
@ -133,11 +146,6 @@ public class MailServiceImpl implements MailService {
// Append parameters to email body // Append parameters to email body
List<MailModel.Parameter> parameters = prioritizedMail.getParameters(); List<MailModel.Parameter> parameters = prioritizedMail.getParameters();
for (MailModel.Parameter parameter : parameters) {
System.out.println("Parameter name: " + parameter.getKey());
System.out.println("Parameter value: " + parameter.getValue());
}
for (MailModel.Parameter parameter : parameters) { for (MailModel.Parameter parameter : parameters) {
emailBody.append("\n").append(parameter.getKey()).append(": ").append(parameter.getValue()); emailBody.append("\n").append(parameter.getKey()).append(": ").append(parameter.getValue());
} }
@ -146,7 +154,10 @@ public class MailServiceImpl implements MailService {
mimeMessageHelper.setText(emailBody.toString(), false); mimeMessageHelper.setText(emailBody.toString(), false);
// Append attachments to email body // Append attachments to email body
mimeMessageHelper.addAttachment(prioritizedMail.getFilename(), prioritizedMail.getFile()); if (prioritizedMail.getAttachment() != null) {
mimeMessageHelper.addAttachment(prioritizedMail.getFilename(), prioritizedMail.getFile());
}
//Store the sent mail to the table //Store the sent mail to the table
mailDAO.addToSentMail(prioritizedMail.getEmailId()); mailDAO.addToSentMail(prioritizedMail.getEmailId());

Loading…
Cancel
Save