|
|
|
@ -2,14 +2,11 @@ package entgra.mailsender.DAO.Impl;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import entgra.mailsender.Controller.MailController;
|
|
|
|
|
import entgra.mailsender.DAO.MailDAO;
|
|
|
|
|
import entgra.mailsender.DTO.MailModel;
|
|
|
|
|
import entgra.mailsender.exception.DatabaseAccessException;
|
|
|
|
|
import entgra.mailsender.exception.FileConversionException;
|
|
|
|
|
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;
|
|
|
|
@ -19,7 +16,15 @@ import java.io.File;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
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.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
@ -39,9 +44,8 @@ public class MailDAOImpl implements MailDAO {
|
|
|
|
|
public int addMail(MailModel mailModel) throws SQLException {
|
|
|
|
|
try {
|
|
|
|
|
PreparedStatement stmt;
|
|
|
|
|
int generatedId;
|
|
|
|
|
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());
|
|
|
|
|
String parametersJson = new ObjectMapper().writeValueAsString(mailModel.getParameters());
|
|
|
|
|
stmt.setString(1, mailModel.getEmailAddress());
|
|
|
|
@ -57,19 +61,16 @@ public class MailDAOImpl implements MailDAO {
|
|
|
|
|
|
|
|
|
|
stmt.setString(5, parametersJson);
|
|
|
|
|
stmt.setTimestamp(6, current_time);
|
|
|
|
|
if (mailModel.getAttachment()!=null) {
|
|
|
|
|
if (mailModel.getAttachment() != null) {
|
|
|
|
|
String filename = mailModel.getAttachment().getOriginalFilename();
|
|
|
|
|
stmt.setString(7, filename);
|
|
|
|
|
} else {
|
|
|
|
|
stmt.setNull(7,Types.VARCHAR);
|
|
|
|
|
stmt.setNull(7, Types.VARCHAR);
|
|
|
|
|
}
|
|
|
|
|
stmt.setDate(8, mailModel.getExpiry_at());
|
|
|
|
|
stmt.setString(9, mailModel.getUuId());
|
|
|
|
|
stmt.executeUpdate();
|
|
|
|
|
ResultSet rs = stmt.getGeneratedKeys();
|
|
|
|
|
if (rs.next()) {
|
|
|
|
|
generatedId = rs.getInt(1);
|
|
|
|
|
return generatedId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
|
throw new SQLException("error processing sql !!", e);
|
|
|
|
@ -84,7 +85,7 @@ public class MailDAOImpl implements MailDAO {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
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<>();
|
|
|
|
|
|
|
|
|
@ -93,7 +94,7 @@ public class MailDAOImpl implements MailDAO {
|
|
|
|
|
ResultSet rs = statement.executeQuery()) {
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
MailModel mailModel = new MailModel();
|
|
|
|
|
mailModel.setEmailId(rs.getInt("EMAIL_ID"));
|
|
|
|
|
mailModel.setUuId(rs.getString("UUID"));
|
|
|
|
|
mailModel.setPriority(rs.getInt("PRIORITY"));
|
|
|
|
|
mailModel.setExpiry_at(rs.getDate("EXPIRY_AT"));
|
|
|
|
|
unsentMails.add(mailModel);
|
|
|
|
@ -102,19 +103,20 @@ public class MailDAOImpl implements MailDAO {
|
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
|
throw new DatabaseAccessException("Error accessing database", e);
|
|
|
|
|
}
|
|
|
|
|
logger.info(unsentMails.toString());
|
|
|
|
|
return unsentMails;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public MailModel getMailDetails(Integer mail_id) {
|
|
|
|
|
String sql = "SELECT * FROM EMAIL WHERE EMAIL_ID = ?";
|
|
|
|
|
public MailModel getMailDetails(String uuId) {
|
|
|
|
|
String sql = "SELECT * FROM EMAIL WHERE UUID = ?";
|
|
|
|
|
MailModel mailModel = new MailModel();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Connection conn = dataSource.getConnection();
|
|
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
|
|
|
|
stmt.setInt(1, mail_id);
|
|
|
|
|
stmt.setString(1, uuId);
|
|
|
|
|
try (ResultSet rs = stmt.executeQuery()) {
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
mailModel.setEmailId(rs.getInt("EMAIL_ID"));
|
|
|
|
@ -173,12 +175,12 @@ public class MailDAOImpl implements MailDAO {
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addToSentMail(Integer mail_id) {
|
|
|
|
|
public void addToSentMail(String uuId) {
|
|
|
|
|
try {
|
|
|
|
|
PreparedStatement stmt;
|
|
|
|
|
Connection connection = dataSource.getConnection();
|
|
|
|
|
stmt = connection.prepareStatement("INSERT INTO SENTEMAIL (EMAIL_ID,SENT_TIME) VALUE (?,?)");
|
|
|
|
|
stmt.setInt(1, mail_id);
|
|
|
|
|
stmt = connection.prepareStatement("INSERT INTO SENTEMAIL (UUID,SENT_TIME) VALUE (?,?)");
|
|
|
|
|
stmt.setString(1, uuId);
|
|
|
|
|
stmt.setDate(2, Date.valueOf(LocalDate.now()));
|
|
|
|
|
stmt.execute();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|