ng-tracker/src/main/java/ru/ulstu/paper/service/PaperNotificationService.java
2018-11-21 15:40:20 +04:00

66 lines
2.7 KiB
Java

package ru.ulstu.paper.service;
import com.google.common.collect.ImmutableMap;
import org.springframework.stereotype.Service;
import ru.ulstu.core.util.DateUtils;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.user.service.MailService;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class PaperNotificationService {
private final static int DAYS_TO_DEADLINE_NOTIFICATION = 7;
private final MailService mailService;
public PaperNotificationService(MailService mailService) {
this.mailService = mailService;
}
public void sendDeadlineNotifications(List<Paper> papers, boolean isDeadlineBeforeWeek) {
Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION);
papers
.stream()
.filter(paper -> needToSendDeadlineNotification(paper, now, isDeadlineBeforeWeek))
.forEach(paper -> sendMessageDeadline(paper));
}
private boolean needToSendDeadlineNotification(Paper paper, Date compareDate, boolean isDeadlineBeforeWeek) {
return (paper.getDeadlineDate() != null)
&& (compareDate.after(paper.getDeadlineDate()) && isDeadlineBeforeWeek
|| compareDate.before(paper.getDeadlineDate()) && !isDeadlineBeforeWeek)
&& paper.getDeadlineDate().after(new Date());
}
private void sendMessageDeadline(Paper paper) {
paper.getAuthors().forEach(user -> {
mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи",
"Срок сдачи статьи " + paper.getTitle() + " " + paper.getDeadlineDate().toString());
});
}
public void sendCreateNotification(Paper paper) {
Map<String, Object> variables = ImmutableMap.of("paper", paper);
paper.getAuthors().forEach(author -> {
mailService.sendEmailFromTemplate(variables, author, "paperCreateNotification", "Создана статья");
});
}
public void statusChangeNotification(Paper paper, Paper.PaperStatus oldStatus) {
Map<String, Object> variables = ImmutableMap.of("paper", paper, "oldStatus", oldStatus);
paper.getAuthors().forEach(author -> {
mailService.sendEmailFromTemplate(variables, author, "paperStatusChangeNotification", "Изменился статус статьи");
});
}
public void sendFailedNotification(Paper paper, Paper.PaperStatus oldStatus) {
Map<String, Object> variables = ImmutableMap.of("paper", paper, "oldStatus", oldStatus);
paper.getAuthors().forEach(author -> {
mailService.sendEmailFromTemplate(variables, author, "paperFailedNotification", "Статья провалена");
});
}
}