Merge pull request #309 from Kitzanos/fix-logging-util-prod

Cherry-pick (Logging for status EVALUATION to NDG)
This commit is contained in:
Rinaldo
2025-06-09 09:32:15 +02:00
committed by GitHub
2 changed files with 1337 additions and 1312 deletions

View File

@@ -52,6 +52,8 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
@@ -160,6 +162,8 @@ public class AppointmentDao {
public NdgResponse checkNdgForAppointment(Long applicationId) { public NdgResponse checkNdgForAppointment(Long applicationId) {
log.info("Starting NDG check for appointment. applicationId: {}", applicationId); log.info("Starting NDG check for appointment. applicationId: {}", applicationId);
ApplicationEntity application = applicationService.validateApplication(applicationId); ApplicationEntity application = applicationService.validateApplication(applicationId);
ApplicationEntity oldApplication = Utils.getClonedEntityForData(application);
NdgResponse ndgResponse = new NdgResponse(); NdgResponse ndgResponse = new NdgResponse();
if (application.getNdgStatus() != null && application.getNdgStatus().equalsIgnoreCase(GepafinConstant.NDG_IN_PROGRESS)) { if (application.getNdgStatus() != null && application.getNdgStatus().equalsIgnoreCase(GepafinConstant.NDG_IN_PROGRESS)) {
log.warn("NDG generation already in progress. applicationId: {}", applicationId); log.warn("NDG generation already in progress. applicationId: {}", applicationId);
@@ -176,6 +180,10 @@ public class AppointmentDao {
application.setNdgStatus(GepafinConstant.NDG_IN_PROGRESS); application.setNdgStatus(GepafinConstant.NDG_IN_PROGRESS);
applicationRepository.save(application); applicationRepository.save(application);
loggingUtil.addVersionHistory(
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplication).newData(application).build());
// Start async processing // Start async processing
HubEntity hub = hubRepository.findByHubId(application.getHubId()); HubEntity hub = hubRepository.findByHubId(application.getHubId());
loginToOdessa(hub, application); loginToOdessa(hub, application);
@@ -409,6 +417,7 @@ public class AppointmentDao {
application.setNdgStatus(GepafinConstant.NDG_FAILED); application.setNdgStatus(GepafinConstant.NDG_FAILED);
applicationRepository.save(application); applicationRepository.save(application);
} }
log.warn("Detected PASSWORD_EXPIRED error during Odessa login. ApplicationId: {}", application.getId());
throw new CustomValidationException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PASSWORD_EXPIRED_LOGIN_TO_ODESSA)); throw new CustomValidationException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PASSWORD_EXPIRED_LOGIN_TO_ODESSA));
} }
@@ -427,19 +436,17 @@ public class AppointmentDao {
} }
} }
} catch (IOException e) { } catch (IOException e) {
log.error("Error parsing JSON response: {}", e.getMessage()); log.error("Unexpected exception during Odessa login.Error: {}",e.getMessage(), e);
} }
catch (Exception e) {
throw new RuntimeException("Authentication failed on Odessa. try again", e);
}
} }
private void startAsyncNdgProcessing(Long applicationId) { private void startAsyncNdgProcessing(Long applicationId) {
// If already polling for this applicationId, do nothing: // If already polling for this applicationId, do nothing:
if (executorMap.containsKey(applicationId)) { if (executorMap.containsKey(applicationId)) {
log.warn("Async processing already running for applicationId: {}", applicationId); log.warn("Async processing already running for applicationId: {}", applicationId);
return; return;
} }
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> { ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
Thread t = new Thread(runnable); Thread t = new Thread(runnable);
@@ -457,6 +464,8 @@ public class AppointmentDao {
AtomicReference<ScheduledFuture<?>> futureRef = new AtomicReference<>(); AtomicReference<ScheduledFuture<?>> futureRef = new AtomicReference<>();
Runnable pollingTask = () -> { Runnable pollingTask = () -> {
RequestContextHolder.setRequestAttributes(requestAttributes, true);
Utils.setHttpServletRequestForThread(request,HttpMethodEnum.POST.getValue(),GepafinConstant.CREATE_NDG, (Long) request.getAttribute(GepafinConstant.USER_ACTION_ID));
try { try {
// 1) If 2 hours have already passed, mark as FAILED and shut down: // 1) If 2 hours have already passed, mark as FAILED and shut down:
if (System.currentTimeMillis() - startTime > twoHoursMs) { if (System.currentTimeMillis() - startTime > twoHoursMs) {
@@ -558,7 +567,8 @@ public class AppointmentDao {
private void handleNdgPolling(ApplicationEntity application, CompanyEntity company, HubEntity hub, String authorizationToken) { private void handleNdgPolling(ApplicationEntity application, CompanyEntity company, HubEntity hub, String authorizationToken) {
log.info("Starting singleshot NDG polling attempt for applicationId: {}, CompanyId: {}, HubId: {}", application.getId(), company.getId(), hub.getId()); log.info("Starting singleshot NDG polling attempt for applicationId: {}, CompanyId: {}, HubId: {}", application.getId(), company.getId(), hub.getId());
ApplicationEntity oldApplication = Utils.getClonedEntityForData(application);
CompanyEntity oldCompanyEntity=Utils.getClonedEntityForData(company);
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
long twoHoursMs = TimeUnit.HOURS.toMillis(2); long twoHoursMs = TimeUnit.HOURS.toMillis(2);
@@ -585,6 +595,10 @@ public class AppointmentDao {
companyRepository.save(company); companyRepository.save(company);
applicationRepository.save(application); applicationRepository.save(application);
loggingUtil.addVersionHistory(
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplication).newData(application).build());
loggingUtil.addVersionHistory(
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldCompanyEntity).newData(company).build());
ApplicationEvaluationEntity eval = applicationEvaluationService.validateApplicationEvaluation(application.getApplicationEvaluationId()); ApplicationEvaluationEntity eval = applicationEvaluationService.validateApplicationEvaluation(application.getApplicationEvaluationId());
Map<String, String> placeholders = new HashMap<>(); Map<String, String> placeholders = new HashMap<>();
@@ -613,6 +627,10 @@ public class AppointmentDao {
companyRepository.save(company); companyRepository.save(company);
applicationRepository.save(application); applicationRepository.save(application);
loggingUtil.addVersionHistory(
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplication).newData(application).build());
loggingUtil.addVersionHistory(
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldCompanyEntity).newData(company).build());
ApplicationEvaluationEntity eval = applicationEvaluationService.validateApplicationEvaluation(application.getApplicationEvaluationId()); ApplicationEvaluationEntity eval = applicationEvaluationService.validateApplicationEvaluation(application.getApplicationEvaluationId());
Map<String, String> placeholders = new HashMap<>(); Map<String, String> placeholders = new HashMap<>();
@@ -663,6 +681,7 @@ public class AppointmentDao {
private void saveNdgAndIdVisura(ApplicationEntity application, CompanyEntity company, String ndg) { private void saveNdgAndIdVisura(ApplicationEntity application, CompanyEntity company, String ndg) {
ApplicationEntity oldApplication = Utils.getClonedEntityForData(application); ApplicationEntity oldApplication = Utils.getClonedEntityForData(application);
CompanyEntity oldCompanyEntity=Utils.getClonedEntityForData(company);
application.setNdg(ndg); application.setNdg(ndg);
application.setNdgStatus(GepafinConstant.NDG_GENERATED); application.setNdgStatus(GepafinConstant.NDG_GENERATED);
application.setStatus(ApplicationStatusTypeEnum.NDG.getValue()); application.setStatus(ApplicationStatusTypeEnum.NDG.getValue());
@@ -671,6 +690,9 @@ public class AppointmentDao {
applicationRepository.save(application); applicationRepository.save(application);
loggingUtil.addVersionHistory( loggingUtil.addVersionHistory(
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplication).newData(application).build()); VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplication).newData(application).build());
loggingUtil.addVersionHistory(
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldCompanyEntity).newData(company).build());
ApplicationEvaluationEntity applicationEvaluationEntity = applicationEvaluationService.validateApplicationEvaluation(application.getApplicationEvaluationId()); ApplicationEvaluationEntity applicationEvaluationEntity = applicationEvaluationService.validateApplicationEvaluation(application.getApplicationEvaluationId());
// Map<String, String> placeHolders = notificationDao.sendNotificationToBeneficiary(application, NotificationTypeEnum.NDG_GENERATION); // Map<String, String> placeHolders = notificationDao.sendNotificationToBeneficiary(application, NotificationTypeEnum.NDG_GENERATION);
Map<String, String> placeHolders = new HashMap<>(); Map<String, String> placeHolders = new HashMap<>();
@@ -971,7 +993,7 @@ public class AppointmentDao {
return appointmentCreationResponse; return appointmentCreationResponse;
} catch (FeignException.Forbidden forbiddenException) { } catch (FeignException.Forbidden forbiddenException) {
log.error("403 Forbidden received while retrieving template. Attempting to regenerate token and retry. Application ID: {}", applicationId); log.error("403 Forbidden received while retrieving template. Attempting to regenerate token and retry. Application ID: {}", applicationId);
regenerateTokenAndSave(hub, application); regenerateTokenAndSave(hub, application);
return createAppointment(applicationId, createAppointmentRequest); return createAppointment(applicationId, createAppointmentRequest);
} }
@@ -1185,6 +1207,7 @@ public class AppointmentDao {
} }
private void uploadDocumentToExternalSystemSync(Long documentId, UploadDocToExternalSystemRequest docToExternalSystemRequest, ApplicationEntity application) { private void uploadDocumentToExternalSystemSync(Long documentId, UploadDocToExternalSystemRequest docToExternalSystemRequest, ApplicationEntity application) {
log.info("Starting sync document upload for documentId: {}", documentId);
// Synchronous upload logic // Synchronous upload logic
DocumentEntity systemDoc = documentDao.validateDocument(documentId); DocumentEntity systemDoc = documentDao.validateDocument(documentId);
@@ -1225,7 +1248,7 @@ public class AppointmentDao {
log.info("Document uploaded successfully to external system: {}", parsedResponse); log.info("Document uploaded successfully to external system: {}", parsedResponse);
} catch (FeignException.Forbidden forbiddenException) { } catch (FeignException.Forbidden forbiddenException) {
log.error("403 Forbidden from external system during upload for documentId: {}. Retrying with new token...", documentId); log.error("403 Forbidden from external system during upload for documentId: {}. Retrying with new token...", documentId);
regenerateTokenAndSave(hub, application); regenerateTokenAndSave(hub, application);
uploadDocumentToExternalSystemSync(documentId, docToExternalSystemRequest, application); uploadDocumentToExternalSystemSync(documentId, docToExternalSystemRequest, application);
} catch (Exception e) { } catch (Exception e) {

View File

@@ -1043,15 +1043,17 @@ public class Utils {
return new ArrayList<>(responseMap.values()); return new ArrayList<>(responseMap.values());
} }
public static void setHttpServletRequestForThread(String redirectURI, String methodType,HttpServletRequest request,String remoteUser) {
public static void setHttpServletRequestForThread(HttpServletRequest request,String methodType, String remoteUser, Long userActionId) {
MockHttpServletRequest mockRequest = new MockHttpServletRequest(); MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.setRequestURI(redirectURI); mockRequest.setRequestURI(request.getRequestURI());
mockRequest.setMethod(methodType); mockRequest.setMethod(methodType);
mockRequest.setRemoteUser(remoteUser); mockRequest.setRemoteUser(remoteUser); // or pass it in if needed
Long userActionId = (Long) request.getAttribute(GepafinConstant.USER_ACTION_ID); mockRequest.setAttribute(GepafinConstant.USER_ACTION_ID, userActionId);
mockRequest.setAttribute(GepafinConstant.USER_ACTION_ID,userActionId );
ServletRequestAttributes attributes = new ServletRequestAttributes(mockRequest); ServletRequestAttributes attributes = new ServletRequestAttributes(mockRequest);
RequestContextHolder.setRequestAttributes(attributes, true); RequestContextHolder.setRequestAttributes(attributes, true);
} }
} }