Merge pull request 'removed unused imports' (#3) from develop into master

Reviewed-on: #3
pull/7/head
commit ca9399feb8

@ -1,15 +1,12 @@
package entgra.mailsender.Config;
import entgra.mailsender.DAO.MailDAO;
import entgra.mailsender.DTO.MailModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;
@Configuration

@ -22,15 +22,16 @@ public class MailController {
@PostMapping
public ResponseEntity<String> sendEmail(@ModelAttribute MailModel emailModel) {
String msg;
try {
mailService.sendEmail(emailModel);
String msg = "Email sent successfully";
msg = "Email sent successfully";
return ResponseEntity.ok(msg);
} catch (RuntimeException e) {
String msg = "Invalid email address 12112121211";
msg = "Invalid email address";
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(msg);
} catch (MessagingException | IOException exception) {
String msg = "Failed to send email";
msg = "Failed to send email";
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(msg + exception);
}
}

@ -1,7 +1,8 @@
package entgra.mailsender.DAO;
package entgra.mailsender.DAO.Impl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import entgra.mailsender.DAO.MailDAO;
import entgra.mailsender.DTO.MailModel;
import org.springframework.stereotype.Service;
@ -9,15 +10,24 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.ResultSet;
import java.sql.Blob;
import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import java.util.logging.Logger;
@Service
public class MailDAOImpl implements MailDAO{
public class MailDAOImpl implements MailDAO {
Logger logger = Logger.getLogger(String.valueOf(MailDAOImpl.class));
@ -54,41 +64,7 @@ public void addMail(MailModel mailModel){
}
}
//@Override
//public List<MailModel> insertInPriorityQueue(){
// logger.info("came into the function");
//
//
// try{
// Connection conn = this.getConnection();
//
// try (PreparedStatement stmt = conn.prepareStatement(sql);
// ResultSet rs = stmt.executeQuery()) {
// while (rs.next()) {
// int priority = rs.getInt("priority");
// int mail_id = rs.getInt("email_id");
// priorityQueue.add(new MailModel(mail_id,null,null,priority,null,null,null,null,null,0));
// }
//
//// MailModel email = priorityQueue.poll();
// //assert email != null;
// for (Object element: priorityQueue){
// System.out.println("element" + element);
// }
// return priorityQueue;
//
// } catch (SQLException e) {
// logger.info("Error processing result set: " + e.getMessage());
// throw new RuntimeException(e);
// }
//
//
// } catch (Exception e) {
// logger.info("Error in preparing statement" + e);
// throw new RuntimeException(e);
// }
//
//}
@Override
public List<MailModel> getUnsentMessages(){
@ -137,7 +113,6 @@ public MailModel getMailDetails(Integer mail_id){
String filename = rs.getString("filename");
mailModel.setFilename(filename);
// logger.info(filename);
Blob blob = rs.getBlob("attachment");
logger.info(blob.toString());

@ -3,14 +3,11 @@ package entgra.mailsender.DAO;
import entgra.mailsender.DTO.MailModel;
import java.util.List;
import java.util.PriorityQueue;
public interface MailDAO {
void addMail(MailModel mailModel);
// List<MailModel> insertInPriorityQueue();
MailModel getMailDetails(Integer mail_id);
void addToSentMail(Integer mail_id);

@ -7,7 +7,6 @@ import lombok.Setter;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.Date;
import java.util.List;
@Getter

@ -1,7 +1,6 @@
package entgra.mailsender;
import entgra.mailsender.Service.MailService;
import entgra.mailsender.Service.MailServiceImpl;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;

@ -1,7 +1,8 @@
package entgra.mailsender.Service;
package entgra.mailsender.Service.Impl;
import entgra.mailsender.DAO.MailDAO;
import entgra.mailsender.DTO.MailModel;
import entgra.mailsender.Service.MailQueueService;
import entgra.mailsender.util.PriorityQueueHolder;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
@ -15,7 +16,7 @@ import java.util.logging.Logger;
@Service
@RequiredArgsConstructor
public class MailQueueServiceImpl implements MailQueueService{
public class MailQueueServiceImpl implements MailQueueService {
Logger logger = Logger.getLogger(String.valueOf(MailQueueServiceImpl.class));
@Autowired
@ -23,7 +24,6 @@ public class MailQueueServiceImpl implements MailQueueService{
private volatile Thread processingThread;
private volatile boolean isProcessingMails;
// private final MailDAO mailDAO;
@Override
public void enqueMails(List<MailModel> mailModelList){
@ -38,11 +38,7 @@ public class MailQueueServiceImpl implements MailQueueService{
uniqueMails.add(mailModel);
}
}
// assert priorityQueue.peek() != null;
// logger.info(String.valueOf(priorityQueue.peek().getEmailId()));
// Integer id = priorityQueue.peek().getEmailId();
// logger.info(mailDAO.getMailDetails(id).getEmailAddress());
// logger.info((Supplier<String>) uniqueMails);
}
public MailModel getHighPriorityMail(){

@ -1,7 +1,9 @@
package entgra.mailsender.Service;
package entgra.mailsender.Service.Impl;
import entgra.mailsender.DAO.MailDAO;
import entgra.mailsender.DTO.MailModel;
import entgra.mailsender.Service.MailQueueService;
import entgra.mailsender.Service.MailService;
import entgra.mailsender.util.PriorityQueueHolder;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
@ -21,8 +23,6 @@ public class MailServiceImpl implements MailService {
@Autowired
private MailQueueService mailQueueService;
// private final mailQueueService mailQueueService;
@PreDestroy
public void shutdown() {
@ -42,29 +42,14 @@ public class MailServiceImpl implements MailService {
logger.warning("email address : "+emailModel.getEmailAddress());
if (isValidEmailAddress(emailModel.getEmailAddress())){
throw new RuntimeException("Invalid Email address 161616161");
throw new RuntimeException("Invalid Email address");
}
mailDAO.addMail(emailModel); // save the mail details in the database
syncMailWithDB();
//here we need to check the unsent mail details
//PriorityQueue<MailModel> mailwithHighPriority = mailDAO.getMailwithHighPriority();
// MailModel mailModel = mailQueueService.getHighPriorityMail();
// assert mailModel != null;
// Integer mail_id = Integer.valueOf(mailModel.getEmailAddress());
// logger.info(mailModel.getEmailAddress());
// MailModel updatedModel = mailDAO.getMailDetails(mail_id);
// logger.info("mail id" + updatedModel.getEmailAddress());
syncMailWithDB(); //here we need to check the unsent mail details
}
public boolean isValidEmailAddress(String email){
String regex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
return !email.matches(regex);
@ -83,7 +68,8 @@ public class MailServiceImpl implements MailService {
while(PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) {
MailModel prioritizedMail = mailQueueService.getHighPriorityMail();
MailModel updatedModel = mailDAO.getMailDetails(prioritizedMail.getEmailId());
logger.info("updated mail" + updatedModel.getEmailAddress());
logger.info("High priority mail : " + updatedModel.getEmailAddress());
javaMailSender.send(mimeMessage -> {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
@ -105,12 +91,15 @@ public class MailServiceImpl implements MailService {
emailBody.append("\n").append(parameter.getKey()).append(": ").append(parameter.getValue());
}
// Append text to email body
mimeMessageHelper.setText(emailBody.toString(), false);
logger.info(updatedModel.getFilename());
// Append attachments to email body
mimeMessageHelper.addAttachment(updatedModel.getFilename(), updatedModel.getFile());
logger.info("Mail sent successfully");
//Store the sent mail to the table
mailDAO.addToSentMail(updatedModel.getEmailId());

@ -1,15 +1,14 @@
package entgra.mailsender.Service;
import entgra.mailsender.DTO.MailModel;
import org.springframework.web.multipart.MultipartFile;
import javax.mail.MessagingException;
import java.io.IOException;
import java.sql.Blob;
public interface MailService {
void sendEmail(MailModel emailModel) throws MessagingException, IOException;
boolean isValidEmailAddress(String email);
void syncMailWithDB();

Loading…
Cancel
Save