move to notification service
This commit is contained in:
parent
34d7927d30
commit
2bf84dc286
@ -4,64 +4,31 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.core.util.DateUtils;
|
||||
import ru.ulstu.paper.model.Paper;
|
||||
import ru.ulstu.user.model.User;
|
||||
import ru.ulstu.user.service.MailService;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Service
|
||||
public class DeadlineScheduler {
|
||||
private final static boolean IS_DEADLINE_NOTIFICATION_BEFORE_WEEK = true;
|
||||
private final static int DAYS_TO_DEADLINE_NOTIFICATION = 7;
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(DeadlineScheduler.class);
|
||||
|
||||
private final PaperService paperService;
|
||||
private final PaperNotificationService paperNotificationService;
|
||||
|
||||
private final MailService mailService;
|
||||
|
||||
public DeadlineScheduler(PaperService paperService, MailService mailService) {
|
||||
this.paperService = paperService;
|
||||
this.mailService = mailService;
|
||||
public DeadlineScheduler(PaperNotificationService paperNotificationService) {
|
||||
this.paperNotificationService = paperNotificationService;
|
||||
}
|
||||
|
||||
|
||||
@Scheduled(cron = "0 0 8 * 1 ?")
|
||||
public void checkDeadlineBeforeWeek() {
|
||||
log.debug("DeadlineScheduler.checkDeadlineBeforeWeek started");
|
||||
sendNotifications(IS_DEADLINE_NOTIFICATION_BEFORE_WEEK);
|
||||
paperNotificationService.sendDeadlineNotifications(!IS_DEADLINE_NOTIFICATION_BEFORE_WEEK);
|
||||
log.debug("DeadlineScheduler.checkDeadlineBeforeWeek finished");
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0 8 * * ?")
|
||||
@Scheduled(cron = "0 */2 9 * * ?")
|
||||
public void checkDeadlineAfterWeek() {
|
||||
log.debug("DeadlineScheduler.checkDeadlineAfterWeek started");
|
||||
sendNotifications(!IS_DEADLINE_NOTIFICATION_BEFORE_WEEK);
|
||||
paperNotificationService.sendDeadlineNotifications(IS_DEADLINE_NOTIFICATION_BEFORE_WEEK);
|
||||
log.debug("DeadlineScheduler.checkDeadlineAfterWeek finished");
|
||||
}
|
||||
|
||||
private void sendNotifications(boolean isDeadlineBeforeWeek) {
|
||||
Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION);
|
||||
paperService
|
||||
.findAll()
|
||||
.stream()
|
||||
.filter(paper -> needToSendDeadlineNotification(paper, now, isDeadlineBeforeWeek))
|
||||
.map(paper -> sendMessageDeadline(paper));
|
||||
}
|
||||
|
||||
private boolean needToSendDeadlineNotification(Paper paper, Date compareDate, boolean isDeadlineBeforeWeek) {
|
||||
return paper.getDeadlineDate() != null
|
||||
&& (compareDate.before(paper.getDeadlineDate()) && isDeadlineBeforeWeek
|
||||
|| compareDate.after(paper.getDeadlineDate()) && !isDeadlineBeforeWeek);
|
||||
}
|
||||
|
||||
private Paper sendMessageDeadline(Paper paper) {
|
||||
for (User user : paper.getAuthors()) {
|
||||
mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи",
|
||||
"Срок сдачи статьи " + paper.getTitle() + " " + paper.getDeadlineDate().toString());
|
||||
}
|
||||
return paper;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,49 @@
|
||||
package ru.ulstu.paper.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.core.util.DateUtils;
|
||||
import ru.ulstu.paper.model.Paper;
|
||||
import ru.ulstu.user.model.User;
|
||||
import ru.ulstu.user.service.MailService;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class PaperNotificationService {
|
||||
private final static int DAYS_TO_DEADLINE_NOTIFICATION = 7;
|
||||
|
||||
private final MailService mailService;
|
||||
|
||||
private final PaperService paperService;
|
||||
|
||||
public PaperNotificationService(MailService mailService,
|
||||
PaperService paperService) {
|
||||
this.mailService = mailService;
|
||||
this.paperService = paperService;
|
||||
}
|
||||
|
||||
public void sendDeadlineNotifications(boolean isDeadlineBeforeWeek) {
|
||||
Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION);
|
||||
paperService
|
||||
.findAll()
|
||||
.stream()
|
||||
.filter(paper -> needToSendDeadlineNotification(paper, now, isDeadlineBeforeWeek))
|
||||
.map(paper -> sendMessageDeadline(paper))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private boolean needToSendDeadlineNotification(Paper paper, Date compareDate, boolean isDeadlineBeforeWeek) {
|
||||
return (paper.getDeadlineDate() != null)
|
||||
&& (compareDate.after(paper.getDeadlineDate()) && isDeadlineBeforeWeek
|
||||
|| compareDate.before(paper.getDeadlineDate()) && !isDeadlineBeforeWeek);
|
||||
}
|
||||
|
||||
private Paper sendMessageDeadline(Paper paper) {
|
||||
for (User user : paper.getAuthors()) {
|
||||
mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи",
|
||||
"Срок сдачи статьи " + paper.getTitle() + " " + paper.getDeadlineDate().toString());
|
||||
}
|
||||
return paper;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user