10-11-2025
This commit is contained in:
@@ -0,0 +1,344 @@
|
||||
package com.billpaymentservice.bbps.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.billpaymentservice.bbps.dto.AmountOptionDTO;
|
||||
import com.billpaymentservice.bbps.dto.BillerIconDTO;
|
||||
import com.billpaymentservice.bbps.dto.CustomerParamGroupsDTO;
|
||||
import com.billpaymentservice.bbps.dto.GroupDTO;
|
||||
import com.billpaymentservice.bbps.dto.MasterCategoryBillerResponse;
|
||||
import com.billpaymentservice.bbps.dto.response.AmountOptionsResponse;
|
||||
import com.billpaymentservice.bbps.dto.response.FetchBillValidatestResponse;
|
||||
import com.billpaymentservice.bbps.dto.response.PlanResponse;
|
||||
import com.billpaymentservice.bbps.entites.AmountOptionByBillerId;
|
||||
import com.billpaymentservice.bbps.entites.AmountOptionDetails;
|
||||
import com.billpaymentservice.bbps.entites.BillerCategoryIcon;
|
||||
import com.billpaymentservice.bbps.entites.BillerCategoryWise;
|
||||
import com.billpaymentservice.bbps.entites.CategoryResponseList;
|
||||
import com.billpaymentservice.bbps.entites.CustomerParamGroupById;
|
||||
import com.billpaymentservice.bbps.entites.FetchBillValidate;
|
||||
import com.billpaymentservice.bbps.entites.GroupParam;
|
||||
import com.billpaymentservice.bbps.entites.PlanResponses;
|
||||
import com.billpaymentservice.bbps.repository.AmountOptionByBillerIdRepository;
|
||||
import com.billpaymentservice.bbps.repository.BillerCategoryWiseRepository;
|
||||
import com.billpaymentservice.bbps.repository.CategoryResponseListRepository;
|
||||
import com.billpaymentservice.bbps.repository.CustomerParamGroupByBillerIdRepository;
|
||||
import com.billpaymentservice.bbps.repository.FetchBillValidateRepository;
|
||||
import com.billpaymentservice.bbps.repository.PlanResponseRepository;
|
||||
|
||||
@Component
|
||||
public class BillerMapperHelper {
|
||||
|
||||
private final BillerCategoryWiseRepository billerRepo;
|
||||
private final CustomerParamGroupByBillerIdRepository customerParamGroupByBillerIdRepo;
|
||||
private final AmountOptionByBillerIdRepository amountOptionByBillerIdRepository;
|
||||
private final PlanResponseRepository planResponseRepository;
|
||||
private final CategoryResponseListRepository categoryResponseListRepository;
|
||||
private final FetchBillValidateRepository fetchBillValidateRepository;
|
||||
|
||||
public BillerMapperHelper(BillerCategoryWiseRepository billerRepo,
|
||||
CustomerParamGroupByBillerIdRepository customerParamGroupByBillerIdRepo,
|
||||
AmountOptionByBillerIdRepository amountOptionByBillerIdRepository,
|
||||
PlanResponseRepository planResponseRepository,
|
||||
CategoryResponseListRepository categoryResponseListRepository,
|
||||
FetchBillValidateRepository fetchBillValidateRepository) {
|
||||
this.billerRepo = billerRepo;
|
||||
this.customerParamGroupByBillerIdRepo = customerParamGroupByBillerIdRepo;
|
||||
this.amountOptionByBillerIdRepository = amountOptionByBillerIdRepository;
|
||||
this.planResponseRepository = planResponseRepository;
|
||||
this.categoryResponseListRepository = categoryResponseListRepository;
|
||||
this.fetchBillValidateRepository = fetchBillValidateRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a list of MasterCategoryBillerResponse DTOs to entities, saves them, and
|
||||
* returns the saved list
|
||||
*/
|
||||
public List<BillerCategoryWise> mapAndSaveAll(List<MasterCategoryBillerResponse> dtos, String requestId,
|
||||
String txnId) {
|
||||
if (dtos == null || dtos.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<BillerCategoryWise> entities = new ArrayList<>();
|
||||
|
||||
for (MasterCategoryBillerResponse dto : dtos) {
|
||||
// Check if biller already exists
|
||||
BillerCategoryWise biller = billerRepo.findById(dto.getBillerId()).orElse(new BillerCategoryWise()); // create
|
||||
// Set/update fields
|
||||
biller.setBillerId(dto.getBillerId());
|
||||
biller.setBillerName(dto.getBillerName());
|
||||
biller.setBillerType(dto.getBillerType());
|
||||
biller.setBillerCategory(dto.getBillerCategory());
|
||||
biller.setBillerCoverage(dto.getBillerCoverage());
|
||||
biller.setBillerResponseType(dto.getBillerResponseType());
|
||||
biller.setBillerDescription(dto.getBillerDescription());
|
||||
biller.setPlanMDMRequirement(dto.getPlanMDMRequirement());
|
||||
biller.setAdhocBiller(dto.getAdhocBiller());
|
||||
biller.setPaymentAmountExactness(dto.getPaymentAmountExactness());
|
||||
biller.setCreateDateTime(new Date());
|
||||
biller.setRequestId(requestId);
|
||||
biller.setTxnId(txnId);
|
||||
|
||||
// Handle icon
|
||||
BillerIconDTO iconDTO = dto.getBiller_icon();
|
||||
if (iconDTO != null) {
|
||||
BillerCategoryIcon icon;
|
||||
if (biller.getIcon() != null) {
|
||||
icon = biller.getIcon(); // update existing icon
|
||||
} else {
|
||||
icon = new BillerCategoryIcon();
|
||||
icon.setBiller(biller);
|
||||
}
|
||||
icon.setIconData(iconDTO.getIcon_data());
|
||||
icon.setIconType(iconDTO.getIcon_type());
|
||||
biller.setIcon(icon); // set to biller
|
||||
}
|
||||
|
||||
entities.add(biller);
|
||||
}
|
||||
|
||||
// Save all at once
|
||||
return billerRepo.saveAll(entities);
|
||||
}
|
||||
|
||||
public CustomerParamGroupsDTO saveCustomerParamGroups(CustomerParamGroupsDTO dtos, String requestId, String txnId) {
|
||||
if (dtos == null || dtos.getGroups() == null || dtos.getGroups().isEmpty()) {
|
||||
return new CustomerParamGroupsDTO(); // Return empty DTO if no groups exist
|
||||
}
|
||||
|
||||
List<CustomerParamGroupById> entities = new ArrayList<>();
|
||||
|
||||
// Loop through the top-level groups and save them (including nested groups)
|
||||
for (GroupDTO groupDTO : dtos.getGroups()) {
|
||||
CustomerParamGroupById entity = saveGroupWithChildren(groupDTO, null, requestId, txnId); // null is for root
|
||||
// (no
|
||||
// parent group)
|
||||
entities.add(entity); // Add the saved entity to the list
|
||||
|
||||
}
|
||||
|
||||
// Save all entities at once using the repository
|
||||
List<CustomerParamGroupById> savedEntities = customerParamGroupByBillerIdRepo.saveAll(entities);
|
||||
|
||||
// Convert saved entities back into DTOs to return
|
||||
return convertEntitiesToDTO(savedEntities);
|
||||
}
|
||||
|
||||
private CustomerParamGroupById saveGroupWithChildren(GroupDTO groupDTO, CustomerParamGroupById parentGroup,
|
||||
String requestId, String txnId) {
|
||||
|
||||
// Create a new entity for the group
|
||||
CustomerParamGroupById groupEntity = new CustomerParamGroupById();
|
||||
groupEntity.setGroupId(groupDTO.getGroupId());
|
||||
groupEntity.setName(groupDTO.getName());
|
||||
groupEntity.setInput(groupDTO.getInput());
|
||||
groupEntity.setParentGroup(parentGroup); // Link to the parent group (if exists)
|
||||
groupEntity.setRequestId(requestId);
|
||||
groupEntity.setTxnId(txnId);
|
||||
groupEntity.setCreateDateTime(new Date());
|
||||
// Save group parameters (if any)
|
||||
if (groupDTO.getParams() != null && !groupDTO.getParams().isEmpty()) {
|
||||
List<GroupParam> groupParams = new ArrayList<>();
|
||||
for (String paramName : groupDTO.getParams()) {
|
||||
GroupParam param = new GroupParam();
|
||||
param.setParamName(paramName); // Set the parameter name (e.g., "Admission No")
|
||||
param.setGroup(groupEntity); // Link the parameter to the current group
|
||||
groupParams.add(param);
|
||||
}
|
||||
groupEntity.setParams(groupParams); // Link parameters to the group
|
||||
}
|
||||
|
||||
// Recursively save child groups if they exist
|
||||
if (groupDTO.getGroups() != null && !groupDTO.getGroups().isEmpty()) {
|
||||
List<CustomerParamGroupById> childEntities = new ArrayList<>();
|
||||
for (GroupDTO childGroupDTO : groupDTO.getGroups()) {
|
||||
CustomerParamGroupById childEntity = saveGroupWithChildren(childGroupDTO, groupEntity, requestId,
|
||||
txnId); // Pass
|
||||
// current
|
||||
// group
|
||||
// as
|
||||
// parent
|
||||
childEntities.add(childEntity);
|
||||
}
|
||||
groupEntity.setChildGroups(childEntities); // Set the child groups
|
||||
}
|
||||
|
||||
// Save the current group entity and return it
|
||||
return groupEntity;
|
||||
}
|
||||
|
||||
private CustomerParamGroupsDTO convertEntitiesToDTO(List<CustomerParamGroupById> savedEntities) {
|
||||
|
||||
if (savedEntities == null || savedEntities.isEmpty()) {
|
||||
// Return an empty or default DTO if no entities are found
|
||||
return new CustomerParamGroupsDTO(); // return an empty DTO object
|
||||
}
|
||||
|
||||
CustomerParamGroupsDTO responseDTO = new CustomerParamGroupsDTO();
|
||||
List<GroupDTO> savedGroups = new ArrayList<>();
|
||||
|
||||
// Convert each saved entity to DTO
|
||||
for (CustomerParamGroupById savedEntity : savedEntities) {
|
||||
GroupDTO groupDTO = new GroupDTO();
|
||||
groupDTO.setGroupId(savedEntity.getGroupId());
|
||||
groupDTO.setName(savedEntity.getName());
|
||||
groupDTO.setInput(savedEntity.getInput());
|
||||
|
||||
// Recursively map child groups if they exist
|
||||
if (savedEntity.getChildGroups() != null) {
|
||||
List<GroupDTO> childGroupDTOs = new ArrayList<>();
|
||||
for (CustomerParamGroupById childEntity : savedEntity.getChildGroups()) {
|
||||
GroupDTO childGroupDTO = new GroupDTO();
|
||||
childGroupDTO.setGroupId(childEntity.getGroupId());
|
||||
childGroupDTO.setName(childEntity.getName());
|
||||
childGroupDTO.setInput(childEntity.getInput());
|
||||
// childGroupDTO.setParams(childEntity.getParams()); // Set parameters of child
|
||||
// group
|
||||
childGroupDTOs.add(childGroupDTO);
|
||||
}
|
||||
groupDTO.setGroups(childGroupDTOs);
|
||||
}
|
||||
|
||||
// Add the DTO to the response
|
||||
savedGroups.add(groupDTO);
|
||||
}
|
||||
|
||||
responseDTO.setGroups(savedGroups);
|
||||
return responseDTO;
|
||||
}
|
||||
|
||||
// Method to save AmountOptionsResponse and AmountOption entities
|
||||
public String saveAmountOptionsResponse(AmountOptionsResponse responseWrapper, String requestId, String txnId) {
|
||||
// Convert AmountOptionsResponseDTO to AmountOptionsResponse entity
|
||||
|
||||
if (responseWrapper == null) {
|
||||
// Return an empty or default DTO if no entities are found
|
||||
return "Data Is Empty"; // return an empty DTO object
|
||||
}
|
||||
AmountOptionByBillerId entity = new AmountOptionByBillerId();
|
||||
entity.setAmountOptions(responseWrapper.getAmountOptions());
|
||||
|
||||
// Map AmountOptionDTO list to AmountOptionDetails entities
|
||||
List<AmountOptionDetails> amountOptionDetailsList = mapAmountOptionDTOToEntity(
|
||||
responseWrapper.getAmountOptionDAOS(), requestId, txnId);
|
||||
|
||||
// Set the mapped AmountOptionDetails list to AmountOptionByBillerId entity
|
||||
entity.setAmountOptionDetails(amountOptionDetailsList);
|
||||
entity.setCreateDateTime(new Date());
|
||||
|
||||
// Save AmountOptionByBillerId entity (this will also save related
|
||||
// AmountOptionDetails entities)
|
||||
amountOptionByBillerIdRepository.save(entity); // Saves the entity and cascades to AmountOptionDetails
|
||||
|
||||
// Return success message
|
||||
return "Data saved successfully!";
|
||||
}
|
||||
|
||||
private List<AmountOptionDetails> mapAmountOptionDTOToEntity(List<AmountOptionDTO> amountOptionDTOs,
|
||||
String requestId, String txnId) {
|
||||
|
||||
// If the list is null or empty, return an empty list of entities
|
||||
if (amountOptionDTOs == null || amountOptionDTOs.isEmpty()) {
|
||||
return new ArrayList<>(); // Return an empty list of AmountOptionDetails
|
||||
}
|
||||
|
||||
// AmountOptionDetails entities
|
||||
return amountOptionDTOs.stream().flatMap(dto ->
|
||||
// For each DTO, convert its breakup sets to AmountOptionDetails entities
|
||||
dto.getAmountBreakupSets().stream().map(breakup -> {
|
||||
// Create a new AmountOptionDetails entity for each breakup set
|
||||
AmountOptionDetails amountOptionDetails = new AmountOptionDetails();
|
||||
|
||||
// Set the breakup set as a separate entity (each breakup set becomes a new
|
||||
// record)
|
||||
amountOptionDetails.setAmountBreakupSet(breakup); // Store each breakup set as an individual record
|
||||
|
||||
// You can optionally set the amountOptionId here, if you want to link this
|
||||
// detail to the parent.
|
||||
// Assuming you want to set it directly here or pass it via the entity later:
|
||||
amountOptionDetails.setAmountOptionId(dto.getAmountOptionId());
|
||||
amountOptionDetails.setRequestId(requestId);
|
||||
amountOptionDetails.setTxnId(txnId);
|
||||
|
||||
// Return the mapped AmountOptionDetails entity
|
||||
return amountOptionDetails;
|
||||
})).collect(Collectors.toList()); // Collect them into a list and return
|
||||
}
|
||||
|
||||
public PlanResponses savePlanResponse(PlanResponse planResponseDTO, String requestId, String txnId) {
|
||||
|
||||
// If the list is null or empty, return an empty list of entities
|
||||
if (planResponseDTO == null) {
|
||||
return new PlanResponses(); // Return an empty list of AmountOptionDetails
|
||||
}
|
||||
|
||||
// Convert DTO to Entity using ModelMapper
|
||||
ModelMapper modelMapper = new ModelMapper();
|
||||
PlanResponses planResponseEntity = modelMapper.map(planResponseDTO, PlanResponses.class);
|
||||
// Save the entity to the database
|
||||
// Set the requestId and txnId in the entity
|
||||
planResponseEntity.setRequestId(requestId);
|
||||
planResponseEntity.setTxnId(txnId);
|
||||
planResponseEntity.setCreateDateTime(new Date());
|
||||
return planResponseRepository.save(planResponseEntity);
|
||||
}
|
||||
|
||||
public String saveCategoryItems(List<String> categoryRespList, String requestId, String txnId) {
|
||||
int savedCount = 0; // To track how many items were saved
|
||||
|
||||
if (categoryRespList == null || categoryRespList.isEmpty()) {
|
||||
return "No items to save.";
|
||||
}
|
||||
|
||||
for (String item : categoryRespList) {
|
||||
CategoryResponseList category = new CategoryResponseList();
|
||||
category.setCategoryRespList(item);
|
||||
category.setRequestId(txnId);
|
||||
category.setRequestId(requestId);
|
||||
category.setCreateDateTime(new Date());
|
||||
categoryResponseListRepository.save(category);
|
||||
savedCount++;
|
||||
}
|
||||
return savedCount + " items were successfully saved.";
|
||||
}
|
||||
|
||||
public FetchBillValidate saveFetchBillValidateResponse(FetchBillValidatestResponse dto, String requestId,
|
||||
String txnId) {
|
||||
|
||||
if (dto == null) {
|
||||
return new FetchBillValidate();
|
||||
}
|
||||
|
||||
// Convert DTO to Entity
|
||||
FetchBillValidate entity = new FetchBillValidate();
|
||||
|
||||
entity.setBillId(dto.getBillId());
|
||||
entity.setRefId(dto.getRefId());
|
||||
entity.setBillerId(dto.getBillerId());
|
||||
entity.setStatus(dto.getStatus());
|
||||
entity.setActionType(dto.getActionType());
|
||||
entity.setUserId(dto.getUserId());
|
||||
entity.setBillerResponse(dto.getBillerResponse());
|
||||
entity.setAdditionalInfo(dto.getAdditionalInfo());
|
||||
entity.setCustomerParams(dto.getCustomerParams());
|
||||
entity.setPlanInfoDaos(dto.getPlanInfoDaos());
|
||||
entity.setCreationDate(dto.getCreationDate());
|
||||
entity.setModifiedDate(dto.getModifiedDate());
|
||||
entity.setCreatedEpochDate(dto.getCreatedEpochDate());
|
||||
entity.setModifiedEpochDate(dto.getModifiedEpochDate());
|
||||
entity.setRequestId(requestId);
|
||||
entity.setTxnId(txnId);
|
||||
entity.setCreateDateTime(new Date());
|
||||
|
||||
// Save the entity in the database
|
||||
return fetchBillValidateRepository.save(entity);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user