From 54b17b5c591c87c4d309f3f35d5af1b7aaccaa2a Mon Sep 17 00:00:00 2001 From: rajesh Date: Mon, 24 Mar 2025 16:29:30 +0530 Subject: [PATCH] Updated code --- .../tendermanagement/dao/CompanyDao.java | 82 +++++++++++-------- .../gepafin/tendermanagement/util/Utils.java | 18 ++++ 2 files changed, 64 insertions(+), 36 deletions(-) diff --git a/src/main/java/net/gepafin/tendermanagement/dao/CompanyDao.java b/src/main/java/net/gepafin/tendermanagement/dao/CompanyDao.java index ca9c2621..909798e6 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/CompanyDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/CompanyDao.java @@ -148,22 +148,7 @@ public class CompanyDao { companyEntity.setJson(Utils.convertMapIntoJsonString(companyRequest.getVatCheckResponse())); Map vatCheckResponse = companyRequest.getVatCheckResponse(); Map data = (Map) vatCheckResponse.get("data"); - if (data != null) { - if (data.containsKey("dettaglio")) { - Map dettaglio = (Map) data.get("dettaglio"); - if (dettaglio != null) { - if (dettaglio.containsKey("codice_ateco")) { - Object codiceAtecoObj = dettaglio.get("codice_ateco"); - String codiceAteco = (codiceAtecoObj != null) ? codiceAtecoObj.toString() : null; - - if (codiceAteco != null) { - companyEntity.setCodiceAteco(codiceAteco); - } - } - } - } - } - + updateCodiceAtecoFieldWithNewJson(companyEntity); companyEntity = companyRepository.save(companyEntity); /** This code is responsible for adding a version history log for "updating company json field" operation. **/ @@ -193,25 +178,8 @@ public class CompanyDao { entity.setAnnualRevenue(request.getAnnualRevenue()); entity.setHub(userEntity.getHub()); entity.setJson(Utils.convertMapIntoJsonString(request.getVatCheckResponse())); - if (request.getVatCheckResponse() != null) { - Map vatCheckResponse = request.getVatCheckResponse(); - Map data = (Map) vatCheckResponse.get("data"); - if (data != null) { - if (data.containsKey("dettaglio")) { - Map dettaglio = (Map) data.get("dettaglio"); - if (dettaglio != null) { - if (dettaglio.containsKey("codice_ateco")) { - Object codiceAtecoObj = dettaglio.get("codice_ateco"); - String codiceAteco = (codiceAtecoObj != null) ? codiceAtecoObj.toString() : null; - - if (codiceAteco != null) { - entity.setCodiceAteco(codiceAteco); - } - } - } - } - } - } + updateCodiceAtecoFieldWithNewJson(entity); + return entity; } @@ -469,7 +437,8 @@ public class CompanyDao { log.info("Company ID {}: JSON field updated successfully.", company.getId()); // Extract and set codiceAteco field - updateCodiceAtecoField(company); +// updateCodiceAtecoField(company); + updateCodiceAtecoFieldWithNewJson(company); successfulUpdates++; } else { @@ -547,5 +516,46 @@ public class CompanyDao { log.warn("Company ID {}: 'data' section missing in the JSON response.", company.getId()); } } + private void updateCodiceAtecoFieldWithNewJson(CompanyEntity company) { + if (company == null || company.getJson() == null || company.getJson().isEmpty()) { + log.warn("Company is null or JSON data is empty."); + return; + } + Map companyDataMap = Utils.convertJsonStringToMap(company.getJson()); + if (companyDataMap == null) { + log.warn("Company ID {}: Failed to parse JSON data.", company.getId()); + return; + } + + // Extract 'data' section + Map dataMap = Utils.extractMap(companyDataMap, "data"); + if (dataMap == null) { + log.warn("Company ID {}: 'data' section is missing or invalid in the JSON.", company.getId()); + return; + } + + // Extract 'atecoClassification' section + Map atecoClassificationMap = Utils.extractMap(dataMap, "atecoClassification"); + if (atecoClassificationMap == null) { + log.warn("Company ID {}: 'atecoClassification' section is missing or invalid.", company.getId()); + return; + } + + // Extract 'ateco' section + Map atecoMap = Utils.extractMap(atecoClassificationMap, "ateco"); + if (atecoMap == null) { + log.warn("Company ID {}: 'ateco' section is missing or invalid.", company.getId()); + return; + } + + // Extract and set 'code' + String atecoCode = Utils.extractString(atecoMap, "code"); + if (atecoCode != null && !atecoCode.isEmpty()) { + company.setCodiceAteco(atecoCode); + log.info("Company ID {}: codiceAteco updated to {}", company.getId(), atecoCode); + } else { + log.warn("Company ID {}: 'code' inside 'ateco' is empty or missing.", company.getId()); + } + } } diff --git a/src/main/java/net/gepafin/tendermanagement/util/Utils.java b/src/main/java/net/gepafin/tendermanagement/util/Utils.java index 1838bd57..0902a9b6 100644 --- a/src/main/java/net/gepafin/tendermanagement/util/Utils.java +++ b/src/main/java/net/gepafin/tendermanagement/util/Utils.java @@ -796,4 +796,22 @@ public class Utils { return null; } } + /** + * Extracts a Map from a parent Map safely. + */ + public static Map extractMap(Map parentMap, String key) { + Object obj = parentMap.get(key); + if (obj instanceof Map map) { + return (Map) map; + } + return null; + } + + /** + * Extracts a String from a Map safely. + */ + public static String extractString(Map parentMap, String key) { + Object obj = parentMap.get(key); + return (obj instanceof String str) ? str : null; + } } \ No newline at end of file