#70 added create notification for all users about new conference

This commit is contained in:
Nightblade73 2019-04-25 17:12:39 +04:00
parent 88ec35faa0
commit c489ebbf91
5 changed files with 129 additions and 5 deletions

View File

@ -21,8 +21,10 @@ import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional;
@Entity @Entity
@Table(name = "conference") @Table(name = "conference")
@ -135,4 +137,22 @@ public class Conference extends BaseEntity {
public void setUsers(List<ConferenceUser> users) { public void setUsers(List<ConferenceUser> users) {
this.users = users; this.users = users;
} }
public Optional<Deadline> getNextDeadline() {
return deadlines
.stream()
.filter(deadline -> deadline.getDate() != null)
.sorted(Comparator.comparing(Deadline::getDate))
.filter(d -> d.getDate().after(new Date()))
.findFirst();
}
public boolean lastDeadlineFailed() {
return !deadlines
.stream()
.filter(deadline -> deadline.getDate() != null)
.filter(d -> d.getDate().after(new Date()))
.findAny()
.isPresent();
}
} }

View File

@ -1,7 +1,80 @@
package ru.ulstu.conference.service; package ru.ulstu.conference.service;
import com.google.common.collect.ImmutableMap;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.conference.model.Conference;
import ru.ulstu.core.util.DateUtils;
import ru.ulstu.user.service.MailService;
import ru.ulstu.user.service.UserService;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service @Service
public class ConferenceNotificationService { public class ConferenceNotificationService {
private final static int DAYS_TO_DEADLINE_NOTIFICATION = 7;
private final static String TEMPLATE_DEADLINE = "conferenceDeadlineNotification";
private final static String TEMPLATE_CREATE_CONFERENCE = "conferenceCreateNotification";
private final static String TEMPLATE_STATUS_CHANGED = "conferenceChangeNotification";
private final static String TEMPLATE_FAILED = "conferenceFailedNotification";
private final static String TITLE_DEADLINE = "Приближается дедлайн конференции";
private final static String TITLE_CREATE = "Создана новая конференция: %s";
private final static String TITLE_STATUS_CHANGED = "Изменения в конференции";
private final static String TITLE_FAILED = "Статья провалена";
private final MailService mailService;
private final UserService userService;
public ConferenceNotificationService(MailService mailService,
UserService userService) {
this.mailService = mailService;
this.userService = userService;
}
public void sendDeadlineNotifications(List<Conference> conferences, boolean isDeadlineBeforeWeek) {
Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION);
conferences
.stream()
.filter(conference -> needToSendDeadlineNotification(conference, now, isDeadlineBeforeWeek))
.forEach(conference -> sendMessageDeadline(conference));
}
private boolean needToSendDeadlineNotification(Conference conference, Date compareDate, boolean isDeadlineBeforeWeek) {
return (conference.getNextDeadline().isPresent())
&& (compareDate.before(conference.getNextDeadline().get().getDate()) && isDeadlineBeforeWeek
|| compareDate.after(conference.getNextDeadline().get().getDate()) && !isDeadlineBeforeWeek)
&& conference.getNextDeadline().get().getDate().after(new Date());
}
private void sendMessageDeadline(Conference conference) {
Map<String, Object> variables = ImmutableMap.of("conference", conference);
sendForAllParticipals(variables, conference, TEMPLATE_DEADLINE, TITLE_DEADLINE);
}
public void sendCreateNotification(Conference conference) {
Map<String, Object> variables = ImmutableMap.of("conference", conference);
sendForAllUsers(variables, TEMPLATE_CREATE_CONFERENCE, String.format(TITLE_CREATE, conference.getTitle()));
}
// public void statusChangeNotification(Paper paper, Paper.PaperStatus oldStatus) {
// Map<String, Object> variables = ImmutableMap.of("paper", paper, "oldStatus", oldStatus);
// sendForAllParticipals(variables, paper, TEMPLATE_STATUS_CHANGED, TITLE_STATUS_CHANGED);
// }
//
// public void sendFailedNotification(Paper paper, Paper.PaperStatus oldStatus) {
// Map<String, Object> variables = ImmutableMap.of("paper", paper, "oldStatus", oldStatus);
// sendForAllParticipals(variables, paper, TEMPLATE_FAILED, TITLE_FAILED);
// }
private void sendForAllUsers(Map<String, Object> variables, String template, String title) {
userService.findAll().forEach(user -> mailService.sendEmailFromTemplate(variables, user, template, title));
}
private void sendForAllParticipals(Map<String, Object> variables, Conference conference, String template, String title) {
conference.getUsers().forEach(conferenceUser -> mailService.sendEmailFromTemplate(variables, conferenceUser.getUser(), template, title));
}
} }

View File

@ -36,19 +36,22 @@ public class ConferenceService {
private final PaperService paperService; private final PaperService paperService;
private final UserService userService; private final UserService userService;
private final PingService pingService; private final PingService pingService;
private final ConferenceNotificationService conferenceNotificationService;
public ConferenceService(ConferenceRepository conferenceRepository, public ConferenceService(ConferenceRepository conferenceRepository,
ConferenceUserService conferenceUserService, ConferenceUserService conferenceUserService,
DeadlineService deadlineService, DeadlineService deadlineService,
PaperService paperService, PaperService paperService,
UserService userService, UserService userService,
PingService pingService) { PingService pingService,
ConferenceNotificationService conferenceNotificationService) {
this.conferenceRepository = conferenceRepository; this.conferenceRepository = conferenceRepository;
this.conferenceUserService = conferenceUserService; this.conferenceUserService = conferenceUserService;
this.deadlineService = deadlineService; this.deadlineService = deadlineService;
this.paperService = paperService; this.paperService = paperService;
this.userService = userService; this.userService = userService;
this.pingService = pingService; this.pingService = pingService;
this.conferenceNotificationService = conferenceNotificationService;
} }
public ConferenceDto getExistConferenceById(Integer id) { public ConferenceDto getExistConferenceById(Integer id) {
@ -91,6 +94,7 @@ public class ConferenceService {
public Integer create(ConferenceDto conferenceDto) throws IOException { public Integer create(ConferenceDto conferenceDto) throws IOException {
Conference newConference = copyFromDto(new Conference(), conferenceDto); Conference newConference = copyFromDto(new Conference(), conferenceDto);
newConference = conferenceRepository.save(newConference); newConference = conferenceRepository.save(newConference);
conferenceNotificationService.sendCreateNotification(newConference);
return newConference.getId(); return newConference.getId();
} }

View File

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Уведомление о создании конференции</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" th:href="@{|${baseUrl}/favicon.ico|}"/>
</head>
<body>
<p>
Уважаемый(ая) <span th:text="${user.firstName + ' ' + user.lastName}">Ivan Ivanov</span>
</p>
<p>
Была создана новая конференция: "<a th:href="@{|${baseUrl}/conferences/conference?id=${conference.id}|}">
<span th:text="${conference.title}">Title</span></a>".
<br/>
Спешите принять участие!
</p>
<p>
Даты проведения:
<span th:text="${#dates.format(conference.beginDate, 'dd.MM.yyyy')}"></span>
-
<span th:text="${#dates.format(conference.endDate, 'dd.MM.yyyy')}"></span>.
</p>
<p>
Regards,
<br/>
<em>NG-tracker.</em>
</p>
</body>
</html>

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" <html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="default" xmlns:th=""> layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/html">
<head> <head>
<link rel="stylesheet" type="text/css" href="../css/conference.css"/> <link rel="stylesheet" type="text/css" href="../css/conference.css"/>
</head> </head>
@ -135,7 +135,6 @@
<div class="form-group"> <div class="form-group">
<label for="papers">Статьи:</label> <label for="papers">Статьи:</label>
<div class="paper-list form-control list-group" id="papers"> <div class="paper-list form-control list-group" id="papers">
<!--<input th:type="hidden" th:field="*{papers}"/>-->
<div class="paper d-flex list-group-item p-0" <div class="paper d-flex list-group-item p-0"
th:each="paper, rowStat : *{papers}"> th:each="paper, rowStat : *{papers}">
<input type="hidden" th:field="*{papers[__${rowStat.index}__].id}"/> <input type="hidden" th:field="*{papers[__${rowStat.index}__].id}"/>
@ -148,8 +147,6 @@
<span th:text="*{papers[__${rowStat.index}__].title}"> <span th:text="*{papers[__${rowStat.index}__].title}">
Имя статьи Имя статьи
</span> </span>
<!--<img class="icon-paper" src="/img/conference/paper.png"/>-->
</a> </a>
<a class="paper-name" <a class="paper-name"
th:unless="*{papers[__${rowStat.index}__].id !=null}"> th:unless="*{papers[__${rowStat.index}__].id !=null}">