Added get api which is responsable for get next or pevious form
This commit is contained in:
237
src/main/java/net/gepafin/tendermanagement/dao/FlowFormDao.java
Normal file
237
src/main/java/net/gepafin/tendermanagement/dao/FlowFormDao.java
Normal file
@@ -0,0 +1,237 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import java.util.List;
|
||||
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.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, FormEntity formEntity,
|
||||
FormActionEnum action) {
|
||||
|
||||
NextOrPreviousFormResponse nextOrPreviousFormResponse = new NextOrPreviousFormResponse();
|
||||
if (action.equals(FormActionEnum.NEXT)) {
|
||||
nextOrPreviousFormResponse.setNextFormId(getNextForm(formEntity, applicationEntity));
|
||||
} else {
|
||||
nextOrPreviousFormResponse.setPreviousFormId(getPreviousForm(formEntity, applicationEntity));
|
||||
}
|
||||
return nextOrPreviousFormResponse;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user