Resolved conflicts

This commit is contained in:
rajesh
2024-09-14 20:42:27 +05:30
19 changed files with 444 additions and 34 deletions

View File

@@ -13,7 +13,7 @@ import net.gepafin.tendermanagement.repositories.ApplicationFormRepository;
import net.gepafin.tendermanagement.repositories.ApplicationRepository;
import net.gepafin.tendermanagement.service.CallService;
import net.gepafin.tendermanagement.service.FormService;
import net.gepafin.tendermanagement.service.UserService;
import net.gepafin.tendermanagement.util.DateTimeUtil;
import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
@@ -23,6 +23,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
@@ -31,13 +32,9 @@ public class ApplicationDao {
private final Logger log = LoggerFactory.getLogger(ApplicationDao.class);
@Autowired
private CallService callService;
@Autowired
private UserService userService;
@Autowired
private ApplicationRepository applicationRepository;
@@ -159,7 +156,7 @@ public class ApplicationDao {
}
public ApplicationEntity getApplicationOrCreate(UserEntity userEntity, CallEntity callEntity, FormEntity formEntity) {
ApplicationEntity applicationEntity = applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(), callEntity.getId());
ApplicationEntity applicationEntity = applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(), callEntity.getId()).orElse(null);
if (applicationEntity == null) {
validateFormId(formEntity, callEntity);
applicationEntity = createApplicationEntity(userEntity, callEntity);
@@ -198,7 +195,7 @@ public class ApplicationDao {
public ApplicationFormFieldEntity validateApplicationFormField(Long applicationFormFieldId) {
Optional<ApplicationFormFieldEntity> applicationFormFieldEntity = applicationFormFieldRepository.findById(applicationFormFieldId);
if (applicationFormFieldEntity.isEmpty()) {
throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.APPLICATION_FORM_FIELD_NOT_FOUND));
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.APPLICATION_FORM_FIELD_NOT_FOUND));
}
return applicationFormFieldEntity.get();
}
@@ -230,18 +227,16 @@ public class ApplicationDao {
}
public ApplicationGetResponseBean getApplicationByFormId( Long applicationId,Long formId, UserEntity userEntity) {
ApplicationEntity applicationEntity=null;
List<FormApplicationResponse> formApplicationResponses = new ArrayList<>();
List<FormEntity> formEntities = new ArrayList<>();
Optional<ApplicationEntity> applicationEntity1 = applicationRepository.findById(applicationId);
applicationEntity=applicationEntity1.get();
if (applicationEntity == null) {
throw new CustomValidationException(Status.BAD_REQUEST,Translator.toLocale(GepafinConstant.APPLICATION_NOT_FOUND_MSG));
}
ApplicationEntity applicationEntity = applicationRepository.findById(applicationId)
.orElseThrow(() -> new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPLICATION_NOT_FOUND_MSG)));
if (formId != null) {
FormEntity formEntity = formService.validateForm(formId);
applicationEntity = applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(), formEntity.getCall().getId());
Optional<ApplicationEntity> application = applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(),
formEntity.getCall().getId());
applicationEntity=application.get();
formEntities.add(formEntity);
processForm(formEntity, applicationEntity, formApplicationResponses);
}
@@ -301,9 +296,26 @@ public class ApplicationDao {
return applicationResponse;
}
public void checkIfApplicationExists(CallEntity call,UserEntity userEntity){
ApplicationEntity applicationEntity=applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(),call.getId());
if(applicationEntity!=null){
Optional<ApplicationEntity> applicationEntity=applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.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);
applicationEntity.setStatus(status.getValue());
if(status.equals(ApplicationStatusTypeEnum.SUBMIT)) {
applicationEntity.setSubmissionDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now()));
}
saveApplicationEntity(applicationEntity);
}
}

View File

@@ -0,0 +1,255 @@
package net.gepafin.tendermanagement.dao;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.ApplicationEntity;
import net.gepafin.tendermanagement.entities.ApplicationFormEntity;
import net.gepafin.tendermanagement.entities.ApplicationFormFieldEntity;
import net.gepafin.tendermanagement.entities.FlowDataEntity;
import net.gepafin.tendermanagement.entities.FlowEdgesEntity;
import net.gepafin.tendermanagement.entities.FormEntity;
import net.gepafin.tendermanagement.enums.FormActionEnum;
import net.gepafin.tendermanagement.model.response.NextOrPreviousFormResponse;
import net.gepafin.tendermanagement.repositories.ApplicationFormFieldRepository;
import net.gepafin.tendermanagement.repositories.ApplicationFormRepository;
import net.gepafin.tendermanagement.repositories.FlowDataRepository;
import net.gepafin.tendermanagement.repositories.FlowEdgesRepository;
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;
@Component
public class FlowFormDao {
@Autowired
private FlowEdgesRepository flowEdgesRepository;
@Autowired
private FlowDataRepository flowDataRepository;
@Autowired
private ApplicationFormFieldRepository applicationFormFieldRepository;
@Autowired
private ApplicationFormRepository applicationFormRepository;
// Long getNextForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) {
// // vlaidation if next form findout and cuuent from is not fill the give error
// List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findBySourceIdAndCallId(currentFormEntity.getId(), applicationEntity.getCall().getId());
// ApplicationFormEntity applicationFormEntity = applicationFormRepository.findByApplicationIdAndFormId(applicationEntity.getId(), currentFormEntity.getId());
// if(applicationFormEntity == null) {
// throw new CustomValidationException(Status.VALIDATION_ERROR,
// Translator.toLocale("current form is not fill"));
// }
// if(flowEdgesList.isEmpty()) {
// throw new ResourceNotFoundException(Status.NOT_FOUND,
// Translator.toLocale(GepafinConstant.NEXT_FORM_NOT_FOUND));
// } else if(flowEdgesList.size() == 1) {
// return flowEdgesList.get(0).getTargetId();
// } else {
// List<Long> nextFormIds = flowEdgesList.stream().map(FlowEdgesEntity::getTargetId).toList();
// FlowDataEntity flowDataEntity = flowDataRepository.findByFormIdAndCallId(currentFormEntity.getId(), applicationEntity.getCall().getId());
//
// ApplicationFormFieldEntity applicationFormFieldEntity = applicationFormFieldRepository
// .findByFieldIdAndApplicationFormFormIdAndApplicationFormApplicationId(flowDataEntity.getChoosenField(),
// currentFormEntity.getId(), applicationEntity.getId()).orElseThrow(()-> new ResourceNotFoundException(Status.NOT_FOUND,
// Translator.toLocale(GepafinConstant.NEXT_FORM_NOT_FOUND)));
//
// flowDataEntity = flowDataRepository.findByChoosenValueAndFormIdIn(applicationFormFieldEntity.getFieldValue(), nextFormIds).orElseThrow(()-> new NullPointerException());
// return flowDataEntity.getFormId();
// }
// }
//
//
// Long getPreviousForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) {
//
// // Step 1: Find all edges where the current form is the target (i.e., reverse flow)
// List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findByTargetIdAndCallId(
// currentFormEntity.getId(), applicationEntity.getCall().getId());
//
// if (flowEdgesList.isEmpty()) {
// // If no previous edges are found, return null or handle this case based on your needs
// throw new ResourceNotFoundException(Status.NOT_FOUND,
// Translator.toLocale(GepafinConstant.PREVIOUS_FORM_NOT_FOUND));
// }
//
// // Step 2: If only one edge exists, return the source (previous) form ID directly have to look into it
// if (flowEdgesList.size() == 1) {
// return flowEdgesList.get(0).getSourceId();
// }
// List<Long> previousFormIds = flowEdgesList.stream().map(FlowEdgesEntity::getSourceId).toList();
//
//
// // Step 3: Try to find flow data for the current form. If not found, we know it's the last step (or no field filtering is needed).
// List<FlowDataEntity> flowDataList = flowDataRepository.findByFormIdInAndCallId(
// previousFormIds, applicationEntity.getCall().getId());
//
// List<String> fieldValue = flowDataList.stream().map(FlowDataEntity::getChoosenValue).toList();
//
//// if (flowDataEntity == null || flowDataEntity.getChoosenField() == null) {
//// // If flow data is not found, simply return the first matching previous form
//// return flowEdgesList.get(0).getSourceId(); // You can modify this logic if needed
//// }
//
// // Step 4: Fetch the field value from the application form fields
// Set<FormEntity> formList = applicationFormFieldRepository
// .findByFieldValueInAndAndApplicationFormApplicationId(
// fieldValue, applicationEntity.getId()).stream().map(applicationFormFieldEntity->applicationFormFieldEntity.getApplicationForm().getForm()).collect(Collectors.toSet());
//
// List<Long> fieldIds = formList.stream().map(formEntity->getNextForm(formEntity, applicationEntity)).toList();
// for(Long formId:previousFormIds) {
// fieldIds.contains(formId);
// return formId;
// }
// return null;
//// .orElseThrow(() -> new NullPointerException("Field value not found for the current form and application."));
//
//
//// // Step 5: Check if there's a matching previous form based on the chosen value
//// FlowDataEntity previousFlowDataEntity = flowDataRepository.findByChoosenValueAndFormIdIn(
//// applicationFormFieldEntity.getFieldValue(), flowEdgesList.stream().map(FlowEdgesEntity::getSourceId).toList())
//// .orElse(null); // If no matching form is found, return null or the first source form
////
//// // Step 6: Return the formId of the matching previous form, or the first one if no specific match is found
//// return previousFlowDataEntity != null ? previousFlowDataEntity.getFormId() : flowEdgesList.get(0).getSourceId();
// }
public Long getNextForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) {
// Validate if the current form has been filled
ApplicationFormEntity applicationFormEntity = applicationFormRepository.findByApplicationIdAndFormId(
applicationEntity.getId(), currentFormEntity.getId());
// Retrieve the flow edges for the next forms
List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findBySourceIdAndCallId(
currentFormEntity.getId(), applicationEntity.getCall().getId());
if (flowEdgesList.isEmpty()) {
return null;
// throw new ResourceNotFoundException(Status.NOT_FOUND,
// Translator.toLocale(GepafinConstant.NEXT_FORM_NOT_FOUND));
}
// If only one edge exists, return the target form ID
if (flowEdgesList.size() == 1) {
return flowEdgesList.get(0).getTargetId();
}
if (applicationFormEntity == null) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.CURRENT_FORM_INCOMPLETE));
}
// For multiple edges, find the next form based on the chosen value
List<Long> nextFormIds = flowEdgesList.stream()
.map(FlowEdgesEntity::getTargetId)
.toList();
// Retrieve the flow data for the current form
FlowDataEntity flowDataEntity = flowDataRepository.findByFormIdAndCallId(
currentFormEntity.getId(), applicationEntity.getCall().getId());
// Fetch the application form field related to the chosen field
ApplicationFormFieldEntity applicationFormFieldEntity = applicationFormFieldRepository
.findByFieldIdAndApplicationFormFormIdAndApplicationFormApplicationId(
flowDataEntity.getChoosenField(), currentFormEntity.getId(), applicationEntity.getId()).orElse(null);
// Find the next form based on the chosen value and return its ID
return flowDataRepository.findByChoosenValueAndFormIdIn(
applicationFormFieldEntity.getFieldValue(), nextFormIds)
.map(FlowDataEntity::getFormId)
.orElse(null);
}
public Long getPreviousForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) {
// Retrieve the flow edges for the previous forms
List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findByTargetIdAndCallId(
currentFormEntity.getId(), applicationEntity.getCall().getId());
if (flowEdgesList.isEmpty()) {
return null;
// throw new ResourceNotFoundException(Status.NOT_FOUND,
// Translator.toLocale(GepafinConstant.PREVIOUS_FORM_NOT_FOUND));
}
// If only one edge exists, return the source form ID
if (flowEdgesList.size() == 1) {
return flowEdgesList.get(0).getSourceId();
}
// For multiple edges, find the previous form based on the chosen value
List<Long> previousFormIds = flowEdgesList.stream()
.map(FlowEdgesEntity::getSourceId)
.toList();
// Fetch the flow data based on previous form IDs
List<FlowDataEntity> flowDataList = flowDataRepository.findByFormIdInAndCallId(
previousFormIds, applicationEntity.getCall().getId());
List<String> chosenValues = flowDataList.stream()
.map(FlowDataEntity::getChoosenValue)
.toList();
// Fetch the previous forms based on the chosen field values
Set<FormEntity> formList = applicationFormFieldRepository
.findByFieldValueInAndApplicationFormApplicationId(chosenValues, applicationEntity.getId()).stream()
.map(fieldEntity -> fieldEntity.getApplicationForm().getForm())
.collect(Collectors.toSet());
// Find next form IDs recursively for all forms in the formList
List<Long> fieldIds = formList.stream()
.map(formEntity -> getNextForm(formEntity, applicationEntity))
.toList();
// Return the first matching previous form ID that corresponds to a next form
return previousFormIds.stream()
.filter(fieldIds::contains)
.findFirst().orElse(null);
}
public NextOrPreviousFormResponse getnextOrPreviousForm(ApplicationEntity applicationEntity, Long formId,
FormActionEnum action) {
FormEntity fromEntity = null;
if(formId == null) {
NextOrPreviousFormResponse nextOrPreviousFormResponse = new NextOrPreviousFormResponse();
nextOrPreviousFormResponse.setNextFormId(getDefaultForm(applicationEntity));
return nextOrPreviousFormResponse;
}else {
fromEntity = Optional
.of(applicationFormRepository.findByApplicationIdAndFormId(applicationEntity.getId(), formId))
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.FORM_NOT_FOUND)))
.getForm();
}
NextOrPreviousFormResponse nextOrPreviousFormResponse = new NextOrPreviousFormResponse();
if (action.equals(FormActionEnum.NEXT)) {
nextOrPreviousFormResponse.setNextFormId(getNextForm(fromEntity, applicationEntity));
} else {
nextOrPreviousFormResponse.setPreviousFormId(getPreviousForm(fromEntity, applicationEntity));
}
return nextOrPreviousFormResponse;
}
private Long getDefaultForm(ApplicationEntity applicationEntity) {
List<ApplicationFormEntity> applicationFormList = applicationFormRepository.findByApplicationIdOrderByCreatedDateAsc(applicationEntity.getId());
if(applicationFormList.isEmpty()) {
return applicationEntity.getCall().getInitialForm();
}
if(applicationFormList.get(applicationFormList.size()-1).getForm().getId().equals(applicationEntity.getCall().getFinalForm())) {
return applicationEntity.getCall().getInitialForm();
}
return getNextForm(applicationFormList.get(applicationFormList.size()-1).getForm(), applicationEntity);
}
}

View File

@@ -10,7 +10,6 @@ import net.gepafin.tendermanagement.model.request.FormRequest;
import net.gepafin.tendermanagement.model.response.ContentResponseBean;
import net.gepafin.tendermanagement.model.response.FormResponseBean;
import net.gepafin.tendermanagement.repositories.ApplicationFormRepository;
import net.gepafin.tendermanagement.repositories.ApplicationRepository;
import net.gepafin.tendermanagement.repositories.FormRepository;
import net.gepafin.tendermanagement.service.CallService;
import net.gepafin.tendermanagement.util.DateTimeUtil;
@@ -36,9 +35,6 @@ public class FormDao {
@Autowired
private CallService callService;
@Autowired
private ApplicationRepository applicationRepository;
@Autowired
private ApplicationFormRepository applicationFormRepository;