Done ticket GEPAFINBE-89
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package net.gepafin.tendermanagement.scheduler;
|
||||
|
||||
import net.gepafin.tendermanagement.dao.ApplicationAmendmentRequestDao;
|
||||
import net.gepafin.tendermanagement.dao.ApplicationEvaluationDao;
|
||||
import net.gepafin.tendermanagement.dao.NotificationDao;
|
||||
import net.gepafin.tendermanagement.entities.*;
|
||||
import net.gepafin.tendermanagement.enums.ExpirationTypeEnum;
|
||||
import net.gepafin.tendermanagement.enums.NotificationTypeEnum;
|
||||
import net.gepafin.tendermanagement.repositories.ApplicationAmendmentRequestRepository;
|
||||
import net.gepafin.tendermanagement.repositories.ApplicationEvaluationRepository;
|
||||
import net.gepafin.tendermanagement.repositories.ExpirationConfigRepository;
|
||||
import net.gepafin.tendermanagement.service.ApplicationService;
|
||||
import net.gepafin.tendermanagement.service.CompanyService;
|
||||
import net.gepafin.tendermanagement.service.UserService;
|
||||
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.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class ExpirationScheduler {
|
||||
|
||||
@Autowired
|
||||
private ApplicationAmendmentRequestRepository applicationAmendmentRequestRepository;
|
||||
|
||||
@Autowired
|
||||
private ApplicationEvaluationRepository applicationEvaluationRepository;
|
||||
|
||||
@Autowired
|
||||
private ExpirationConfigRepository expirationNotificationConfigRepository;
|
||||
|
||||
@Autowired
|
||||
private ApplicationEvaluationDao applicationEvaluationDao;
|
||||
|
||||
@Autowired
|
||||
private ApplicationAmendmentRequestDao applicationAmendmentRequestDao;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private ApplicationService applicationService;
|
||||
|
||||
@Autowired
|
||||
private NotificationDao notificationDao;
|
||||
|
||||
@Autowired
|
||||
private CompanyService companyService;
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ExpirationScheduler.class);
|
||||
|
||||
@Scheduled(cron = "0 0 3 * * ?")
|
||||
public void processExpiration(){
|
||||
log.info("Starting the Expiration scheduler...");
|
||||
try {
|
||||
Utils.setHttpServletRequestForScheduler();
|
||||
|
||||
log.info("Starting processing expiration notifications for Amendment");
|
||||
processExpiration(ExpirationTypeEnum.AMENDMENT);
|
||||
|
||||
log.info("Starting processing expiration notifications for Evaluation");
|
||||
processExpiration(ExpirationTypeEnum.EVALUATION);
|
||||
|
||||
log.info("Expiration scheduler completed successfully.");
|
||||
|
||||
}
|
||||
catch (Exception e){
|
||||
log.error("An error occurred during the Notification Expiration Scheduler: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void processExpiration(ExpirationTypeEnum type) {
|
||||
List<ExpirationConfigEntity> configEntities = expirationNotificationConfigRepository.findByTypeAndIsDeletedFalse(type.getValue());
|
||||
|
||||
for (ExpirationConfigEntity config : configEntities) {
|
||||
Long daysBefore = config.getIntervalDays();
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
LocalDateTime startDate = now.plusDays(daysBefore).withHour(0).withMinute(0).withSecond(0).withNano(0);
|
||||
LocalDateTime endDate = startDate.plusDays(1).minusNanos(1).withNano(0);
|
||||
|
||||
if (ExpirationTypeEnum.AMENDMENT.equals(type)) {
|
||||
processAmendmentExpiration(startDate, endDate, daysBefore);
|
||||
} else if (ExpirationTypeEnum.EVALUATION.equals(type)) {
|
||||
processEvaluationExpiration(startDate, endDate, daysBefore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processAmendmentExpiration(LocalDateTime startDate, LocalDateTime endDate, Long daysBefore) {
|
||||
List<ApplicationAmendmentRequestEntity> amendmentEntities = applicationAmendmentRequestRepository.findExpiringBetween(startDate, endDate);
|
||||
|
||||
for (ApplicationAmendmentRequestEntity entity : amendmentEntities) {
|
||||
ApplicationEntity application = entity.getApplicationEvaluationEntity().getAssignedApplicationsEntity().getApplication();
|
||||
Map<String, String> placeHolders = replacePlaceholders(application,daysBefore);
|
||||
notificationDao.sendNotificationToInstructor(placeHolders,entity.getApplicationEvaluationEntity(), NotificationTypeEnum.AMENDMENT_EXPIRATION_REMINDER);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> replacePlaceholders (ApplicationEntity application, Long daysBefore){
|
||||
Long companyId = application.getCompanyId();
|
||||
CompanyEntity company = companyService.validateCompany(companyId);
|
||||
Map<String, String> placeHolders = new HashMap<>();
|
||||
placeHolders.put("{{call_name}}",application.getCall().getName());
|
||||
placeHolders.put("{{company_name}}", company.getCompanyName());
|
||||
placeHolders.put("{{days_before}}", daysBefore.toString());
|
||||
return placeHolders;
|
||||
}
|
||||
|
||||
private void processEvaluationExpiration(LocalDateTime startDate, LocalDateTime endDate, Long daysBefore) {
|
||||
List<ApplicationEvaluationEntity> evaluationEntities = applicationEvaluationRepository.findExpiringBetween(startDate, endDate);
|
||||
|
||||
for (ApplicationEvaluationEntity entity : evaluationEntities) {
|
||||
ApplicationEntity application = entity.getAssignedApplicationsEntity().getApplication();
|
||||
Map<String, String> placeHolders = replacePlaceholders(application,daysBefore);
|
||||
notificationDao.sendNotificationToInstructor(placeHolders,entity,NotificationTypeEnum.EVALUATION_EXPIRATION_REMINDER);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user