remove null checks for call and put some other validation

This commit is contained in:
rajesh
2024-08-28 17:44:20 +05:30
parent 88e019350a
commit 75db09eb12
15 changed files with 134 additions and 104 deletions

View File

@@ -1,6 +1,5 @@
package net.gepafin.tendermanagement.dao;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
@@ -8,7 +7,6 @@ import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import net.gepafin.tendermanagement.enums.UserStatusEnum;
import net.gepafin.tendermanagement.model.response.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -78,8 +76,10 @@ public class CallDao {
private UserService userService;
public CallResponse createCallStep1(CreateCallRequestStep1 createCallRequest, Long userId) {
UserEntity userEntity = userService.validateUser(userId);
createCallRequest.setRegionId(userEntity.getRoleEntity().getRegion().getId());
CallEntity callEntity = convertToCallEntity(createCallRequest);
convertToFaqEntities(createCallRequest.getFaq(), callEntity, userId);
convertToFaqEntities(createCallRequest.getFaq(), callEntity, userEntity);
convertLookUpDataEntities(createCallRequest.getAimedTo(), callEntity,
LookUpDataTypeEnum.AIMED_TO);
// createCallResponseBean = assembleCreateCallResponseBean(callEntity, Collections.emptyList(),
@@ -94,7 +94,7 @@ public class CallDao {
public CallEntity convertToCallEntity(CreateCallRequestStep1 createCallRequest) {
CallEntity callEntity = new CallEntity();
validateCallEntity(createCallRequest);
// validateCallEntity(createCallRequest);
RegionEntity region = regionRepository.findById(createCallRequest.getRegionId())
.orElseThrow(() -> new ResourceNotFoundException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.REGION_NOT_FOUND)));
@@ -102,8 +102,13 @@ public class CallDao {
callEntity.setName(createCallRequest.getName());
callEntity.setDescriptionShort(createCallRequest.getDescriptionShort());
callEntity.setDescriptionLong(createCallRequest.getDescriptionLong());
callEntity.setStartDate(createCallRequest.getStartDate());
callEntity.setEndDate(createCallRequest.getEndDate());
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.setAmountMax(createCallRequest.getAmountMax());
callEntity.setAmount(createCallRequest.getAmountMax());
@@ -194,20 +199,19 @@ public class CallDao {
return documentEntity;
}
public List<FaqEntity> convertToFaqEntities(List<FaqReq> faqReqList, CallEntity callEntity, Long userId) {
public List<FaqEntity> convertToFaqEntities(List<FaqReq> faqReqList, CallEntity callEntity, UserEntity userEntity) {
if (faqReqList == null) {
return null;
}
List<FaqEntity> faqEntities = faqReqList.stream().map(req -> convertToFaqEntity(req, callEntity, userId))
List<FaqEntity> faqEntities = faqReqList.stream().map(req -> convertToFaqEntity(req, callEntity, userEntity))
.collect(Collectors.toList());
faqRepository.saveAll(faqEntities);
return faqEntities;
}
public FaqEntity convertToFaqEntity(FaqReq faqReq, CallEntity callEntity, Long userId) {
public FaqEntity convertToFaqEntity(FaqReq faqReq, CallEntity callEntity, UserEntity userEntity) {
FaqEntity faqEntity = new FaqEntity();
validateFaqEntity(faqReq.getQuestion());
UserEntity userEntity = userService.validateUser(userId);
faqEntity.setUser(userEntity);
faqEntity.setIsVisible(true);
if (faqReq.getIsVisible() != null) {
@@ -246,32 +250,38 @@ public class CallDao {
}
}
public void validateCallEntity(CreateCallRequestStep1 createCallRequest) {
if (createCallRequest.getRegionId() == null) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.REGION_NOT_FOUND_MSG));
}
if (createCallRequest.getAmount().compareTo(BigDecimal.ZERO) <= 0
|| createCallRequest.getAmountMax().compareTo(BigDecimal.ZERO) <= 0) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.AMOUNT_GREATER_THAN_ZERO_MSG));
}
if (createCallRequest.getStartDate().toLocalDate().isBefore(LocalDate.now())
|| createCallRequest.getEndDate().toLocalDate().isBefore(LocalDate.now()) || createCallRequest
.getStartDate().toLocalDate().isAfter(createCallRequest.getEndDate().toLocalDate())) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.INVALID_DATE_MSG));
}
}
// public void validateCallEntity(CreateCallRequestStep1 createCallRequest) {
// if (createCallRequest.getRegionId() == null) {
// throw new CustomValidationException(Status.VALIDATION_ERROR,
// Translator.toLocale(GepafinConstant.REGION_NOT_FOUND_MSG));
// }
//
// if (createCallRequest.getAmount().compareTo(BigDecimal.ZERO) <= 0
// || createCallRequest.getAmountMax().compareTo(BigDecimal.ZERO) <= 0) {
// throw new CustomValidationException(Status.VALIDATION_ERROR,
// Translator.toLocale(GepafinConstant.AMOUNT_GREATER_THAN_ZERO_MSG));
// }
// if (createCallRequest.getStartDate().toLocalDate().isBefore(LocalDate.now())
// || createCallRequest.getEndDate().toLocalDate().isBefore(LocalDate.now()) || createCallRequest
// .getStartDate().toLocalDate().isAfter(createCallRequest.getEndDate().toLocalDate())) {
// throw new CustomValidationException(Status.VALIDATION_ERROR,
// Translator.toLocale(GepafinConstant.INVALID_DATE_MSG));
// }
//
// }
public CallResponse convertToCallResponseBean(CallEntity callEntity) {
CallResponse createCallResponseBean = new CallResponse();
createCallResponseBean.setId(callEntity.getId());
createCallResponseBean.setName(callEntity.getName());
createCallResponseBean.setStartDate(callEntity.getStartDate());
createCallResponseBean.setEndDate(callEntity.getEndDate());
List<LocalDateTime> dates = new ArrayList<>();
if(callEntity.getStartDate() != null) {
dates.add(callEntity.getStartDate());
}
if(callEntity.getStartDate() != null) {
dates.add(callEntity.getEndDate());
}
createCallResponseBean.setDates(dates);
createCallResponseBean.setDescriptionShort(callEntity.getDescriptionShort());
createCallResponseBean.setDescriptionLong(callEntity.getDescriptionLong());
createCallResponseBean.setStatus(CallStatusEnum.valueOf(callEntity.getStatus()));
@@ -353,6 +363,9 @@ public class CallDao {
public List<LookUpDataResponse> convertLookUpDataEntities(List<LookUpDataReq> lookUpData, CallEntity callEntity,
LookUpDataEntity.LookUpDataTypeEnum type) {
if(lookUpData == null) {
return null;
}
List<LookUpDataEntity> lookUpDataEntities = lookUpData.stream()
.map(req -> convertLookUpDataRequestIntoLookUpDataEntity(req, type)).collect(Collectors.toList());
@@ -418,7 +431,7 @@ public class CallDao {
CallEntity callEntity = callRepository.findById(callId)
.orElseThrow(() -> new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.CALL_NOT_FOUND)));
validateUpdate(callEntity);
setIfUpdated(callEntity::getThreshold, callEntity::setThreshold, createCallRequest.getThreshold());
callRepository.save(callEntity);
convertToEvaluationCriteriaEntities(createCallRequest.getCriteria(), callEntity, LookUpDataTypeEnum.EVALUATION_CRITERIA);
@@ -442,11 +455,24 @@ public class CallDao {
return createCallResponseBean;
}
private 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) {
LocalDate startDate = updateCallRequest.getStartDate() != null ? updateCallRequest.getStartDate().toLocalDate()
: null;
LocalDate endDate = updateCallRequest.getEndDate() != null ? updateCallRequest.getEndDate().toLocalDate()
: null;
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;
@@ -473,8 +499,18 @@ public class CallDao {
updateCallRequest.getDescriptionShort());
setIfUpdated(callEntity::getDescriptionLong, callEntity::setDescriptionLong,
updateCallRequest.getDescriptionLong());
setIfUpdated(callEntity::getStartDate, callEntity::setStartDate, updateCallRequest.getStartDate());
setIfUpdated(callEntity::getEndDate, callEntity::setEndDate, updateCallRequest.getEndDate());
List<LocalDateTime> dates=updateCallRequest.getDates();
if (dates != null && dates.size()>1) {
if (dates.size() > 0) {
setIfUpdated(callEntity::getStartDate, callEntity::setStartDate, dates.get(0));
}
if (dates.size() > 1) {
setIfUpdated(callEntity::getEndDate, callEntity::setEndDate, dates.get(1));
}
}
// 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,
@@ -657,7 +693,7 @@ public class CallDao {
}
break;
case PUBLISH:
case EXPIRE:
case EXPIRED:
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.STATUS_CANNOT_BE_CHANGED));
default:

View File

@@ -9,7 +9,7 @@ import net.gepafin.tendermanagement.model.request.FaqReq;
import net.gepafin.tendermanagement.model.response.FaqResponseBean;
import net.gepafin.tendermanagement.repositories.CallRepository;
import net.gepafin.tendermanagement.repositories.FaqRepository;
import net.gepafin.tendermanagement.repositories.UserRepository;
import net.gepafin.tendermanagement.service.UserService;
import net.gepafin.tendermanagement.util.DateTimeUtil;
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
@@ -25,7 +25,7 @@ public class FaqDao {
private FaqRepository faqRepository;
@Autowired
private UserRepository userRepository;
private UserService userService;
@Autowired
private CallDao callDao;
@@ -38,7 +38,8 @@ public class FaqDao {
CallEntity callEntity = callRepository.findById(callId)
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.CALL_NOT_FOUND)));
entity = callDao.convertToFaqEntity(faqRequest, callEntity, userId);
UserEntity userEntity = userService.validateUser(userId);
entity = callDao.convertToFaqEntity(faqRequest, callEntity, userEntity);
faqRepository.save(entity);
return convertFaqEntityToResponseBean(entity);
}
@@ -81,9 +82,7 @@ public class FaqDao {
private void updateFaqEntity(FaqEntity faqEntity, FaqReq faqReq, Long userId, CallEntity callEntity) {
faqEntity.setQuestion(faqReq.getQuestion());
UserEntity userEntity = userRepository.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.LOOK_UP_DATA_NOT_VALID_MSG)));
UserEntity userEntity = userService.validateUser(userId);
faqEntity.setUser(userEntity);
faqEntity.setIsVisible(true);
if (faqReq.getIsVisible() != null) {