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:1729 - Change the mdms master to adminSchemas in resource generator #1295

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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 @@ -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 = "boundaryWithTarget";
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,11 @@ private List<String> getBoundaryCodeList(PlanConfigurationRequest planConfigurat
* @param planConfig The configuration details specific to the plan.
* @return A map of attribute names to their corresponding indices or data types.
*/


//TODO: fetch from adminSchema master
private Map<String, Object> 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());
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -31,6 +27,8 @@

import lombok.extern.slf4j.Slf4j;

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

@Slf4j
@Component
public class MdmsUtil {
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<ModuleDetail> 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();
}
Expand All @@ -109,33 +109,53 @@ private ModuleDetail getPlanModuleDetail() {
.moduleName(ServiceConstants.MDMS_PLAN_MODULE_NAME).build();
}

/**
* Retrieves the module details for the HCM-ADMIN-CONSOLE module.
*
* @return ModuleDetail object containing master details for the HCM-ADMIN-CONSOLE module.
*/
private ModuleDetail getAdminConsoleModuleDetail() {
tanishi-egov marked this conversation as resolved.
Show resolved Hide resolved
List<MasterDetail> 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<String, Object> filterMasterData(String masterDataJson, File.InputFileTypeEnum fileType,
String templateIdentifier, String campaignType) {
public Map<String, Object> filterMasterData(String masterDataJson, String campaignType) {
Map<String, Object> properties = new HashMap<>();
Map<String, Object> masterData = JsonUtils.parseJson(masterDataJson, Map.class);
Map<String, Object> planModule = (Map<String, Object>) masterData.get(ServiceConstants.MDMS_PLAN_MODULE_NAME);
List<Map<String, Object>> schemas = (List<Map<String, Object>>) planModule
.get(ServiceConstants.MDMS_MASTER_SCHEMAS);
log.info("masterDataJson ==>" + schemas);
for (Map<String, Object> schema : schemas) {
String type = (String) schema.get(ServiceConstants.MDMS_SCHEMA_TYPE);
Map<String, Object> adminConsoleModule = (Map<String, Object>) masterData.get(ServiceConstants.MDMS_ADMIN_CONSOLE_MODULE_NAME);
List<Map<String, Object>> adminSchema = (List<Map<String, Object>>) adminConsoleModule
.get(ServiceConstants.MDMS_MASTER_ADMIN_SCHEMA);
log.info("masterDataJson ==> " + adminSchema);

for (Map<String, Object> 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<String, Object> schemaProperties = (Map<String, Object>) schema.get("schema");
properties = (Map<String, Object>) schemaProperties.get("Properties");

// Check if the schema's title matches the required template identifier
// and the campaign matches the specified campaign type.
if (schema.get(ServiceConstants.MDMS_SCHEMA_TITLE).equals(ServiceConstants.FILE_TEMPLATE_IDENTIFIER_BOUNDARY)
&& campaign.equals(MICROPLAN_PREFIX + campaignType)) {
Map<String, List<Object>> schemaProperties = (Map<String, List<Object>>) schema.get("properties");

schemaProperties.forEach((propertyType, propertyList) ->
// For each property in the property list, extract its name and add it to the map with the property.
propertyList.forEach(property -> {
String propertyName = (String) parsingUtil.extractFieldsFromJsonObject(property, "name");
properties.put(propertyName, property);
})
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
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.*;
import java.util.stream.Collectors;
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
Expand All @@ -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<String> fetchAttributeNamesFromJson(JsonNode jsonNode)
Expand Down Expand Up @@ -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);
}
}

}
Loading