|
|
@ -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());
|
|
|
@ -64,12 +68,9 @@ public class MailDAOImpl implements MailDAO {
|
|
|
|
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) {
|
|
|
|