Updated Code

This commit is contained in:
piyuskag
2024-10-27 16:07:56 +05:30
parent 452a661389
commit 90cdc9ba50
19 changed files with 574 additions and 302 deletions

View File

@@ -2,12 +2,22 @@ package net.gepafin.tendermanagement.dao;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.ApplicationAmendmentRequestEntity;
import net.gepafin.tendermanagement.entities.CommunicationAmendmentEntity;
import net.gepafin.tendermanagement.entities.ApplicationEntity;
import net.gepafin.tendermanagement.entities.CallEntity;
import net.gepafin.tendermanagement.entities.CommunicationEntity;
import net.gepafin.tendermanagement.entities.CompanyEntity;
import net.gepafin.tendermanagement.entities.ProtocolEntity;
import net.gepafin.tendermanagement.entities.SystemEmailTemplatesEntity;
import net.gepafin.tendermanagement.entities.UserEntity;
import net.gepafin.tendermanagement.model.request.CommunicationRequestBean;
import net.gepafin.tendermanagement.model.response.ApplicationAmendmentResponse;
import net.gepafin.tendermanagement.model.response.CommunicationResponseBean;
import net.gepafin.tendermanagement.repositories.CommunicationAmendmentRepository;
import net.gepafin.tendermanagement.web.rest.api.ApplicationAmendmentRepository;
import net.gepafin.tendermanagement.model.response.SystemEmailTemplateResponse;
import net.gepafin.tendermanagement.repositories.CommunicationRepository;
import net.gepafin.tendermanagement.service.SystemEmailTemplatesService;
import net.gepafin.tendermanagement.util.DateTimeUtil;
import net.gepafin.tendermanagement.util.MailUtil;
import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
import org.slf4j.Logger;
@@ -15,112 +25,223 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.Optional;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class CommunicationAmendmentDao {
private static final Logger log = LoggerFactory.getLogger(CommunicationAmendmentDao.class);
@Autowired
CommunicationAmendmentRepository communicationAmendmentRepository;
CommunicationRepository communicationRepository;
@Autowired
ApplicationAmendmentRepository applicationAmendmentRepository;
// @Autowired
// ApplicationAmendmentRequestRepository applicationAmendmentRequestRepository;
@Autowired
private MailUtil mailUtil;
public CommunicationResponseBean addCommentToAmendmentRequest(CommunicationRequestBean communicationReq) {
@Autowired
private SystemEmailTemplatesService systemEmailTemplatesService;
public CommunicationResponseBean addCommentToAmendmentRequest(CommunicationRequestBean communicationReq, Long amendmentId) {
log.info("Adding communication request...");
// Fetch amendment request by ID to set the relationship
ApplicationAmendmentRequestEntity amendmentRequest = applicationAmendmentRepository
.findAmendmentById(communicationReq.getAmendmentId());
// Create and populate CommunicationAmendmentEntity
CommunicationAmendmentEntity communicationAmendmentEntity = new CommunicationAmendmentEntity();
communicationAmendmentEntity.setAmendmentRequest(amendmentRequest);
communicationAmendmentEntity.setCommunicationTitle(communicationReq.getTitle());
communicationAmendmentEntity.setCommunicationComment(communicationReq.getComment());
communicationAmendmentEntity.setIsDeleted(false);
// Save the communication amendment entity
communicationAmendmentEntity = communicationAmendmentRepository.save(communicationAmendmentEntity);
log.info("Added comment: {}", communicationAmendmentEntity);
// Convert and return the response bean
return convertToCommunicationResponseBean(communicationAmendmentEntity);
CommunicationEntity communicationEntity = convertToCommunicationCommentEntity(communicationReq, amendmentId);
communicationEntity = communicationRepository.save(communicationEntity);
log.info("Added comment: {}", communicationEntity);
return convertToCommunicationResponseBean(communicationEntity);
}
public String deleteCommunicationAmendmentComment(Long amendmentId, Long commentId) {
// Optional<ApplicationAmendmentRequestEntity> amendmentData = communicationAmendmentRepository.findById(amendmentId);
// if(amendmentData.isEmpty()) {
// throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMMENT_NOT_ASSOCIATE_WITH_AMENDMENT_ID_ERROR_MSG));
// }
Optional<CommunicationAmendmentEntity> data = communicationAmendmentRepository.findById(commentId);
if (data.isEmpty()) {
throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMMENT_NOT_FOUND));
CommunicationEntity data = communicationRepository.findById(commentId)
.orElseThrow(() -> new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMMENT_NOT_FOUND)));
if (!data.getAmendmentRequest().getId().equals(amendmentId)) {
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.INVALID_AMENDMENT_FOR_COMMENT));
}
communicationAmendmentRepository.deleteById(commentId);
communicationRepository.deleteById(commentId);
return "Deleted Comment Successfully.";
}
public CommunicationResponseBean updateCommunicationAmendment(CommunicationRequestBean communicationRequestBean) {
public ApplicationAmendmentResponse getAmendmentComments(Long amendmentId) {
ApplicationAmendmentRequestEntity amendmentData = applicationAmendmentRepository.findAmendmentById(amendmentId);
if (amendmentData == null) {
throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.AMENDMENT_NOT_FOUND));
}
List<CommunicationResponseBean> commentsList = communicationRepository.findCommentDetailsByAmendmentId(amendmentId);
if (commentsList == null) {
throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMMENT_NOT_FOUND));
}
return new ApplicationAmendmentResponse(amendmentData, commentsList);
}
public CommunicationResponseBean updateCommunicationAmendment(CommunicationRequestBean communicationRequestBean, Long amendmentId, Long commentId) {
log.info("Updating communication comment...");
CommunicationAmendmentEntity communicationAmendmentEntity = convertToCommunicationAmendmentEntity(communicationRequestBean);
communicationAmendmentEntity = communicationAmendmentRepository.save(communicationAmendmentEntity);
log.info("Updated Comment {}", communicationAmendmentEntity);
return convertToCommunicationResponseBean(communicationAmendmentEntity);
CommunicationEntity existingComment = communicationRepository.findById(commentId)
.orElseThrow(() -> new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMMENT_NOT_FOUND)));
if (!existingComment.getAmendmentRequest().getId().equals(amendmentId)) {
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.COMMENT_NOT_ASSOCIATE_WITH_AMENDMENT_ID_ERROR_MSG));
}
existingComment.setCommunicationTitle(communicationRequestBean.getTitle());
existingComment.setCommunicationComment(communicationRequestBean.getComment());
existingComment.setCommentedDate(LocalDateTime.now());
existingComment = communicationRepository.save(existingComment);
log.info("Updated Comment: {}", existingComment);
return convertToCommunicationResponseBean(existingComment);
}
public CommunicationResponseBean getAmendmentComments(Long amendmentId, Long commentId) {
// Optional<ApplicationAmendmentRequestEntity> amendmentData = communicationAmendmentRepository.findById(amendmentId);
// if(amendmentData.isEmpty()) {
// throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.AMENDMENT_NOT_FOUND));
// }
CommunicationAmendmentEntity commentData = communicationAmendmentRepository.findCommentsById(commentId);
CommunicationResponseBean data = convertToCommunicationResponseBean1(commentData);
return data;
}
private CommunicationResponseBean convertToCommunicationResponseBean(CommunicationAmendmentEntity entity) {
private CommunicationResponseBean convertToCommunicationResponseBean(CommunicationEntity entity) {
CommunicationResponseBean response = new CommunicationResponseBean();
response.setComment(entity.getCommunicationComment());
response.setCommunicationAddedDate(Instant.now());
return response;
}
private CommunicationResponseBean convertToCommunicationResponseBean1(CommunicationAmendmentEntity entity) {
CommunicationResponseBean response = new CommunicationResponseBean();
response.setComment(entity.getCommunicationComment());
response.setCommunicationAddedDate(Instant.now());
response.setCommentedDate(entity.setCommentedDate(););
response.setAmendmentId(entity.getAmendmentRequest().getId());
response.setCreatedDate(entity.getCreatedDate());
response.setUpdatedDate(entity.getUpdatedDate());
return response;
}
private CommunicationAmendmentEntity convertToCommunicationAmendmentEntity(CommunicationRequestBean communicationReq) {
private CommunicationEntity convertToCommunicationCommentEntity(CommunicationRequestBean communicationReq, Long amendmentId) {
CommunicationAmendmentEntity communicationAmendmentEntity = new CommunicationAmendmentEntity();
ApplicationAmendmentRequestEntity entity = applicationAmendmentRepository.findAmendmentById(communicationReq.getAmendmentId());
if (entity == null) {
throw new CustomValidationException(Status.NOT_FOUND, "Amendment Request not found for id: " + communicationReq.getAmendmentId());
}
communicationAmendmentEntity.setAmendmentRequest(entity);
communicationAmendmentEntity.setCommunicationTitle(communicationReq.getComment());
communicationAmendmentEntity.setCommunicationComment(communicationReq.getComment());
return communicationAmendmentEntity;
ApplicationAmendmentRequestEntity amendmentRequest = applicationAmendmentRepository.findAmendmentById(amendmentId);
CommunicationEntity communicationEntity = new CommunicationEntity();
communicationEntity.setAmendmentRequest(amendmentRequest);
communicationEntity.setCommunicationTitle(communicationReq.getTitle());
communicationEntity.setCommunicationComment(communicationReq.getComment());
communicationEntity.setIsDeleted(false);
communicationEntity.setCommentedDate(LocalDateTime.now());
return communicationEntity;
}
public ApplicationAmendmentRequestEntity getAmendmentRequestById(Long id) {
if(id==null){
throw new CustomValidationException(Status.BAD_REQUEST, "Please provide amendmentId" + null);
private void sendMailToNotifyBeneficiaryRegardingNewAmendment(UserEntity userEntity, ApplicationEntity applicationEntity) {
CallEntity call = applicationEntity.getCall();
CompanyEntity company = applicationEntity.getCompany();
ProtocolEntity protocol = applicationEntity.getProtocol();
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService
.retrieveTemplateByTypeAndCall(SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.DOCUMENTATION_INTEGRATION_REQUEST,
call, null);
// Create the map for subject placeholders
Map<String, String> subjectPlaceholders = new HashMap<>();
subjectPlaceholders.put("{{call_name}}", call.getName());
subjectPlaceholders.put("{{company_name}}", company.getCompanyName());
// Create the map for body placeholders
Map<String, String> bodyPlaceholders = new HashMap<>();
bodyPlaceholders.put("{{call_name}}", call.getName());
bodyPlaceholders.put("{{protocol_number}}", protocol.getProtocolNumber().toString());
bodyPlaceholders.put("{{protocol_date}}", DateTimeUtil.formatCreatedDate(protocol.getCreatedDate()));
bodyPlaceholders.put("{{protocol_time}}", DateTimeUtil.parseLocalTimeToString(protocol.getTime(), GepafinConstant.HH_MM_SS));
bodyPlaceholders.put("{{form_dataInput}}", "YOUR_FORM_DATA_HERE");
// Replace placeholders in the subject and body
String subject = Utils.replacePlaceholders(systemEmailTemplateResponse.getSubject(), subjectPlaceholders);
String body = Utils.replacePlaceholders(systemEmailTemplateResponse.getHtmlContent(), bodyPlaceholders);
String email = userEntity.getEmail();
if (userEntity.getBeneficiary() != null) {
email = userEntity.getBeneficiary().getEmail();
}
ApplicationAmendmentRequestEntity applicationAmendmentData = applicationAmendmentRepository.findAmendmentById(id);
if(Boolean.FALSE.equals(applicationAmendmentData)){
throw new CustomValidationException(Status.NOT_FOUND, "Amendment Request not found for id: " + applicationAmendmentData.getId());
mailUtil.sendByMailGun(subject, body, List.of(email), null);
mailUtil.sendByMailGun(subject, body, List.of(applicationEntity.getCompany().getEmail()), null);
}
public void sendApplicationFailureNotificationEmail(String userEmail, ApplicationEntity applicationEntity) {
CallEntity call = applicationEntity.getCall();
CompanyEntity company = applicationEntity.getCompany();
ProtocolEntity protocol = applicationEntity.getProtocol();
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService
.retrieveTemplateByTypeAndCall(SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.INADMISSIBILITY_NOTIFICATION_DUE_TO_FAILURE,
call, null);
// Create the map for subject placeholders
Map<String, String> subjectPlaceholders = new HashMap<>();
subjectPlaceholders.put("{{call_name}}", call.getName());
subjectPlaceholders.put("{{company_name}}", company.getCompanyName());
// Create the map for body placeholders
Map<String, String> bodyPlaceholders = new HashMap<>();
bodyPlaceholders.put("{{call_name}}", call.getName());
bodyPlaceholders.put("{{date_time_emailSend}}", DateTimeUtil.formatLocalDateTime(protocol.getCreatedDate(), GepafinConstant.DD_MM_YYYY_HH_MM));
// Replace placeholders in the subject and body
String subject = Utils.replacePlaceholders(systemEmailTemplateResponse.getSubject(), subjectPlaceholders);
String body = Utils.replacePlaceholders(systemEmailTemplateResponse.getHtmlContent(), bodyPlaceholders);
mailUtil.sendByMailGun(subject, body, List.of(userEmail), null);
mailUtil.sendByMailGun(subject, body, List.of(applicationEntity.getCompany().getEmail()), null);
}
private void sendAdmissibilityNotificationEmail(UserEntity userEntity, ApplicationEntity applicationEntity) {
CallEntity call = applicationEntity.getCall();
CompanyEntity company = applicationEntity.getCompany();
ProtocolEntity protocol = applicationEntity.getProtocol();
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService
.retrieveTemplateByTypeAndCall(SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.ADMISSIBILITY_NOTIFICATION,
call, null);
// Create the map for subject placeholders
Map<String, String> subjectPlaceholders = new HashMap<>();
subjectPlaceholders.put("{{call_name}}", call.getName());
subjectPlaceholders.put("{{company_name}}", company.getCompanyName());
// Create the map for body placeholders
Map<String, String> bodyPlaceholders = new HashMap<>();
bodyPlaceholders.put("{{call_name}}", call.getName());
bodyPlaceholders.put("{{protocol_number}}", protocol.getProtocolNumber().toString());
bodyPlaceholders.put("{{protocol_date}}", DateTimeUtil.formatCreatedDate(protocol.getCreatedDate()));
bodyPlaceholders.put("{{protocol_time}}", DateTimeUtil.parseLocalTimeToString(protocol.getTime(), GepafinConstant.HH_MM_SS));
// Replace placeholders in the subject and body
String subject = Utils.replacePlaceholders(systemEmailTemplateResponse.getSubject(), subjectPlaceholders);
String body = Utils.replacePlaceholders(systemEmailTemplateResponse.getHtmlContent(), bodyPlaceholders);
String email = userEntity.getEmail();
if (userEntity.getBeneficiary() != null) {
email = userEntity.getBeneficiary().getEmail();
}
return applicationAmendmentData;
mailUtil.sendByMailGun(subject, body, List.of(email), null);
mailUtil.sendByMailGun(subject, body, List.of(applicationEntity.getCompany().getEmail()), null);
}
private void sendInadmissibilityTemplateEmail(UserEntity userEntity, ApplicationEntity applicationEntity) {
CallEntity call = applicationEntity.getCall();
CompanyEntity company = applicationEntity.getCompany();
ProtocolEntity protocol = applicationEntity.getProtocol();
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService
.retrieveTemplateByTypeAndCall(SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.INADMISSIBILITY_TEMPLATE,
call, null);
// Create the map for subject placeholders
Map<String, String> subjectPlaceholders = new HashMap<>();
subjectPlaceholders.put("{{call_name}}", call.getName());
subjectPlaceholders.put("{{company_name}}", company.getCompanyName());
// Create the map for body placeholders
Map<String, String> bodyPlaceholders = new HashMap<>();
bodyPlaceholders.put("{{call_name}}", call.getName());
bodyPlaceholders.put("{{protocol_number}}", protocol.getProtocolNumber().toString());
bodyPlaceholders.put("{{protocol_date}}", DateTimeUtil.formatCreatedDate(protocol.getCreatedDate()));
bodyPlaceholders.put("{{protocol_time}}", DateTimeUtil.parseLocalTimeToString(protocol.getTime(), GepafinConstant.HH_MM_SS));
bodyPlaceholders.put("{{form_text}}", "YOUR_FORM_TEXT_HERE"); // Replace with actual data input if available
// Replace placeholders in the subject and body
String subject = Utils.replacePlaceholders(systemEmailTemplateResponse.getSubject(), subjectPlaceholders);
String body = Utils.replacePlaceholders(systemEmailTemplateResponse.getHtmlContent(), bodyPlaceholders);
String email = userEntity.getEmail();
if (userEntity.getBeneficiary() != null) {
email = userEntity.getBeneficiary().getEmail();
}
mailUtil.sendByMailGun(subject, body, List.of(email), null);
mailUtil.sendByMailGun(subject, body, List.of(applicationEntity.getCompany().getEmail()), null);
}
}