Done ticket GEPAFINBE-223 & GEPAFINBE-224
This commit is contained in:
@@ -557,25 +557,69 @@ public class ApplicationDao {
|
||||
VersionActionTypeEnum actionType = VersionActionTypeEnum.INSERT;
|
||||
List<ContentResponseBean> contentResponseBeans = formDao.convertFormEntityToFormResponseBean(formEntity).getContent();
|
||||
|
||||
contentResponseBeans.stream()
|
||||
.filter(content -> "numberinput".equals(content.getName()) && content.getId().toString().equals(applicationFormFieldRequestBean.getFieldId()))
|
||||
.map(ContentResponseBean::getSettings)
|
||||
.flatMap(List::stream)
|
||||
.filter(setting -> "isRequestedAmount".equals(setting.getName()) && Boolean.TRUE.equals(setting.getValue()))
|
||||
.findFirst()
|
||||
.ifPresent(setting -> {
|
||||
Object fieldValue = applicationFormFieldRequestBean.getFieldValue();
|
||||
if(fieldValue!=null) {
|
||||
try {
|
||||
BigDecimal amountRequested = new BigDecimal(fieldValue.toString());
|
||||
applicationFormEntity.getApplication().setAmountRequested(amountRequested);
|
||||
log.info("Set amountRequested to {} for Application ID: {}", amountRequested, applicationFormEntity.getApplication().getId());
|
||||
} catch (NumberFormatException e) {
|
||||
log.error("Invalid number format for requested amount: {}", fieldValue, e);
|
||||
throw new IllegalArgumentException("Field value is not a valid number: " + fieldValue, e);
|
||||
}
|
||||
}
|
||||
});
|
||||
// contentResponseBeans.stream()
|
||||
// .filter(content -> "numberinput".equals(content.getName()) && content.getId().toString().equals(applicationFormFieldRequestBean.getFieldId()))
|
||||
// .map(ContentResponseBean::getSettings)
|
||||
// .flatMap(List::stream)
|
||||
// .filter(setting -> "isRequestedAmount".equals(setting.getName()) && Boolean.TRUE.equals(setting.getValue()))
|
||||
// .findFirst()
|
||||
// .ifPresent(setting -> {
|
||||
// Object fieldValue = applicationFormFieldRequestBean.getFieldValue();
|
||||
// if(fieldValue!=null) {
|
||||
// try {
|
||||
// BigDecimal amountRequested = new BigDecimal(fieldValue.toString());
|
||||
// applicationFormEntity.getApplication().setAmountRequested(amountRequested);
|
||||
// log.info("Set amountRequested to {} for Application ID: {}", amountRequested, applicationFormEntity.getApplication().getId());
|
||||
// } catch (NumberFormatException e) {
|
||||
// log.error("Invalid number format for requested amount: {}", fieldValue, e);
|
||||
// throw new IllegalArgumentException("Field value is not a valid number: " + fieldValue, e);
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
|
||||
contentResponseBeans.stream()
|
||||
.filter(content -> content.getId().toString().equals(applicationFormFieldRequestBean.getFieldId()))
|
||||
.findFirst()
|
||||
.ifPresent(content -> {
|
||||
Object fieldValue = applicationFormFieldRequestBean.getFieldValue();
|
||||
if (fieldValue == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert settings list to a map
|
||||
Map<String, Object> settingMap = content.getSettings().stream()
|
||||
.collect(Collectors.toMap(SettingResponseBean::getName, SettingResponseBean::getValue, (v1, v2) -> v1));
|
||||
|
||||
String fieldType = content.getName();
|
||||
|
||||
// Define handlers for different (fieldType + settingName) combinations
|
||||
Map<String, Runnable> handlers = new HashMap<>();
|
||||
|
||||
// Add handler for isRequestedAmount
|
||||
if ("numberinput".equals(fieldType) && Boolean.TRUE.equals(settingMap.get("isRequestedAmount"))) {
|
||||
handlers.put("isRequestedAmount", () -> {
|
||||
try {
|
||||
BigDecimal amountRequested = new BigDecimal(fieldValue.toString());
|
||||
applicationFormEntity.getApplication().setAmountRequested(amountRequested);
|
||||
log.info("Set amountRequested to {} for Application ID: {}", amountRequested, applicationFormEntity.getApplication().getId());
|
||||
} catch (NumberFormatException e) {
|
||||
log.error("Invalid number format for requested amount: {}", fieldValue, e);
|
||||
throw new IllegalArgumentException("Field value is not a valid number: " + fieldValue, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Add handler for isPecEmail
|
||||
if ("textinput".equals(fieldType) && Boolean.TRUE.equals(settingMap.get("isPecEmail"))) {
|
||||
handlers.put("isPecEmail", () -> {
|
||||
applicationFormEntity.getApplication().setPecEmail(fieldValue.toString());
|
||||
log.info("Set PEC to {} for Application ID: {}", fieldValue, applicationFormEntity.getApplication().getId());
|
||||
});
|
||||
}
|
||||
|
||||
// Run all applicable handlers
|
||||
handlers.values().forEach(Runnable::run);
|
||||
});
|
||||
|
||||
|
||||
ApplicationFormFieldEntity oldApplicationFormFieldData = null;
|
||||
|
||||
@@ -138,6 +138,9 @@ public class AppointmentDao {
|
||||
@Autowired
|
||||
private ApplicationEvaluationDao applicationEvaluationDao;
|
||||
|
||||
@Autowired
|
||||
private NdganagRepository ndganagRepository;
|
||||
|
||||
private final Map<Long, ScheduledExecutorService> executorMap = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
@@ -535,7 +538,13 @@ public class AppointmentDao {
|
||||
String authorizationToken = getBearerToken(hub);
|
||||
|
||||
// Try retrieving NDG by VAT number
|
||||
AppointmentLoginResponse ndgResponse = retrieveNdgByVatNumber(company.getVatNumber(), authorizationToken, hub, application);
|
||||
NdganagEntity ndganagEntity = ndganagRepository.findByVatNumber(company.getVatNumber());
|
||||
AppointmentLoginResponse ndgResponse=new AppointmentLoginResponse();
|
||||
if (ndganagEntity != null || ndganagEntity.getNdg() != null) {
|
||||
ndgResponse.setNdg(ndganagEntity.getNdg());
|
||||
}else {
|
||||
ndgResponse = retrieveNdgByVatNumber(company.getVatNumber(), authorizationToken, hub, application);
|
||||
}
|
||||
if (isNdgValid(ndgResponse.getNdg())) {
|
||||
saveNdg(application, company, ndgResponse.getNdg());
|
||||
log.info("NDG successfully generated for applicationId: {}", applicationId);
|
||||
@@ -716,10 +725,7 @@ public class AppointmentDao {
|
||||
try {
|
||||
log.info("Initiating NDG retrieval by VAT number | ApplicationId: {}, HubId: {}, VAT: {}", application.getId(), hub.getId(), vatNumber);
|
||||
// Prepare the NDG request
|
||||
AppointmentNdgRequest ndgRequest = getAppointmentNdgRequest(vatNumber);
|
||||
// Call the API to retrieve NDG
|
||||
ResponseEntity<Object> response = appointmentApiService.getNdgByVatNumber(ndgRequest, authorizationToken);
|
||||
String responseJson = Utils.convertObjectToJson(response.getBody());
|
||||
String responseJson = getNdgFromExternalService(vatNumber, authorizationToken);
|
||||
// Parse and return the NDG response
|
||||
return parseNdgResponse(responseJson);
|
||||
} catch (FeignException.Forbidden forbiddenException) {
|
||||
@@ -875,30 +881,38 @@ public class AppointmentDao {
|
||||
}
|
||||
|
||||
public AppointmentLoginResponse parseNdgResponse(String jsonResponse) {
|
||||
AppointmentLoginResponse loginResponse = new AppointmentLoginResponse();
|
||||
String ndg=extractNdg(jsonResponse);
|
||||
if (ndg==null){ return null;}
|
||||
else {
|
||||
loginResponse.setNdg(ndg);
|
||||
}
|
||||
return loginResponse;
|
||||
}
|
||||
|
||||
private String extractNdg(String jsonResponse) {
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode rootNode = objectMapper.readTree(jsonResponse);
|
||||
JsonNode dataArray = rootNode.get(GepafinConstant.DATA_STRING);
|
||||
if (dataArray == null || !dataArray.isArray() || dataArray.isEmpty()) {
|
||||
log.info("NDG data is empty or missing in the response.");
|
||||
AppointmentLoginResponse emptyResponse = new AppointmentLoginResponse();
|
||||
emptyResponse.setNdg(null);
|
||||
return emptyResponse;
|
||||
return null;
|
||||
}
|
||||
JsonNode firstDataEntry = dataArray.get(0);
|
||||
AppointmentLoginResponse response = new AppointmentLoginResponse();
|
||||
if (firstDataEntry.has(GepafinConstant.NDG_STRING)) {
|
||||
response.setNdg(normalizeNullValue(firstDataEntry.get(GepafinConstant.NDG_STRING).asText()));
|
||||
String ndg=normalizeNullValue(firstDataEntry.get(GepafinConstant.NDG_STRING).asText());
|
||||
return ndg;
|
||||
}
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to parse response: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("Failed to parse NDG response.", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String normalizeNullValue(String value) {
|
||||
public String normalizeNullValue(String value) {
|
||||
|
||||
return (value == null || GepafinConstant.NULL_STRING.equalsIgnoreCase(value.trim())) ? null : value;
|
||||
}
|
||||
@@ -1415,5 +1429,54 @@ public class AppointmentDao {
|
||||
futureRef.set(future);
|
||||
}
|
||||
|
||||
public NdgResponse getNdgByVatNumber(String vatNumber, UserEntity userEntity) {
|
||||
HubEntity hub=userEntity.getHub();
|
||||
NdganagEntity ndganagEntity = ndganagRepository.findByVatNumber(vatNumber);
|
||||
NdgResponse ndgResponse=new NdgResponse();
|
||||
String jsonResponse=null;
|
||||
String authorizationToken = hub.getAppointmentAuthTokenId();
|
||||
if (ndganagEntity == null || ndganagEntity.getNdg() == null) {
|
||||
|
||||
try {
|
||||
log.info("Initiating NDG retrieval by VAT number | HubId: {}, VAT: {}", hub.getId(), vatNumber);
|
||||
// Prepare the NDG request
|
||||
jsonResponse=getNdgFromExternalService(vatNumber, authorizationToken);
|
||||
checkAndSaveNdg(jsonResponse, ndgResponse);
|
||||
// Parse and return the NDG response
|
||||
} catch (FeignException.Forbidden forbiddenException) {
|
||||
log.error("403 Forbidden during NDG retrieval | HubId: {}", hub.getId());
|
||||
logForbiddenError();
|
||||
// Regenerate the token and retry
|
||||
String newAuthorizationToken = regenerateTokenAndSave(hub, null);
|
||||
jsonResponse= getNdgFromExternalService(vatNumber,newAuthorizationToken);
|
||||
if (checkAndSaveNdg(jsonResponse, ndgResponse)) return null;
|
||||
} catch (Exception e) {
|
||||
log.error("Error during NDG retrieval |, HubId: {}, Message: {}", hub.getId(), e.getMessage(), e);
|
||||
throw new RuntimeException("NDG retrieval failed.", e);
|
||||
}
|
||||
}else {
|
||||
ndgResponse.setNdg(ndganagEntity.getNdg());
|
||||
}
|
||||
return ndgResponse;
|
||||
}
|
||||
|
||||
private boolean checkAndSaveNdg(String jsonResponse, NdgResponse ndgResponse) {
|
||||
String ndg=extractNdg(jsonResponse);
|
||||
if (ndg==null){
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
ndgResponse.setNdg(ndg);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String getNdgFromExternalService(String vatNumber, String authorizationToken) {
|
||||
AppointmentNdgRequest ndgRequest = getAppointmentNdgRequest(vatNumber);
|
||||
// Call the API to retrieve NDG
|
||||
ResponseEntity<Object> response = appointmentApiService.getNdgByVatNumber(ndgRequest, authorizationToken);
|
||||
String responseJson = Utils.convertObjectToJson(response.getBody());
|
||||
return responseJson;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import org.springframework.data.domain.Pageable; // Correct package
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -413,7 +414,7 @@ public class CompanyDao {
|
||||
Translator.toLocale(GepafinConstant.INVALID_LIMIT));
|
||||
}
|
||||
|
||||
int successfulUpdates = 0;
|
||||
int successfulUpdates = 0;
|
||||
int failedUpdates = 0;
|
||||
int invalidVatNumbers = 0;
|
||||
|
||||
@@ -531,7 +532,11 @@ public class CompanyDao {
|
||||
Map<String, Object> dataMap = Utils.extractMap(companyDataMap, "data");
|
||||
Object dataObj = companyDataMap.get("data");
|
||||
|
||||
if (dataMap == null) {
|
||||
if (dataObj instanceof Map<?, ?> singleMap) {
|
||||
dataMap = (Map<String, Object>) singleMap;
|
||||
} else if (dataObj instanceof List<?> list && !list.isEmpty() && list.get(0) instanceof Map<?, ?> firstMap) {
|
||||
dataMap = (Map<String, Object>) firstMap;
|
||||
} else {
|
||||
log.warn("Company ID {}: 'data' section is missing or invalid in the JSON.", company.getId());
|
||||
return;
|
||||
}
|
||||
@@ -540,11 +545,11 @@ public class CompanyDao {
|
||||
updateCodiceAtecoField(company);
|
||||
} else {
|
||||
|
||||
Object pecEmail = Utils.extractMap(dataMap, "pec");
|
||||
Object pecEmail = dataMap.get("pec");
|
||||
if (pecEmail == null) {
|
||||
log.warn("Company ID {}: 'pec' section is missing or invalid.", company.getId());
|
||||
company.setPec((String) pecEmail);
|
||||
}
|
||||
company.setPec((String) pecEmail);
|
||||
|
||||
// Extract 'atecoClassification' section
|
||||
Map<String, Object> atecoClassificationMap = Utils.extractMap(dataMap, "atecoClassification");
|
||||
@@ -577,49 +582,79 @@ public class CompanyDao {
|
||||
}
|
||||
|
||||
public void getCompanyEntity() {
|
||||
List<CompanyEntity> companyEntities=companyRepository.findAll();
|
||||
List<CompanyEntity> companyEntities=companyRepository.findByJsonIsNotNullAndPecIsNull();
|
||||
List<CompanyEntity> companyEntityList=new ArrayList<>();
|
||||
for (CompanyEntity company:companyEntities){
|
||||
if(company.getJson()!=null){
|
||||
if(company.getJson()!=null && company.getPec()==null){
|
||||
if (company == null || company.getJson() == null || company.getJson().isEmpty()) {
|
||||
log.warn("Company is null or JSON data is empty.");
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> vatCheckResponse = Utils.convertJsonStringToMap(company.getJson());
|
||||
|
||||
if (vatCheckResponse == null) {
|
||||
log.warn("Company ID {}: Invalid JSON response.", company.getId());
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> companyDataMap = Utils.convertJsonStringToMap(company.getJson());
|
||||
if (companyDataMap == null) {
|
||||
log.warn("Company ID {}: Failed to parse JSON data.", company.getId());
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Object dataObj = vatCheckResponse.get("data");
|
||||
if (!(dataObj instanceof Map<?, ?> dataMap)) {
|
||||
log.warn("Company ID {}: 'data' is missing or not a valid object.", company.getId());
|
||||
return;
|
||||
Map<String, Object> dataMap=null;
|
||||
// if (!(dataObj instanceof Map<?, ?> dataMap)) {
|
||||
// log.warn("Company ID {}: 'data' is missing or not a valid object.", company.getId());
|
||||
// continue;
|
||||
// }
|
||||
|
||||
if (dataObj instanceof Map<?, ?> singleMap) {
|
||||
dataMap = (Map<String, Object>) singleMap;
|
||||
} else if (dataObj instanceof List<?> list && !list.isEmpty() && list.get(0) instanceof Map<?, ?> firstMap) {
|
||||
dataMap = (Map<String, Object>) firstMap;
|
||||
} else {
|
||||
log.warn("Company ID {}: 'data' section is missing or invalid in the JSON.", company.getId());
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (dataMap.containsKey("pec")) {
|
||||
Object pecEmailObj = dataMap.get("pec");
|
||||
if (pecEmailObj instanceof String pec && !pec.isEmpty()) {
|
||||
company.setPec(pec); // Only set if valid string
|
||||
companyEntityList.add(company);
|
||||
} else {
|
||||
log.warn("Company ID {}: 'pec' is missing, empty, or not a string.", company.getId());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!dataMap.containsKey("dettaglio")) {
|
||||
log.warn("Company ID {}: 'dettaglio' not present inside 'data'. Skipping codiceAteco update.", company.getId());
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Object dettaglioObj = dataMap.get("dettaglio");
|
||||
if (!(dettaglioObj instanceof Map<?, ?> dettaglio)) {
|
||||
log.warn("Company ID {}: 'dettaglio' is not a valid object.", company.getId());
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
|
||||
Object pecEmailObj = dettaglio.get("pec");
|
||||
if (pecEmailObj instanceof String pec && !pec.isEmpty()) {
|
||||
company.setPec(pec); // Only set if valid string
|
||||
if(pec!=null) {
|
||||
company.setPec(pec); // Only set if valid string
|
||||
companyEntityList.add(company);
|
||||
}
|
||||
} else {
|
||||
log.warn("Company ID {}: 'pec' is missing, empty, or not a string.", company.getId());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
companyRepository.saveAll(companyEntityList);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user