207 lines
12 KiB
Java
207 lines
12 KiB
Java
package net.gepafin.tendermanagement.dao;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
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.entities.DocumentEntity;
|
|
import net.gepafin.tendermanagement.enums.CommunicationInitiatorTypeEnum;
|
|
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
|
|
import net.gepafin.tendermanagement.enums.DocumentTypeEnum;
|
|
import net.gepafin.tendermanagement.enums.VersionActionTypeEnum;
|
|
import net.gepafin.tendermanagement.model.request.CommunicationRequestBean;
|
|
import net.gepafin.tendermanagement.model.request.VersionHistoryRequest;
|
|
import net.gepafin.tendermanagement.model.response.CommunicationResponseBean;
|
|
import net.gepafin.tendermanagement.model.response.DocumentResponseBean;
|
|
import net.gepafin.tendermanagement.repositories.CommunicationRepository;
|
|
import net.gepafin.tendermanagement.service.ApplicationAmendmentRequestService;
|
|
import net.gepafin.tendermanagement.service.DocumentService;
|
|
import net.gepafin.tendermanagement.util.LoggingUtil;
|
|
import net.gepafin.tendermanagement.util.Utils;
|
|
import net.gepafin.tendermanagement.util.Validator;
|
|
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
|
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
|
import org.checkerframework.checker.units.qual.A;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
public class CommunicationDao {
|
|
private static final Logger log = LoggerFactory.getLogger(CommunicationDao.class);
|
|
|
|
@Autowired
|
|
private CommunicationRepository communicationRepository;
|
|
|
|
@Autowired
|
|
private ApplicationAmendmentRequestService applicationAmendmentRequestService;
|
|
|
|
@Autowired
|
|
private Validator validator;
|
|
|
|
@Autowired
|
|
private LoggingUtil loggingUtil;
|
|
|
|
@Autowired
|
|
private HttpServletRequest request;
|
|
|
|
@Autowired
|
|
private DocumentDao documentDao;
|
|
|
|
@Autowired
|
|
private ApplicationAmendmentRequestDao applicationAmendmentRequestDao;
|
|
|
|
@Autowired
|
|
private DocumentService documentService;
|
|
|
|
public CommunicationResponseBean addCommentToAmendmentRequest(HttpServletRequest request, CommunicationRequestBean communicationReq, Long amendmentId, List<MultipartFile> files) {
|
|
|
|
log.info("Adding communication request...");
|
|
List<DocumentResponseBean> communicationDocumentBeans = new ArrayList<>();
|
|
|
|
CommunicationEntity communicationEntity = convertToCommunicationCommentEntity(communicationReq, amendmentId,communicationDocumentBeans,files);
|
|
/** This code is responsible for adding a version history log for the "adding comment to amendment request" operation. **/
|
|
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.INSERT).oldData(null).newData(communicationEntity).build());
|
|
|
|
log.info("Added comment: {}", communicationEntity);
|
|
communicationDocumentBeans=getDocumentResponseBean(communicationEntity);
|
|
return convertToCommunicationResponseBean(communicationEntity,communicationDocumentBeans);
|
|
}
|
|
|
|
private List<DocumentResponseBean> getDocumentResponseBean(CommunicationEntity communicationEntity) {
|
|
List<Long> documentIds = applicationAmendmentRequestDao.extractIds(communicationEntity.getDocuments());
|
|
List<DocumentResponseBean> documentResponseBeans = documentIds.stream()
|
|
.map(id -> {
|
|
DocumentEntity documentEntity = documentService.validateDocument(id);
|
|
DocumentResponseBean responseBean = new DocumentResponseBean();
|
|
responseBean.setId(documentEntity.getId());
|
|
responseBean.setName(documentEntity.getFileName());
|
|
responseBean.setType(DocumentTypeEnum.valueOf(documentEntity.getType()));
|
|
responseBean.setSource(DocumentSourceTypeEnum.valueOf(documentEntity.getSource()));
|
|
responseBean.setSourceId(documentEntity.getSourceId());
|
|
responseBean.setFilePath(documentEntity.getFilePath());
|
|
responseBean.setCreatedDate(documentEntity.getCreatedDate());
|
|
responseBean.setUpdatedDate(documentEntity.getUpdatedDate());
|
|
responseBean.setDocumentAttachmentId(documentEntity.getDocumentAttachmentId());
|
|
return responseBean;
|
|
})
|
|
.toList();
|
|
return documentResponseBeans;
|
|
}
|
|
|
|
public String deleteComment(Long amendmentId, Long commentId) {
|
|
|
|
CommunicationEntity data = communicationRepository.findByIdAndIsDeletedFalse(commentId)
|
|
.orElseThrow(() -> new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMMENT_NOT_FOUND)));
|
|
//cloned for old commentData
|
|
CommunicationEntity oldComment = Utils.getClonedEntityForData(data);
|
|
|
|
if (!data.getApplicationAmendmentRequest().getId().equals(amendmentId)) {
|
|
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.INVALID_AMENDMENT_FOR_COMMENT));
|
|
}
|
|
data.setIsDeleted(true);
|
|
data = communicationRepository.save(data);
|
|
|
|
/** This code is responsible for adding a version history log for the "soft deleting comment to amendment request" operation. **/
|
|
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldComment).newData(data).build());
|
|
|
|
return "Deleted Comment Successfully.";
|
|
}
|
|
|
|
public List<CommunicationResponseBean> getAmendmentComments(Long amendmentId) {
|
|
|
|
ApplicationAmendmentRequestEntity amendmentData = applicationAmendmentRequestService.validateApplicationAmendmentRequest(amendmentId);
|
|
List<CommunicationEntity> commentsList = communicationRepository.findByApplicationAmendmentRequestIdAndIsDeletedFalse(amendmentId);
|
|
if (commentsList == null) {
|
|
throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMMENT_NOT_FOUND));
|
|
}
|
|
List<CommunicationResponseBean> communicationResponseBeans=new ArrayList<>();
|
|
for(CommunicationEntity communicationEntity:commentsList){
|
|
List<DocumentResponseBean> communicationDocumentBeans=getDocumentResponseBean(communicationEntity);
|
|
CommunicationResponseBean communicationResponseBean=convertToCommunicationResponseBean(communicationEntity,communicationDocumentBeans);
|
|
communicationResponseBeans.add(communicationResponseBean);
|
|
}
|
|
return communicationResponseBeans;
|
|
}
|
|
|
|
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)));
|
|
//cloned for old data for communication
|
|
CommunicationEntity oldCommentData = Utils.getClonedEntityForData(existingComment);
|
|
|
|
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);
|
|
|
|
/** This code is responsible for adding a version history log for the "updating comment to amendment request" operation. **/
|
|
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldCommentData).newData(existingComment).build());
|
|
List<DocumentResponseBean> communicationDocumentBeans=getDocumentResponseBean(existingComment);
|
|
return convertToCommunicationResponseBean(existingComment,communicationDocumentBeans);
|
|
}
|
|
|
|
private CommunicationResponseBean convertToCommunicationResponseBean(CommunicationEntity entity,List<DocumentResponseBean> communicationResponseBean) {
|
|
|
|
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());
|
|
response.setSenderUserId(entity.getSenderUserId());
|
|
response.setReceiverUserId(entity.getReceiverUserId());
|
|
response.setId(entity.getId());
|
|
response.setDocuments(communicationResponseBean);
|
|
return response;
|
|
}
|
|
|
|
private CommunicationEntity convertToCommunicationCommentEntity(CommunicationRequestBean communicationReq, Long amendmentId,List<DocumentResponseBean> communicationDocumentBean,List<MultipartFile> files) {
|
|
|
|
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());
|
|
if(validator.checkIsPreInstructor()){
|
|
communicationEntity.setIntiatorType(CommunicationInitiatorTypeEnum.INSTRUCTOR.getValue());
|
|
communicationEntity.setSenderUserId(amendmentRequest.getApplicationEvaluationEntity().getUserId());
|
|
communicationEntity.setReceiverUserId(amendmentRequest.getApplicationEvaluationEntity().getAssignedApplicationsEntity().getApplication().getUserId());
|
|
} else if(Boolean.TRUE.equals(validator.checkIsBeneficiary()) || Boolean.TRUE.equals(validator.checkIsConfidi())) {
|
|
communicationEntity.setIntiatorType(CommunicationInitiatorTypeEnum.BENEFICIARY.getValue());
|
|
communicationEntity.setSenderUserId(amendmentRequest.getApplicationEvaluationEntity().getAssignedApplicationsEntity().getApplication().getUserId());
|
|
communicationEntity.setReceiverUserId(amendmentRequest.getApplicationEvaluationEntity().getUserId());
|
|
}
|
|
communicationEntity = communicationRepository.save(communicationEntity);
|
|
communicationDocumentBean=documentDao.uploadFiles(communicationEntity.getSenderUserId(),files,communicationEntity.getId(), DocumentSourceTypeEnum.COMMUNICATION, DocumentTypeEnum.DOCUMENT);
|
|
List<Long> communicationDocumentIds = communicationDocumentBean.stream()
|
|
.map(DocumentResponseBean::getId)
|
|
.collect(Collectors.toList());
|
|
String communicationDocumentId = communicationDocumentIds.stream()
|
|
.map(String::valueOf)
|
|
.collect(Collectors.joining(","));
|
|
communicationEntity.setDocuments(communicationDocumentId);
|
|
communicationEntity = communicationRepository.save(communicationEntity);
|
|
return communicationEntity;
|
|
}
|
|
}
|