#70 added ping notification

This commit is contained in:
Nightblade73 2019-04-30 10:43:36 +04:00
parent 0e8752e460
commit 9edcf35338
5 changed files with 79 additions and 3 deletions

View File

@ -4,6 +4,7 @@ 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.conference.model.Conference;
import ru.ulstu.core.util.DateUtils; import ru.ulstu.core.util.DateUtils;
import ru.ulstu.ping.service.PingService;
import ru.ulstu.user.service.MailService; import ru.ulstu.user.service.MailService;
import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserService;
@ -14,24 +15,30 @@ import java.util.Map;
@Service @Service
public class ConferenceNotificationService { public class ConferenceNotificationService {
private final static int YESTERDAY = 0;
private final static int DAYS_TO_DEADLINE_NOTIFICATION = 7; 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_DEADLINE = "conferenceDeadlineNotification";
private final static String TEMPLATE_CREATE = "conferenceCreateNotification"; private final static String TEMPLATE_CREATE = "conferenceCreateNotification";
private final static String TEMPLATE_UPDATE_DEADLINES = "conferenceUpdateDeadlinesNotification"; private final static String TEMPLATE_UPDATE_DEADLINES = "conferenceUpdateDeadlinesNotification";
private final static String TEMPLATE_UPDATE_DATES = "conferenceUpdateDatesNotification"; 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_CREATE = "Создана новая конференция: %s";
private final static String TITLE_UPDATE_DEADLINES = "Изменения дедлайнов в конференции: %s"; private final static String TITLE_UPDATE_DEADLINES = "Изменения дедлайнов в конференции: %s";
private final static String TITLE_UPDATE_DATES = "Изменение дат проведения конференции: %s"; private final static String TITLE_UPDATE_DATES = "Изменение дат проведения конференции: %s";
private final MailService mailService; private final MailService mailService;
private final UserService userService; private final UserService userService;
private final PingService pingService;
public ConferenceNotificationService(MailService mailService, public ConferenceNotificationService(MailService mailService,
UserService userService) { UserService userService,
PingService pingService) {
this.mailService = mailService; this.mailService = mailService;
this.userService = userService; this.userService = userService;
this.pingService = pingService;
} }
public void sendDeadlineNotifications(List<Conference> conferences, boolean isDeadlineBeforeWeek) { public void sendDeadlineNotifications(List<Conference> conferences, boolean isDeadlineBeforeWeek) {
@ -78,4 +85,27 @@ public class ConferenceNotificationService {
} }
public void sendPingNotifications(List<Conference> 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<String, Object> variables = ImmutableMap.of("conference", conference);
sendForAllParticipals(variables, conference, TEMPLATE_PING, String.format(TITLE_PING, conference.getTitle()));
}
} }

View File

@ -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() { public void checkDeadlineBeforeWeek() {
log.debug("ConferenceScheduler.checkDeadlineBeforeWeek started"); log.debug("ConferenceScheduler.checkDeadlineBeforeWeek started");
conferenceNotificationService.sendDeadlineNotifications(conferenceService.findAll(), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); 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); conferenceNotificationService.sendDeadlineNotifications(conferenceService.findAll(), !IS_DEADLINE_NOTIFICATION_BEFORE_WEEK);
log.debug("ConferenceScheduler.checkDeadlineAfterWeek finished"); 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");
}
} }

View File

@ -1,7 +1,13 @@
package ru.ulstu.ping.repository; package ru.ulstu.ping.repository;
import org.springframework.data.jpa.repository.JpaRepository; 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; import ru.ulstu.ping.model.Ping;
public interface PingRepository extends JpaRepository<Ping, Integer> { public interface PingRepository extends JpaRepository<Ping, Integer> {
@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);
} }

View File

@ -8,6 +8,7 @@ import ru.ulstu.ping.repository.PingRepository;
import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserService;
import java.io.IOException; import java.io.IOException;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
@Service @Service
@ -27,4 +28,11 @@ public class PingService {
newPing.setConference(conference); newPing.setConference(conference);
pingRepository.save(newPing); 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));
}
} }

View File

@ -0,0 +1,25 @@
<!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>" была пропингована
<b><span th:text="${conference.ping}"></span></b> раз.
<br/>
Обратите внимание.
</p>
<p>
Regards,
<br/>
<em>NG-tracker.</em>
</p>
</body>
</html>