Resolve "Список периодических событий" #129

Merged
romanov73 merged 57 commits from 4-show-time-line into master 2018-10-30 23:32:01 +04:00
Showing only changes of commit 61c19e5b87 - Show all commits

View File

@ -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<Paper> 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<User> 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<Paper> 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<User> users = element.getAuthors();
for (User user : users) {
mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи",
"Срок сдачи статьи " + element.getTitle() + " " + element.getDeadlineDate().toString());
}
}
}
log.debug("DeadlineSсheduler.sendMessageDeadlineAfterWeek finished");
}
}