Resolved conflicts

This commit is contained in:
rajesh
2025-03-24 19:21:50 +05:30
6 changed files with 181 additions and 72 deletions

View File

@@ -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;
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<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());
}
}
}

View File

@@ -23,6 +23,7 @@ import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Component
@@ -45,7 +46,97 @@ public class VatCheckDao {
@Autowired
private HttpServletRequest request;
// public VatCheckResponseBean checkVatNumberApi(String vatNumber) {
// VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
// vatCheckResponseBean.setValid(false);
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
// if (Boolean.TRUE.equals(Boolean.parseBoolean(isVatCheckGloballyDisabled))) {
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
// return vatCheckResponseBean;
// }
// Map<String, Object> responseBody = new HashMap<>();
// try {
// HttpHeaders headers = new HttpHeaders();
// headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
// headers.setContentType(MediaType.APPLICATION_JSON);
// headers.set(GepafinConstant.AUTHORIZATION, "Bearer " + vatCheckNewToken);
// headers.add(org.apache.http.HttpHeaders.USER_AGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0");
//
// URI baseUrl = URI.create(GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL);
// ResponseEntity<Map<String, Object>> response = vatCheckService.checkVatNumber(baseUrl, vatNumber, headers);
//
//
// if (response.getStatusCode() == HttpStatus.OK && response.hasBody()) {
// log.info("Successfully checked vat number");
// Map<String, Object> responseMap = response.getBody();
// processValidResponse(responseMap, vatCheckResponseBean);
// }
// } catch (FeignException ex) {
// if (ex.status() == 406) {
// try {
// Map<String, Object> errorResponse = Utils.parseErrorResponse(ex.contentUTF8());
// processValidResponse(errorResponse, vatCheckResponseBean);
// } catch (Exception parseEx) {
// log.error("Failed to parse 406 error response: {0}", parseEx);
// }
// } else {
// log.error("Exception occurred while checking vat number: {0}", ex);
// Utils.callException(ex.status(), ex);
// }
// }
// return vatCheckResponseBean;
// }
// public static void processValidResponse(Map<String, Object> responseMap, VatCheckResponseBean vatCheckResponseBean) {
// if (responseMap != null && responseMap.containsKey("data")) {
// Map<String, Object> responseBody = (Map<String, Object>) responseMap.get("data");
//
// if (responseBody != null) {
// responseBody.remove("timestamp_creation");
// responseBody.remove("timestamp_last_update");
// responseBody.remove("data_iscrizione");
// responseBody.remove("id");
//
// Map<String, Object> data = new LinkedHashMap<>();
// data.put("data", responseBody);
//
// vatCheckResponseBean.setValid(true);
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.VALID_VATNUMBER_MSG));
// vatCheckResponseBean.setVatCheckResponse(data);
// } else {
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
// }
// } else {
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
// }
// }
//
// public VatCheckResponseBean checkVatNumber(String vatNumber) {
// try {
// return checkVatNumberApi(vatNumber);
// } catch (Exception e) {
// log.error("Error in checkVatNumber: {}", e.getMessage());
// VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
// vatCheckResponseBean.setValid(false);
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
// return vatCheckResponseBean;
// }
// }
public VatCheckResponseBean checkVatNumber(String vatNumber) {
try {
return checkVatNumberApi(vatNumber);
} catch (Exception e) {
log.error("Error in checkVatNumber: {}", e.getMessage());
VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
vatCheckResponseBean.setValid(false);
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
return vatCheckResponseBean;
}
}
public VatCheckResponseBean checkVatNumberApi(String vatNumber) {
VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
vatCheckResponseBean.setValid(false);
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
@@ -53,46 +144,48 @@ public class VatCheckDao {
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
return vatCheckResponseBean;
}
Map<String, Object> responseBody = new HashMap<>();
try {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set(GepafinConstant.AUTHORIZATION, "Bearer " + vatCheckNewToken);
headers.add(org.apache.http.HttpHeaders.USER_AGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0");
URI baseUrl = URI.create(GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL);
URI baseUrl = URI.create(GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL_IT_ADVANCE);
ResponseEntity<Map<String, Object>> response = vatCheckService.checkVatNumber(baseUrl, vatNumber, headers);
if (response.getStatusCode() == HttpStatus.OK && response.hasBody()) {
log.info("Successfully checked vat number");
Map<String, Object> responseMap = response.getBody();
processValidResponse(responseMap, vatCheckResponseBean);
}
} catch (FeignException ex) {
if (ex.status() == 406) {
try {
Map<String, Object> errorResponse = Utils.parseErrorResponse(ex.contentUTF8());
processValidResponse(errorResponse, vatCheckResponseBean);
} catch (Exception parseEx) {
log.error("Failed to parse 406 error response: {0}", parseEx);
}
} else {
log.error("Exception occurred while checking vat number: {0}", ex);
Utils.callException(ex.status(), ex);
if (ex.status() == 406) {
try {
Map<String, Object> errorResponse = Utils.parseErrorResponse(ex.contentUTF8());
processValidResponse(errorResponse, vatCheckResponseBean);
} catch (Exception parseEx) {
log.error("Failed to parse 406 error response: {0}", parseEx);
}
} else {
log.error("Exception occurred while checking vat number: {0}", ex);
Utils.callException(ex.status(), ex);
}
}
return vatCheckResponseBean;
}
public static void processValidResponse(Map<String, Object> responseMap, VatCheckResponseBean vatCheckResponseBean) {
if (responseMap != null && responseMap.containsKey("data")) {
Map<String, Object> responseBody = (Map<String, Object>) responseMap.get("data");
if (responseBody != null) {
responseBody.remove("timestamp_creation");
responseBody.remove("timestamp_last_update");
responseBody.remove("data_iscrizione");
if (responseMap != null && responseMap.containsKey("data")) {
Object dataObject = responseMap.get("data");
if (dataObject instanceof List && !((List<?>) dataObject).isEmpty()) {
dataObject = ((List<?>) dataObject).get(0);
}
if (dataObject instanceof Map) {
Map<String, Object> responseBody = (Map<String, Object>) dataObject;
responseBody.remove("creationTimestamp");
responseBody.remove("lastUpdateTimestamp");
responseBody.remove("id");
Map<String, Object> data = new LinkedHashMap<>();
@@ -108,17 +201,4 @@ public class VatCheckDao {
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
}
}
public VatCheckResponseBean checkVatNumber(String vatNumber) {
try {
return checkVatNumberApi(vatNumber);
} catch (Exception e) {
log.error("Error in checkVatNumber: {}", e.getMessage());
VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
vatCheckResponseBean.setValid(false);
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
return vatCheckResponseBean;
}
}
}