323 lines
17 KiB
Java
323 lines
17 KiB
Java
package net.gepafin.tendermanagement.dao;
|
|
|
|
import net.gepafin.tendermanagement.config.Translator;
|
|
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
|
import net.gepafin.tendermanagement.entities.*;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.time.LocalDateTime;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
import net.gepafin.tendermanagement.enums.CallTypeEnum;
|
|
import net.gepafin.tendermanagement.model.request.*;
|
|
import net.gepafin.tendermanagement.model.response.*;
|
|
import net.gepafin.tendermanagement.repositories.*;
|
|
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
|
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
|
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
|
|
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.util.StringUtils;
|
|
import net.gepafin.tendermanagement.entities.LookUpDataEntity.LookUpDataTypeEnum;
|
|
|
|
@Component
|
|
public class CallDao {
|
|
|
|
@Autowired
|
|
private CallRepository callRepository;
|
|
|
|
@Autowired
|
|
private DocumentRepository documentRepository;
|
|
|
|
@Autowired
|
|
private EvaluationCriteriaRepository evaluationCriteriaRepository;
|
|
|
|
@Autowired
|
|
private FaqRepository faqRepository;
|
|
|
|
@Autowired
|
|
private RegionRepository regionRepository;
|
|
|
|
@Autowired
|
|
private LookUpDataRepository lookUpDataRepository;
|
|
|
|
@Autowired
|
|
private CallTargetAudienceChecklistRepository callTargetAudienceChecklistRepository;
|
|
|
|
@Autowired
|
|
private UserRepository userRepository;
|
|
|
|
public CreateCallResponseBean createCall(CreateCallRequest createCallRequest, Long userId) {
|
|
try {
|
|
CreateCallResponseBean createCallResponseBean=null;
|
|
CallEntity callEntity = convertToCallEntity(createCallRequest);
|
|
List<EvaluationCriteriaEntity> evaluationCriteriaEntities = convertToEvaluationCriteriaEntities(createCallRequest.getCriteria(), callEntity);
|
|
List<DocumentEntity> documentEntities = convertToDocumentEntities(createCallRequest.getDocs(), callEntity);
|
|
List<DocumentEntity> imageEntities=convertToDocumentEntities(createCallRequest.getImages(),callEntity);
|
|
List<FaqEntity> faqEntities = convertToFaqEntities(createCallRequest.getFaq(), callEntity, userId);
|
|
List<LookUpDataResponse> amiedTo=convertLookUpDataEntities(createCallRequest.getAimedTo(),callEntity,LookUpDataTypeEnum.AIMED_TO);
|
|
List<LookUpDataResponse> checkList=convertLookUpDataEntities(createCallRequest.getAimedTo(),callEntity,LookUpDataTypeEnum.CHECKLIST);
|
|
createCallResponseBean= assembleCreateCallResponseBean(callEntity, evaluationCriteriaEntities, documentEntities, faqEntities,imageEntities);
|
|
createCallResponseBean.setAimedTo(amiedTo);
|
|
createCallResponseBean.setCheckList(checkList);
|
|
return createCallResponseBean;
|
|
} catch (Exception e) {
|
|
throw new RuntimeException("Error processing create call request", e);
|
|
}
|
|
}
|
|
|
|
|
|
public CallEntity convertToCallEntity(CreateCallRequest createCallRequest) {
|
|
CallEntity callEntity = new CallEntity();
|
|
validateCallEntity(createCallRequest.getRegionId(), createCallRequest.getAmount());
|
|
RegionEntity region = regionRepository.findById(createCallRequest.getRegionId())
|
|
.orElseThrow(() -> new ResourceNotFoundException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.REGION_NOT_FOUND)));
|
|
callEntity.setRegion(region);
|
|
callEntity.setName(createCallRequest.getName());
|
|
callEntity.setDescriptionShort(createCallRequest.getDescriptionShort());
|
|
callEntity.setDescriptionLong(createCallRequest.getDescriptionLong());
|
|
callEntity.setStartDate(createCallRequest.getStartDate());
|
|
callEntity.setEndDate(createCallRequest.getEndDate());
|
|
callEntity.setStatus(String.valueOf(createCallRequest.getStatus()));
|
|
callEntity.setAmountMax(createCallRequest.getAmountMax());
|
|
callEntity.setAmount(createCallRequest.getAmountMax());
|
|
callEntity.setThreshold(createCallRequest.getThreshold());
|
|
callEntity.setConfidi(false);
|
|
if(createCallRequest.getConfidi()!=null){
|
|
callEntity.setConfidi(createCallRequest.getConfidi());
|
|
}
|
|
callEntity.setDocumentation_requested(createCallRequest.getDocumentationRequested());
|
|
callEntity = callRepository.save(callEntity);
|
|
return callEntity;
|
|
}
|
|
|
|
public List<EvaluationCriteriaEntity> convertToEvaluationCriteriaEntities(List<EvaluationCriteriaReq> criteriaReqList, CallEntity callEntity) {
|
|
List<EvaluationCriteriaEntity> evaluationCriteriaEntities = criteriaReqList.stream().map(req -> convertToEvaluationCriteriaEntity(req, callEntity)).collect(Collectors.toList());
|
|
evaluationCriteriaRepository.saveAll(evaluationCriteriaEntities);
|
|
return evaluationCriteriaEntities;
|
|
}
|
|
|
|
private EvaluationCriteriaEntity convertToEvaluationCriteriaEntity(EvaluationCriteriaReq criteriaReq, CallEntity callEntity) {
|
|
EvaluationCriteriaEntity criteriaEntity = new EvaluationCriteriaEntity();
|
|
validateEvolutionCrieteriaEntity(criteriaReq.getName());
|
|
criteriaEntity.setName(criteriaReq.getName());
|
|
criteriaEntity.setDescription(criteriaReq.getValue());
|
|
criteriaEntity.setScore(criteriaReq.getScore());
|
|
criteriaEntity.setCall(callEntity);
|
|
return criteriaEntity;
|
|
}
|
|
|
|
|
|
public List<DocumentEntity> convertToDocumentEntities(List<DocumentReq> documentReqList, CallEntity callEntity) {
|
|
List<DocumentEntity> documentEntities = documentReqList.stream().map(req -> convertToDocumentEntity(req, callEntity)).collect(Collectors.toList());
|
|
documentRepository.saveAll(documentEntities);
|
|
return documentEntities;
|
|
}
|
|
|
|
private DocumentEntity convertToDocumentEntity(DocumentReq documentReq, CallEntity callEntity) {
|
|
validateDocumentEntity(documentReq.getId(),documentReq.getFileName());
|
|
DocumentEntity documentEntity = documentRepository.findById(documentReq.getId());
|
|
if(documentEntity==null){
|
|
throw new ResourceNotFoundException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
|
|
}
|
|
documentEntity.setFileName(documentReq.getFileName());
|
|
documentEntity.setFilePath(documentReq.getUrl());
|
|
documentEntity.setCall(callEntity);
|
|
return documentEntity;
|
|
}
|
|
|
|
|
|
public List<FaqEntity> convertToFaqEntities(List<FaqReq> faqReqList, CallEntity callEntity, Long userId) {
|
|
List<FaqEntity> faqEntities = faqReqList.stream().map(req -> convertToFaqEntity(req, callEntity, userId)).collect(Collectors.toList());
|
|
faqRepository.saveAll(faqEntities);
|
|
return faqEntities;
|
|
}
|
|
|
|
private FaqEntity convertToFaqEntity(FaqReq faqReq, CallEntity callEntity, Long userId) {
|
|
FaqEntity faqEntity = new FaqEntity();
|
|
validateFaqEntity(faqReq.getQuestion());
|
|
UserEntity userEntity= userRepository.findById(userId)
|
|
.orElseThrow(() -> new ResourceNotFoundException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.LOOK_UP_DATA_NOT_VALID_MSG)));
|
|
faqEntity.setUser(userEntity);
|
|
faqEntity.setIsVisible(true);
|
|
if(faqReq.getIsVisible()!=null){
|
|
faqEntity.setIsVisible(faqReq.getIsVisible());
|
|
}
|
|
faqEntity.setQuestionShort(faqReq.getQuestionShort());
|
|
faqEntity.setQuestion(faqReq.getQuestion());
|
|
if(faqReq.getResponse()!=null ||faqReq.getResponseShort()!=null){
|
|
faqEntity.setResponseDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now()));
|
|
}
|
|
faqEntity.setResponseShort(faqReq.getResponseShort());
|
|
faqEntity.setResponse(faqReq.getResponse());
|
|
faqEntity.setCall(callEntity);
|
|
return faqEntity;
|
|
}
|
|
|
|
public void validateFaqEntity( String question) {
|
|
if (!StringUtils.hasText(question)) {
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.QUESTION_NOT_EMPTY_MSG));
|
|
}
|
|
}
|
|
|
|
public void validateDocumentEntity(Long documentId,String name) {
|
|
if(documentId==null){
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.DOCUMENT_ID_NOT_FOUND));
|
|
}
|
|
|
|
if (!StringUtils.hasText(name)) {
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.NAME_NOT_EMPTY_MSG));
|
|
}
|
|
}
|
|
|
|
public void validateEvolutionCrieteriaEntity(String name) {
|
|
if (!StringUtils.hasText(name)) {
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.NAME_NOT_EMPTY_MSG));
|
|
}
|
|
}
|
|
|
|
public void validateCallEntity(Long regionId, BigDecimal fundingAmount) {
|
|
if (regionId == null) {
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.REGION_NOT_FOUND_MSG));
|
|
}
|
|
|
|
if (fundingAmount == null || fundingAmount.compareTo(BigDecimal.ZERO) <= 0) {
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.AMOUNT_GREATER_THAN_ZERO_MSG));
|
|
}
|
|
}
|
|
public CreateCallResponseBean convertToCallResponseBean(CallEntity callEntity) {
|
|
CreateCallResponseBean createCallResponseBean = new CreateCallResponseBean();
|
|
createCallResponseBean.setId(callEntity.getId());
|
|
createCallResponseBean.setName(callEntity.getName());
|
|
createCallResponseBean.setDates(List.of(callEntity.getStartDate(), callEntity.getEndDate()));
|
|
createCallResponseBean.setDescriptionShort(callEntity.getDescriptionShort());
|
|
createCallResponseBean.setDescriptionLong(callEntity.getDescriptionLong());
|
|
createCallResponseBean.setStatus(CallTypeEnum.valueOf(callEntity.getStatus()));
|
|
createCallResponseBean.setRegionId(callEntity.getRegion().getId());
|
|
createCallResponseBean.setAmount(callEntity.getAmount());
|
|
createCallResponseBean.setAmountMax(callEntity.getAmountMax());
|
|
createCallResponseBean.setContactInfo(callEntity.getContactInfo());
|
|
createCallResponseBean.setSubmissionMethod(callEntity.getSubmissionMethod());
|
|
createCallResponseBean.setThreshold(callEntity.getThreshold());
|
|
createCallResponseBean.setDocumentationReqested(callEntity.getDocumentation_requested());
|
|
createCallResponseBean.setPriorityArea(callEntity.getPriorityArea());
|
|
createCallResponseBean.setCreatedDate(callEntity.getCreatedDate());
|
|
createCallResponseBean.setUpdatedDate(callEntity.getUpdatedDate());
|
|
return createCallResponseBean;
|
|
}
|
|
public EvaluationCriteriaResponseBean convertToEvaluationCriteriaResponseBean(EvaluationCriteriaEntity entity) {
|
|
EvaluationCriteriaResponseBean responseBean = new EvaluationCriteriaResponseBean();
|
|
responseBean.setId(entity.getId());
|
|
responseBean.setName(entity.getName());
|
|
responseBean.setDescription(entity.getDescription());
|
|
responseBean.setScore(entity.getScore());
|
|
responseBean.setCreatedDate(entity.getCreatedDate());
|
|
responseBean.setUpdatedDate(entity.getUpdatedDate());
|
|
return responseBean;
|
|
}
|
|
public DocumentResponseBean convertToDocumentResponseBean(DocumentEntity entity) {
|
|
DocumentResponseBean responseBean = new DocumentResponseBean();
|
|
responseBean.setId(entity.getId());
|
|
responseBean.setName(entity.getFileName());
|
|
responseBean.setDescription(entity.getDescription());
|
|
responseBean.setFilePath(entity.getFilePath());
|
|
responseBean.setCreatedDate(entity.getCreatedDate());
|
|
responseBean.setUpdatedDate(entity.getUpdatedDate());
|
|
return responseBean;
|
|
}
|
|
public FaqResponseBean convertToFaqResponseBean(FaqEntity entity) {
|
|
FaqResponseBean responseBean = new FaqResponseBean();
|
|
responseBean.setId(entity.getId());
|
|
responseBean.setQuestionShort(entity.getQuestionShort());
|
|
responseBean.setResponseShort(entity.getResponseShort());
|
|
responseBean.setResponse(entity.getResponse());
|
|
responseBean.setQuestion(entity.getQuestion());
|
|
responseBean.setUserId(entity.getUser().getId());
|
|
responseBean.setIsVisible(entity.getIsVisible());
|
|
responseBean.setCreatedDate(entity.getCreatedDate());
|
|
responseBean.setUpdatedDate(entity.getUpdatedDate());
|
|
return responseBean;
|
|
}
|
|
public CreateCallResponseBean assembleCreateCallResponseBean(
|
|
CallEntity callEntity,
|
|
List<EvaluationCriteriaEntity> evaluationCriteriaEntities,
|
|
List<DocumentEntity> documentEntities,
|
|
List<FaqEntity> faqEntities,List<DocumentEntity> images) {
|
|
|
|
CreateCallResponseBean callResponseBean = convertToCallResponseBean(callEntity);
|
|
|
|
List<EvaluationCriteriaResponseBean> evaluationCriteriaResponseBeans = evaluationCriteriaEntities.stream()
|
|
.map(this::convertToEvaluationCriteriaResponseBean)
|
|
.collect(Collectors.toList());
|
|
|
|
List<DocumentResponseBean> documentResponseBeans = documentEntities.stream()
|
|
.map(this::convertToDocumentResponseBean)
|
|
.collect(Collectors.toList());
|
|
|
|
List<FaqResponseBean> faqResponseBeans = faqEntities.stream()
|
|
.map(this::convertToFaqResponseBean)
|
|
.collect(Collectors.toList());
|
|
|
|
List<DocumentResponseBean> imagesResponseBean = images.stream()
|
|
.map(this::convertToDocumentResponseBean)
|
|
.collect(Collectors.toList());
|
|
CreateCallResponseBean createCallResponseBean =callResponseBean;
|
|
createCallResponseBean.setCriteria(evaluationCriteriaResponseBeans);
|
|
createCallResponseBean.setDocs(documentResponseBeans);
|
|
createCallResponseBean.setFaq(faqResponseBeans);
|
|
createCallResponseBean.setImages(imagesResponseBean);
|
|
return createCallResponseBean;
|
|
}
|
|
public List<LookUpDataResponse> convertLookUpDataEntities(List<LookUpDataReq> lookUpData, CallEntity callEntity, LookUpDataEntity.LookUpDataTypeEnum type) {
|
|
List<LookUpDataEntity> lookUpDataEntities = lookUpData.stream()
|
|
.map(req -> convertLookUpDataRequestIntoLookUpDataEntity(req, type))
|
|
.collect(Collectors.toList());
|
|
|
|
lookUpDataRepository.saveAll(lookUpDataEntities);
|
|
|
|
return createCallTargetAudienceCheckList(callEntity, lookUpDataEntities);
|
|
}
|
|
|
|
private List<LookUpDataResponse> createCallTargetAudienceCheckList(CallEntity callEntity, List<LookUpDataEntity> lookUpDataEntities) {
|
|
List<LookUpDataResponse> lookUpDataResponses=new ArrayList<>();
|
|
for(LookUpDataEntity lookUpDataEntity:lookUpDataEntities){
|
|
CallTargetAudienceChecklistEntity callTargetAudienceChecklistEntity=new CallTargetAudienceChecklistEntity();
|
|
callTargetAudienceChecklistEntity.setIsValidated(false);
|
|
callTargetAudienceChecklistEntity.setLookupData(lookUpDataEntity);
|
|
callTargetAudienceChecklistEntity.setCall(callEntity);
|
|
callTargetAudienceChecklistEntity= callTargetAudienceChecklistRepository.save(callTargetAudienceChecklistEntity);
|
|
lookUpDataResponses.add(convertToLookUpDataResponseBean(lookUpDataEntity,callTargetAudienceChecklistEntity));
|
|
}
|
|
return lookUpDataResponses;
|
|
}
|
|
|
|
private LookUpDataEntity convertLookUpDataRequestIntoLookUpDataEntity(LookUpDataReq req, LookUpDataEntity.LookUpDataTypeEnum type) {
|
|
if (req.getLookUpDataId() == null || req.getLookUpDataId().equals(BigDecimal.ZERO)) {
|
|
LookUpDataEntity newEntity = new LookUpDataEntity();
|
|
newEntity.setValue(req.getValue());
|
|
newEntity.setType(type.getValue());
|
|
return newEntity;
|
|
}
|
|
|
|
return lookUpDataRepository.findById(req.getLookUpDataId())
|
|
.orElseThrow(() -> new ResourceNotFoundException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.LOOK_UP_DATA_NOT_VALID_MSG)));
|
|
}
|
|
|
|
public LookUpDataResponse convertToLookUpDataResponseBean(LookUpDataEntity lookUpDataEntity,CallTargetAudienceChecklistEntity callTargetAudienceChecklistEntity) {
|
|
LookUpDataResponse lookUpDataResponse = new LookUpDataResponse();
|
|
lookUpDataResponse.setId(callTargetAudienceChecklistEntity.getId());
|
|
lookUpDataResponse.setLookUpDataId(lookUpDataEntity.getId());
|
|
lookUpDataResponse.setValue(lookUpDataEntity.getValue());
|
|
lookUpDataResponse.setTitle(lookUpDataEntity.getTitle());
|
|
lookUpDataResponse.setCreatedDate(lookUpDataEntity.getCreatedDate());
|
|
lookUpDataResponse.setUpdatedDate(lookUpDataEntity.getUpdatedDate());
|
|
return lookUpDataResponse;
|
|
}
|
|
}
|
|
|
|
|
|
|