Files
bflows-bandi-be/src/main/java/net/gepafin/tendermanagement/dao/CallDao.java
2025-02-28 11:42:11 +05:30

1122 lines
54 KiB
Java

package net.gepafin.tendermanagement.dao;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import jakarta.servlet.http.HttpServletRequest;
import net.gepafin.tendermanagement.entities.*;
import net.gepafin.tendermanagement.enums.*;
import net.gepafin.tendermanagement.model.request.*;
import net.gepafin.tendermanagement.model.response.*;
import net.gepafin.tendermanagement.model.util.SortBy;
import net.gepafin.tendermanagement.repositories.*;
import net.gepafin.tendermanagement.service.*;
import net.gepafin.tendermanagement.util.DateTimeUtil;
import net.gepafin.tendermanagement.util.LoggingUtil;
import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.util.Validator;
import org.h2.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.LookUpDataEntity.LookUpDataTypeEnum;
import net.gepafin.tendermanagement.service.impl.CallValidatorServiceImpl;
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 static net.gepafin.tendermanagement.enums.RoleStatusEnum.ROLE_SUPER_ADMIN;
import static net.gepafin.tendermanagement.util.Utils.setIfUpdated;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.hibernate.internal.util.collections.CollectionHelper.listOf;
@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 LookUpDataService lookUpDataService;
@Autowired
private CallTargetAudienceChecklistRepository callTargetAudienceChecklistRepository;
@Autowired
private FaqService faqService;
@Autowired
private FlowDao flowDao;
@Autowired
private FormDao formDao;
// @Value("${aws.s3.url.folder}")
// private String s3Folder;
@Autowired
private AmazonS3Service amazonS3Service;
@Autowired
private CriteriaFormFieldRepository criteriaFormFieldRepository;
@Autowired
private S3PathConfig s3PathConfig;
@Autowired
private BeneficiaryPreferredCallRepository beneficiaryPreferredCallRepository;
@Autowired
private Validator validator;
@Autowired
private CompanyService companyService;
@Autowired
private LoggingUtil loggingUtil;
@Autowired
private HttpServletRequest request;
@Autowired
private NotificationDao notificationDao;
@Autowired
private BeneficiaryRepository beneficiaryRepository;
@Autowired
private NotificationTypeRepository notificationTypeRepository;
@Autowired
private EvaluationFormDao evalualtionFormDao;
public CallResponse createCallStep1(CreateCallRequestStep1 createCallRequest, UserEntity userEntity) {
createCallRequest.setRegionId(userEntity.getRoleEntity().getRegion().getId());
CallEntity callEntity = convertToCallEntity(createCallRequest, userEntity);
updateFaq(createCallRequest.getFaq(), callEntity, userEntity,LookUpDataTypeEnum.FAQ);
updateLookUpData(callEntity, createCallRequest.getAimedTo(), LookUpDataTypeEnum.AIMED_TO);
CallResponse createCallResponseBean = getCallResponseBean(callEntity);
createCallResponseBean.setCurrentStep(GepafinConstant.STEP_1);
return createCallResponseBean;
}
public byte[] downloadCallDocumentsAsZip(Long callId) {
List<DocumentEntity> documents = documentRepository.findBySourceIdAndSourceAndTypeAndIsDeletedFalse(callId, DocumentSourceTypeEnum.CALL.getValue(),DocumentTypeEnum.DOCUMENT.getValue());
if (documents.isEmpty()) {
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
}
try (ByteArrayOutputStream zipOutputStream = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(zipOutputStream)) {
for (DocumentEntity document : documents) {
String s3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.CALL, callId, 0L,0L);
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, document.getFilePath())) {
String fileName = Utils.extractFileName(document.getFilePath());
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
IOUtils.copy(fileInputStream, zos);
zos.closeEntry();
} catch (IOException e) {
throw new RuntimeException("Error downloading or adding document to ZIP: " + document.getFileName(), e);
}
}
zos.finish();
return zipOutputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Error while creating ZIP file", e);
}
}
public CallEntity convertToCallEntity(CreateCallRequestStep1 createCallRequest, UserEntity userEntity) {
CallEntity callEntity = new CallEntity();
// validateCallEntity(createCallRequest);
RegionEntity region = regionRepository.findById(createCallRequest.getRegionId())
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.REGION_NOT_FOUND)));
callEntity.setRegion(region);
callEntity.setName(createCallRequest.getName());
callEntity.setDescriptionShort(createCallRequest.getDescriptionShort());
callEntity.setDescriptionLong(createCallRequest.getDescriptionLong());
List<LocalDateTime> dates = createCallRequest.getDates();
if(dates!=null) {
if(dates.size()>1) {
callEntity.setStartDate(dates.get(0));
callEntity.setEndDate(dates.get(1));
}
}
callEntity.setStatus(CallStatusEnum.DRAFT.getValue());
callEntity.setEvaluationVersion(createCallRequest.getEvaluationVersion().getValue());
callEntity.setAmountMax(createCallRequest.getAmountMax());
callEntity.setAmount(createCallRequest.getAmount());
callEntity.setConfidi(false);
if (createCallRequest.getConfidi() != null) {
callEntity.setConfidi(createCallRequest.getConfidi());
}
callEntity.setDocumentationRequested(createCallRequest.getDocumentationRequested());
if (createCallRequest.getAmountMin() != null && createCallRequest.getAmountMin().compareTo(BigDecimal.ZERO) < 0) {
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.AMOUNT_GREATER_THAN_ZERO_MSG));
}
callEntity.setAmountMin(createCallRequest.getAmountMin());
if(createCallRequest.getEmail()!=null && Boolean.FALSE.equals(Utils.isValidEmail(createCallRequest.getEmail()))){
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.VALIDATION_EMAIL,createCallRequest.getEmail()));
}
callEntity.setEmail(createCallRequest.getEmail());
callEntity.setPhoneNumber(createCallRequest.getPhoneNumber());
callEntity.setStartTime(DateTimeUtil.parseTime(createCallRequest.getStartTime()));
callEntity.setEndTime(DateTimeUtil.parseTime(createCallRequest.getEndTime()));
callEntity.setHub(userEntity.getHub());
callEntity.setNumberOfCheck(createCallRequest.getNumberOfCheck());
callEntity.setAppointmentTemplateId(createCallRequest.getAppointmentTemplateId());
callEntity = callRepository.save(callEntity);
/** This code is responsible for adding a version history log for the "Create Call" operation. **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.INSERT).oldData(null).newData(callEntity).build());
return callEntity;
}
public List<EvaluationCriteriaEntity> convertToEvaluationCriteriaEntities(
List<EvaluationCriteriaReq> criteriaReqList, CallEntity callEntity, LookUpDataTypeEnum type) {
if (criteriaReqList == null) {
return null;
}
List<EvaluationCriteriaEntity> existingCriteria = evaluationCriteriaRepository.findByCallIdAndLookupDataTypeAndIsDeletedFalse(callEntity.getId(), type.getValue());
List<Long> incomingIds = criteriaReqList.stream().map(EvaluationCriteriaReq::getId)
.filter(id -> id != null && id > 0).collect(Collectors.toList());
existingCriteria.stream().filter(criteria -> !incomingIds.contains(criteria.getId())).forEach(this::softDeleteEvaluationCriteria);
List<EvaluationCriteriaEntity> evaluationCriteriaEntities = criteriaReqList.stream()
.map(req -> convertToEvaluationCriteriaEntity(req, callEntity, type)).collect(Collectors.toList());
return evaluationCriteriaEntities;
}
private void softDeleteEvaluationCriteria(EvaluationCriteriaEntity evaluationCriteriaEntity) {
EvaluationCriteriaEntity oldEvaluationCriteriaEntity = Utils.getClonedEntityForData(evaluationCriteriaEntity);
evaluationCriteriaEntity.setIsDeleted(true);
evaluationCriteriaRepository.save(evaluationCriteriaEntity);
/** This code is responsible for adding a version history log for the "soft delete evaluation criteria" operation **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldEvaluationCriteriaEntity).newData(evaluationCriteriaEntity).build());
List<CriteriaFormFieldEntity> list = criteriaFormFieldRepository
.findByEvaluationCriteriaIdAndIsDeletedFalse(evaluationCriteriaEntity.getId());
if(Boolean.FALSE.equals(CollectionUtils.isEmpty(list))) {
list.stream().peek(data->{
CriteriaFormFieldEntity oldCriteriaFormFieldEntity = Utils.getClonedEntityForData(data);
data.setIsDeleted(Boolean.TRUE);
/** This code is responsible for adding a version history log for the "soft delete criteria form field" operation **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldCriteriaFormFieldEntity).newData(data).build());
});
criteriaFormFieldRepository.saveAll(list);
}
}
private EvaluationCriteriaEntity convertToEvaluationCriteriaEntity(EvaluationCriteriaReq criteriaReq,
CallEntity callEntity, LookUpDataTypeEnum type) {
EvaluationCriteriaEntity criteriaEntity = null;
EvaluationCriteriaEntity oldCriteriaEntity = null;
VersionActionTypeEnum actionType = VersionActionTypeEnum.INSERT;
LookUpDataEntity lookupDataEntity = lookUpDataService.getOrCreateLookUpDataEntity(criteriaReq, type);
if (criteriaReq.getId() != null && criteriaReq.getId() > 0) {
criteriaEntity = evaluationCriteriaRepository.findById(criteriaReq.getId())
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.EVALUATION_CRITERIA_NOT_FOUND)));
oldCriteriaEntity = Utils.getClonedEntityForData(oldCriteriaEntity);
actionType = VersionActionTypeEnum.UPDATE;
} else {
criteriaEntity = new EvaluationCriteriaEntity();
criteriaEntity.setCall(callEntity);
criteriaEntity.setLookupData(lookupDataEntity);
criteriaEntity.setScore(0L);
criteriaEntity.setIsDeleted(false);
actionType = VersionActionTypeEnum.INSERT;
}
setIfUpdated(criteriaEntity::getScore, criteriaEntity::setScore, criteriaReq.getScore());
if (Boolean.FALSE.equals(criteriaEntity.getLookupData().getId().equals(lookupDataEntity.getId()))) {
criteriaEntity.setLookupData(lookupDataEntity);
}
criteriaEntity = evaluationCriteriaRepository.save(criteriaEntity) ;
/** This code is responsible for adding a version history log for the "create or update evaluation criteria" operation **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(actionType).oldData(oldCriteriaEntity).newData(criteriaEntity).build());
return criteriaEntity;
}
public List<DocumentEntity> convertToDocumentEntities(List<DocumentReq> documentReqList, Long sourceId,
DocumentTypeEnum documentType) {
if (documentReqList == null) {
return null;
}
List<DocumentEntity> existingDocuments = documentRepository
.findBySourceIdAndSourceAndTypeAndIsDeletedFalse(sourceId, DocumentSourceTypeEnum.CALL.getValue(), documentType.getValue());
List<Long> incomingIds = documentReqList.stream().map(DocumentReq::getId).filter(id -> id != null && id > 0)
.collect(Collectors.toList());
existingDocuments.stream().filter(document -> !incomingIds.contains(document.getId()))
.forEach(this::softDeleteDocument);
List<DocumentEntity> documentEntities = documentReqList.stream()
.map(req -> convertToDocumentEntity(req, sourceId)).collect(Collectors.toList());
// documentRepository.saveAll(documentEntities);
return documentEntities;
}
private void softDeleteDocument(DocumentEntity documentEntity) {
DocumentEntity oldDocumentEntity = Utils.getClonedEntityForData(documentEntity);
documentEntity.setIsDeleted(true);
documentEntity = documentRepository.save(documentEntity);
/** This code is responsible for adding a version history log for the "soft delete for document" operation **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldDocumentEntity).newData(documentEntity).build());
}
private DocumentEntity convertToDocumentEntity(DocumentReq documentReq,Long sourceId) {
validateDocumentEntity(documentReq.getId());
DocumentEntity documentEntity = documentRepository.findByIdAndSourceIdAndSourceAndIsDeletedFalse(documentReq.getId(),sourceId, DocumentSourceTypeEnum.CALL.getValue())
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)));
return documentEntity;
}
public List<FaqEntity> updateFaq(List<FaqReq> faqReqList, CallEntity callEntity, UserEntity userEntity, LookUpDataTypeEnum type) {
if (faqReqList == null) {
return null;
}
List<FaqEntity> existingFaqEntities = faqRepository.findByCallIdAndIsDeletedFalse(callEntity.getId());
List<Long> incomingIds = faqReqList.stream()
.map(FaqReq::getId)
.filter(id -> id != null && id > 0)
.collect(Collectors.toList());
existingFaqEntities.stream()
.filter(entity -> !incomingIds.contains(entity.getId()))
.forEach(this::softDeleteFaq);
List<FaqEntity> faqEntities = faqReqList.stream()
.map(req -> faqService.createOrUpdateFaqEntity(req, callEntity, userEntity, type))
.collect(Collectors.toList());
return faqEntities;
}
public void validateDocumentEntity(Long documentId) {
if (documentId == null || documentId < 1) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.DOCUMENT_ID_NOT_FOUND));
}
}
public void validateEvaluationCriteriaEntity(String name) {
if (!StringUtils.hasText(name)) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.NAME_NOT_EMPTY_MSG));
}
}
public CallResponse convertToCallResponseBean(CallEntity callEntity) {
CallResponse createCallResponseBean = new CallResponse();
createCallResponseBean.setId(callEntity.getId());
createCallResponseBean.setName(callEntity.getName());
List<LocalDateTime> dates = new ArrayList<>();
dates.add(callEntity.getStartDate());
dates.add(callEntity.getEndDate());
createCallResponseBean.setDates(dates);
createCallResponseBean.setDescriptionShort(callEntity.getDescriptionShort());
createCallResponseBean.setDescriptionLong(callEntity.getDescriptionLong());
createCallResponseBean.setStatus(CallStatusEnum.valueOf(callEntity.getStatus()));
createCallResponseBean.setEvaluationVersion(EvaluationVersionEnum.valueOf(callEntity.getEvaluationVersion()));
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.setDocumentationRequested(callEntity.getDocumentationRequested());
createCallResponseBean.setPriorityArea(callEntity.getPriorityArea());
createCallResponseBean.setConfidi(callEntity.getConfidi());
createCallResponseBean.setAmountMin(callEntity.getAmountMin());
createCallResponseBean.setPhoneNumber(callEntity.getPhoneNumber());
createCallResponseBean.setEndTime(callEntity.getEndTime());
createCallResponseBean.setStartTime(callEntity.getStartTime());
createCallResponseBean.setEmail(callEntity.getEmail());
createCallResponseBean.setCreatedDate(callEntity.getCreatedDate());
createCallResponseBean.setUpdatedDate(callEntity.getUpdatedDate());
return createCallResponseBean;
}
public EvaluationCriteriaResponseBean convertToEvaluationCriteriaResponseBean(EvaluationCriteriaEntity entity) {
EvaluationCriteriaResponseBean responseBean = new EvaluationCriteriaResponseBean();
responseBean.setId(entity.getId());
responseBean.setLookUpDataId(entity.getLookupData().getId());
responseBean.setTitle(entity.getLookupData().getTitle());
responseBean.setValue(entity.getLookupData().getValue());
responseBean.setResponse(entity.getLookupData().getResponse());
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.setType(DocumentTypeEnum.valueOf(entity.getType()));
responseBean.setSource(DocumentSourceTypeEnum.valueOf(entity.getSource()));
responseBean.setSourceId(entity.getSourceId());
responseBean.setFilePath(entity.getFilePath());
responseBean.setCreatedDate(entity.getCreatedDate());
responseBean.setUpdatedDate(entity.getUpdatedDate());
return responseBean;
}
public CallResponse assembleCreateCallResponseBean(CallEntity callEntity,
List<EvaluationCriteriaEntity> evaluationCriteriaEntities, List<DocumentEntity> documentEntities,
List<DocumentEntity> images) {
CallResponse 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<DocumentResponseBean> imagesResponseBean = images.stream().map(this::convertToDocumentResponseBean)
.collect(Collectors.toList());
CallResponse createCallResponseBean = callResponseBean;
createCallResponseBean.setCriteria(evaluationCriteriaResponseBeans);
createCallResponseBean.setDocs(documentResponseBeans);
createCallResponseBean.setImages(imagesResponseBean);
return createCallResponseBean;
}
// public List<LookUpDataResponse> convertLookUpDataEntities(List<LookUpDataReq> lookUpData, CallEntity callEntity,
// LookUpDataEntity.LookUpDataTypeEnum type) {
// if(lookUpData == null) {
// return null;
// }
// List<LookUpDataEntity> lookUpDataEntities = lookUpData.stream()
// .map(req -> lookUpDataService.getOrCreateLookUpDataEntity(req, type)).collect(Collectors.toList());
//
// 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.setIsDeleted(false);
// callTargetAudienceChecklistEntity = callTargetAudienceChecklistRepository
// .save(callTargetAudienceChecklistEntity);
// versionHistoryRequest.setOldData(null);
// versionHistoryRequest.setNewData(callTargetAudienceChecklistEntity);
// versionHistoryRequest.setActionType(VersionActionTypeEnum.INSERT);
// versionHistoryRequest.setRequest(request);
// loggingUtil.addVersionHistory(versionHistoryRequest);
// lookUpDataResponses.add(convertToLookUpDataResponseBean(callTargetAudienceChecklistEntity));
// }
// return lookUpDataResponses;
// }
public LookUpDataResponse convertToLookUpDataResponseBean(
CallTargetAudienceChecklistEntity callTargetAudienceChecklistEntity) {
LookUpDataResponse lookUpDataResponse = new LookUpDataResponse();
LookUpDataEntity lookUpDataEntity = callTargetAudienceChecklistEntity.getLookupData();
lookUpDataResponse.setId(callTargetAudienceChecklistEntity.getId());
lookUpDataResponse.setLookUpDataId(lookUpDataEntity.getId());
lookUpDataResponse.setValue(lookUpDataEntity.getValue());
lookUpDataResponse.setTitle(lookUpDataEntity.getTitle());
lookUpDataResponse.setResponse(lookUpDataEntity.getResponse());
lookUpDataResponse.setCreatedDate(callTargetAudienceChecklistEntity.getCreatedDate());
lookUpDataResponse.setUpdatedDate(callTargetAudienceChecklistEntity.getUpdatedDate());
return lookUpDataResponse;
}
public CallEntity validateCall(Long callId) {
return callRepository.findById(callId).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.CALL_NOT_FOUND)));
}
public CallResponse getCallById(HttpServletRequest request,UserEntity user, CallEntity callEntity, Long companyId) {
Long userId = user.getId();
Long callId = callEntity.getId();
BeneficiaryPreferredCallEntity preferredCall;
if (companyId != null) {
validator.validateUserWithCompany(request, companyId);
UserWithCompanyEntity userWithCompanyEntity=companyService.getUserWithCompany(user.getId(),companyId);
preferredCall = beneficiaryPreferredCallRepository
.findByUserIdAndCallIdAndUserWithCompanyIdAndIsDeletedFalse(userId, callId, userWithCompanyEntity.getId())
.orElse(null);
} else {
preferredCall = beneficiaryPreferredCallRepository
.findByUserIdAndCallIdInAndIsDeletedFalse(userId, List.of(callId))
.stream()
.findFirst()
.orElse(null);
}
CallResponse callResponse = getCallResponseBean(callEntity);
callResponse.setPreferredCallId(preferredCall != null ? preferredCall.getId() : null);
return callResponse;
}
public CallResponse createCallStep2(CallEntity callEntity, CreateCallRequestStep2 createCallRequest, UserEntity user) {
// validateUpdate(callEntity);
if(createCallRequest.getThreshold() != null && Boolean.FALSE.equals(createCallRequest.getThreshold().equals(callEntity.getThreshold()))) {
CallEntity oldCallEntity = Utils.getClonedEntityForData(callEntity);
setIfUpdated(callEntity::getThreshold, callEntity::setThreshold, createCallRequest.getThreshold());
callEntity = callRepository.save(callEntity);
/** This code is responsible for adding a version history log for the "update call step 2" operation **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldCallEntity).newData(callEntity).build());
}
convertToEvaluationCriteriaEntities(createCallRequest.getCriteria(), callEntity, LookUpDataTypeEnum.EVALUATION_CRITERIA);
convertToDocumentEntities(createCallRequest.getDocs(), callEntity.getId(), DocumentTypeEnum.DOCUMENT);
convertToDocumentEntities(createCallRequest.getImages(), callEntity.getId(), DocumentTypeEnum.IMAGES);
updateLookUpData(callEntity, createCallRequest.getCheckList(), LookUpDataTypeEnum.CHECKLIST);
CallResponse createCallResponseBean = getCallResponseBean(callEntity);
createCallResponseBean.setCurrentStep(GepafinConstant.STEP_2);
return createCallResponseBean;
}
public void validateUpdate(CallEntity callEntity) {
if(callEntity.getStatus().equals(CallStatusEnum.PUBLISH.getValue())) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.PUBLISHED_CALL_NOT_UPDATE));
}
}
public void isValidDateRange(UpdateCallRequestStep1 updateCallRequest, CallEntity callEntity) {
List<LocalDateTime> dates = updateCallRequest.getDates();
LocalDate startDate = (dates != null && dates.size() > 0 && dates.get(0) != null)
? dates.get(0).toLocalDate()
: null;
LocalDate endDate = (dates != null && dates.size() > 1 && dates.get(1) != null)
? dates.get(1).toLocalDate()
: null;
Boolean isValid = true;
if (startDate != null && endDate != null && startDate.isAfter(endDate)) {
isValid = false;
} else if (startDate != null && endDate == null && callEntity.getEndDate() != null
&& startDate.isAfter(callEntity.getEndDate().toLocalDate())) {
isValid = false;
} else if (startDate == null && endDate != null && callEntity.getStartDate() != null
&& callEntity.getStartDate().toLocalDate().isAfter(endDate)) {
isValid = false;
}
if (Boolean.FALSE.equals(isValid)) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.INVALID_DATE_MSG));
}
}
public CallResponse updateCallStep1(HttpServletRequest request,CallEntity callEntity, UpdateCallRequestStep1 updateCallRequest, UserEntity userEntity) {
CallEntity oldCallEntity = Utils.getClonedEntityForData(callEntity);
isValidDateRange(updateCallRequest, callEntity);
setIfUpdated(callEntity::getName, callEntity::setName, updateCallRequest.getName());
setIfUpdated(callEntity::getDescriptionShort, callEntity::setDescriptionShort,
updateCallRequest.getDescriptionShort());
setIfUpdated(callEntity::getDescriptionLong, callEntity::setDescriptionLong,
updateCallRequest.getDescriptionLong());
List<LocalDateTime> dates=updateCallRequest.getDates();
boolean isEndDateUpdated = false;
boolean isEndTimeUpdated = false;
if (dates != null && dates.size()>1) {
if (dates.size() > 0) {
setIfUpdated(callEntity::getStartDate, callEntity::setStartDate, dates.get(0));
}
if (dates.size() > 1) {
LocalDate requestEndDate = dates.get(1).toLocalDate(); // Extract only the date
LocalDate storedEndDate = callEntity.getEndDate().toLocalDate(); // Extract only the date
if (!requestEndDate.equals(storedEndDate)) { // Check if dates are different
setIfUpdated(callEntity::getEndDate, callEntity::setEndDate, dates.get(1));
callEntity.setStatus(CallStatusEnum.PUBLISH.getValue());
callRepository.save(callEntity);
isEndDateUpdated = true;
}
}
}
if (updateCallRequest.getEndTime() != null) {
LocalTime requestEndTime = DateTimeUtil.parseTime(updateCallRequest.getEndTime());
LocalTime storedEndTime = callEntity.getEndTime();
if (!requestEndTime.equals(storedEndTime)) {
setIfUpdated(callEntity::getEndTime, callEntity::setEndTime, DateTimeUtil.parseTime(updateCallRequest.getEndTime()));
callEntity.setStatus(CallStatusEnum.PUBLISH.getValue());
callRepository.save(callEntity);
isEndTimeUpdated = true;
}
}
if (isEndDateUpdated || isEndTimeUpdated) {
loggingUtil.logUserAction(UserActionRequest.builder()
.request(request)
.actionType(UserActionLogsEnum.UPDATE)
.actionContext(UserActionContextEnum.UPDATE_CALL_END_DATE_AND_TIME)
.build());
}
// setIfUpdated(callEntity::getStartDate, callEntity::setStartDate, updateCallRequest.getStartDate());
// setIfUpdated(callEntity::getEndDate, callEntity::setEndDate, updateCallRequest.getEndDate());
setIfUpdated(callEntity::getAmount, callEntity::setAmount, updateCallRequest.getAmount());
setIfUpdated(callEntity::getAmountMax, callEntity::setAmountMax, updateCallRequest.getAmountMax());
setIfUpdated(callEntity::getDocumentationRequested, callEntity::setDocumentationRequested,
updateCallRequest.getDocumentationRequested());
if (updateCallRequest.getAmountMin() != null && updateCallRequest.getAmountMin().compareTo(BigDecimal.ZERO) < 0) {
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.AMOUNT_GREATER_THAN_ZERO_MSG));
}
if(updateCallRequest.getEmail()!=null && Boolean.FALSE.equals(Utils.isValidEmail(updateCallRequest.getEmail()))){
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.VALIDATION_EMAIL,updateCallRequest.getEmail()));
}
setIfUpdated(callEntity::getAmountMin, callEntity::setAmountMin, updateCallRequest.getAmountMin());
setIfUpdated(callEntity::getEmail, callEntity::setEmail, updateCallRequest.getEmail());
setIfUpdated(callEntity::getPhoneNumber, callEntity::setPhoneNumber, updateCallRequest.getPhoneNumber());
setIfUpdated(callEntity::getStartTime, callEntity::setStartTime, DateTimeUtil.parseTime(updateCallRequest.getStartTime()));
setIfUpdated(callEntity::getConfidi, callEntity::setConfidi, updateCallRequest.getConfidi());
setIfUpdated(callEntity::getEvaluationVersion, callEntity::setEvaluationVersion, updateCallRequest.getEvaluationVersion().getValue());
setIfUpdated(callEntity::getNumberOfCheck, callEntity::setNumberOfCheck, updateCallRequest.getNumberOfCheck());
setIfUpdated(callEntity::getAppointmentTemplateId, callEntity::setAppointmentTemplateId, updateCallRequest.getAppointmentTemplateId());
callEntity = callRepository.save(callEntity);
/** This code is responsible for adding a version history log for the "update call step 1" operation **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldCallEntity).newData(callEntity).build());
updateLookUpData(callEntity, updateCallRequest.getAimedTo(), LookUpDataTypeEnum.AIMED_TO);
updateFaq(updateCallRequest.getFaq(), callEntity, userEntity, LookUpDataTypeEnum.FAQ);
CallResponse createCallResponseBean = getCallResponseBean(callEntity);
createCallResponseBean.setCurrentStep(GepafinConstant.STEP_1);
return createCallResponseBean;
}
private void softDeleteFaq(FaqEntity faqEntity) {
FaqEntity oldFaqEntity = Utils.getClonedEntityForData(faqEntity);
faqEntity.setIsDeleted(true);
faqRepository.save(faqEntity);
/** This code is responsible for adding a version history log for the "soft delete faq" operation **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldFaqEntity).newData(faqEntity).build());
}
private void updateLookUpData(CallEntity callEntity, List<LookUpDataReq> lookupDataReqList, LookUpDataTypeEnum type) {
if (lookupDataReqList == null) {
return;
}
List<CallTargetAudienceChecklistEntity> existingChecklist = callTargetAudienceChecklistRepository
.findByCallIdAndLookupDataTypeAndIsDeletedFalse(callEntity.getId(), type.getValue());
List<Long> incomingIds = lookupDataReqList.stream().map(LookUpDataReq::getId)
.filter(id -> id != null && id > 0).collect(Collectors.toList());
existingChecklist.stream().filter(checklist -> !incomingIds.contains(checklist.getId()))
.forEach(this::softDeleteCallTargetAudienceChecklist);
lookupDataReqList
.forEach(lookUpDataReq -> createOrUpdateCallTargetAudienceChecklist(lookUpDataReq, callEntity, type));
}
private void createOrUpdateCallTargetAudienceChecklist(LookUpDataReq lookUpDataReq, CallEntity callEntity,
LookUpDataTypeEnum type) {
CallTargetAudienceChecklistEntity checklistEntity = null;
CallTargetAudienceChecklistEntity oldChecklistEntity = null;
VersionActionTypeEnum actionType = VersionActionTypeEnum.INSERT;
LookUpDataEntity lookupDataEntity = lookUpDataService.getOrCreateLookUpDataEntity(lookUpDataReq, type);
if (lookUpDataReq.getId() != null && lookUpDataReq.getId() > 0) {
checklistEntity = callTargetAudienceChecklistRepository.findById(lookUpDataReq.getId())
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.CALL_NOT_FOUND)));
if (Boolean.FALSE.equals(checklistEntity.getLookupData().getId().equals(lookupDataEntity.getId()))) {
checklistEntity.setLookupData(lookupDataEntity);
}
oldChecklistEntity = Utils.getClonedEntityForData(checklistEntity);
actionType = VersionActionTypeEnum.UPDATE;
} else {
checklistEntity = new CallTargetAudienceChecklistEntity();
checklistEntity.setCall(callEntity);
checklistEntity.setLookupData(lookupDataEntity);
checklistEntity.setIsValidated(false);
checklistEntity.setIsDeleted(false);
actionType = VersionActionTypeEnum.INSERT;
}
checklistEntity = callTargetAudienceChecklistRepository.save(checklistEntity);
/** This code is responsible for adding a version history log for the "create or update aimedTo Checklist" operation **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(actionType).oldData(oldChecklistEntity).newData(checklistEntity).build());
}
private void softDeleteCallTargetAudienceChecklist(
CallTargetAudienceChecklistEntity callTargetAudienceChecklistEntity) {
CallTargetAudienceChecklistEntity oldCallTargetAudienceChecklistEntity = Utils.getClonedEntityForData(callTargetAudienceChecklistEntity);
callTargetAudienceChecklistEntity.setIsDeleted(true);
callTargetAudienceChecklistRepository.save(callTargetAudienceChecklistEntity);
/** This code is responsible for adding a version history log for the "soft delete for aimedTo or Checklist" operation **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldCallTargetAudienceChecklistEntity).newData(callTargetAudienceChecklistEntity).build());
}
public CallDetailsResponseBean convertToCallDetailsResponseBean(CallEntity callEntity) {
CallDetailsResponseBean callDetailsResponseBean = new CallDetailsResponseBean();
callDetailsResponseBean.setId(callEntity.getId());
callDetailsResponseBean.setName(callEntity.getName());
List<LocalDateTime> dates = new ArrayList<>();
dates.add(callEntity.getStartDate());
dates.add(callEntity.getEndDate());
callDetailsResponseBean.setDates(dates);
callDetailsResponseBean.setConfidi(callEntity.getConfidi());
callDetailsResponseBean.setDescriptionShort(callEntity.getDescriptionShort());
callDetailsResponseBean.setDescriptionLong(callEntity.getDescriptionLong());
callDetailsResponseBean.setStatus(CallStatusEnum.valueOf(callEntity.getStatus()));
callDetailsResponseBean.setEvaluationVersion(EvaluationVersionEnum.valueOf(callEntity.getEvaluationVersion()));
callDetailsResponseBean.setRegionId(callEntity.getRegion().getId());
callDetailsResponseBean.setAmount(callEntity.getAmount());
callDetailsResponseBean.setAmountMax(callEntity.getAmountMax());
callDetailsResponseBean.setContactInfo(callEntity.getContactInfo());
callDetailsResponseBean.setSubmissionMethod(callEntity.getSubmissionMethod());
callDetailsResponseBean.setThreshold(callEntity.getThreshold());
callDetailsResponseBean.setDocumentationRequested(callEntity.getDocumentationRequested());
callDetailsResponseBean.setPriorityArea(callEntity.getPriorityArea());
callDetailsResponseBean.setAmountMin(callEntity.getAmountMin());
callDetailsResponseBean.setEmail(callEntity.getEmail());
callDetailsResponseBean.setEndTime(callEntity.getEndTime());
callDetailsResponseBean.setStartTime(callEntity.getStartTime());
callDetailsResponseBean.setPhoneNumber(callEntity.getPhoneNumber());
callDetailsResponseBean.setCreatedDate(callEntity.getCreatedDate());
callDetailsResponseBean.setUpdatedDate(callEntity.getUpdatedDate());
callDetailsResponseBean.setNumberOfCheck(callEntity.getNumberOfCheck());
callDetailsResponseBean.setAppointmentTemplateId(callEntity.getAppointmentTemplateId());
return callDetailsResponseBean;
}
private CallResponse getCallResponseBean(CallEntity callEntity) {
List<DocumentEntity> documentEntities = documentRepository.findBySourceIdAndSourceAndTypeAndIsDeletedFalse(callEntity.getId(),DocumentSourceTypeEnum.CALL.getValue()
, DocumentTypeEnum.DOCUMENT.getValue());
List<DocumentEntity> imageEntities = documentRepository.findBySourceIdAndSourceAndTypeAndIsDeletedFalse(callEntity.getId(), DocumentSourceTypeEnum.CALL.getValue()
, DocumentTypeEnum.IMAGES.getValue());
List<LookUpDataResponse> amiedTo = callTargetAudienceChecklistRepository
.findByCallIdAndLookupDataTypeAndIsDeletedFalse(callEntity.getId(), LookUpDataTypeEnum.AIMED_TO.getValue()).stream()
.map(this::convertToLookUpDataResponseBean).toList();
List<LookUpDataResponse> checkList = callTargetAudienceChecklistRepository
.findByCallIdAndLookupDataTypeAndIsDeletedFalse(callEntity.getId(), LookUpDataTypeEnum.CHECKLIST.getValue()).stream()
.map(this::convertToLookUpDataResponseBean).toList();
List<EvaluationCriteriaEntity> evaluationCriteriaEntities = evaluationCriteriaRepository
.findByCallIdAndLookupDataTypeAndIsDeletedFalse(callEntity.getId(), LookUpDataTypeEnum.EVALUATION_CRITERIA.getValue());
CallResponse createCallResponseBean = assembleCreateCallResponseBean(callEntity, evaluationCriteriaEntities,
documentEntities, imageEntities);
createCallResponseBean.setFaq(faqService.getFaqByCallId(callEntity.getId()));
createCallResponseBean.setAimedTo(amiedTo);
createCallResponseBean.setCheckList(checkList);
createCallResponseBean.setNumberOfCheck(callEntity.getNumberOfCheck());
createCallResponseBean.setAppointmentTemplateId(callEntity.getAppointmentTemplateId());
return createCallResponseBean;
}
public List<CallDetailsResponseBean> getAllCalls(HttpServletRequest request,UserEntity user, Long companyId,Boolean onlyPreferredCall) {
String type = user.getRoleEntity().getRoleType();
List<String> callStatusList = CallStatusEnum.getStatusValues();
if (Boolean.FALSE.equals(ROLE_SUPER_ADMIN.getValue().equals(type))) {
callStatusList = List.of(CallStatusEnum.PUBLISH.getValue());
}
List<CallEntity> calls;
if (Boolean.TRUE.equals(onlyPreferredCall) && companyId == null) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.COMPANY_ID_REQUIRED_FOR_PREFERRED_CALL));
}
if (Boolean.TRUE.equals(onlyPreferredCall)) {
validator.validateUserWithCompany(request, companyId);
UserWithCompanyEntity userWithCompanyEntity=companyService.getUserWithCompany(user.getId(),companyId);
List<BeneficiaryPreferredCallEntity> preferredCalls = beneficiaryPreferredCallRepository
.findByUserIdAndUserWithCompanyIdAndIsDeletedFalse(user.getId(), userWithCompanyEntity.getId());
List<Long> preferredCallIds = preferredCalls.stream()
.map(BeneficiaryPreferredCallEntity::getCallId)
.collect(Collectors.toList());
calls = callRepository.findByIdInAndStatusIn(preferredCallIds, callStatusList);
} else {
calls = callRepository.findByStatusInAndHubId(callStatusList, user.getHub().getId());
}
List<Long> callIds = calls.stream().map(CallEntity::getId).collect(Collectors.toList());
Map<String, BeneficiaryPreferredCallEntity> preferredCallsMap =
getBeneficiaryPreferredCallsForUser(request,user, callIds, companyId);
return calls.stream()
.map(call -> {
CallDetailsResponseBean responseBean = convertToCallDetailsResponseBean(call);
String key = user.getId() + "_" + call.getId();
BeneficiaryPreferredCallEntity preferredCall = preferredCallsMap.get(key);
Long preferredId = (preferredCall != null && !preferredCall.getIsDeleted()) ? preferredCall.getId() : null;
responseBean.setPreferredCallId(preferredId);
return responseBean;
})
.collect(Collectors.toList());
}
public Map<String, BeneficiaryPreferredCallEntity> getBeneficiaryPreferredCallsForUser(HttpServletRequest request, UserEntity user, List<Long> callIds, Long companyId) {
List<BeneficiaryPreferredCallEntity> beneficiaryPreferredCalls;
if (companyId != null && Boolean.TRUE.equals(validator.checkIsBeneficiary())) {
validator.validateUserWithCompany(request, companyId);
UserWithCompanyEntity userWithCompanyEntity=companyService.getUserWithCompany(user.getId(),companyId);
beneficiaryPreferredCalls = beneficiaryPreferredCallRepository
.findByUserIdAndCallIdInAndUserWithCompanyIdAndIsDeletedFalse(user.getId(), callIds, userWithCompanyEntity.getId());
} else {
beneficiaryPreferredCalls = beneficiaryPreferredCallRepository
.findByUserIdAndCallIdInAndIsDeletedFalse(user.getId(), callIds);
beneficiaryPreferredCalls = beneficiaryPreferredCalls.stream()
.collect(Collectors.collectingAndThen(
Collectors.toMap(
BeneficiaryPreferredCallEntity::getCallId,
call -> call,
(existing, replacement) -> existing
),
map -> new ArrayList<>(map.values())
));
}
return beneficiaryPreferredCalls.stream()
.collect(Collectors.toMap(
call -> user.getId() + "_" + call.getCallId(),
call -> call
));
}
public CallResponse validateCallData(CallEntity callEntity) {
CallEntity oldCallEntity = Utils.getClonedEntityForData(callEntity);
validateUpdate(callEntity);
CallResponse callResponseBean = getCallResponseBean(callEntity);
FlowResponseBean flowResponseBean = flowDao.getFlowByCallId(callEntity.getId());
List<FormResponseBean> formResponseBean = formDao.getFormsByCallId(callEntity);
EvaluationFormResponseBean evaluationFormResponseBean = evalualtionFormDao.getEvaluationFormByCallId(callEntity);
CallValidatorServiceImpl.validateResponse(callResponseBean,flowResponseBean,formResponseBean,evaluationFormResponseBean);
callEntity.setStatus(CallStatusEnum.READY_TO_PUBLISH.getValue());
callEntity = callRepository.save(callEntity);
/** This code is responsible for adding a version history log for the "validate call" operation **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldCallEntity).newData(callEntity).build());
callResponseBean.setCurrentStep(GepafinConstant.VALIDATE_REQUEST);
callResponseBean.setStatus(CallStatusEnum.valueOf(callEntity.getStatus()));
return callResponseBean;
}
// public CallEntity getCallEntityById(Long id){
// CallEntity callEntity=callRepository.findByIdAndStatusNotInAndHubId(id, List.of(CallStatusEnum.PUBLISH.getValue()));
// if(callEntity==null){
// throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.CALL_NOT_FOUND));
// }
// return callEntity;
// }
public CallResponse updateCallStatus(CallEntity callEntity, CallStatusEnum statusReq) {
CallEntity oldCallEntity = Utils.getClonedEntityForData(callEntity);
CallStatusEnum currentStatus = CallStatusEnum.valueOf(callEntity.getStatus());
validateStatusChange(currentStatus, statusReq);
callEntity.setStatus(statusReq.getValue());
callEntity = callRepository.save(callEntity);
//Creating notification.
List<Long> userIds = beneficiaryRepository.findUserIdsByHubIdAndBeneficiaryId(callEntity.getHub().getId());
Map<String, String> placeholders = new HashMap<>();
placeholders.put("{{call_name}}", callEntity.getName());
userIds.forEach(userId -> {
List<Long> companyIds = notificationDao.getAllCompanyIdsForUser(userId);
NotificationReq notificationReq = notificationDao.createNotificationReq(NotificationTypeEnum.CALL_CREATED.getValue(), placeholders, userId, null, companyIds);
notificationDao.sendNotification(notificationReq);
});
/** This code is responsible for adding a version history log for the "update call status" operation **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldCallEntity).newData(callEntity).build());
return convertToCallResponseBean(callEntity);
}
private void validateStatusChange(CallStatusEnum currentStatus, CallStatusEnum newStatus) {
if (currentStatus == newStatus) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.STATUS_SAME_ERROR));
}
switch (currentStatus) {
case DRAFT:
if (newStatus == CallStatusEnum.READY_TO_PUBLISH || newStatus == CallStatusEnum.PUBLISH) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.INVALID_STATUS_CHANGE_FROM_DRAFT));
}
break;
case PUBLISH:
if (newStatus == CallStatusEnum.READY_TO_PUBLISH || newStatus == CallStatusEnum.DRAFT) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.INVALID_STATUS_CHANGE_FROM_PUBLISH));
}
break;
case EXPIRED:
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.STATUS_CANNOT_BE_CHANGED));
case READY_TO_PUBLISH:
break;
default:
break;
}
}
public CallEntity validatePublishedCall(Long callId, Long hubId) {
CallEntity callEntity= callRepository
.findByIdAndStatusAndHubId(callId, CallStatusEnum.PUBLISH.getValue(), hubId);
if(callEntity==null){
throw new ResourceNotFoundException(
Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.CALL_NOT_PUBLISHED));
}
LocalDate currentDate = LocalDate.now();
LocalTime currentTime = LocalTime.now();
if (currentDate.isBefore(callEntity.getStartDate().toLocalDate()) ||
(currentDate.isEqual(callEntity.getStartDate().toLocalDate()) && currentTime.isBefore(callEntity.getStartTime()))) {
throw new CustomValidationException(
Status.BAD_REQUEST,
Translator.toLocale(GepafinConstant.CALL_NOT_STARTED_YET)
);
}
if (currentDate.isAfter(callEntity.getEndDate().toLocalDate()) ||
(currentDate.isEqual(callEntity.getEndDate().toLocalDate()) && currentTime.isAfter(callEntity.getEndTime()))) {
throw new CustomValidationException(
Status.BAD_REQUEST,
Translator.toLocale(GepafinConstant.CALL_ALREADY_ENDED)
);
}
return callEntity;
}
public PageableResponseBean<List<CallDetailsResponseBean>> getAllCallsByPagination(HttpServletRequest request,UserEntity user,Long companyId , Boolean onlyPreferredCall, CallPageableRequestBean callPageableRequestBean) {
Integer pageNo = null;
Integer pageLimit = null;
if (callPageableRequestBean.getGlobalFilters() != null) {
pageNo = callPageableRequestBean.getGlobalFilters().getPage();
pageLimit = callPageableRequestBean.getGlobalFilters().getLimit();
}
if (pageLimit == null || pageLimit <= 0) {
pageLimit = GepafinConstant.DEFAULT_PAGE_LIMIT;
}
if (pageNo == null || pageNo <= 0) {
pageNo = GepafinConstant.DEFAULT_PAGE;
}
if (Boolean.TRUE.equals(onlyPreferredCall) && companyId == null) {
throw new CustomValidationException(
Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.COMPANY_ID_REQUIRED_FOR_PREFERRED_CALL)
);
}
Specification<CallEntity> spec = search(user, callPageableRequestBean);
Page<CallEntity> entityPage;
if (Boolean.TRUE.equals(onlyPreferredCall)) {
validator.validateUserWithCompany(request, companyId);
UserWithCompanyEntity userWithCompanyEntity = companyService.getUserWithCompany(user.getId(), companyId);
List<BeneficiaryPreferredCallEntity> preferredCalls = beneficiaryPreferredCallRepository
.findByUserIdAndUserWithCompanyIdAndIsDeletedFalse(user.getId(), userWithCompanyEntity.getId());
List<Long> preferredCallIds = preferredCalls.stream()
.map(BeneficiaryPreferredCallEntity::getCallId)
.collect(Collectors.toList());
// Add preferredCallIds filtering to the specification
spec = spec.and((root, query, criteriaBuilder) ->
root.get(GepafinConstant.ID).in(preferredCallIds)
);
}
entityPage = callRepository.findAll(spec, PageRequest.of(pageNo - 1, pageLimit));
List<Long> callIds=new ArrayList<>();
if(entityPage!=null && entityPage.getContent()!=null && Boolean.FALSE.equals(entityPage.getContent().isEmpty())) {
callIds = entityPage.getContent().stream().map(CallEntity::getId).collect(Collectors.toList());
}
Map<String, BeneficiaryPreferredCallEntity> preferredCallsMap =
getBeneficiaryPreferredCallsForUser(request,user, callIds, companyId);
List<CallDetailsResponseBean> callDetailsResponseBeans = entityPage.getContent().stream()
.map(callEntity -> {
CallDetailsResponseBean responseBean = convertToCallDetailsResponseBean(callEntity);
String key = user.getId() + "_" + callEntity.getId();
BeneficiaryPreferredCallEntity preferredCall = preferredCallsMap.get(key);
Long preferredId = preferredCall != null ? preferredCall.getId() : null;
responseBean.setPreferredCallId(preferredId);
return responseBean;
})
.collect(Collectors.toList());
PageableResponseBean<List<CallDetailsResponseBean>> pageableResponseBean = new PageableResponseBean<>();
pageableResponseBean.setBody(callDetailsResponseBeans);
pageableResponseBean.setCurrentPage(entityPage.getNumber() + 1); // Page numbers typically start from 0, so add 1 for user-friendly indexing
pageableResponseBean.setTotalPages(entityPage.getTotalPages());
pageableResponseBean.setTotalRecords(entityPage.getTotalElements());
pageableResponseBean.setPageSize(entityPage.getSize());
return pageableResponseBean;
}
public Specification<CallEntity> search(UserEntity userEntity, CallPageableRequestBean callPageableRequestBean) {
return (root, query, criteriaBuilder) -> {
List<Predicate> predicates = getPredicates(callPageableRequestBean, criteriaBuilder, root, userEntity);
SortBy sortBy = new SortBy(GepafinConstant.CREATED_DATE, true);
if (callPageableRequestBean.getGlobalFilters() != null
&& callPageableRequestBean.getGlobalFilters().getSortBy() != null &&
callPageableRequestBean.getGlobalFilters().getSortBy().getColumnName() != null && Boolean.FALSE.equals(
isEmpty(callPageableRequestBean.getGlobalFilters().getSortBy().getColumnName()))) {
sortBy.setColumnName(callPageableRequestBean.getGlobalFilters().getSortBy().getColumnName());
sortBy.setSortDesc(true);
if (callPageableRequestBean.getGlobalFilters().getSortBy().getSortDesc() != null) {
sortBy.setSortDesc(callPageableRequestBean.getGlobalFilters().getSortBy().getSortDesc());
}
}
query.orderBy(criteriaBuilder.desc(root.get(sortBy.getColumnName())));
if (Boolean.FALSE.equals(sortBy.getSortDesc())) {
query.orderBy(criteriaBuilder.asc(root.get(sortBy.getColumnName())));
}
return query.where(criteriaBuilder.and(predicates.toArray(new Predicate[0]))).getRestriction();
};
}
private List<Predicate> getPredicates(CallPageableRequestBean callPageableRequestBean,
CriteriaBuilder criteriaBuilder, Root<CallEntity> root, UserEntity userEntity) {
Integer year = null;
String search = null;
if (callPageableRequestBean.getGlobalFilters() != null) {
year = callPageableRequestBean.getGlobalFilters().getYear();
search = callPageableRequestBean.getGlobalFilters().getSearch();
}
List<Predicate> predicates = new ArrayList<>();
if (year != null && year > 0) {
int filterYear = callPageableRequestBean.getGlobalFilters().getYear();
// Create LocalDateTime boundaries for the start and end of the year
LocalDateTime startOfYear = LocalDateTime.of(filterYear, 1, 1, 0, 0);
LocalDateTime endOfYear = LocalDateTime.of(filterYear, 12, 31, 23, 59, 59);
// Add the range comparison to filter records within the year
predicates.add(criteriaBuilder.between(root.get(GepafinConstant.CREATED_DATE), startOfYear, endOfYear));
}
// Search in `title` and `message` (if search term is provided)
if (search != null && !search.isEmpty()) {
Predicate descriptionShort = criteriaBuilder.like(
criteriaBuilder.upper(root.get(GepafinConstant.DESCRIPTION_SHORT)),
"%" + search.toUpperCase() + "%"
);
predicates.add(criteriaBuilder.or(descriptionShort));
Predicate descriptionLong = criteriaBuilder.like(
criteriaBuilder.upper(root.get(GepafinConstant.DESCRIPTION_LONG)),
"%" + search.toUpperCase() + "%"
);
predicates.add(criteriaBuilder.or(descriptionLong));
Predicate name = criteriaBuilder.like(
criteriaBuilder.upper(root.get(GepafinConstant.NAME)),
"%" + search.toUpperCase() + "%"
);
predicates.add(criteriaBuilder.or(name));
}
// Filter by `status` (if status list is provided)
if (callPageableRequestBean.getStatus() != null && !callPageableRequestBean.getStatus().isEmpty()) {
List<String> statusValues = callPageableRequestBean.getStatus().stream()
.map(CallStatusEnum::name) // Convert enum to string
.toList();
predicates.add(root.get(GepafinConstant.STATUS).in(statusValues));
}
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.HUB).get(GepafinConstant.ID), userEntity.getHub().getId()));
return predicates;
}
public CallResponse createCallStep2EvaluationV2(CallEntity callEntity, CreateCallRequestStep2EvaluationV2 createCallRequest, UserEntity user) {
convertToDocumentEntities(createCallRequest.getDocs(), callEntity.getId(), DocumentTypeEnum.DOCUMENT);
convertToDocumentEntities(createCallRequest.getImages(), callEntity.getId(), DocumentTypeEnum.IMAGES);
CallResponse createCallResponseBean = getCallResponseBean(callEntity);
createCallResponseBean.setCurrentStep(GepafinConstant.EVALUATION_V2_STEP_2);
return createCallResponseBean;
}
}