fix scheduler code to use service
This commit is contained in:
parent
cd09cf6d60
commit
34d7927d30
@ -4,28 +4,26 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.ulstu.core.util.DateUtils;
|
||||||
import ru.ulstu.paper.model.Paper;
|
import ru.ulstu.paper.model.Paper;
|
||||||
import ru.ulstu.paper.repository.PaperRepository;
|
|
||||||
import ru.ulstu.user.model.User;
|
import ru.ulstu.user.model.User;
|
||||||
import ru.ulstu.user.scheduler.UserScheduler;
|
|
||||||
import ru.ulstu.user.service.MailService;
|
import ru.ulstu.user.service.MailService;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Date;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class DeadlineScheduler {
|
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 Logger log = LoggerFactory.getLogger(DeadlineScheduler.class);
|
||||||
|
|
||||||
private final PaperRepository paperRepository;
|
private final PaperService paperService;
|
||||||
|
|
||||||
private final MailService mailService;
|
private final MailService mailService;
|
||||||
|
|
||||||
private final static boolean IS_DEADLINE_NOTIFICATION_BEFORE_WEEK = true;
|
public DeadlineScheduler(PaperService paperService, MailService mailService) {
|
||||||
|
this.paperService = paperService;
|
||||||
public DeadlineScheduler(PaperRepository paperRepository, MailService mailService) {
|
|
||||||
this.paperRepository = paperRepository;
|
|
||||||
this.mailService = mailService;
|
this.mailService = mailService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,21 +43,25 @@ public class DeadlineScheduler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void sendNotifications(boolean isDeadlineBeforeWeek) {
|
private void sendNotifications(boolean isDeadlineBeforeWeek) {
|
||||||
List<Paper> allPapers = paperRepository.findAll();
|
Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION);
|
||||||
for (Paper paper : allPapers) {
|
paperService
|
||||||
Calendar c = Calendar.getInstance();
|
.findAll()
|
||||||
c.add(Calendar.DAY_OF_YEAR, 7);
|
.stream()
|
||||||
if (((c.getTime().compareTo(paper.getDeadlineDate()) < 0) && isDeadlineBeforeWeek) ||
|
.filter(paper -> needToSendDeadlineNotification(paper, now, isDeadlineBeforeWeek))
|
||||||
(c.getTime().compareTo(paper.getDeadlineDate()) >= 0) && !isDeadlineBeforeWeek) {
|
.map(paper -> sendMessageDeadline(paper));
|
||||||
sendMessageDeadline(paper);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendMessageDeadline(Paper 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()) {
|
for (User user : paper.getAuthors()) {
|
||||||
mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи",
|
mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи",
|
||||||
"Срок сдачи статьи " + paper.getTitle() + " " + paper.getDeadlineDate().toString());
|
"Срок сдачи статьи " + paper.getTitle() + " " + paper.getDeadlineDate().toString());
|
||||||
}
|
}
|
||||||
|
return paper;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user