From 61c19e5b87d6919f4bdd04d4db78468a366e954c Mon Sep 17 00:00:00 2001 From: Alyona Date: Sat, 27 Oct 2018 22:32:51 +0400 Subject: [PATCH] =?UTF-8?q?add=20deadlineS=D1=81heduler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../paper/service/DeadlineSсheduler.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/main/java/ru/ulstu/paper/service/DeadlineSсheduler.java diff --git a/src/main/java/ru/ulstu/paper/service/DeadlineSсheduler.java b/src/main/java/ru/ulstu/paper/service/DeadlineSсheduler.java new file mode 100644 index 0000000..d1810f4 --- /dev/null +++ b/src/main/java/ru/ulstu/paper/service/DeadlineSсheduler.java @@ -0,0 +1,70 @@ +package ru.ulstu.paper.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; +import ru.ulstu.core.util.DateUtils; +import ru.ulstu.paper.model.PaperDto; +import ru.ulstu.paper.repository.PaperRepository; +import ru.ulstu.paper.service.PaperService; +import ru.ulstu.paper.model.Paper; +import ru.ulstu.user.model.User; +import ru.ulstu.user.model.UserDto; +import ru.ulstu.user.repository.UserRepository; +import ru.ulstu.user.scheduler.UserScheduler; +import ru.ulstu.user.service.MailService; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.Calendar; +import java.util.List; + +public class DeadlineSсheduler { + + private final Logger log = LoggerFactory.getLogger(UserScheduler.class); + + private final PaperRepository paperRepository; + private final MailService mailService; + + public DeadlineSсheduler(PaperRepository paperRepository, MailService mailService) { + this.paperRepository = paperRepository; + this.mailService = mailService; + } + + @Scheduled(cron = "0 0 8 * 1 ?") + public void sendMessageDeadlineBeforeWeek() { + log.debug("DeadlineSсheduler.sendMessageDeadlineBeforeWeek started"); + List allPapers = paperRepository.findAll(); + for (Paper element: allPapers) { + Calendar c = Calendar.getInstance(); + c.add(Calendar.DAY_OF_YEAR, 7); + if(c.getTime().compareTo(element.getDeadlineDate()) < 0 ) { + List users = element.getAuthors(); + for (User user : users) { + mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи", + "Срок сдачи статьи " + element.getTitle() + " " + element.getDeadlineDate().toString()); + } + } + } + log.debug("DeadlineSсheduler.sendMessageDeadlineBeforeWeek finished"); + } + @Scheduled(cron = "0 0 8 * * ?") + public void sendMessageDeadlineAfterWeek() { + log.debug("DeadlineSсheduler.sendMessageDeadlineAfterWeek started"); + List allPapers = paperRepository.findAll(); + for (Paper element: allPapers) { + Calendar c = Calendar.getInstance(); + c.add(Calendar.DAY_OF_YEAR, 7); + if(c.getTime().compareTo(element.getDeadlineDate()) >= 0 ) { + List users = element.getAuthors(); + for (User user : users) { + mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи", + "Срок сдачи статьи " + element.getTitle() + " " + element.getDeadlineDate().toString()); + } + } + } + log.debug("DeadlineSсheduler.sendMessageDeadlineAfterWeek finished"); + } +}