116 lines
4.9 KiB
Java
116 lines
4.9 KiB
Java
package net.gepafin.tendermanagement.scheduler;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import net.gepafin.tendermanagement.dao.NotificationDao;
|
|
import net.gepafin.tendermanagement.entities.ApplicationEntity;
|
|
import net.gepafin.tendermanagement.entities.ApplicationEvaluationEntity;
|
|
import net.gepafin.tendermanagement.enums.ApplicationEvaluationStatusTypeEnum;
|
|
import net.gepafin.tendermanagement.enums.NotificationTypeEnum;
|
|
import net.gepafin.tendermanagement.enums.UserActionContextEnum;
|
|
import net.gepafin.tendermanagement.enums.UserActionLogsEnum;
|
|
import net.gepafin.tendermanagement.enums.VersionActionTypeEnum;
|
|
import net.gepafin.tendermanagement.model.request.UserActionRequest;
|
|
import net.gepafin.tendermanagement.model.request.VersionHistoryRequest;
|
|
import net.gepafin.tendermanagement.repositories.ApplicationEvaluationRepository;
|
|
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
|
import net.gepafin.tendermanagement.util.LoggingUtil;
|
|
import net.gepafin.tendermanagement.util.Utils;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalTime;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@Component
|
|
public class ApplicationEvaluationScheduler {
|
|
|
|
@Autowired
|
|
private ApplicationEvaluationRepository applicationEvaluationRepository;
|
|
|
|
@Autowired
|
|
private LoggingUtil loggingUtil;
|
|
|
|
@Autowired
|
|
private HttpServletRequest httpServletRequest;
|
|
|
|
@Autowired
|
|
private NotificationDao notificationDao;
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ApplicationEvaluationScheduler.class);
|
|
|
|
@Scheduled(cron = "0 0 2 * * ?") // Runs daily at midnight
|
|
public void updateExpiredEvaluations() {
|
|
log.info("Starting the Application Evaluation Expiration scheduler...");
|
|
try {
|
|
|
|
Utils.setHttpServletRequestForScheduler();
|
|
|
|
// Logging user action for the scheduler operation
|
|
loggingUtil.logUserActionWithoutToken(
|
|
UserActionRequest.builder().request(httpServletRequest).actionType(UserActionLogsEnum.SCHEDULER)
|
|
.actionContext(UserActionContextEnum.EVALUATION_EXPIRATION_SCHEDULER).build());
|
|
|
|
|
|
processExpiredEvaluation();
|
|
log.info("Completed the process of updating expiring application evaluations.");
|
|
} catch (Exception e) {
|
|
log.error("An error occurred during the Application Evaluation Expiration scheduler: {}", e.getMessage(),
|
|
e);
|
|
} finally {
|
|
Utils.clearHttpServletRequest();
|
|
}
|
|
}
|
|
|
|
private void processExpiredEvaluation() {
|
|
log.info("Starting the process of expiring application evaluations.");
|
|
|
|
|
|
try {
|
|
List<ApplicationEvaluationEntity> evaluations = applicationEvaluationRepository
|
|
.findAllByIsDeletedFalseAndEndDateBefore(
|
|
DateTimeUtil.DateServerToUTC(LocalDateTime.now()).with(LocalTime.MIN).plusSeconds(5));
|
|
log.info("Found {} evaluations to process for expiration.", evaluations.size());
|
|
|
|
for (ApplicationEvaluationEntity evaluation : evaluations) {
|
|
try {
|
|
log.debug("Processing evaluation with ID: {}", evaluation.getId());
|
|
|
|
ApplicationEvaluationEntity oldApplicationEvaluationEntity = Utils
|
|
.getClonedEntityForData(evaluation);
|
|
ApplicationEntity application=evaluation.getAssignedApplicationsEntity().getApplication();
|
|
evaluation.setStatus(ApplicationEvaluationStatusTypeEnum.EXPIRED.getValue());
|
|
evaluation = applicationEvaluationRepository.save(evaluation);
|
|
|
|
// Map<String, String> placeHolders = notificationDao.sendNotificationToBeneficiary(application, NotificationTypeEnum.EVALUATION_EXPIRED);
|
|
Map<String, String> placeHolders = new HashMap<>();
|
|
placeHolders.put("{{call_name}}", application.getCall().getName());
|
|
placeHolders.put("{{protocol_number}}", String.valueOf(application.getProtocol().getProtocolNumber()));
|
|
notificationDao.sendNotificationToSuperUser(application,placeHolders,NotificationTypeEnum.EVALUATION_EXPIRED);
|
|
notificationDao.sendNotificationToInstructor(placeHolders,evaluation,NotificationTypeEnum.EVALUATION_EXPIRED);
|
|
notificationDao.sendNotificationToInstructorManager(placeHolders,evaluation,NotificationTypeEnum.EVALUATION_EXPIRED);
|
|
|
|
// Logging version history for the update operation
|
|
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(httpServletRequest)
|
|
.actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationEvaluationEntity)
|
|
.newData(evaluation).build());
|
|
|
|
log.info("Updated evaluation status to EXPIRED for ID: {}", evaluation.getId());
|
|
} catch (Exception e) {
|
|
log.error("Error processing evaluation with ID: {}. Error: {}", evaluation.getId(), e.getMessage(),
|
|
e);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
log.error("An error occurred while fetching evaluations for expiration. Error: {}", e.getMessage(), e);
|
|
}
|
|
|
|
}
|
|
}
|