Updated Logic for Retrieving Previous Form
This commit is contained in:
@@ -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,52 +176,79 @@ 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());
|
||||
// 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);
|
||||
// }
|
||||
|
||||
if (flowEdgesList.isEmpty()) {
|
||||
return null;
|
||||
public Long getPreviousForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) {
|
||||
|
||||
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();
|
||||
}
|
||||
// // 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();
|
||||
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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user