|
|
@ -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;
|
|
|
@ -33,11 +33,10 @@ private Connection getConnection() throws SQLException {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int addMail(MailModel mailModel){
|
|
|
|
public int addMail(MailModel mailModel) throws SQLException {
|
|
|
|
PreparedStatement stmt;
|
|
|
|
|
|
|
|
int generatedId = -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
PreparedStatement stmt;
|
|
|
|
|
|
|
|
int generatedId;
|
|
|
|
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());
|
|
|
@ -55,17 +54,18 @@ public int addMail(MailModel mailModel){
|
|
|
|
ResultSet rs = stmt.getGeneratedKeys();
|
|
|
|
ResultSet rs = stmt.getGeneratedKeys();
|
|
|
|
if (rs.next()) {
|
|
|
|
if (rs.next()) {
|
|
|
|
generatedId = rs.getInt(1);
|
|
|
|
generatedId = rs.getInt(1);
|
|
|
|
|
|
|
|
return generatedId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.info("Stored successfully");
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
} catch (SQLException e) {
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
throw new SQLException("error processing sql !!", e);
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
}
|
|
|
|
throw new MailProcessingException("Error processing mailModel", e);
|
|
|
|
return generatedId;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -85,7 +85,8 @@ public List<MailModel> getUnsentMessages(){
|
|
|
|
unsentMails.add(mailModel);
|
|
|
|
unsentMails.add(mailModel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
} catch (SQLException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
|
|
|
|
throw new DatabaseAccessException("Error accessing database", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unsentMails;
|
|
|
|
return unsentMails;
|
|
|
|
|
|
|
|
|
|
|
@ -117,17 +118,16 @@ public MailModel getMailDetails(Integer mail_id){
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -138,6 +138,7 @@ private List<MailModel.Parameter> parseJsonParameters(String parametersJson) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -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 {
|
|
|
|
try {
|
|
|
|
|
|
|
|
PreparedStatement stmt;
|
|
|
|
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) {
|
|
|
|
} catch (SQLException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
logger.info(e.getMessage());
|
|
|
|
|
|
|
|
throw new DatabaseAccessException("Error accessing database", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|