Updated Logic for Retrieving Previous Form

This commit is contained in:
harish
2024-09-20 16:25:57 +05:30
parent 79603a0c0d
commit 43cc11ba02
2 changed files with 68 additions and 40 deletions

View File

@@ -1,10 +1,12 @@
package net.gepafin.tendermanagement.dao; package net.gepafin.tendermanagement.dao;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import net.gepafin.tendermanagement.repositories.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -18,10 +20,6 @@ import net.gepafin.tendermanagement.entities.FlowEdgesEntity;
import net.gepafin.tendermanagement.entities.FormEntity; import net.gepafin.tendermanagement.entities.FormEntity;
import net.gepafin.tendermanagement.enums.FormActionEnum; import net.gepafin.tendermanagement.enums.FormActionEnum;
import net.gepafin.tendermanagement.model.response.NextOrPreviousFormResponse; 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.service.FormService; import net.gepafin.tendermanagement.service.FormService;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException; 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.ResourceNotFoundException;
@@ -47,7 +45,8 @@ public class FlowFormDao {
@Autowired @Autowired
private FormService formService; private FormService formService;
@Autowired
private FormRepository formRepository;
// Long getNextForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) { // Long getNextForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) {
// // vlaidation if next form findout and cuuent from is not fill the give error // // vlaidation if next form findout and cuuent from is not fill the give error
@@ -177,52 +176,79 @@ public class FlowFormDao {
.orElse(null); .orElse(null);
} }
public Long getPreviousForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) { // public Long getPreviousForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) {
// Retrieve the flow edges for the previous forms // // Retrieve the flow edges for the previous forms
List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findByTargetIdAndCallId( // List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findByTargetIdAndCallId(
currentFormEntity.getId(), applicationEntity.getCall().getId()); // 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);
// }
if (flowEdgesList.isEmpty()) { public Long getPreviousForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) {
return null;
List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findByTargetIdAndCallId(
currentFormEntity.getId(), applicationEntity.getCall().getId());
if (flowEdgesList.isEmpty()) {
return null;
// throw new ResourceNotFoundException(Status.NOT_FOUND, // throw new ResourceNotFoundException(Status.NOT_FOUND,
// Translator.toLocale(GepafinConstant.PREVIOUS_FORM_NOT_FOUND)); // Translator.toLocale(GepafinConstant.PREVIOUS_FORM_NOT_FOUND));
} }
// If only one edge exists, return the source form ID // // If only one edge exists, return the source form ID
if (flowEdgesList.size() == 1) { // if (flowEdgesList.size() == 1) {
return flowEdgesList.get(0).getSourceId(); // return flowEdgesList.get(0).getSourceId();
} // }
// For multiple edges, find the previous form based on the chosen value // For multiple edges, find the previous form based on the chosen value
List<Long> previousFormIds = flowEdgesList.stream() List<Long> previousFormIds = flowEdgesList.stream()
.map(FlowEdgesEntity::getSourceId) .map(FlowEdgesEntity::getSourceId)
.toList(); .toList();
// Fetch the flow data based on previous form IDs List<FormEntity> previousForms = formRepository.findByIdIn(previousFormIds);
List<FlowDataEntity> flowDataList = flowDataRepository.findByFormIdInAndCallId(
previousFormIds, applicationEntity.getCall().getId());
List<String> chosenValues = flowDataList.stream() previousForms.sort(Comparator.comparing(FormEntity::getCreatedDate).reversed());
.map(FlowDataEntity::getChoosenValue)
.toList();
// Fetch the previous forms based on the chosen field values return previousForms.isEmpty() ? null : previousForms.get(0).getId();
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, public NextOrPreviousFormResponse getnextOrPreviousForm(ApplicationEntity applicationEntity, Long formId,
FormActionEnum action) { FormActionEnum action) {
Long calculatedFormId = null; Long calculatedFormId = null;

View File

@@ -10,4 +10,6 @@ import java.util.List;
public interface FormRepository extends JpaRepository<FormEntity,Long> { public interface FormRepository extends JpaRepository<FormEntity,Long> {
List<FormEntity> findByCallId(Long callId); List<FormEntity> findByCallId(Long callId);
List<FormEntity> findByIdIn(List<Long> formId);
} }