Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HCMPRE:1782 - Plan-facility integration to fetch fixedPost details #1325

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class Configuration {
@Value("${egov.plan.search.endpoint}")
private String planSearchEndPoint;

@Value("${egov.plan.facility.search.endpoint}")
private String planFacilitySearchEndPoint;

// Filestore

@Value("${egov.filestore.service.host}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public class ServiceConstants {
public static final String NO_PLAN_FOUND_FOR_GIVEN_DETAILS_CODE = "NO_PLAN_FOUND_FOR_GIVEN_DETAILS";
public static final String NO_PLAN_FOUND_FOR_GIVEN_DETAILS_MESSAGE = "Plan records do not exists for the given details: ";

public static final String NO_PLAN_FACILITY_FOUND_FOR_GIVEN_DETAILS_CODE = "NO_PLAN_FACILITY_FOUND_FOR_GIVEN_DETAILS";
public static final String NO_PLAN_FACILITY_FOUND_FOR_GIVEN_DETAILS_MESSAGE = "Plan facilities do not exists for the given details: ";

public static final String BOUNDARY_CODE = "HCM_ADMIN_CONSOLE_BOUNDARY_CODE";
public static final String TOTAL_POPULATION = "HCM_ADMIN_CONSOLE_TOTAL_POPULATION";

Expand Down Expand Up @@ -91,7 +94,8 @@ public class ServiceConstants {
public static final String NAME = "name";

public static final String ERROR_WHILE_UPDATING_PLAN_CONFIG = "Exception occurred while updating plan configuration.";
public static final String ERROR_WHILE_SEARCHING_PLAN = "Exception occurred while search plans.";
public static final String ERROR_WHILE_SEARCHING_PLAN = "Exception occurred while searching plans.";
public static final String ERROR_WHILE_SEARCHING_PLAN_FACILITY = "Exception occurred while searching plan facility.";

public static final String VALIDATE_STRING_REGX = "^(?!\\d+$).+$";
public static final String VALIDATE_NUMBER_REGX = "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$";
Expand Down Expand Up @@ -126,6 +130,7 @@ public class ServiceConstants {
public static final String MICROPLAN_ID_KEY = "microplanId";
public static final String FACILITY_NAME = "facilityName";
public static final String HCM_MICROPLAN_SERVING_FACILITY = "HCM_MICROPLAN_SERVING_FACILITY";
public static final String FIXED_POST = "fixedPost";

//Census additional field constants
public static final String UPLOADED_KEY = "UPLOADED_";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
import org.egov.processor.web.models.campaignManager.Boundary;
import org.egov.processor.web.models.campaignManager.CampaignResources;
import org.egov.processor.web.models.campaignManager.CampaignResponse;
import org.egov.processor.web.models.planFacility.PlanFacility;
import org.egov.processor.web.models.planFacility.PlanFacilityResponse;
import org.egov.processor.web.models.planFacility.PlanFacilitySearchCriteria;
import org.egov.processor.web.models.planFacility.PlanFacilitySearchRequest;
import org.egov.tracer.model.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -30,6 +35,8 @@
import java.util.*;
import java.util.stream.Collectors;

import static org.egov.processor.config.ServiceConstants.*;

@Slf4j
@Service
public class ExcelParser implements FileParser {
Expand Down Expand Up @@ -62,9 +69,11 @@ public class ExcelParser implements FileParser {

private OutputEstimationGenerationUtil outputEstimationGenerationUtil;

private PlanFacilityUtil planFacilityUtil;

public ExcelParser(ObjectMapper objectMapper, ParsingUtil parsingUtil, FilestoreUtil filestoreUtil,
CalculationUtil calculationUtil, PlanUtil planUtil, CampaignIntegrationUtil campaignIntegrationUtil,
Configuration config, MdmsUtil mdmsUtil, BoundaryUtil boundaryUtil, LocaleUtil localeUtil, CensusUtil censusUtil, EnrichmentUtil enrichmentUtil, PlanConfigurationUtil planConfigurationUtil, OutputEstimationGenerationUtil outputEstimationGenerationUtil) {
Configuration config, MdmsUtil mdmsUtil, BoundaryUtil boundaryUtil, LocaleUtil localeUtil, CensusUtil censusUtil, EnrichmentUtil enrichmentUtil, PlanConfigurationUtil planConfigurationUtil, OutputEstimationGenerationUtil outputEstimationGenerationUtil, PlanFacilityUtil planFacilityUtil) {
this.objectMapper = objectMapper;
this.parsingUtil = parsingUtil;
this.filestoreUtil = filestoreUtil;
Expand All @@ -79,6 +88,7 @@ public ExcelParser(ObjectMapper objectMapper, ParsingUtil parsingUtil, Filestore
this.enrichmentUtil = enrichmentUtil;
this.planConfigurationUtil = planConfigurationUtil;
this.outputEstimationGenerationUtil = outputEstimationGenerationUtil;
this.planFacilityUtil = planFacilityUtil;
}

/**
Expand Down Expand Up @@ -230,12 +240,62 @@ private void processSheets(PlanConfigurationRequest request, String fileStoreId,
processRowsForCensusRecords(request, excelWorkbookSheet,
fileStoreId, attributeNameVsDataTypeMap, boundaryCodeList, campaign.getCampaign().get(0).getHierarchyType());
} else if (request.getPlanConfiguration().getStatus().equals(config.getPlanConfigUpdatePlanEstimatesIntoOutputFileStatus())) {

// Create a Map of Boundary Code to Facility's fixed post detail.
Map<String, String> boundaryCodeToFixedPostMap = fetchFixedPostDetails(request, excelWorkbook, fileStoreId);

enrichmentUtil.enrichsheetWithApprovedPlanEstimates(excelWorkbookSheet, request, fileStoreId, mappedValues);
}
}
});
}

/**
* This method makes plan facility search call and creates a map of boundary code to it's fixed post facility details.
*
* @param request the plan configuration request.
* @param excelWorkbook the Excel workbook to be processed.
* @param fileStoreId the fileStore id of the file.
* @return returns a map of boundary code to it's fixed post facility details.
*/
private Map<String, String> fetchFixedPostDetails(PlanConfigurationRequest request, Workbook excelWorkbook, String fileStoreId) {
PlanConfiguration planConfiguration = request.getPlanConfiguration();

// Create the map of boundary code to the facility assigned to that boundary.
Map<String, String> boundaryCodeToFacilityNameMap = outputEstimationGenerationUtil.getBoundaryCodeToFacilityMap(excelWorkbook, request, fileStoreId);

//Create plan facility search request
PlanFacilitySearchRequest searchRequest = PlanFacilitySearchRequest.builder()
.requestInfo(request.getRequestInfo())
.planFacilitySearchCriteria(PlanFacilitySearchCriteria.builder()
.tenantId(planConfiguration.getTenantId())
.planConfigurationId(planConfiguration.getId())
.build())
.build();

PlanFacilityResponse planFacilityResponse = planFacilityUtil.search(searchRequest);
tanishi-egov marked this conversation as resolved.
Show resolved Hide resolved

if (CollectionUtils.isEmpty(planFacilityResponse.getPlanFacility())) {
throw new CustomException(NO_PLAN_FACILITY_FOUND_FOR_GIVEN_DETAILS_CODE, NO_PLAN_FACILITY_FOUND_FOR_GIVEN_DETAILS_MESSAGE);
}

// Create a Boundary Code to Facility's fixed post detail map.
Map<String, String> boundaryCodeToFixedPostMap = new HashMap<>();

for (PlanFacility planFacility : planFacilityResponse.getPlanFacility()) {
// Find the boundary code corresponding to the facility name.
String boundaryCode = findByValue(boundaryCodeToFacilityNameMap, planFacility.getFacilityName());

// Extract the 'FIXED_POST' field from additional details.
String fixedPostValue = (String) parsingUtil.extractFieldsFromJsonObject(planFacility.getAdditionalDetails(), FIXED_POST);

// Populate the map.
boundaryCodeToFixedPostMap.put(boundaryCode, fixedPostValue);
}

return boundaryCodeToFixedPostMap;
}

/**
* Processes rows of data in an Excel sheet, performs calculations, updates
* campaign boundaries, and creates plans.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void addAssignedFacility(Workbook workbook, PlanConfigurationRequest requ
LinkedHashMap::new
));

// Get the map of boundary code to the facility assigned to that boundary.
// Create the map of boundary code to the facility assigned to that boundary.
Map<String, String> boundaryCodeToFacility = getBoundaryCodeToFacilityMap(workbook, request, fileStoreId);

// Iterate through all sheets in the workbook.
Expand All @@ -160,7 +160,7 @@ public void addAssignedFacility(Workbook workbook, PlanConfigurationRequest requ
* @param fileStoreId the associated file store ID for filtering.
* @return a map of boundary codes to their assigned facility names.
*/
private Map<String, String> getBoundaryCodeToFacilityMap(Workbook workbook, PlanConfigurationRequest request, String fileStoreId) {
public Map<String, String> getBoundaryCodeToFacilityMap(Workbook workbook, PlanConfigurationRequest request, String fileStoreId) {
List<String> boundaryCodes = new ArrayList<>();

// Iterate through all sheets in the workbook.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.egov.processor.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.egov.processor.config.Configuration;
import org.egov.processor.config.ServiceConstants;
import org.egov.processor.repository.ServiceRequestRepository;
import org.egov.processor.web.models.planFacility.PlanFacilityResponse;
import org.egov.processor.web.models.planFacility.PlanFacilitySearchRequest;
import org.egov.tracer.model.CustomException;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import static org.egov.processor.config.ServiceConstants.*;

@Slf4j
@Component
public class PlanFacilityUtil {
private Configuration config;

private ServiceRequestRepository serviceRequestRepository;

private ObjectMapper mapper;

public PlanFacilityUtil(Configuration config, ServiceRequestRepository serviceRequestRepository, ObjectMapper mapper) {
this.config = config;
this.serviceRequestRepository = serviceRequestRepository;
this.mapper = mapper;
}

/**
* Searches for plan facilities based on the provided search request.
*
* @param planfacilitySearchRequest The search request containing the search criteria.
* @return A response with a list of plan facilities that matches the search criteria.
*/
public PlanFacilityResponse search(PlanFacilitySearchRequest planfacilitySearchRequest) {
tanishi-egov marked this conversation as resolved.
Show resolved Hide resolved

PlanFacilityResponse planFacilityResponse = null;
try {
Object response = serviceRequestRepository.fetchResult(getPlanFacilitySearchUri(), planfacilitySearchRequest);
planFacilityResponse = mapper.convertValue(response, PlanFacilityResponse.class);
} catch (Exception e) {
log.error(ServiceConstants.ERROR_WHILE_SEARCHING_PLAN_FACILITY);
}

return planFacilityResponse;
}

/**
* Creates a search uri for plan facility search.
* @return
*/
private StringBuilder getPlanFacilitySearchUri() {
return new StringBuilder().append(config.getPlanConfigHost()).append(config.getPlanFacilitySearchEndPoint());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.egov.processor.web.models.planFacility;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.egov.common.contract.models.AuditDetails;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* Plan Facility
*/
@Validated
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PlanFacility {

@JsonProperty("id")
private String id = null;

@JsonProperty("tenantId")
@NotNull
@Size(min = 2, max = 64)
private String tenantId = null;

@JsonProperty("planConfigurationId")
@NotNull
@Size(max = 64)
private String planConfigurationId = null;

@JsonProperty("planConfigurationName")
private String planConfigurationName = null;

@JsonProperty("boundaryAncestralPath")
private String boundaryAncestralPath = null;

@JsonProperty("facilityId")
@NotNull
@Size(max = 64)
private String facilityId = null;

@JsonProperty("facilityName")
private String facilityName = null;

@JsonProperty("residingBoundary")
@NotNull
@Size(min = 1, max = 64)
private String residingBoundary = null;

@JsonProperty("serviceBoundaries")
@NotNull
@Valid
private List<String> serviceBoundaries;

@JsonIgnore
private List<String> initiallySetServiceBoundaries;

@JsonProperty("jurisdictionMapping")
private Map<String, String> jurisdictionMapping;

@JsonProperty("additionalDetails")
private Object additionalDetails = null;

@JsonProperty("active")
@NotNull
private Boolean active = null;

@JsonProperty("auditDetails")
private AuditDetails auditDetails = null;

public PlanFacility addServiceBoundariesItem(String serviceBoundariesItem) {
if (this.serviceBoundaries == null) {
this.serviceBoundaries = new ArrayList<>();
}
this.serviceBoundaries.add(serviceBoundariesItem);
return this;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.egov.processor.web.models.planFacility;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.egov.common.contract.response.ResponseInfo;
import org.springframework.validation.annotation.Validated;
import java.util.List;

/**
* PlanFacilityResponse
*/
@Validated
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PlanFacilityResponse {

@JsonProperty("ResponseInfo")
private ResponseInfo responseInfo = null;

@JsonProperty("PlanFacility")
@Valid
private List<PlanFacility> planFacility = null;

@JsonProperty("TotalCount")
@Valid
private Integer totalCount = null;
}
Loading
Loading