#119 create grants notification service
This commit is contained in:
parent
2dff183662
commit
2dae39a100
@ -0,0 +1,66 @@
|
|||||||
|
package ru.ulstu.grant.service;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.ulstu.core.util.DateUtils;
|
||||||
|
import ru.ulstu.grant.model.Grant;
|
||||||
|
import ru.ulstu.user.model.User;
|
||||||
|
import ru.ulstu.user.service.MailService;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GrantNotificationService {
|
||||||
|
private final static int DAYS_TO_DEADLINE_NOTIFICATION = 7;
|
||||||
|
private final static String TEMPLATE_DEADLINE = "grantDeadlineNotification";
|
||||||
|
private final static String TEMPLATE_CREATE = "grantCreateNotification";
|
||||||
|
private final static String TEMPLATE_AUTHORS_CHANGED = "grantAuthorsChangeNotification";
|
||||||
|
|
||||||
|
private final static String TITLE_DEADLINE = "Приближается дедлайн гранта: %s";
|
||||||
|
private final static String TITLE_CREATE = "Создан грант: %s";
|
||||||
|
private final static String TITLE_AUTHORS_CHANGED = "Изменился состав рабочей группы гранта: %s";
|
||||||
|
|
||||||
|
private final MailService mailService;
|
||||||
|
|
||||||
|
public GrantNotificationService(MailService mailService) {
|
||||||
|
this.mailService = mailService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendDeadlineNotifications(List<Grant> grants, boolean isDeadlineBeforeWeek) {
|
||||||
|
Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION);
|
||||||
|
grants.stream()
|
||||||
|
.filter(grant -> needToSendDeadlineNotification(grant, now, isDeadlineBeforeWeek))
|
||||||
|
.forEach(grant -> sendMessageDeadline(grant));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean needToSendDeadlineNotification(Grant grant, Date compareDate, boolean isDeadlineBeforeWeek) {
|
||||||
|
return (grant.getNextDeadline().isPresent())
|
||||||
|
&& (compareDate.before(grant.getNextDeadline().get().getDate()) && isDeadlineBeforeWeek
|
||||||
|
|| compareDate.after(grant.getNextDeadline().get().getDate()) && !isDeadlineBeforeWeek)
|
||||||
|
&& grant.getNextDeadline().get().getDate().after(new Date());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendMessageDeadline(Grant grant) {
|
||||||
|
Map<String, Object> variables = ImmutableMap.of("grant", grant);
|
||||||
|
sendForAllAuthors(variables, grant, TEMPLATE_DEADLINE, String.format(TITLE_DEADLINE, grant.getTitle()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendCreateNotification(Grant grant) {
|
||||||
|
Map<String, Object> variables = ImmutableMap.of("grant", grant);
|
||||||
|
sendForAllAuthors(variables, grant, TEMPLATE_CREATE, String.format(TITLE_CREATE, grant.getTitle()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void authorsChangeNotification(Grant grant, List<User> oldAuthors) {
|
||||||
|
Map<String, Object> variables = ImmutableMap.of("grant", grant, "oldAuthors", oldAuthors);
|
||||||
|
sendForAllAuthors(variables, grant, TEMPLATE_AUTHORS_CHANGED, String.format(TITLE_AUTHORS_CHANGED, grant.getTitle()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendForAllAuthors(Map<String, Object> variables, Grant grant, String template, String title) {
|
||||||
|
Set<User> allAuthors = grant.getAuthors();
|
||||||
|
allAuthors.add(grant.getLeader());
|
||||||
|
allAuthors.forEach(author -> mailService.sendEmailFromTemplate(variables, author, template, title));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user