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.CommunicationEntity; 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.CommunicationRepository; import net.gepafin.tendermanagement.service.ApplicationAmendmentRequestService; import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException; import net.gepafin.tendermanagement.web.rest.api.errors.Status; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.util.List; @Component public class CommunicationDao { private static final Logger log = LoggerFactory.getLogger(CommunicationDao.class); @Autowired private CommunicationRepository communicationRepository; @Autowired ApplicationAmendmentRequestService applicationAmendmentRequestService; public CommunicationResponseBean addCommentToAmendmentRequest(CommunicationRequestBean communicationReq, Long amendmentId) { log.info("Adding communication request..."); CommunicationEntity communicationEntity = convertToCommunicationCommentEntity(communicationReq, amendmentId); communicationEntity = communicationRepository.save(communicationEntity); log.info("Added comment: {}", communicationEntity); return convertToCommunicationResponseBean(communicationEntity); } public String deleteComment(Long amendmentId, Long commentId) { CommunicationEntity data = communicationRepository.findById(commentId) .orElseThrow(() -> new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMMENT_NOT_FOUND))); if (!data.getApplicationAmendmentRequest().getId().equals(amendmentId)) { throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.INVALID_AMENDMENT_FOR_COMMENT)); } data.setIsDeleted(true); communicationRepository.save(data); return "Deleted Comment Successfully."; } public ApplicationAmendmentResponse getAmendmentComments(Long amendmentId) { ApplicationAmendmentRequestEntity amendmentData = applicationAmendmentRequestService.validateApplicationAmendmentRequest(amendmentId); List 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 updateAmendmentComment(CommunicationRequestBean communicationRequestBean, Long amendmentId, Long commentId) { log.info("Updating communication comment..."); CommunicationEntity existingComment = communicationRepository.findById(commentId) .orElseThrow(() -> new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMMENT_NOT_FOUND))); if (!existingComment.getApplicationAmendmentRequest().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); } private CommunicationResponseBean convertToCommunicationResponseBean(CommunicationEntity entity) { CommunicationResponseBean response = new CommunicationResponseBean(); response.setComment(entity.getCommunicationComment()); response.setCommentedDate(entity.getCommentedDate()); response.setAmendmentId(entity.getApplicationAmendmentRequest().getId()); response.setCreatedDate(entity.getCreatedDate()); response.setUpdatedDate(entity.getUpdatedDate()); response.setTitle(entity.getCommunicationTitle()); return response; } private CommunicationEntity convertToCommunicationCommentEntity(CommunicationRequestBean communicationReq, Long amendmentId) { ApplicationAmendmentRequestEntity amendmentRequest = applicationAmendmentRequestService.validateApplicationAmendmentRequest(amendmentId); CommunicationEntity communicationEntity = new CommunicationEntity(); communicationEntity.setApplicationAmendmentRequest(amendmentRequest); communicationEntity.setCommunicationTitle(communicationReq.getTitle()); communicationEntity.setCommunicationComment(communicationReq.getComment()); communicationEntity.setIsDeleted(false); communicationEntity.setCommentedDate(LocalDateTime.now()); return communicationEntity; } }