From 38667e8c9bf2eb1a156360b1c223ededeac799fd Mon Sep 17 00:00:00 2001 From: Tanishi Goyal Date: Fri, 20 Dec 2024 17:53:34 +0530 Subject: [PATCH] Change the mdms master to adminSchemas in resource generator --- .../processor/config/ServiceConstants.java | 6 ++ .../egov/processor/service/ExcelParser.java | 5 +- .../org/egov/processor/util/MdmsUtil.java | 62 +++++++++++-------- .../org/egov/processor/util/ParsingUtil.java | 42 ++++++++++++- 4 files changed, 84 insertions(+), 31 deletions(-) diff --git a/health-services/resource-generator/src/main/java/org/egov/processor/config/ServiceConstants.java b/health-services/resource-generator/src/main/java/org/egov/processor/config/ServiceConstants.java index 7200f29cec..d677c5f06b 100644 --- a/health-services/resource-generator/src/main/java/org/egov/processor/config/ServiceConstants.java +++ b/health-services/resource-generator/src/main/java/org/egov/processor/config/ServiceConstants.java @@ -61,13 +61,19 @@ public class ServiceConstants { public static final String FILE_NAME = "output.xls"; public static final String FILE_TYPE = "boundaryWithTarget"; public static final String FILE_TEMPLATE_IDENTIFIER_POPULATION = "Population"; + public static final String FILE_TEMPLATE_IDENTIFIER_BOUNDARY = "boundary"; public static final String FILE_TEMPLATE_IDENTIFIER_FACILITY = "Facilities"; public static final String INPUT_IS_NOT_VALID = "File does not contain valid input for row "; + + public static final String PROVIDED_KEY_IS_NOT_PRESENT_IN_JSON_OBJECT_CODE = "PROVIDED_KEY_IS_NOT_PRESENT_IN_JSON_OBJECT"; + public static final String PROVIDED_KEY_IS_NOT_PRESENT_IN_JSON_OBJECT_MESSAGE = "Key is not present in json object - "; public static final String MDMS_SCHEMA_TYPE = "type"; public static final String MDMS_SCHEMA_SECTION = "section"; + public static final String MDMS_SCHEMA_TITLE = "title"; public static final String MDMS_PLAN_MODULE_NAME = "hcm-microplanning"; public static final String MDMS_MASTER_SCHEMAS = "Schemas"; + public static final String MDMS_MASTER_ADMIN_SCHEMA = "adminSchema"; public static final String MDMS_CAMPAIGN_TYPE = "campaignType"; public static final String MDMS_SCHEMA_ADMIN_SCHEMA = "adminSchema"; public static final String MDMS_ADMIN_CONSOLE_MODULE_NAME = "HCM-ADMIN-CONSOLE"; diff --git a/health-services/resource-generator/src/main/java/org/egov/processor/service/ExcelParser.java b/health-services/resource-generator/src/main/java/org/egov/processor/service/ExcelParser.java index 6f2326d628..a78fc84cb4 100644 --- a/health-services/resource-generator/src/main/java/org/egov/processor/service/ExcelParser.java +++ b/health-services/resource-generator/src/main/java/org/egov/processor/service/ExcelParser.java @@ -312,14 +312,11 @@ private List getBoundaryCodeList(PlanConfigurationRequest planConfigurat * @return A map of attribute names to their corresponding indices or data types. */ - - //TODO: fetch from adminSchema master private Map prepareAttributeVsIndexMap(PlanConfigurationRequest planConfigurationRequest, String fileStoreId, CampaignResponse campaign, PlanConfiguration planConfig, Object mdmsData) { org.egov.processor.web.models.File file = planConfig.getFiles().stream() .filter(f -> f.getFilestoreId().equalsIgnoreCase(fileStoreId)).findFirst().get(); - return mdmsUtil.filterMasterData(mdmsData.toString(), file.getInputFileType(), - file.getTemplateIdentifier(), campaign.getCampaign().get(0).getProjectType()); + return mdmsUtil.filterMasterData(mdmsData.toString(), campaign.getCampaign().get(0).getProjectType()); } diff --git a/health-services/resource-generator/src/main/java/org/egov/processor/util/MdmsUtil.java b/health-services/resource-generator/src/main/java/org/egov/processor/util/MdmsUtil.java index e3019ded61..0cff31c44c 100644 --- a/health-services/resource-generator/src/main/java/org/egov/processor/util/MdmsUtil.java +++ b/health-services/resource-generator/src/main/java/org/egov/processor/util/MdmsUtil.java @@ -1,9 +1,5 @@ package org.egov.processor.util; -import static org.egov.processor.config.ServiceConstants.ERROR_WHILE_FETCHING_FROM_MDMS; -import static org.egov.processor.config.ServiceConstants.NO_MDMS_DATA_FOUND_FOR_GIVEN_TENANT_CODE; -import static org.egov.processor.config.ServiceConstants.NO_MDMS_DATA_FOUND_FOR_GIVEN_TENANT_MESSAGE; - import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; @@ -31,6 +27,8 @@ import lombok.extern.slf4j.Slf4j; +import static org.egov.processor.config.ServiceConstants.*; + @Slf4j @Component public class MdmsUtil { @@ -41,11 +39,14 @@ public class MdmsUtil { private Configuration configs; - public MdmsUtil(RestTemplate restTemplate, ObjectMapper mapper, Configuration configs) { + private ParsingUtil parsingUtil; + + public MdmsUtil(RestTemplate restTemplate, ObjectMapper mapper, Configuration configs, ParsingUtil parsingUtil) { this.restTemplate = restTemplate; this.mapper = mapper; this.configs = configs; - } + this.parsingUtil = parsingUtil; + } /** * Fetches MDMS (Municipal Data Management System) data using the provided @@ -74,7 +75,7 @@ public Object fetchMdmsData(RequestInfo requestInfo, String tenantId) { if (result == null || ObjectUtils.isEmpty(result)) { log.error(NO_MDMS_DATA_FOUND_FOR_GIVEN_TENANT_MESSAGE + " - " + tenantId); throw new CustomException(NO_MDMS_DATA_FOUND_FOR_GIVEN_TENANT_CODE, - "no data found for the given tenantid "+tenantId + " for master name "+ServiceConstants.MDMS_MASTER_SCHEMAS); + "no data found for the given tenantid "+tenantId + " for master name "+ServiceConstants.MDMS_MASTER_ADMIN_SCHEMA); } return result; } @@ -87,10 +88,9 @@ public Object fetchMdmsData(RequestInfo requestInfo, String tenantId) { * @return The MDMS criteria request object. */ public MdmsCriteriaReq getMdmsRequest(RequestInfo requestInfo, String tenantId) { - - ModuleDetail moduleDetail = getPlanModuleDetail(); + ModuleDetail adminConsoleModuleDetail = getAdminConsoleModuleDetail(); List moduleDetails = new LinkedList<>(); - moduleDetails.add(moduleDetail); + moduleDetails.add(adminConsoleModuleDetail); MdmsCriteria mdmsCriteria = MdmsCriteria.builder().moduleDetails(moduleDetails).tenantId(tenantId).build(); return MdmsCriteriaReq.builder().mdmsCriteria(mdmsCriteria).requestInfo(requestInfo).build(); } @@ -109,33 +109,45 @@ private ModuleDetail getPlanModuleDetail() { .moduleName(ServiceConstants.MDMS_PLAN_MODULE_NAME).build(); } + private ModuleDetail getAdminConsoleModuleDetail() { + List adminSchemaMasterDetails = new ArrayList<>(); + MasterDetail schemaDetails = MasterDetail.builder().name(ServiceConstants.MDMS_MASTER_ADMIN_SCHEMA).build(); + adminSchemaMasterDetails.add(schemaDetails); + + return ModuleDetail.builder().masterDetails(adminSchemaMasterDetails) + .moduleName(ServiceConstants.MDMS_ADMIN_CONSOLE_MODULE_NAME).build(); + } + /** * Filters master data based on the provided parameters. * * @param masterDataJson The JSON string representing the master data. - * @param fileType The type of input file. - * @param templateIdentifier The template identifier. * @param campaignType The campaign type. * @return A map containing filtered properties from the master data. * @throws JsonMappingException if there's an issue mapping JSON. * @throws JsonProcessingException if there's an issue processing JSON. */ - public Map filterMasterData(String masterDataJson, File.InputFileTypeEnum fileType, - String templateIdentifier, String campaignType) { + public Map filterMasterData(String masterDataJson, String campaignType) { Map properties = new HashMap<>(); Map masterData = JsonUtils.parseJson(masterDataJson, Map.class); - Map planModule = (Map) masterData.get(ServiceConstants.MDMS_PLAN_MODULE_NAME); - List> schemas = (List>) planModule - .get(ServiceConstants.MDMS_MASTER_SCHEMAS); - log.info("masterDataJson ==>" + schemas); - for (Map schema : schemas) { - String type = (String) schema.get(ServiceConstants.MDMS_SCHEMA_TYPE); + Map adminConsoleModule = (Map) masterData.get(ServiceConstants.MDMS_ADMIN_CONSOLE_MODULE_NAME); + List> adminSchema = (List>) adminConsoleModule + .get(ServiceConstants.MDMS_MASTER_ADMIN_SCHEMA); + log.info("masterDataJson ==> " + adminSchema); + + for (Map schema : adminSchema) { String campaign = (String) schema.get(ServiceConstants.MDMS_CAMPAIGN_TYPE); - // String fileT = InputFileTypeEnum.valueOf(type); - if (schema.get(ServiceConstants.MDMS_SCHEMA_SECTION).equals(ServiceConstants.FILE_TEMPLATE_IDENTIFIER_POPULATION) - && campaign.equals(campaignType) && type.equals(fileType.toString())) { - Map schemaProperties = (Map) schema.get("schema"); - properties = (Map) schemaProperties.get("Properties"); + + if (schema.get(ServiceConstants.MDMS_SCHEMA_TITLE).equals(ServiceConstants.FILE_TEMPLATE_IDENTIFIER_BOUNDARY) + && campaign.equals(MICROPLAN_PREFIX + campaignType)) { + Map> schemaProperties = (Map>) schema.get("properties"); + + schemaProperties.forEach((propertyType, propertyList) -> + propertyList.forEach(property -> { + String propertyName = (String) parsingUtil.extractFieldsFromJsonObject(property, "name"); + properties.put(propertyName, property); + }) + ); } } diff --git a/health-services/resource-generator/src/main/java/org/egov/processor/util/ParsingUtil.java b/health-services/resource-generator/src/main/java/org/egov/processor/util/ParsingUtil.java index ba10327e4c..7825ac2101 100644 --- a/health-services/resource-generator/src/main/java/org/egov/processor/util/ParsingUtil.java +++ b/health-services/resource-generator/src/main/java/org/egov/processor/util/ParsingUtil.java @@ -17,6 +17,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.math.BigDecimal; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.*; @@ -24,7 +25,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import static org.egov.processor.config.ServiceConstants.PROPERTIES; +import static org.egov.processor.config.ServiceConstants.*; @Slf4j @Component @@ -36,10 +37,13 @@ public class ParsingUtil { private CalculationUtil calculationUtil; - public ParsingUtil(PlanConfigurationUtil planConfigurationUtil, FilestoreUtil filestoreUtil, CalculationUtil calculationUtil) { + private ObjectMapper objectMapper; + + public ParsingUtil(PlanConfigurationUtil planConfigurationUtil, FilestoreUtil filestoreUtil, CalculationUtil calculationUtil, ObjectMapper objectMapper) { this.planConfigurationUtil = planConfigurationUtil; this.filestoreUtil = filestoreUtil; this.calculationUtil = calculationUtil; + this.objectMapper = objectMapper; } public List fetchAttributeNamesFromJson(JsonNode jsonNode) @@ -392,4 +396,38 @@ public void printRow(Sheet sheet, Row row) { System.out.println(); // Move to the next line after printing the row } + /** + * Extracts provided field from the additional details object + * + * @param additionalDetails the additionalDetails object from PlanConfigurationRequest + * @param fieldToExtract the name of the field to be extracted from the additional details + * @return the value of the specified field as a string + * @throws CustomException if the field does not exist + */ + public Object extractFieldsFromJsonObject(Object additionalDetails, String fieldToExtract) { + try { + String jsonString = objectMapper.writeValueAsString(additionalDetails); + JsonNode rootNode = objectMapper.readTree(jsonString); + + JsonNode node = rootNode.get(fieldToExtract); + if (node != null && !node.isNull()) { + + // Check for different types of JSON nodes + if (node.isDouble() || node.isFloat()) { + return BigDecimal.valueOf(node.asDouble()); // Convert Double to BigDecimal + } else if (node.isLong() || node.isInt()) { + return BigDecimal.valueOf(node.asLong()); // Convert Long to BigDecimal + } else if (node.isBoolean()) { + return node.asBoolean(); + } else if (node.isTextual()) { + return node.asText(); + } + } + return null; + } catch (Exception e) { + log.error(e.getMessage() + fieldToExtract); + throw new CustomException(PROVIDED_KEY_IS_NOT_PRESENT_IN_JSON_OBJECT_CODE, PROVIDED_KEY_IS_NOT_PRESENT_IN_JSON_OBJECT_MESSAGE + fieldToExtract); + } + } + }