pull/13/head
parent 44e23a6d93
commit 954e90b8ac

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 854 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 297 KiB

Binary file not shown.

@ -2,14 +2,11 @@ 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -19,7 +16,15 @@ 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.*;//remove the star import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.Timestamp;
import java.sql.Types;
import java.sql.ResultSet;
import java.sql.Blob;
import java.sql.Date;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -39,9 +44,8 @@ public class MailDAOImpl implements MailDAO {
public int addMail(MailModel mailModel) throws SQLException { public int addMail(MailModel mailModel) throws SQLException {
try { try {
PreparedStatement stmt; PreparedStatement stmt;
int generatedId;
Connection conn = dataSource.getConnection(); Connection conn = dataSource.getConnection();
stmt = conn.prepareStatement("INSERT INTO EMAIL (EMAIL_ADDRESS, MSG_TEMPLATE, PRIORITY, ATTACHMENT, PARAMETERS, TIME, FILENAME, EXPIRY_AT) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", PreparedStatement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement("INSERT INTO EMAIL (EMAIL_ADDRESS, MSG_TEMPLATE, PRIORITY, ATTACHMENT, PARAMETERS, TIME, FILENAME, EXPIRY_AT,UUID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", PreparedStatement.RETURN_GENERATED_KEYS);
Timestamp current_time = new Timestamp(System.currentTimeMillis()); Timestamp current_time = new Timestamp(System.currentTimeMillis());
String parametersJson = new ObjectMapper().writeValueAsString(mailModel.getParameters()); String parametersJson = new ObjectMapper().writeValueAsString(mailModel.getParameters());
stmt.setString(1, mailModel.getEmailAddress()); stmt.setString(1, mailModel.getEmailAddress());
@ -57,19 +61,16 @@ public class MailDAOImpl implements MailDAO {
stmt.setString(5, parametersJson); stmt.setString(5, parametersJson);
stmt.setTimestamp(6, current_time); stmt.setTimestamp(6, current_time);
if (mailModel.getAttachment()!=null) { if (mailModel.getAttachment() != null) {
String filename = mailModel.getAttachment().getOriginalFilename(); String filename = mailModel.getAttachment().getOriginalFilename();
stmt.setString(7, filename); stmt.setString(7, filename);
} else { } else {
stmt.setNull(7,Types.VARCHAR); stmt.setNull(7, Types.VARCHAR);
} }
stmt.setDate(8, mailModel.getExpiry_at()); stmt.setDate(8, mailModel.getExpiry_at());
stmt.setString(9, mailModel.getUuId());
stmt.executeUpdate(); stmt.executeUpdate();
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
generatedId = rs.getInt(1);
return generatedId;
}
} catch (SQLException e) { } catch (SQLException e) {
logger.info(e.getMessage()); logger.info(e.getMessage());
throw new SQLException("error processing sql !!", e); throw new SQLException("error processing sql !!", e);
@ -84,7 +85,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)";//use expiry date and use left join for get the id String sql = "SELECT * FROM EMAIL WHERE UUID NOT IN (SELECT UUID FROM SENTEMAIL)";//use expiry date and use left join for get the id
List<MailModel> unsentMails = new ArrayList<>(); List<MailModel> unsentMails = new ArrayList<>();
@ -93,7 +94,7 @@ public class MailDAOImpl implements MailDAO {
ResultSet rs = statement.executeQuery()) { ResultSet rs = statement.executeQuery()) {
while (rs.next()) { while (rs.next()) {
MailModel mailModel = new MailModel(); MailModel mailModel = new MailModel();
mailModel.setEmailId(rs.getInt("EMAIL_ID")); mailModel.setUuId(rs.getString("UUID"));
mailModel.setPriority(rs.getInt("PRIORITY")); mailModel.setPriority(rs.getInt("PRIORITY"));
mailModel.setExpiry_at(rs.getDate("EXPIRY_AT")); mailModel.setExpiry_at(rs.getDate("EXPIRY_AT"));
unsentMails.add(mailModel); unsentMails.add(mailModel);
@ -102,19 +103,20 @@ public class MailDAOImpl implements MailDAO {
logger.info(e.getMessage()); logger.info(e.getMessage());
throw new DatabaseAccessException("Error accessing database", e); throw new DatabaseAccessException("Error accessing database", e);
} }
logger.info(unsentMails.toString());
return unsentMails; return unsentMails;
} }
@Override @Override
public MailModel getMailDetails(Integer mail_id) { public MailModel getMailDetails(String uuId) {
String sql = "SELECT * FROM EMAIL WHERE EMAIL_ID = ?"; String sql = "SELECT * FROM EMAIL WHERE UUID = ?";
MailModel mailModel = new MailModel(); MailModel mailModel = new MailModel();
try { try {
Connection conn = dataSource.getConnection(); Connection conn = dataSource.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, mail_id); stmt.setString(1, uuId);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) { while (rs.next()) {
mailModel.setEmailId(rs.getInt("EMAIL_ID")); mailModel.setEmailId(rs.getInt("EMAIL_ID"));
@ -173,12 +175,12 @@ public class MailDAOImpl implements MailDAO {
return file; return file;
} }
public void addToSentMail(Integer mail_id) { public void addToSentMail(String uuId) {
try { try {
PreparedStatement stmt; PreparedStatement stmt;
Connection connection = dataSource.getConnection(); Connection connection = dataSource.getConnection();
stmt = connection.prepareStatement("INSERT INTO SENTEMAIL (EMAIL_ID,SENT_TIME) VALUE (?,?)"); stmt = connection.prepareStatement("INSERT INTO SENTEMAIL (UUID,SENT_TIME) VALUE (?,?)");
stmt.setInt(1, mail_id); stmt.setString(1, uuId);
stmt.setDate(2, Date.valueOf(LocalDate.now())); stmt.setDate(2, Date.valueOf(LocalDate.now()));
stmt.execute(); stmt.execute();
} catch (SQLException e) { } catch (SQLException e) {

@ -9,9 +9,9 @@ import java.util.List;
public interface MailDAO { public interface MailDAO {
int addMail(MailModel mailModel) throws SQLException; int addMail(MailModel mailModel) throws SQLException;
MailModel getMailDetails(Integer mail_id); MailModel getMailDetails(String uuId);
void addToSentMail(Integer mail_id); void addToSentMail(String uuId);
List<MailModel> getUnsentMessages(); List<MailModel> getUnsentMessages();

@ -8,6 +8,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import java.util.PrimitiveIterator;
@Getter @Getter
@Setter @Setter
@ -26,6 +27,7 @@ public class MailModel {
private String filename; private String filename;
private java.sql.Date expiry_at; private java.sql.Date expiry_at;
private long insertionOrder; private long insertionOrder;
private String uuId;

@ -48,7 +48,6 @@ public class MailQueueServiceImpl implements MailQueueService {
if (PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null){ if (PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null){
MailModel mailModel = priorityQueue.poll(); MailModel mailModel = priorityQueue.poll();
uniqueMails.remove(mailModel); uniqueMails.remove(mailModel);
//logger.info(String.valueOf(mailModel.getEmailId()));
return mailModel; return mailModel;
} }
return null; return null;

@ -1,6 +1,5 @@
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;
@ -20,6 +19,7 @@ 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.UUID;
@Service @Service
@ -44,7 +44,7 @@ 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. Mail requests will not be accepted.";
logger.info(msg); logger.info(msg);
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
@ -61,13 +61,15 @@ public class MailServiceImpl implements MailService {
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
//generating the uuid
emailModel.setUuId(String.valueOf(UUID.randomUUID()));
int emailid = mailDAO.addMail(emailModel); // save the mail details in the database and get the auto generated id mailDAO.addMail(emailModel); // save the mail details in the database and get the auto generated id
emailModel.setEmailId(emailid); //uuid
if (emailModel.getAttachment() != null) { if (emailModel.getAttachment() != null) {
emailModel.setFilename(emailModel.getAttachment().getOriginalFilename()); 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);
@ -87,7 +89,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 ");//input as a param key mimeMessageHelper.setSubject("Bill details for March ");
StringBuilder emailBody = new StringBuilder(); StringBuilder emailBody = new StringBuilder();
emailBody.append(prioritizedMail.getMsgTemplate()).append("\n"); emailBody.append(prioritizedMail.getMsgTemplate()).append("\n");
@ -108,7 +110,7 @@ public class MailServiceImpl implements MailService {
} }
//Store the sent mail to the table //Store the sent mail to the table
mailDAO.addToSentMail(prioritizedMail.getEmailId()); mailDAO.addToSentMail(prioritizedMail.getUuId());
}); });
} }
@ -133,7 +135,8 @@ public class MailServiceImpl implements MailService {
} }
while (PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) { while (PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) {
MailModel prioritizedMail = mailDAO.getMailDetails(mailQueueService.getHighPriorityMail().getEmailId()); MailModel prioritizedMail = mailDAO.getMailDetails(mailQueueService.getHighPriorityMail().getUuId());
logger.info("uuid : " ,prioritizedMail.getUuId());
javaMailSender.send(mimeMessage -> { javaMailSender.send(mimeMessage -> {
@ -159,8 +162,10 @@ public class MailServiceImpl implements MailService {
} }
logger.info(prioritizedMail.getUuId());
//Store the sent mail to the table //Store the sent mail to the table
mailDAO.addToSentMail(prioritizedMail.getEmailId()); mailDAO.addToSentMail(prioritizedMail.getUuId());
} }

Loading…
Cancel
Save