diff --git a/src/main/java/net/gepafin/tendermanagement/dao/FlowFormDao.java b/src/main/java/net/gepafin/tendermanagement/dao/FlowFormDao.java index 013f018c..1ffb9059 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/FlowFormDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/FlowFormDao.java @@ -1,6 +1,7 @@ package net.gepafin.tendermanagement.dao; import java.util.*; +import java.util.stream.Collectors; import net.gepafin.tendermanagement.enums.ApplicationStatusTypeEnum; import net.gepafin.tendermanagement.repositories.*; @@ -174,81 +175,81 @@ public class FlowFormDao { .orElse(null); } -// public Long getPreviousForm(FormEntity currentFormEntity, ApplicationEntity applicationEntity) { -// // Retrieve the flow edges for the previous forms -// List 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 previousFormIds = flowEdgesList.stream() -// .map(FlowEdgesEntity::getSourceId) -// .toList(); -// -// // Fetch the flow data based on previous form IDs -// List flowDataList = flowDataRepository.findByFormIdInAndCallId( -// previousFormIds, applicationEntity.getCall().getId()); -// -// List chosenValues = flowDataList.stream() -// .map(FlowDataEntity::getChoosenValue) -// .toList(); -// -// // Fetch the previous forms based on the chosen field values -// Set 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 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 flowEdgesList = flowEdgesRepository.findByTargetIdAndCallId( + currentFormEntity.getId(), applicationEntity.getCall().getId()); - List flowEdgesList = flowEdgesRepository.findByTargetIdAndCallId( - currentFormEntity.getId(), applicationEntity.getCall().getId()); - - if (flowEdgesList.isEmpty()) { - return null; + 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 previousFormIds = flowEdgesList.stream() - .map(FlowEdgesEntity::getSourceId) - .toList(); - if (previousFormIds.size() == 1) { - return previousFormIds.get(0); - } + List previousFormIds = flowEdgesList.stream() + .map(FlowEdgesEntity::getSourceId) + .toList(); - List applicationFormEntities=applicationFormRepository.findByFormIdInAndApplicationId(previousFormIds,applicationEntity.getId()); - applicationFormEntities.sort(Comparator.comparing(ApplicationFormEntity::getCreatedDate).reversed()); + // Fetch the flow data based on previous form IDs + List flowDataList = flowDataRepository.findByFormIdInAndCallId( + previousFormIds, applicationEntity.getCall().getId()); - return applicationFormEntities.isEmpty() ? null : applicationFormEntities.get(0).getForm().getId(); + List chosenValues = flowDataList.stream() + .map(FlowDataEntity::getChoosenValue) + .toList(); + + // Fetch the previous forms based on the chosen field values + Set 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 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) { +// +// List 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 previousFormIds = flowEdgesList.stream() +// .map(FlowEdgesEntity::getSourceId) +// .toList(); +// if (previousFormIds.size() == 1) { +// return previousFormIds.get(0); +// } +// +// List applicationFormEntities=applicationFormRepository.findByFormIdInAndApplicationId(previousFormIds,applicationEntity.getId()); +// applicationFormEntities.sort(Comparator.comparing(ApplicationFormEntity::getCreatedDate).reversed()); +// +// return applicationFormEntities.isEmpty() ? null : applicationFormEntities.get(0).getForm().getId(); +// } public NextOrPreviousFormResponse getNextOrPreviousForm(ApplicationEntity applicationEntity, Long formId, FormActionEnum action) { Long calculatedFormId = null; @@ -354,8 +355,7 @@ public class FlowFormDao { for (ApplicationFormEntity applicationFormEntity : applicationFormList) { Boolean isCompleted = formDao.validateCompletedSteps(applicationFormFieldRepository.findByApplicationFormId(applicationFormEntity.getId()), applicationEntity, applicationFormEntity.getForm()); if (Boolean.FALSE.equals(isCompleted)) { - currentFormEntity = applicationFormEntity.getForm(); - break; + return applicationFormEntity.getForm().getId(); } } return getNextForm(currentFormEntity, applicationEntity);