diff --git a/src/main/java/ru/ulstu/paper/service/DeadlineScheduler.java b/src/main/java/ru/ulstu/paper/service/DeadlineScheduler.java index 0388f2e..af850a6 100644 --- a/src/main/java/ru/ulstu/paper/service/DeadlineScheduler.java +++ b/src/main/java/ru/ulstu/paper/service/DeadlineScheduler.java @@ -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 MailService mailService; + private final PaperNotificationService paperNotificationService; - 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; - } } diff --git a/src/main/java/ru/ulstu/paper/service/PaperNotificationService.java b/src/main/java/ru/ulstu/paper/service/PaperNotificationService.java new file mode 100644 index 0000000..fc5dd27 --- /dev/null +++ b/src/main/java/ru/ulstu/paper/service/PaperNotificationService.java @@ -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; + } +}