|
|
@ -4,12 +4,12 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
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.FileConversionException;
|
|
|
|
|
|
|
|
import entgra.mailsender.exception.MailProcessingException;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.*;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
@ -26,82 +26,83 @@ 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));
|
|
|
|
Logger logger = Logger.getLogger(String.valueOf(MailDAOImpl.class));
|
|
|
|
|
|
|
|
|
|
|
|
private Connection getConnection() throws SQLException {
|
|
|
|
private Connection getConnection() throws SQLException {
|
|
|
|
return DriverManager.getConnection("jdbc:mysql://localhost:3306/email_sending", "root", "StrongPassword123!");
|
|
|
|
return DriverManager.getConnection("jdbc:mysql://localhost:3306/email_sending", "root", "StrongPassword123!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int addMail(MailModel mailModel){
|
|
|
|
public int addMail(MailModel mailModel) throws SQLException {
|
|
|
|
PreparedStatement stmt;
|
|
|
|
try {
|
|
|
|
int generatedId = -1;
|
|
|
|
PreparedStatement stmt;
|
|
|
|
|
|
|
|
int generatedId;
|
|
|
|
try {
|
|
|
|
Connection conn = this.getConnection();
|
|
|
|
Connection conn = this.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) 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());
|
|
|
|
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());
|
|
|
|
stmt.setBytes(4,mailModel.getAttachment().getBytes());
|
|
|
|
|
|
|
|
stmt.setString(5, parametersJson);
|
|
|
|
stmt.setString(5, parametersJson);
|
|
|
|
stmt.setTimestamp(6,current_time);
|
|
|
|
stmt.setTimestamp(6, current_time);
|
|
|
|
String filename = mailModel.getAttachment().getOriginalFilename();
|
|
|
|
String filename = mailModel.getAttachment().getOriginalFilename();
|
|
|
|
stmt.setString(7,filename);
|
|
|
|
stmt.setString(7, filename);
|
|
|
|
stmt.setDate(8,mailModel.getExpiry_at());
|
|
|
|
stmt.setDate(8, mailModel.getExpiry_at());
|
|
|
|
stmt.executeUpdate();
|
|
|
|
stmt.executeUpdate();
|
|
|
|
ResultSet rs = stmt.getGeneratedKeys();
|
|
|
|
ResultSet rs = stmt.getGeneratedKeys();
|
|
|
|
if (rs.next()) {
|
|
|
|
if (rs.next()) {
|
|
|
|
generatedId = rs.getInt(1);
|
|
|
|
generatedId = rs.getInt(1);
|
|
|
|
|
|
|
|
return generatedId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
|
|
|
|
throw new SQLException("error processing sql !!", e);
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
|
|
|
|
throw new MailProcessingException("Error processing mailModel", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.info("Stored successfully");
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return generatedId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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)";
|
|
|
|
|
|
|
|
|
|
|
|
List<MailModel> unsentMails = new ArrayList<>();
|
|
|
|
List<MailModel> unsentMails = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
try(Connection connection = this.getConnection();
|
|
|
|
try (Connection connection = this.getConnection();
|
|
|
|
PreparedStatement statement = connection.prepareStatement(sql);
|
|
|
|
PreparedStatement statement = connection.prepareStatement(sql);
|
|
|
|
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.setEmailId(rs.getInt("email_id"));
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
|
|
|
|
throw new DatabaseAccessException("Error accessing database", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
return unsentMails;
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return unsentMails;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public MailModel getMailDetails(Integer mail_id){
|
|
|
|
public MailModel getMailDetails(Integer mail_id) {
|
|
|
|
String sql = "SELECT * FROM email WHERE email_id = ?";
|
|
|
|
String sql = "SELECT * FROM email WHERE email_id = ?";
|
|
|
|
MailModel mailModel = new MailModel();
|
|
|
|
MailModel mailModel = new MailModel();
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Connection conn = this.getConnection();
|
|
|
|
Connection conn = this.getConnection();
|
|
|
|
try(PreparedStatement stmt = conn.prepareStatement(sql)) {
|
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
|
|
|
stmt.setInt(1,mail_id);
|
|
|
|
stmt.setInt(1, mail_id);
|
|
|
|
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"));
|
|
|
|
mailModel.setEmailAddress(rs.getString("email_address"));
|
|
|
|
mailModel.setEmailAddress(rs.getString("email_address"));
|
|
|
|
mailModel.setMsgTemplate(rs.getString("msg_template"));
|
|
|
|
mailModel.setMsgTemplate(rs.getString("msg_template"));
|
|
|
@ -111,38 +112,38 @@ public MailModel getMailDetails(Integer mail_id){
|
|
|
|
List<MailModel.Parameter> parameters = parseJsonParameters(jsonData);
|
|
|
|
List<MailModel.Parameter> parameters = parseJsonParameters(jsonData);
|
|
|
|
mailModel.setParameters(parameters);
|
|
|
|
mailModel.setParameters(parameters);
|
|
|
|
|
|
|
|
|
|
|
|
String filename = rs.getString("filename");
|
|
|
|
String filename = rs.getString("filename");
|
|
|
|
mailModel.setFilename(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) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
|
|
|
|
throw new MailProcessingException("Error processing mail", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
} catch (SQLException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
|
|
|
|
throw new DatabaseAccessException("Error accessing database", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private List<MailModel.Parameter> parseJsonParameters(String parametersJson) {
|
|
|
|
private List<MailModel.Parameter> parseJsonParameters(String parametersJson) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
return objectMapper.readValue(parametersJson, new TypeReference<>() {
|
|
|
|
return objectMapper.readValue(parametersJson, new TypeReference<>() {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
throw new IllegalArgumentException("Error parsing parameters JSON", e);
|
|
|
|
throw new IllegalArgumentException("Error parsing parameters JSON", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static File convertBlobToFile(Blob blob, String fileName) throws IOException, SQLException {
|
|
|
|
public static File convertBlobToFile(Blob blob, String fileName) throws IOException, SQLException {
|
|
|
|
File file = new File(fileName);
|
|
|
|
File file = new File(fileName);
|
|
|
|
try (FileOutputStream outputStream = new FileOutputStream(file)) {
|
|
|
|
try (FileOutputStream outputStream = new FileOutputStream(file)) {
|
|
|
|
InputStream inputStream = blob.getBinaryStream();
|
|
|
|
InputStream inputStream = blob.getBinaryStream();
|
|
|
@ -150,31 +151,25 @@ public static File convertBlobToFile(Blob blob, String fileName) throws IOExcept
|
|
|
|
while (inputStream.read(buffer) > 0) {
|
|
|
|
while (inputStream.read(buffer) > 0) {
|
|
|
|
outputStream.write(buffer);
|
|
|
|
outputStream.write(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
throw new FileConversionException("Error converting Blob to File", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void addToSentMail(Integer mail_id){
|
|
|
|
public void addToSentMail(Integer mail_id) {
|
|
|
|
PreparedStatement stmt;
|
|
|
|
try {
|
|
|
|
|
|
|
|
PreparedStatement stmt;
|
|
|
|
try {
|
|
|
|
Connection connection = this.getConnection();
|
|
|
|
Connection connection = this.getConnection();
|
|
|
|
stmt = connection.prepareStatement("INSERT INTO sentEmail (email_id,sent_time) VALUE (?,?)");
|
|
|
|
stmt = connection.prepareStatement("INSERT INTO sentEmail (email_id,sent_time) VALUE (?,?)");
|
|
|
|
stmt.setInt(1, mail_id);
|
|
|
|
stmt.setInt(1,mail_id);
|
|
|
|
|
|
|
|
stmt.setDate(2, Date.valueOf(LocalDate.now()));
|
|
|
|
stmt.setDate(2, Date.valueOf(LocalDate.now()));
|
|
|
|
stmt.execute();
|
|
|
|
stmt.execute();
|
|
|
|
logger.info("added to sent_email successfully");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
} catch (SQLException e) {
|
|
|
|
throw new DatabaseAccessException("Error accessing database", e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|