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.model.request.CommunicationRequestBean; 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.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.Instant; import java.util.Optional; @Component public class CommunicationAmendmentDao { private static final Logger log = LoggerFactory.getLogger(CommunicationAmendmentDao.class); @Autowired CommunicationAmendmentRepository communicationAmendmentRepository; @Autowired ApplicationAmendmentRepository applicationAmendmentRepository; // @Autowired // ApplicationAmendmentRequestRepository applicationAmendmentRequestRepository; public CommunicationResponseBean addCommentToAmendmentRequest(CommunicationRequestBean communicationReq) { 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); } public String deleteCommunicationAmendmentComment(Long amendmentId, Long commentId) { // Optional 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 data = communicationAmendmentRepository.findById(commentId); if (data.isEmpty()) { throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMMENT_NOT_FOUND)); } communicationAmendmentRepository.deleteById(commentId); return "Deleted Comment Successfully."; } public CommunicationResponseBean updateCommunicationAmendment(CommunicationRequestBean communicationRequestBean) { log.info("Updating communication comment..."); CommunicationAmendmentEntity communicationAmendmentEntity = convertToCommunicationAmendmentEntity(communicationRequestBean); communicationAmendmentEntity = communicationAmendmentRepository.save(communicationAmendmentEntity); log.info("Updated Comment {}", communicationAmendmentEntity); return convertToCommunicationResponseBean(communicationAmendmentEntity); } public CommunicationResponseBean getAmendmentComments(Long amendmentId, Long commentId) { // Optional 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) { 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()); return response; } private CommunicationAmendmentEntity convertToCommunicationAmendmentEntity(CommunicationRequestBean communicationReq) { 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; } public ApplicationAmendmentRequestEntity getAmendmentRequestById(Long id) { if(id==null){ throw new CustomValidationException(Status.BAD_REQUEST, "Please provide amendmentId" + null); } ApplicationAmendmentRequestEntity applicationAmendmentData = applicationAmendmentRepository.findAmendmentById(id); if(Boolean.FALSE.equals(applicationAmendmentData)){ throw new CustomValidationException(Status.NOT_FOUND, "Amendment Request not found for id: " + applicationAmendmentData.getId()); } return applicationAmendmentData; } }