Merge pull request #249 from Kitzanos/feature/GEPAFINBE-191
GEPAFINBE-191 (Change OpenApi Enterprise To Company)
This commit is contained in:
@@ -137,6 +137,7 @@ public class GepafinConstant {
|
||||
public static final String APPLICATION_IS_INCOMPLETE_MSG = "application.is.incomplete";
|
||||
public static final String AUTHORIZATION = "Authorization";
|
||||
public static final String CHECK_VATNUMBER_V2_NEW_URL = "https://imprese.openapi.it/advance";
|
||||
public static final String CHECK_VATNUMBER_V2_NEW_URL_IT_ADVANCE = "https://company.openapi.com/IT-advanced";
|
||||
public static final String VALIDATION_FIELD_CUSTOM = "validation.field.custom";
|
||||
public static final String VALIDATION_CODICE_FISCALE = "validation.codice.fiscale";
|
||||
public static final String VALIDATION_CAP = "validation.cap";
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
@FeignClient(value = "vat-check-service", url = GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL)
|
||||
@FeignClient(value = "vat-check-service", url = GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL_IT_ADVANCE)
|
||||
public interface VatCheckService {
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ fe.base.url=https://bandi-staging.memento.credit
|
||||
spring.main.allow-circular-references=true
|
||||
|
||||
isVatCheckGloballyDisabled = true
|
||||
vatCheckNewToken: 66026bd891a51044e90e08c4
|
||||
vatCheckNewToken: 671916c76c6e822f660774d4
|
||||
|
||||
#SPID configuration
|
||||
spid.ipd.base.url=https://federatest.umbriadigitale.it
|
||||
|
||||
Reference in New Issue
Block a user