From c489ebbf9149da82f7ab1364b52c883131c02c24 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Thu, 25 Apr 2019 17:12:39 +0400 Subject: [PATCH] #70 added create notification for all users about new conference --- .../ru/ulstu/conference/model/Conference.java | 20 +++++ .../ConferenceNotificationService.java | 73 +++++++++++++++++++ .../conference/service/ConferenceService.java | 6 +- .../conferenceCreateNotification.html | 30 ++++++++ .../templates/conferences/conference.html | 5 +- 5 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 src/main/resources/mail_templates/conferenceCreateNotification.html diff --git a/src/main/java/ru/ulstu/conference/model/Conference.java b/src/main/java/ru/ulstu/conference/model/Conference.java index 3f77699..126c1bc 100644 --- a/src/main/java/ru/ulstu/conference/model/Conference.java +++ b/src/main/java/ru/ulstu/conference/model/Conference.java @@ -21,8 +21,10 @@ import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import java.util.ArrayList; +import java.util.Comparator; import java.util.Date; import java.util.List; +import java.util.Optional; @Entity @Table(name = "conference") @@ -135,4 +137,22 @@ public class Conference extends BaseEntity { public void setUsers(List users) { this.users = users; } + + public Optional 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(); + } } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java b/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java index ff9f69a..c69d862 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java @@ -1,7 +1,80 @@ package ru.ulstu.conference.service; +import com.google.common.collect.ImmutableMap; 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 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 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 variables = ImmutableMap.of("conference", conference); + sendForAllParticipals(variables, conference, TEMPLATE_DEADLINE, TITLE_DEADLINE); + } + + public void sendCreateNotification(Conference conference) { + Map 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 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 variables = ImmutableMap.of("paper", paper, "oldStatus", oldStatus); +// sendForAllParticipals(variables, paper, TEMPLATE_FAILED, TITLE_FAILED); +// } + + private void sendForAllUsers(Map variables, String template, String title) { + userService.findAll().forEach(user -> mailService.sendEmailFromTemplate(variables, user, template, title)); + } + + private void sendForAllParticipals(Map variables, Conference conference, String template, String title) { + conference.getUsers().forEach(conferenceUser -> mailService.sendEmailFromTemplate(variables, conferenceUser.getUser(), template, title)); + } } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 905c208..bed0deb 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -36,19 +36,22 @@ public class ConferenceService { private final PaperService paperService; private final UserService userService; private final PingService pingService; + private final ConferenceNotificationService conferenceNotificationService; public ConferenceService(ConferenceRepository conferenceRepository, ConferenceUserService conferenceUserService, DeadlineService deadlineService, PaperService paperService, UserService userService, - PingService pingService) { + PingService pingService, + ConferenceNotificationService conferenceNotificationService) { this.conferenceRepository = conferenceRepository; this.conferenceUserService = conferenceUserService; this.deadlineService = deadlineService; this.paperService = paperService; this.userService = userService; this.pingService = pingService; + this.conferenceNotificationService = conferenceNotificationService; } public ConferenceDto getExistConferenceById(Integer id) { @@ -91,6 +94,7 @@ public class ConferenceService { public Integer create(ConferenceDto conferenceDto) throws IOException { Conference newConference = copyFromDto(new Conference(), conferenceDto); newConference = conferenceRepository.save(newConference); + conferenceNotificationService.sendCreateNotification(newConference); return newConference.getId(); } diff --git a/src/main/resources/mail_templates/conferenceCreateNotification.html b/src/main/resources/mail_templates/conferenceCreateNotification.html new file mode 100644 index 0000000..055e85d --- /dev/null +++ b/src/main/resources/mail_templates/conferenceCreateNotification.html @@ -0,0 +1,30 @@ + + + + Уведомление о создании конференции + + + + +

+ Уважаемый(ая) Ivan Ivanov +

+

+ Была создана новая конференция: " + Title". +
+ Спешите принять участие! +

+

+ Даты проведения: + + - + . +

+

+ Regards, +
+ NG-tracker. +

+ + \ No newline at end of file diff --git a/src/main/resources/templates/conferences/conference.html b/src/main/resources/templates/conferences/conference.html index 2d93a5e..69c754a 100644 --- a/src/main/resources/templates/conferences/conference.html +++ b/src/main/resources/templates/conferences/conference.html @@ -1,7 +1,7 @@ + layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/html"> @@ -135,7 +135,6 @@
-
@@ -148,8 +147,6 @@ Имя статьи - -