Merge pull request #22 from Kitzanos/update-previous-form-logic

Updated Logic for Retrieving Previous Form
This commit is contained in:
rajeshkhore
2024-09-22 23:22:53 -07:00
committed by GitHub
2 changed files with 68 additions and 40 deletions

View File

@@ -1,10 +1,12 @@
package net.gepafin.tendermanagement.dao;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import net.gepafin.tendermanagement.repositories.*;
import org.springframework.beans.factory.annotation.Autowired;
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.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.service.FormService;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
@@ -47,7 +45,8 @@ public class FlowFormDao {
@Autowired
private FormService formService;
@Autowired
private FormRepository formRepository;
// Long getNextForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) {
// // vlaidation if next form findout and cuuent from is not fill the give error
@@ -177,8 +176,54 @@ public class FlowFormDao {
.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 Long getPreviousForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) {
// Retrieve the flow edges for the previous forms
List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findByTargetIdAndCallId(
currentFormEntity.getId(), applicationEntity.getCall().getId());
@@ -188,41 +233,22 @@ public class FlowFormDao {
// 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();
}
// // 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<FormEntity> previousForms = formRepository.findByIdIn(previousFormIds);
List<String> chosenValues = flowDataList.stream()
.map(FlowDataEntity::getChoosenValue)
.toList();
previousForms.sort(Comparator.comparing(FormEntity::getCreatedDate).reversed());
// 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);
return previousForms.isEmpty() ? null : previousForms.get(0).getId();
}
public NextOrPreviousFormResponse getnextOrPreviousForm(ApplicationEntity applicationEntity, Long formId,
FormActionEnum action) {
Long calculatedFormId = null;

View File

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