Updated code
This commit is contained in:
@@ -148,22 +148,7 @@ public class CompanyDao {
|
||||
companyEntity.setJson(Utils.convertMapIntoJsonString(companyRequest.getVatCheckResponse()));
|
||||
Map<String, Object> vatCheckResponse = companyRequest.getVatCheckResponse();
|
||||
Map<String, Object> data = (Map<String, Object>) vatCheckResponse.get("data");
|
||||
if (data != null) {
|
||||
if (data.containsKey("dettaglio")) {
|
||||
Map<String, Object> dettaglio = (Map<String, Object>) 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<String, Object> vatCheckResponse = request.getVatCheckResponse();
|
||||
Map<String, Object> data = (Map<String, Object>) vatCheckResponse.get("data");
|
||||
if (data != null) {
|
||||
if (data.containsKey("dettaglio")) {
|
||||
Map<String, Object> dettaglio = (Map<String, Object>) data.get("dettaglio");
|
||||
if (dettaglio != null) {
|
||||
if (dettaglio.containsKey("codice_ateco")) {
|
||||
Object codiceAtecoObj = dettaglio.get("codice_ateco");
|
||||
String codiceAteco = (codiceAtecoObj != null) ? codiceAtecoObj.toString() : null;
|
||||
updateCodiceAtecoFieldWithNewJson(entity);
|
||||
|
||||
if (codiceAteco != null) {
|
||||
entity.setCodiceAteco(codiceAteco);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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<String, Object> companyDataMap = Utils.convertJsonStringToMap(company.getJson());
|
||||
if (companyDataMap == null) {
|
||||
log.warn("Company ID {}: Failed to parse JSON data.", company.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract 'data' section
|
||||
Map<String, Object> 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<String, Object> 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<String, Object> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -796,4 +796,22 @@ public class Utils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Extracts a Map from a parent Map safely.
|
||||
*/
|
||||
public static Map<String, Object> extractMap(Map<String, Object> parentMap, String key) {
|
||||
Object obj = parentMap.get(key);
|
||||
if (obj instanceof Map<?, ?> map) {
|
||||
return (Map<String, Object>) map;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts a String from a Map safely.
|
||||
*/
|
||||
public static String extractString(Map<String, Object> parentMap, String key) {
|
||||
Object obj = parentMap.get(key);
|
||||
return (obj instanceof String str) ? str : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user