Merge pull request #29 from Kitzanos/create-beneficiary-and-company-crud
Created beneficiary table and company crud Operations
This commit is contained in:
@@ -17,15 +17,18 @@ import net.gepafin.tendermanagement.service.FormService;
|
||||
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
||||
import net.gepafin.tendermanagement.util.FieldValidator;
|
||||
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.ResourceNotFoundException;
|
||||
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.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
@@ -65,14 +68,17 @@ public class ApplicationDao {
|
||||
|
||||
@Autowired
|
||||
private FlowDataRepository flowDataRepository;
|
||||
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
|
||||
|
||||
public ApplicationResponseBean createApplication(ApplicationRequestBean applicationRequestBean, UserEntity userEntity, Long formId,Long applicationId) {
|
||||
public ApplicationResponseBean createApplication(ApplicationRequestBean applicationRequestBean, UserEntity userEntity, Long formId, Long applicationId) {
|
||||
FormEntity formEntity = formService.validateForm(formId);
|
||||
CallEntity call = callService.validatePublishedCall(formEntity.getCall().getId());
|
||||
callService.validatePublishedCall(formEntity.getCall().getId());
|
||||
validateFormFields(applicationRequestBean,formEntity);
|
||||
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
||||
if(Boolean.TRUE.equals(applicationEntity.getStatus().equals(ApplicationStatusTypeEnum.SUBMIT.getValue()))){
|
||||
if(Boolean.TRUE.equals(applicationEntity.getStatus().equals(ApplicationStatusTypeEnum.SUBMIT.getValue()))) {
|
||||
throw new CustomValidationException(Status.BAD_REQUEST,Translator.toLocale(GepafinConstant.APPLICATION_ALREADY_SUBMITTED));
|
||||
}
|
||||
formService.validateFormField(applicationRequestBean.getFormFields(),applicationEntity,formEntity);
|
||||
@@ -94,9 +100,10 @@ public class ApplicationDao {
|
||||
return applicationFormEntity;
|
||||
}
|
||||
|
||||
public ApplicationEntity createApplicationEntity(UserEntity user, CallEntity call) {
|
||||
public ApplicationEntity createApplicationEntity(UserEntity user, CallEntity call, CompanyEntity companyEntity) {
|
||||
ApplicationEntity entity = new ApplicationEntity();
|
||||
entity.setUser(user);
|
||||
entity.setUserId(user.getId());
|
||||
entity.setCompany(companyEntity);
|
||||
entity.setCall(call);
|
||||
entity.setIsDeleted(false);
|
||||
entity.setStatus(ApplicationStatusTypeEnum.DRAFT.getValue());
|
||||
@@ -109,7 +116,6 @@ public class ApplicationDao {
|
||||
ApplicationEntity applicationEntity = validateApplication(id);
|
||||
ApplicationFormEntity applicationFormEntity = applicationFormRepository.findByApplicationIdAndFormId(applicationEntity.getId(),formId);
|
||||
List<ApplicationFormFieldResponseBean> applicationFormFieldResponseBeans=new ArrayList<>();
|
||||
ApplicationFormFieldResponseBean applicationFormFieldResponseBeans1=null;
|
||||
List<ApplicationFormFieldEntity> applicationFormFieldEntities = applicationFormFieldRepository.findByApplicationFormId(applicationFormEntity.getId());
|
||||
applicationFormFieldResponseBeans=createApplicationFormFieldResponse(applicationFormFieldEntities, applicationFormEntity, applicationFormFieldResponseBeans);
|
||||
ApplicationResponseBean applicationResponseBean= convertApplicationEntityToApplicationResponseBean(applicationEntity);
|
||||
@@ -170,44 +176,74 @@ public class ApplicationDao {
|
||||
log.info("Application deleted with ID: {}", id);
|
||||
}
|
||||
|
||||
public List<ApplicationResponse> getAllApplications(UserEntity userEntity, Long callId) {
|
||||
boolean isBeneficiary = isBeneficiary(userEntity);
|
||||
|
||||
// public List<ApplicationResponse> getAllApplications(UserEntity userEntity, Long callId, CompanyEntity companyEntity) {
|
||||
// boolean isBeneficiary = validator.checkIsBeneficiary();
|
||||
//
|
||||
// log.info("Fetching applications for RoleType: {}", userEntity.getRoleEntity().getRoleType());
|
||||
// List<ApplicationResponse> applicationResponses = new ArrayList<>();
|
||||
//
|
||||
// if (callId != null) {
|
||||
// // Fetch based on callId and user if role is BENEFICIARY, otherwise fetch all for the call
|
||||
// log.info("Fetching applications for callId: {}", callId);
|
||||
// CallEntity call = callService.validateCall(callId);
|
||||
//
|
||||
// // Use a single method to handle both conditions for consistency
|
||||
// List<ApplicationEntity> applicationEntities = isBeneficiary
|
||||
// ? applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(), call.getId())
|
||||
// .map(List::of) // Convert Optional<ApplicationEntity> to a List of one element
|
||||
// .orElse(List.of()) // If not present, return an empty list
|
||||
// : applicationRepository.findByCallIdAndIsDeletedFalse(call.getId());
|
||||
//
|
||||
// applicationResponses = applicationEntities.stream()
|
||||
// .map(this::getApplicationResponse)
|
||||
// .collect(Collectors.toList());
|
||||
//
|
||||
// } else {
|
||||
// // Fetch all applications for the user if BENEFICIARY, or fetch all applications in general
|
||||
// List<ApplicationEntity> applicationEntities = isBeneficiary
|
||||
// ? applicationRepository.findByUserIdAndIsDeletedFalse(companyEntity.getId())
|
||||
// : applicationRepository.findByIsDeletedFalse();
|
||||
//
|
||||
// applicationResponses = applicationEntities.stream()
|
||||
// .map(this::getApplicationResponse)
|
||||
// .collect(Collectors.toList());
|
||||
// }
|
||||
//
|
||||
// return applicationResponses;
|
||||
// }
|
||||
|
||||
public List<ApplicationResponse> getAllApplications(UserEntity userEntity, Long callId, Long companyId) {
|
||||
|
||||
log.info("Fetching applications for RoleType: {}", userEntity.getRoleEntity().getRoleType());
|
||||
List<ApplicationResponse> applicationResponses = new ArrayList<>();
|
||||
|
||||
if (callId != null) {
|
||||
// Fetch based on callId and user if role is BENEFICIARY, otherwise fetch all for the call
|
||||
log.info("Fetching applications for callId: {}", callId);
|
||||
CallEntity call = callService.validateCall(callId);
|
||||
Specification<ApplicationEntity> spec = search(userEntity.getId(), callId, companyId);
|
||||
|
||||
// Use a single method to handle both conditions for consistency
|
||||
List<ApplicationEntity> applicationEntities = isBeneficiary
|
||||
? applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(), call.getId())
|
||||
.map(List::of) // Convert Optional<ApplicationEntity> to a List of one element
|
||||
.orElse(List.of()) // If not present, return an empty list
|
||||
: applicationRepository.findByCallIdAndIsDeletedFalse(call.getId());
|
||||
List<ApplicationEntity> applicationEntities = applicationRepository.findAll(spec);
|
||||
|
||||
applicationResponses = applicationEntities.stream()
|
||||
.map(this::getApplicationResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
} else {
|
||||
// Fetch all applications for the user if BENEFICIARY, or fetch all applications in general
|
||||
List<ApplicationEntity> applicationEntities = isBeneficiary
|
||||
? applicationRepository.findByUserIdAndIsDeletedFalse(userEntity.getId())
|
||||
: applicationRepository.findByIsDeletedFalse();
|
||||
|
||||
applicationResponses = applicationEntities.stream()
|
||||
.map(this::getApplicationResponse)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
return applicationResponses;
|
||||
return applicationEntities.stream()
|
||||
.map(this::getApplicationResponse)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
private ApplicationResponse getApplicationResponse(ApplicationEntity applicationEntity) {
|
||||
private Specification<ApplicationEntity> search(Long userId, Long callId, Long companyId) {
|
||||
return (root, query, builder) -> {
|
||||
Boolean isBeneficiary = validator.checkIsBeneficiary();
|
||||
Predicate predicate = builder.isFalse(root.get("isDeleted"));
|
||||
if (isBeneficiary) {
|
||||
predicate = builder.and(predicate, builder.equal(root.get("userId"), userId));
|
||||
}
|
||||
if (callId != null) {
|
||||
predicate = builder.and(predicate, builder.equal(root.get("call").get("id"), callId));
|
||||
}
|
||||
if (companyId != null) {
|
||||
predicate = builder.and(predicate, builder.equal(root.get("company").get("id"), companyId));
|
||||
}
|
||||
return predicate;
|
||||
};
|
||||
}
|
||||
|
||||
private ApplicationResponse getApplicationResponse(ApplicationEntity applicationEntity) {
|
||||
ApplicationResponse responseBean = new ApplicationResponse();
|
||||
List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findByCallId(applicationEntity.getCall().getId());
|
||||
Long totalFormSteps = flowFormDao.calculateTotalSteps(flowEdgesList);
|
||||
@@ -222,13 +258,17 @@ public class ApplicationDao {
|
||||
responseBean.setSubmissionDate(applicationEntity.getSubmissionDate());
|
||||
responseBean.setStatus(applicationEntity.getStatus());
|
||||
responseBean.setComments(applicationEntity.getComments());
|
||||
responseBean.setCompanyId(applicationEntity.getCompany().getId());
|
||||
responseBean.setCompanyName(applicationEntity.getCompany().getCompanyName());
|
||||
return responseBean;
|
||||
}
|
||||
|
||||
public ApplicationEntity validateApplication(Long id) {
|
||||
ApplicationEntity applicationEntity= applicationRepository.findById(id).orElseThrow(() ->new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.APPLICATION_NOT_FOUND_MSG)));
|
||||
return applicationEntity;
|
||||
}
|
||||
public ApplicationEntity validateApplication(Long id) {
|
||||
ApplicationEntity applicationEntity = applicationRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.APPLICATION_NOT_FOUND_MSG)));
|
||||
return applicationEntity;
|
||||
}
|
||||
|
||||
private ApplicationResponseBean convertApplicationEntityToApplicationResponseBean(ApplicationEntity entity) {
|
||||
ApplicationResponseBean response = new ApplicationResponseBean();
|
||||
@@ -348,20 +388,19 @@ public class ApplicationDao {
|
||||
return applicationEntity;
|
||||
}
|
||||
|
||||
public ApplicationGetResponseBean getApplicationByFormId( Long applicationId,Long formId, UserEntity userEntity) {
|
||||
public ApplicationGetResponseBean getApplicationByFormId( Long applicationId, Long formId, UserEntity userEntity) {
|
||||
List<FormApplicationResponse> formApplicationResponses = new ArrayList<>();
|
||||
List<FormEntity> formEntities = new ArrayList<>();
|
||||
boolean isBeneficiary = isBeneficiary(userEntity);
|
||||
ApplicationEntity applicationEntity = isBeneficiary
|
||||
? applicationRepository.findByIdAndUserIdAndIsDeletedFalse(applicationId,userEntity.getId())
|
||||
? applicationRepository.findByIdAndUserIdAndIsDeletedFalse(applicationId, userEntity.getId())
|
||||
.orElseThrow(() -> new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPLICATION_NOT_FOUND_MSG)))
|
||||
: applicationRepository.findById(applicationId)
|
||||
.stream().findFirst()
|
||||
.orElseThrow(() -> new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPLICATION_NOT_FOUND_MSG)));
|
||||
|
||||
if (formId != null) {
|
||||
FormEntity formEntity = formService.validateForm(formId);
|
||||
Optional<ApplicationEntity> application = applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(),
|
||||
Optional<ApplicationEntity> application = applicationRepository.findByIdAndUserIdAndCallIdAndIsDeletedFalse(applicationId, userEntity.getId(),
|
||||
formEntity.getCall().getId());
|
||||
applicationEntity=application.get();
|
||||
formEntities.add(formEntity);
|
||||
@@ -421,6 +460,8 @@ public class ApplicationDao {
|
||||
applicationGetResponseBean.setSubmissionDate(applicationEntity.getSubmissionDate());
|
||||
applicationGetResponseBean.setCallId(applicationEntity.getCall().getId());
|
||||
applicationGetResponseBean.setCallTitle(applicationEntity.getCall().getName());
|
||||
applicationGetResponseBean.setCompanyId(applicationEntity.getCompany().getId());
|
||||
applicationGetResponseBean.setCompanyName(applicationEntity.getCompany().getCompanyName());
|
||||
return applicationGetResponseBean;
|
||||
}
|
||||
|
||||
@@ -433,36 +474,29 @@ public class ApplicationDao {
|
||||
return formApplicationResponse;
|
||||
}
|
||||
|
||||
public ApplicationResponse createApplicationByCallId(ApplicationRequest applicationRequest,Long callId,UserEntity userEntity){
|
||||
CallEntity call=callService.validateCall(callId);
|
||||
call = callService.validatePublishedCall(call.getId());
|
||||
checkIfApplicationExists(call,userEntity);
|
||||
ApplicationEntity applicationEntity=createApplicationEntity(userEntity,call);
|
||||
applicationEntity.setComments(applicationRequest.getComments());
|
||||
applicationEntity=saveApplicationEntity(applicationEntity);
|
||||
ApplicationResponse applicationResponse=getApplicationResponse(applicationEntity);
|
||||
return applicationResponse;
|
||||
}
|
||||
public void checkIfApplicationExists(CallEntity call,UserEntity userEntity){
|
||||
Optional<ApplicationEntity> applicationEntity=applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(),call.getId());
|
||||
public ApplicationResponse createApplicationByCallId(CompanyEntity companyEntity,
|
||||
ApplicationRequest applicationRequest, Long callId, UserEntity userEntity) {
|
||||
CallEntity call = callService.validateCall(callId);
|
||||
call = callService.validatePublishedCall(call.getId());
|
||||
checkIfApplicationExists(call, companyEntity);
|
||||
ApplicationEntity applicationEntity = createApplicationEntity(userEntity, call, companyEntity);
|
||||
applicationEntity.setComments(applicationRequest.getComments());
|
||||
applicationEntity = saveApplicationEntity(applicationEntity);
|
||||
ApplicationResponse applicationResponse = getApplicationResponse(applicationEntity);
|
||||
return applicationResponse;
|
||||
}
|
||||
public void checkIfApplicationExists(CallEntity call, CompanyEntity companyEntity){
|
||||
Optional<ApplicationEntity> applicationEntity=applicationRepository.findByCompanyIdAndCallIdAndIsDeletedFalse(companyEntity.getId(),call.getId());
|
||||
if(applicationEntity.isPresent()){
|
||||
throw new CustomValidationException(Status.BAD_REQUEST,Translator.toLocale(GepafinConstant.APPLICATION_ALREADY_EXISTS));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public ApplicationEntity getApplicationByCallAndUser(CallEntity call, UserEntity userEntity) {
|
||||
return applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(), call.getId())
|
||||
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.APPLICATION_NOT_FOUND_MSG)));
|
||||
|
||||
}
|
||||
|
||||
public void updateApplicationStatus(Long applicationId, ApplicationStatusTypeEnum status) {
|
||||
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
||||
|
||||
if (status.equals(ApplicationStatusTypeEnum.SUBMIT)) {
|
||||
CallEntity callEntity = applicationEntity.getCall();
|
||||
// CallEntity callEntity = applicationEntity.getCall();
|
||||
// Long initialFormId = callEntity.getInitialForm();
|
||||
// Long finalFormId = callEntity.getFinalForm();
|
||||
//// if (initialFormId == null || finalFormId == null) {
|
||||
@@ -500,7 +534,7 @@ public class ApplicationDao {
|
||||
return (int) Math.round(progress);
|
||||
}
|
||||
public void validateFormFields(ApplicationRequestBean request, FormEntity formEntity) {
|
||||
List<String> errors=new ArrayList<>();
|
||||
|
||||
List<ContentResponseBean> contentResponseBeans=Utils.convertJsonStringToList(formEntity.getContent(),ContentResponseBean.class);
|
||||
|
||||
List<ApplicationFormFieldRequestBean> requestFields = request.getFormFields();
|
||||
|
||||
Reference in New Issue
Block a user