From 9edcf353389d453389dcc5e897564a822fb2cbb5 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Tue, 30 Apr 2019 10:43:36 +0400 Subject: [PATCH] #70 added ping notification --- .../ConferenceNotificationService.java | 34 +++++++++++++++++-- .../service/ConferenceScheduler.java | 9 ++++- .../ulstu/ping/repository/PingRepository.java | 6 ++++ .../ru/ulstu/ping/service/PingService.java | 8 +++++ .../conferencePingNotification.html | 25 ++++++++++++++ 5 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/mail_templates/conferencePingNotification.html diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java b/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java index 54faa49..9449b12 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java @@ -4,6 +4,7 @@ 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.ping.service.PingService; import ru.ulstu.user.service.MailService; import ru.ulstu.user.service.UserService; @@ -14,24 +15,30 @@ import java.util.Map; @Service public class ConferenceNotificationService { + private final static int YESTERDAY = 0; private final static int DAYS_TO_DEADLINE_NOTIFICATION = 7; + private final static String TEMPLATE_PING = "conferencePingNotification"; private final static String TEMPLATE_DEADLINE = "conferenceDeadlineNotification"; private final static String TEMPLATE_CREATE = "conferenceCreateNotification"; private final static String TEMPLATE_UPDATE_DEADLINES = "conferenceUpdateDeadlinesNotification"; private final static String TEMPLATE_UPDATE_DATES = "conferenceUpdateDatesNotification"; - private final static String TITLE_DEADLINE = "Приближается дедлайн конференции"; + private final static String TITLE_PING = "Обратите внимание на конференцию: %s"; + private final static String TITLE_DEADLINE = "Приближается дедлайн конференции: %s"; private final static String TITLE_CREATE = "Создана новая конференция: %s"; private final static String TITLE_UPDATE_DEADLINES = "Изменения дедлайнов в конференции: %s"; private final static String TITLE_UPDATE_DATES = "Изменение дат проведения конференции: %s"; private final MailService mailService; private final UserService userService; + private final PingService pingService; public ConferenceNotificationService(MailService mailService, - UserService userService) { + UserService userService, + PingService pingService) { this.mailService = mailService; this.userService = userService; + this.pingService = pingService; } public void sendDeadlineNotifications(List conferences, boolean isDeadlineBeforeWeek) { @@ -78,4 +85,27 @@ public class ConferenceNotificationService { } + public void sendPingNotifications(List conferences) { + Date yesterday = DateUtils.addDays(new Date(), YESTERDAY); + conferences + .stream() + .filter(conference -> { + Integer pingCount = pingService.countPingYesterday(conference, yesterday); + return needToSendPingNotification(conference, pingCount); + }) + .forEach(this::sendMessagePing); + } + + private boolean needToSendPingNotification(Conference conference, Integer pingCount) { + if (pingCount > 0) { + conference.setPing((Integer) pingCount); + return true; + } + return false; + } + + private void sendMessagePing(Conference conference) { + Map variables = ImmutableMap.of("conference", conference); + sendForAllParticipals(variables, conference, TEMPLATE_PING, String.format(TITLE_PING, conference.getTitle())); + } } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java b/src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java index 863abb5..f55f885 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java @@ -21,7 +21,7 @@ public class ConferenceScheduler { } - @Scheduled(cron = "0 0 8 * * *", zone = "Europe/Samara") + @Scheduled(cron = "0 0 8 * * MON", zone = "Europe/Samara") public void checkDeadlineBeforeWeek() { log.debug("ConferenceScheduler.checkDeadlineBeforeWeek started"); conferenceNotificationService.sendDeadlineNotifications(conferenceService.findAll(), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); @@ -34,4 +34,11 @@ public class ConferenceScheduler { conferenceNotificationService.sendDeadlineNotifications(conferenceService.findAll(), !IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); log.debug("ConferenceScheduler.checkDeadlineAfterWeek finished"); } + + @Scheduled(cron = "0 0 8 * * *", zone = "Europe/Samara") + public void checkNewPing() { + log.debug("ConferenceScheduler.checkPing started"); + conferenceNotificationService.sendPingNotifications(conferenceService.findAll()); + log.debug("ConferenceScheduler.checkPing finished"); + } } diff --git a/src/main/java/ru/ulstu/ping/repository/PingRepository.java b/src/main/java/ru/ulstu/ping/repository/PingRepository.java index ebafc0c..8e69111 100644 --- a/src/main/java/ru/ulstu/ping/repository/PingRepository.java +++ b/src/main/java/ru/ulstu/ping/repository/PingRepository.java @@ -1,7 +1,13 @@ package ru.ulstu.ping.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import ru.ulstu.conference.model.Conference; import ru.ulstu.ping.model.Ping; public interface PingRepository extends JpaRepository { + + @Query("SELECT count(*) FROM Ping p WHERE (DAY(p.date) = :day) AND (MONTH(p.date) = :month) AND (p.conference = :conference)") + long countByConferenceAndDate(@Param("conference") Conference conference, @Param("day") Integer day, @Param("month") Integer month); } diff --git a/src/main/java/ru/ulstu/ping/service/PingService.java b/src/main/java/ru/ulstu/ping/service/PingService.java index ff0d249..3152e5a 100644 --- a/src/main/java/ru/ulstu/ping/service/PingService.java +++ b/src/main/java/ru/ulstu/ping/service/PingService.java @@ -8,6 +8,7 @@ import ru.ulstu.ping.repository.PingRepository; import ru.ulstu.user.service.UserService; import java.io.IOException; +import java.util.Calendar; import java.util.Date; @Service @@ -27,4 +28,11 @@ public class PingService { newPing.setConference(conference); pingRepository.save(newPing); } + + public Integer countPingYesterday(Conference conference, Date yesterday) { + Calendar cal = Calendar.getInstance(); + cal.setTime(yesterday); + + return Math.toIntExact(pingRepository.countByConferenceAndDate(conference, cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1)); + } } diff --git a/src/main/resources/mail_templates/conferencePingNotification.html b/src/main/resources/mail_templates/conferencePingNotification.html new file mode 100644 index 0000000..a80a3f2 --- /dev/null +++ b/src/main/resources/mail_templates/conferencePingNotification.html @@ -0,0 +1,25 @@ + + + + Обратите внимание на конференциию + + + + +

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

+

+ Конференция " + Title" была пропингована + раз. +
+ Обратите внимание. +

+

+ Regards, +
+ NG-tracker. +

+ + \ No newline at end of file