From c489ebbf9149da82f7ab1364b52c883131c02c24 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Thu, 25 Apr 2019 17:12:39 +0400 Subject: [PATCH 01/11] #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 @@ Имя статьи - - From 16e9bf1fb56c092e6a57fc7afa45306f18a6f406 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Thu, 25 Apr 2019 19:15:19 +0400 Subject: [PATCH 02/11] #70 added update notification for all participants --- .../ConferenceNotificationService.java | 25 ++++++++---- .../conference/service/ConferenceService.java | 40 +++++++++++++++++++ .../conferenceUpdateDatesNotification.html | 31 ++++++++++++++ ...conferenceUpdateDeadlinesNotification.html | 24 +++++++++++ 4 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 src/main/resources/mail_templates/conferenceUpdateDatesNotification.html create mode 100644 src/main/resources/mail_templates/conferenceUpdateDeadlinesNotification.html diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java b/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java index c69d862..39472aa 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java @@ -16,15 +16,14 @@ 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 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_CREATE = "Создана новая конференция: %s"; - private final static String TITLE_STATUS_CHANGED = "Изменения в конференции"; - private final static String TITLE_FAILED = "Статья провалена"; - + private final static String TITLE_UPDATE_DEADLINES = "Изменения дедлайнов в конференции: %s"; + private final static String TITLE_UPDATE_DATES = "Изменение дат проведения конференции: %s"; private final MailService mailService; private final UserService userService; @@ -57,7 +56,17 @@ public class ConferenceNotificationService { public void sendCreateNotification(Conference conference) { Map variables = ImmutableMap.of("conference", conference); - sendForAllUsers(variables, TEMPLATE_CREATE_CONFERENCE, String.format(TITLE_CREATE, conference.getTitle())); + sendForAllUsers(variables, TEMPLATE_CREATE, String.format(TITLE_CREATE, conference.getTitle())); + } + + public void updateDeadlineNotification(Conference conference) { + Map variables = ImmutableMap.of("conference", conference); + sendForAllParticipals(variables, conference, TEMPLATE_UPDATE_DEADLINES, String.format(TITLE_UPDATE_DEADLINES, conference.getTitle())); + } + + public void updateConferencesDatesNotification(Conference conference, Date oldBeginDate, Date oldEndDate) { + Map variables = ImmutableMap.of("conference", conference, "oldBeginDate", oldBeginDate, "oldEndDate", oldEndDate); + sendForAllParticipals(variables, conference, TEMPLATE_UPDATE_DATES, String.format(TITLE_UPDATE_DATES, conference.getTitle())); } // public void statusChangeNotification(Paper paper, Paper.PaperStatus oldStatus) { @@ -77,4 +86,6 @@ public class ConferenceNotificationService { 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 bed0deb..af43ae9 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -10,6 +10,7 @@ import ru.ulstu.conference.model.ConferenceDto; import ru.ulstu.conference.model.ConferenceFilterDto; import ru.ulstu.conference.model.ConferenceUser; import ru.ulstu.conference.repository.ConferenceRepository; +import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.service.PaperService; @@ -22,6 +23,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.stream.Collectors; import static org.springframework.util.ObjectUtils.isEmpty; import static ru.ulstu.core.util.StreamApiUtils.convert; @@ -101,7 +103,13 @@ public class ConferenceService { @Transactional public Integer update(ConferenceDto conferenceDto) throws IOException { Conference conference = conferenceRepository.findOne(conferenceDto.getId()); + List oldDeadlines = conference.getDeadlines().stream() + .map(this::copyDeadline) + .collect(Collectors.toList()); + Date oldBeginDate = conference.getBeginDate(); + Date oldEndDate = conference.getEndDate(); conferenceRepository.save(copyFromDto(conference, conferenceDto)); + sendNotificationAfterUpdate(conference, oldDeadlines, oldBeginDate, oldEndDate); conferenceDto.getRemovedDeadlineIds().forEach(deadlineService::remove); return conference.getId(); } @@ -228,4 +236,36 @@ public class ConferenceService { modelMap.addAttribute("nearshoreSales", nearshoreSales); modelMap.addAttribute("offshoreSales", offshoreSales); } + + public void sendNotificationAfterUpdate(Conference conference, List oldDeadlines, Date oldBeginDate, Date oldEndDate) { + boolean isSendNotificationAboutDeadlines = false; + if (oldDeadlines.size() != conference.getDeadlines().size()) { + isSendNotificationAboutDeadlines = true; + } + for (Deadline deadline : conference.getDeadlines()) { + if (isSendNotificationAboutDeadlines) { + break; + } + for (Deadline oldDeadline : oldDeadlines) { + if (deadline.getId().equals(oldDeadline.getId())) { + if (!deadline.getDescription().equals(oldDeadline.getDescription()) || !deadline.getDate().equals(oldDeadline.getDate())) { + isSendNotificationAboutDeadlines = true; + break; + } + } + } + } + if (isSendNotificationAboutDeadlines) { + conferenceNotificationService.updateDeadlineNotification(conference); + } + if (!conference.getBeginDate().equals(oldBeginDate) || !conference.getEndDate().equals(oldEndDate)) { + conferenceNotificationService.updateConferencesDatesNotification(conference, oldBeginDate, oldEndDate); + } + } + + public Deadline copyDeadline(Deadline oldDeadline) { + Deadline newDeadline = new Deadline(oldDeadline.getDate(), oldDeadline.getDescription()); + newDeadline.setId(oldDeadline.getId()); + return newDeadline; + } } diff --git a/src/main/resources/mail_templates/conferenceUpdateDatesNotification.html b/src/main/resources/mail_templates/conferenceUpdateDatesNotification.html new file mode 100644 index 0000000..479336f --- /dev/null +++ b/src/main/resources/mail_templates/conferenceUpdateDatesNotification.html @@ -0,0 +1,31 @@ + + + + Уведомление об изменении дат проведения конференции + + + + +

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

+

+ Даты проведения конференции " + Title" изменились с
+ "oldBeginDate" + - + "oldEndDate" +
+ + на
+ "" + - + "". +

+

+ Regards, +
+ NG-tracker. +

+ + \ No newline at end of file diff --git a/src/main/resources/mail_templates/conferenceUpdateDeadlinesNotification.html b/src/main/resources/mail_templates/conferenceUpdateDeadlinesNotification.html new file mode 100644 index 0000000..f010fb8 --- /dev/null +++ b/src/main/resources/mail_templates/conferenceUpdateDeadlinesNotification.html @@ -0,0 +1,24 @@ + + + + Уведомление об изменении дедлайнов конференции + + + + +

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

+

+ Дедлайны конференции " + Title" притерпели изменения. +
+ Ознакомтесь с изменениями. +

+

+ Regards, +
+ NG-tracker. +

+ + \ No newline at end of file From 34925b81ffc1fc03cbfa7ca5c798921483447b7d Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Thu, 25 Apr 2019 21:17:48 +0400 Subject: [PATCH 03/11] #70 added schedule for deadline of conference --- .../ConferenceNotificationService.java | 12 +----- .../service/ConferenceScheduler.java | 37 +++++++++++++++++++ .../conferenceDeadlineNotification.html | 29 +++++++++++++++ 3 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java create mode 100644 src/main/resources/mail_templates/conferenceDeadlineNotification.html diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java b/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java index 39472aa..54faa49 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java @@ -39,7 +39,7 @@ public class ConferenceNotificationService { conferences .stream() .filter(conference -> needToSendDeadlineNotification(conference, now, isDeadlineBeforeWeek)) - .forEach(conference -> sendMessageDeadline(conference)); + .forEach(this::sendMessageDeadline); } private boolean needToSendDeadlineNotification(Conference conference, Date compareDate, boolean isDeadlineBeforeWeek) { @@ -69,16 +69,6 @@ public class ConferenceNotificationService { sendForAllParticipals(variables, conference, TEMPLATE_UPDATE_DATES, String.format(TITLE_UPDATE_DATES, 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)); } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java b/src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java new file mode 100644 index 0000000..ead2913 --- /dev/null +++ b/src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java @@ -0,0 +1,37 @@ +package ru.ulstu.conference.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +@Service +public class ConferenceScheduler { + private final static boolean IS_DEADLINE_NOTIFICATION_BEFORE_WEEK = true; + + private final Logger log = LoggerFactory.getLogger(ConferenceScheduler.class); + + private final ConferenceNotificationService conferenceNotificationService; + private final ConferenceService conferenceService; + + public ConferenceScheduler(ConferenceNotificationService conferenceNotificationService, + ConferenceService conferenceService) { + this.conferenceNotificationService = conferenceNotificationService; + this.conferenceService = conferenceService; + } + + + @Scheduled(cron = "0 0 21-22 * * *", zone = "Europe/Samara") + public void checkDeadlineBeforeWeek() { + log.debug("ConferenceScheduler.checkDeadlineBeforeWeek started"); + conferenceNotificationService.sendDeadlineNotifications(conferenceService.findAll(), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); + log.debug("ConferenceScheduler.checkDeadlineBeforeWeek finished"); + } + + @Scheduled(cron = "0 0 21-22 * * ?", zone = "Europe/Samara") + public void checkDeadlineAfterWeek() { + log.debug("ConferenceScheduler.checkDeadlineAfterWeek started"); + conferenceNotificationService.sendDeadlineNotifications(conferenceService.findAll(), !IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); + log.debug("ConferenceScheduler.checkDeadlineAfterWeek finished"); + } +} diff --git a/src/main/resources/mail_templates/conferenceDeadlineNotification.html b/src/main/resources/mail_templates/conferenceDeadlineNotification.html new file mode 100644 index 0000000..77c89b1 --- /dev/null +++ b/src/main/resources/mail_templates/conferenceDeadlineNotification.html @@ -0,0 +1,29 @@ + + + + Уведомление о дедлайне конференции + + + + +

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

+

+ Приближается дедлайн конференции " + Title". +

+

+ Срок исполнения: . +

+ +

+ Примечание: . +

+

+ Regards, +
+ NG-tracker. +

+ + From 51877023d122a5ea98198ce254ef57af5d4f245f Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Fri, 26 Apr 2019 09:28:20 +0400 Subject: [PATCH 04/11] #70 added new column to event --- .../ru/ulstu/conference/model/Conference.java | 9 ------- .../service/ConferenceScheduler.java | 4 +-- .../conference/service/ConferenceService.java | 7 +++++- .../ulstu/timeline/service/EventService.java | 25 +++++++++++++++++++ .../db/changelog-20190426_000000-schema.xml | 13 ++++++++++ src/main/resources/db/changelog-master.xml | 1 + 6 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 src/main/resources/db/changelog-20190426_000000-schema.xml diff --git a/src/main/java/ru/ulstu/conference/model/Conference.java b/src/main/java/ru/ulstu/conference/model/Conference.java index 126c1bc..9c91a7e 100644 --- a/src/main/java/ru/ulstu/conference/model/Conference.java +++ b/src/main/java/ru/ulstu/conference/model/Conference.java @@ -146,13 +146,4 @@ public class Conference extends BaseEntity { .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/ConferenceScheduler.java b/src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java index ead2913..863abb5 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java @@ -21,14 +21,14 @@ public class ConferenceScheduler { } - @Scheduled(cron = "0 0 21-22 * * *", zone = "Europe/Samara") + @Scheduled(cron = "0 0 8 * * *", zone = "Europe/Samara") public void checkDeadlineBeforeWeek() { log.debug("ConferenceScheduler.checkDeadlineBeforeWeek started"); conferenceNotificationService.sendDeadlineNotifications(conferenceService.findAll(), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); log.debug("ConferenceScheduler.checkDeadlineBeforeWeek finished"); } - @Scheduled(cron = "0 0 21-22 * * ?", zone = "Europe/Samara") + @Scheduled(cron = "0 0 8 * * ?", zone = "Europe/Samara") public void checkDeadlineAfterWeek() { log.debug("ConferenceScheduler.checkDeadlineAfterWeek started"); conferenceNotificationService.sendDeadlineNotifications(conferenceService.findAll(), !IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index af43ae9..6550814 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -15,6 +15,7 @@ import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.service.PaperService; import ru.ulstu.ping.service.PingService; +import ru.ulstu.timeline.service.EventService; import ru.ulstu.user.model.User; import ru.ulstu.user.service.UserService; @@ -39,6 +40,7 @@ public class ConferenceService { private final UserService userService; private final PingService pingService; private final ConferenceNotificationService conferenceNotificationService; + private final EventService eventService; public ConferenceService(ConferenceRepository conferenceRepository, ConferenceUserService conferenceUserService, @@ -46,7 +48,8 @@ public class ConferenceService { PaperService paperService, UserService userService, PingService pingService, - ConferenceNotificationService conferenceNotificationService) { + ConferenceNotificationService conferenceNotificationService, + EventService eventService) { this.conferenceRepository = conferenceRepository; this.conferenceUserService = conferenceUserService; this.deadlineService = deadlineService; @@ -54,6 +57,7 @@ public class ConferenceService { this.userService = userService; this.pingService = pingService; this.conferenceNotificationService = conferenceNotificationService; + this.eventService = eventService; } public ConferenceDto getExistConferenceById(Integer id) { @@ -97,6 +101,7 @@ public class ConferenceService { Conference newConference = copyFromDto(new Conference(), conferenceDto); newConference = conferenceRepository.save(newConference); conferenceNotificationService.sendCreateNotification(newConference); + eventService.createFromConference(newConference); return newConference.getId(); } diff --git a/src/main/java/ru/ulstu/timeline/service/EventService.java b/src/main/java/ru/ulstu/timeline/service/EventService.java index 2d2b83d..d0e50a9 100644 --- a/src/main/java/ru/ulstu/timeline/service/EventService.java +++ b/src/main/java/ru/ulstu/timeline/service/EventService.java @@ -4,6 +4,7 @@ import org.apache.commons.lang3.time.DateUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.conference.model.Conference; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.paper.model.Paper; import ru.ulstu.timeline.model.Event; @@ -140,4 +141,28 @@ public class EventService { public List findAllFutureDto() { return convert(findAllFuture(), EventDto::new); } + + public void createFromConference(Conference newConference) { + List timelines = timelineService.findAll(); + Timeline timeline = timelines.isEmpty() ? new Timeline() : timelines.get(0); + + for (Deadline deadline : newConference.getDeadlines() + .stream() + .filter(d -> d.getDate().after(new Date()) || DateUtils.isSameDay(d.getDate(), new Date())) + .collect(Collectors.toList())) { + Event newEvent = new Event(); + newEvent.setTitle("Дедлайн статьи"); + newEvent.setStatus(Event.EventStatus.NEW); + newEvent.setExecuteDate(deadline.getDate()); + newEvent.setCreateDate(new Date()); + newEvent.setUpdateDate(new Date()); +// newEvent.setDescription("Дедлайн '" + deadline.getDescription() + "' cтатьи '" + newPaper.getTitle() + "'"); +// newEvent.setRecipients(new ArrayList(newPaper.getAuthors())); +// newEvent.setConference(newConference); + eventRepository.save(newEvent); + + timeline.getEvents().add(newEvent); + timelineService.save(timeline); + } + } } diff --git a/src/main/resources/db/changelog-20190426_000000-schema.xml b/src/main/resources/db/changelog-20190426_000000-schema.xml new file mode 100644 index 0000000..5304032 --- /dev/null +++ b/src/main/resources/db/changelog-20190426_000000-schema.xml @@ -0,0 +1,13 @@ + + + + + + + + + diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml index 68b294a..f5a1451 100644 --- a/src/main/resources/db/changelog-master.xml +++ b/src/main/resources/db/changelog-master.xml @@ -34,4 +34,5 @@ + \ No newline at end of file From dbcdb96dbf066655ebe4ff30048a7c38321902cc Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Fri, 26 Apr 2019 15:12:34 +0400 Subject: [PATCH 05/11] #70 added event method, don't work, because trying to find null entity --- .../ru/ulstu/conference/model/Conference.java | 1 + .../conference/service/ConferenceService.java | 1 + src/main/java/ru/ulstu/timeline/model/Event.java | 13 +++++++++++++ .../java/ru/ulstu/timeline/model/EventDto.java | 15 ++++++++++++++- .../timeline/repository/EventRepository.java | 3 +++ .../ru/ulstu/timeline/service/EventService.java | 13 +++++++++---- 6 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/main/java/ru/ulstu/conference/model/Conference.java b/src/main/java/ru/ulstu/conference/model/Conference.java index 9c91a7e..7e18ac0 100644 --- a/src/main/java/ru/ulstu/conference/model/Conference.java +++ b/src/main/java/ru/ulstu/conference/model/Conference.java @@ -59,6 +59,7 @@ public class Conference extends BaseEntity { @JoinTable(name = "paper_conference", joinColumns = {@JoinColumn(name = "conference_id")}, inverseJoinColumns = {@JoinColumn(name = "paper_id")}) + @Fetch(FetchMode.SUBSELECT) private List papers = new ArrayList<>(); @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 6550814..0c1d37d 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -114,6 +114,7 @@ public class ConferenceService { Date oldBeginDate = conference.getBeginDate(); Date oldEndDate = conference.getEndDate(); conferenceRepository.save(copyFromDto(conference, conferenceDto)); + eventService.updateConferenceDeadlines(conference); sendNotificationAfterUpdate(conference, oldDeadlines, oldBeginDate, oldEndDate); conferenceDto.getRemovedDeadlineIds().forEach(deadlineService::remove); return conference.getId(); diff --git a/src/main/java/ru/ulstu/timeline/model/Event.java b/src/main/java/ru/ulstu/timeline/model/Event.java index ef0a4b8..f53a52d 100644 --- a/src/main/java/ru/ulstu/timeline/model/Event.java +++ b/src/main/java/ru/ulstu/timeline/model/Event.java @@ -1,6 +1,7 @@ package ru.ulstu.timeline.model; import org.hibernate.validator.constraints.NotBlank; +import ru.ulstu.conference.model.Conference; import ru.ulstu.core.model.BaseEntity; import ru.ulstu.paper.model.Paper; import ru.ulstu.user.model.User; @@ -76,6 +77,10 @@ public class Event extends BaseEntity { @JoinColumn(name = "paper_id") private Paper paper; + @ManyToOne + @JoinColumn(name = "conference_id") + private Conference conference; + public String getTitle() { return title; } @@ -163,4 +168,12 @@ public class Event extends BaseEntity { public void setPaper(Paper paper) { this.paper = paper; } + + public Conference getConference() { + return conference; + } + + public void setConference(Conference conference) { + this.conference = conference; + } } diff --git a/src/main/java/ru/ulstu/timeline/model/EventDto.java b/src/main/java/ru/ulstu/timeline/model/EventDto.java index 6a4a90b..1d2a29a 100644 --- a/src/main/java/ru/ulstu/timeline/model/EventDto.java +++ b/src/main/java/ru/ulstu/timeline/model/EventDto.java @@ -3,6 +3,7 @@ package ru.ulstu.timeline.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.hibernate.validator.constraints.NotBlank; +import ru.ulstu.conference.model.ConferenceDto; import ru.ulstu.paper.model.PaperDto; import ru.ulstu.user.model.UserDto; @@ -25,6 +26,7 @@ public class EventDto { private final String description; private final List recipients; private PaperDto paperDto; + private ConferenceDto conferenceDto; @JsonCreator public EventDto(@JsonProperty("id") Integer id, @@ -36,7 +38,8 @@ public class EventDto { @JsonProperty("updateDate") Date updateDate, @JsonProperty("description") String description, @JsonProperty("paperDto") PaperDto paperDto, - @JsonProperty("recipients") List recipients) { + @JsonProperty("recipients") List recipients, + @JsonProperty("paperDto") ConferenceDto conferenceDto) { this.id = id; this.title = title; this.period = period; @@ -47,6 +50,7 @@ public class EventDto { this.description = description; this.recipients = recipients; this.paperDto = paperDto; + this.conferenceDto = conferenceDto; } public EventDto(Event event) { @@ -60,6 +64,7 @@ public class EventDto { this.description = event.getDescription(); this.paperDto = new PaperDto(event.getPaper()); this.recipients = convert(event.getRecipients(), UserDto::new); + this.conferenceDto = new ConferenceDto(event.getConference()); } public Integer getId() { @@ -105,4 +110,12 @@ public class EventDto { public void setPaperDto(PaperDto paperDto) { this.paperDto = paperDto; } + + public ConferenceDto getConferenceDto() { + return conferenceDto; + } + + public void setConferenceDto(ConferenceDto conferenceDto) { + this.conferenceDto = conferenceDto; + } } diff --git a/src/main/java/ru/ulstu/timeline/repository/EventRepository.java b/src/main/java/ru/ulstu/timeline/repository/EventRepository.java index eb5c08b..a4b1e47 100644 --- a/src/main/java/ru/ulstu/timeline/repository/EventRepository.java +++ b/src/main/java/ru/ulstu/timeline/repository/EventRepository.java @@ -2,6 +2,7 @@ package ru.ulstu.timeline.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import ru.ulstu.conference.model.Conference; import ru.ulstu.paper.model.Paper; import ru.ulstu.timeline.model.Event; @@ -15,4 +16,6 @@ public interface EventRepository extends JpaRepository { List findAllFuture(); List findAllByPaper(Paper paper); + + List findAllByConference(Conference conference); } diff --git a/src/main/java/ru/ulstu/timeline/service/EventService.java b/src/main/java/ru/ulstu/timeline/service/EventService.java index d0e50a9..f4e2873 100644 --- a/src/main/java/ru/ulstu/timeline/service/EventService.java +++ b/src/main/java/ru/ulstu/timeline/service/EventService.java @@ -151,18 +151,23 @@ public class EventService { .filter(d -> d.getDate().after(new Date()) || DateUtils.isSameDay(d.getDate(), new Date())) .collect(Collectors.toList())) { Event newEvent = new Event(); - newEvent.setTitle("Дедлайн статьи"); + newEvent.setTitle("Дедлайн конференции"); newEvent.setStatus(Event.EventStatus.NEW); newEvent.setExecuteDate(deadline.getDate()); newEvent.setCreateDate(new Date()); newEvent.setUpdateDate(new Date()); -// newEvent.setDescription("Дедлайн '" + deadline.getDescription() + "' cтатьи '" + newPaper.getTitle() + "'"); -// newEvent.setRecipients(new ArrayList(newPaper.getAuthors())); -// newEvent.setConference(newConference); + newEvent.setDescription("Дедлайн '" + deadline.getDescription() + "' конференции '" + newConference.getTitle() + "'"); + newConference.getUsers().forEach(conferenceUser -> newEvent.getRecipients().add(conferenceUser.getUser())); + newEvent.setConference(newConference); eventRepository.save(newEvent); timeline.getEvents().add(newEvent); timelineService.save(timeline); } } + + public void updateConferenceDeadlines(Conference conference) { + eventRepository.delete(eventRepository.findAllByConference(conference)); + createFromConference(conference); + } } From e178cd163909119693001cf225a96dc8eee71bc3 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Sat, 27 Apr 2019 09:45:47 +0400 Subject: [PATCH 06/11] #70 added checking empty fields of deadline, fix bug on EventDto --- .../ulstu/conference/controller/ConferenceController.java | 6 ++++++ src/main/java/ru/ulstu/timeline/model/EventDto.java | 8 ++++++-- src/main/resources/templates/conferences/conference.html | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java index 4c5ea91..d324eb4 100644 --- a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java +++ b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java @@ -78,6 +78,12 @@ public class ConferenceController { @PostMapping(value = "/conference", params = "save") public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { filterEmptyDeadlines(conferenceDto); + for (Deadline deadline : conferenceDto.getDeadlines()) { + if (deadline.getDate() == null || deadline.getDescription().isEmpty()) { + errors.rejectValue("deadlines", "errorCode", "Все поля дедлайна должны быть заполнены"); + return CONFERENCE_PAGE; + } + } if (errors.hasErrors()) { return CONFERENCE_PAGE; } diff --git a/src/main/java/ru/ulstu/timeline/model/EventDto.java b/src/main/java/ru/ulstu/timeline/model/EventDto.java index 1d2a29a..e92d2cb 100644 --- a/src/main/java/ru/ulstu/timeline/model/EventDto.java +++ b/src/main/java/ru/ulstu/timeline/model/EventDto.java @@ -62,9 +62,13 @@ public class EventDto { this.createDate = event.getCreateDate(); this.updateDate = event.getUpdateDate(); this.description = event.getDescription(); - this.paperDto = new PaperDto(event.getPaper()); this.recipients = convert(event.getRecipients(), UserDto::new); - this.conferenceDto = new ConferenceDto(event.getConference()); + if (paperDto != null) { + this.paperDto = new PaperDto(event.getPaper()); + } + if (conferenceDto != null) { + this.conferenceDto = new ConferenceDto(event.getConference()); + } } public Integer getId() { diff --git a/src/main/resources/templates/conferences/conference.html b/src/main/resources/templates/conferences/conference.html index 69c754a..d5906b6 100644 --- a/src/main/resources/templates/conferences/conference.html +++ b/src/main/resources/templates/conferences/conference.html @@ -150,10 +150,10 @@ + Имя статьи - From 0e8752e4607dbf4b2561668b8e657be57df37495 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Sat, 27 Apr 2019 20:34:49 +0400 Subject: [PATCH 07/11] #70 style fixes --- .../java/ru/ulstu/timeline/model/EventDto.java | 2 +- src/main/resources/public/css/conference.css | 18 ++++++++++++++++-- .../fragments/confLineFragment.html | 6 +----- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/main/java/ru/ulstu/timeline/model/EventDto.java b/src/main/java/ru/ulstu/timeline/model/EventDto.java index e92d2cb..ccce25a 100644 --- a/src/main/java/ru/ulstu/timeline/model/EventDto.java +++ b/src/main/java/ru/ulstu/timeline/model/EventDto.java @@ -39,7 +39,7 @@ public class EventDto { @JsonProperty("description") String description, @JsonProperty("paperDto") PaperDto paperDto, @JsonProperty("recipients") List recipients, - @JsonProperty("paperDto") ConferenceDto conferenceDto) { + @JsonProperty("conferenceDto") ConferenceDto conferenceDto) { this.id = id; this.title = title; this.period = period; diff --git a/src/main/resources/public/css/conference.css b/src/main/resources/public/css/conference.css index 6018b9c..73a96ae 100644 --- a/src/main/resources/public/css/conference.css +++ b/src/main/resources/public/css/conference.css @@ -7,6 +7,10 @@ body { border-radius: .25rem; } +.conference-row .d-flex:hover .icon-delete { + visibility: visible; +} + .filter-option-inner-inner { color: white; } @@ -17,10 +21,20 @@ body { .conference-row .d-flex .text-decoration { text-decoration: none; + margin: 0; } .conference-row .d-flex .text-decoration:nth-child(1) { - margin-left: 10px; + margin-left: 5px; +} + +.conference-row .d-flex .icon-delete { + width: 29px; + height: 29px; + margin: auto; + border: none; + visibility: hidden; + background-color: transparent; } @@ -159,7 +173,7 @@ body { } .icon-delete:hover { - background-color: #ff0000; + background-color: #ff0000 !important; transition: background-color .15s ease-in-out; } diff --git a/src/main/resources/templates/conferences/fragments/confLineFragment.html b/src/main/resources/templates/conferences/fragments/confLineFragment.html index ecd166d..21b4191 100644 --- a/src/main/resources/templates/conferences/fragments/confLineFragment.html +++ b/src/main/resources/templates/conferences/fragments/confLineFragment.html @@ -7,17 +7,13 @@
- + - - - -
From 9edcf353389d453389dcc5e897564a822fb2cbb5 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Tue, 30 Apr 2019 10:43:36 +0400 Subject: [PATCH 08/11] #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 From 0e062f133707716faa6843f73164f415adf8d34e Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Tue, 30 Apr 2019 11:41:36 +0400 Subject: [PATCH 09/11] #70 fast fix --- src/main/java/ru/ulstu/ping/repository/PingRepository.java | 4 ++-- src/main/java/ru/ulstu/ping/service/PingService.java | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/ru/ulstu/ping/repository/PingRepository.java b/src/main/java/ru/ulstu/ping/repository/PingRepository.java index 8e69111..de79dd7 100644 --- a/src/main/java/ru/ulstu/ping/repository/PingRepository.java +++ b/src/main/java/ru/ulstu/ping/repository/PingRepository.java @@ -8,6 +8,6 @@ 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); + @Query("SELECT count(*) FROM Ping p WHERE (DAY(p.date) = :day) AND (MONTH(p.date) = :month) AND (YEAR(p.date) = :year) AND (p.conference = :conference)") + long countByConferenceAndDate(@Param("conference") Conference conference, @Param("day") Integer day, @Param("month") Integer month, @Param("year") Integer year); } diff --git a/src/main/java/ru/ulstu/ping/service/PingService.java b/src/main/java/ru/ulstu/ping/service/PingService.java index 3152e5a..f24ed9d 100644 --- a/src/main/java/ru/ulstu/ping/service/PingService.java +++ b/src/main/java/ru/ulstu/ping/service/PingService.java @@ -33,6 +33,7 @@ public class PingService { Calendar cal = Calendar.getInstance(); cal.setTime(yesterday); - return Math.toIntExact(pingRepository.countByConferenceAndDate(conference, cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1)); + return Math.toIntExact(pingRepository.countByConferenceAndDate(conference, cal.get(Calendar.DAY_OF_MONTH), + cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR))); } } From 60bdd9d233292b7f778b271d10cfa95b1fb70ddf Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Thu, 2 May 2019 14:35:40 +0400 Subject: [PATCH 10/11] #70 fixes --- .../controller/ConferenceController.java | 21 ++------ .../ConferenceNotificationService.java | 30 ++++++----- .../service/ConferenceScheduler.java | 9 +--- .../conference/service/ConferenceService.java | 54 +++++++++++-------- .../ru/ulstu/deadline/model/Deadline.java | 17 ++++++ .../ru/ulstu/ping/service/PingService.java | 9 ++-- .../java/ru/ulstu/timeline/model/Event.java | 3 +- .../ulstu/timeline/service/EventService.java | 2 +- 8 files changed, 77 insertions(+), 68 deletions(-) diff --git a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java index d324eb4..5aecb51 100644 --- a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java +++ b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java @@ -13,7 +13,6 @@ import ru.ulstu.conference.model.ConferenceDto; import ru.ulstu.conference.model.ConferenceFilterDto; import ru.ulstu.conference.model.ConferenceUser; import ru.ulstu.conference.service.ConferenceService; -import ru.ulstu.deadline.model.Deadline; import ru.ulstu.user.model.User; import springfox.documentation.annotations.ApiIgnore; @@ -22,9 +21,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; import java.util.List; -import java.util.stream.Collectors; -import static org.springframework.util.StringUtils.isEmpty; import static ru.ulstu.core.controller.Navigation.CONFERENCES_PAGE; import static ru.ulstu.core.controller.Navigation.CONFERENCE_PAGE; import static ru.ulstu.core.controller.Navigation.REDIRECT_TO; @@ -77,13 +74,8 @@ public class ConferenceController { @PostMapping(value = "/conference", params = "save") public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { - filterEmptyDeadlines(conferenceDto); - for (Deadline deadline : conferenceDto.getDeadlines()) { - if (deadline.getDate() == null || deadline.getDescription().isEmpty()) { - errors.rejectValue("deadlines", "errorCode", "Все поля дедлайна должны быть заполнены"); - return CONFERENCE_PAGE; - } - } + conferenceService.filterEmptyDeadlines(conferenceDto); + conferenceService.checkEmptyFieldsOfDeadline(conferenceDto, errors); if (errors.hasErrors()) { return CONFERENCE_PAGE; } @@ -93,11 +85,11 @@ public class ConferenceController { @PostMapping(value = "/conference", params = "addDeadline") public String addDeadline(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { - filterEmptyDeadlines(conferenceDto); + conferenceService.filterEmptyDeadlines(conferenceDto); if (errors.hasErrors()) { return CONFERENCE_PAGE; } - conferenceDto.getDeadlines().add(new Deadline()); + conferenceService.addDeadline(conferenceDto); return CONFERENCE_PAGE; } @@ -173,9 +165,4 @@ public class ConferenceController { return years; } - private void filterEmptyDeadlines(ConferenceDto conferenceDto) { - conferenceDto.setDeadlines(conferenceDto.getDeadlines().stream() - .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) - .collect(Collectors.toList())); - } } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java b/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java index 9449b12..ddb8130 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceNotificationService.java @@ -8,6 +8,7 @@ import ru.ulstu.ping.service.PingService; import ru.ulstu.user.service.MailService; import ru.ulstu.user.service.UserService; +import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; @@ -15,7 +16,7 @@ import java.util.Map; @Service public class ConferenceNotificationService { - private final static int YESTERDAY = 0; + private final static int YESTERDAY = -1; private final static int DAYS_TO_DEADLINE_NOTIFICATION = 7; private final static String TEMPLATE_PING = "conferencePingNotification"; private final static String TEMPLATE_DEADLINE = "conferenceDeadlineNotification"; @@ -41,24 +42,23 @@ public class ConferenceNotificationService { this.pingService = pingService; } - public void sendDeadlineNotifications(List conferences, boolean isDeadlineBeforeWeek) { + public void sendDeadlineNotifications(List conferences) { Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION); conferences .stream() - .filter(conference -> needToSendDeadlineNotification(conference, now, isDeadlineBeforeWeek)) + .filter(conference -> needToSendDeadlineNotification(conference, now)) .forEach(this::sendMessageDeadline); } - private boolean needToSendDeadlineNotification(Conference conference, Date compareDate, boolean isDeadlineBeforeWeek) { + private boolean needToSendDeadlineNotification(Conference conference, Date compareDate) { 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()); + && conference.getNextDeadline().get().getDate().after(new Date()) + && conference.getNextDeadline().get().getDate().before(compareDate); } private void sendMessageDeadline(Conference conference) { Map variables = ImmutableMap.of("conference", conference); - sendForAllParticipals(variables, conference, TEMPLATE_DEADLINE, TITLE_DEADLINE); + sendForAllParticipants(variables, conference, TEMPLATE_DEADLINE, String.format(TITLE_DEADLINE, conference.getTitle())); } public void sendCreateNotification(Conference conference) { @@ -68,29 +68,31 @@ public class ConferenceNotificationService { public void updateDeadlineNotification(Conference conference) { Map variables = ImmutableMap.of("conference", conference); - sendForAllParticipals(variables, conference, TEMPLATE_UPDATE_DEADLINES, String.format(TITLE_UPDATE_DEADLINES, conference.getTitle())); + sendForAllParticipants(variables, conference, TEMPLATE_UPDATE_DEADLINES, String.format(TITLE_UPDATE_DEADLINES, conference.getTitle())); } public void updateConferencesDatesNotification(Conference conference, Date oldBeginDate, Date oldEndDate) { Map variables = ImmutableMap.of("conference", conference, "oldBeginDate", oldBeginDate, "oldEndDate", oldEndDate); - sendForAllParticipals(variables, conference, TEMPLATE_UPDATE_DATES, String.format(TITLE_UPDATE_DATES, conference.getTitle())); + sendForAllParticipants(variables, conference, TEMPLATE_UPDATE_DATES, String.format(TITLE_UPDATE_DATES, conference.getTitle())); } 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) { + private void sendForAllParticipants(Map variables, Conference conference, String template, String title) { conference.getUsers().forEach(conferenceUser -> mailService.sendEmailFromTemplate(variables, conferenceUser.getUser(), template, title)); } public void sendPingNotifications(List conferences) { - Date yesterday = DateUtils.addDays(new Date(), YESTERDAY); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(new Date()); + calendar.add(Calendar.DAY_OF_MONTH, YESTERDAY); conferences .stream() .filter(conference -> { - Integer pingCount = pingService.countPingYesterday(conference, yesterday); + Integer pingCount = pingService.countPingYesterday(conference, calendar); return needToSendPingNotification(conference, pingCount); }) .forEach(this::sendMessagePing); @@ -106,6 +108,6 @@ public class ConferenceNotificationService { private void sendMessagePing(Conference conference) { Map variables = ImmutableMap.of("conference", conference); - sendForAllParticipals(variables, conference, TEMPLATE_PING, String.format(TITLE_PING, conference.getTitle())); + sendForAllParticipants(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 f55f885..6f8fe90 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceScheduler.java @@ -24,17 +24,10 @@ public class ConferenceScheduler { @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); + conferenceNotificationService.sendDeadlineNotifications(conferenceService.findAll()); log.debug("ConferenceScheduler.checkDeadlineBeforeWeek finished"); } - @Scheduled(cron = "0 0 8 * * ?", zone = "Europe/Samara") - public void checkDeadlineAfterWeek() { - log.debug("ConferenceScheduler.checkDeadlineAfterWeek started"); - 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"); diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 0c1d37d..38de435 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -5,6 +5,7 @@ import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.ModelMap; +import org.springframework.validation.Errors; import ru.ulstu.conference.model.Conference; import ru.ulstu.conference.model.ConferenceDto; import ru.ulstu.conference.model.ConferenceFilterDto; @@ -115,7 +116,10 @@ public class ConferenceService { Date oldEndDate = conference.getEndDate(); conferenceRepository.save(copyFromDto(conference, conferenceDto)); eventService.updateConferenceDeadlines(conference); - sendNotificationAfterUpdate(conference, oldDeadlines, oldBeginDate, oldEndDate); + sendNotificationAfterUpdateDeadlines(conference, oldDeadlines); + if (!conference.getBeginDate().equals(oldBeginDate) || !conference.getEndDate().equals(oldEndDate)) { + conferenceNotificationService.updateConferencesDatesNotification(conference, oldBeginDate, oldEndDate); + } conferenceDto.getRemovedDeadlineIds().forEach(deadlineService::remove); return conference.getId(); } @@ -127,6 +131,11 @@ public class ConferenceService { } } + public void addDeadline(ConferenceDto conferenceDto) { + conferenceDto.getDeadlines().add(new Deadline()); + } + + public void removeDeadline(ConferenceDto conferenceDto, Integer deadlineIndex) throws IOException { if (conferenceDto.getDeadlines().get(deadlineIndex).getId() != null) { conferenceDto.getRemovedDeadlineIds().add(conferenceDto.getDeadlines().get(deadlineIndex).getId()); @@ -243,29 +252,17 @@ public class ConferenceService { modelMap.addAttribute("offshoreSales", offshoreSales); } - public void sendNotificationAfterUpdate(Conference conference, List oldDeadlines, Date oldBeginDate, Date oldEndDate) { - boolean isSendNotificationAboutDeadlines = false; + public void sendNotificationAfterUpdateDeadlines(Conference conference, List oldDeadlines) { if (oldDeadlines.size() != conference.getDeadlines().size()) { - isSendNotificationAboutDeadlines = true; - } - for (Deadline deadline : conference.getDeadlines()) { - if (isSendNotificationAboutDeadlines) { - break; - } - for (Deadline oldDeadline : oldDeadlines) { - if (deadline.getId().equals(oldDeadline.getId())) { - if (!deadline.getDescription().equals(oldDeadline.getDescription()) || !deadline.getDate().equals(oldDeadline.getDate())) { - isSendNotificationAboutDeadlines = true; - break; - } - } - } - } - if (isSendNotificationAboutDeadlines) { conferenceNotificationService.updateDeadlineNotification(conference); + return; } - if (!conference.getBeginDate().equals(oldBeginDate) || !conference.getEndDate().equals(oldEndDate)) { - conferenceNotificationService.updateConferencesDatesNotification(conference, oldBeginDate, oldEndDate); + + if (conference.getDeadlines() + .stream() + .filter(deadline -> !oldDeadlines.contains(deadline)) + .count() > 0) { + conferenceNotificationService.updateDeadlineNotification(conference); } } @@ -274,4 +271,19 @@ public class ConferenceService { newDeadline.setId(oldDeadline.getId()); return newDeadline; } + + public void checkEmptyFieldsOfDeadline(ConferenceDto conferenceDto, Errors errors) { + for (Deadline deadline : conferenceDto.getDeadlines()) { + if (deadline.getDate() == null || deadline.getDescription().isEmpty()) { + errors.rejectValue("deadlines", "errorCode", "Все поля дедлайна должны быть заполнены"); + } + } + } + + + public void filterEmptyDeadlines(ConferenceDto conferenceDto) { + conferenceDto.setDeadlines(conferenceDto.getDeadlines().stream() + .filter(dto -> dto.getDate() != null || !org.springframework.util.StringUtils.isEmpty(dto.getDescription())) + .collect(Collectors.toList())); + } } diff --git a/src/main/java/ru/ulstu/deadline/model/Deadline.java b/src/main/java/ru/ulstu/deadline/model/Deadline.java index 404e5c8..199427c 100644 --- a/src/main/java/ru/ulstu/deadline/model/Deadline.java +++ b/src/main/java/ru/ulstu/deadline/model/Deadline.java @@ -51,4 +51,21 @@ public class Deadline extends BaseEntity { public void setDate(Date date) { this.date = date; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + if (!super.equals(o)) { + return false; + } + Deadline deadline = (Deadline) o; + return getId().equals(deadline.getId()) && + description.equals(deadline.description) && + date.equals(deadline.date); + } } diff --git a/src/main/java/ru/ulstu/ping/service/PingService.java b/src/main/java/ru/ulstu/ping/service/PingService.java index f24ed9d..f7156f0 100644 --- a/src/main/java/ru/ulstu/ping/service/PingService.java +++ b/src/main/java/ru/ulstu/ping/service/PingService.java @@ -29,11 +29,8 @@ public class PingService { 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, cal.get(Calendar.YEAR))); + public Integer countPingYesterday(Conference conference, Calendar calendar) { + return Math.toIntExact(pingRepository.countByConferenceAndDate(conference, calendar.get(Calendar.DAY_OF_MONTH), + calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.YEAR))); } } diff --git a/src/main/java/ru/ulstu/timeline/model/Event.java b/src/main/java/ru/ulstu/timeline/model/Event.java index f53a52d..9409f21 100644 --- a/src/main/java/ru/ulstu/timeline/model/Event.java +++ b/src/main/java/ru/ulstu/timeline/model/Event.java @@ -19,6 +19,7 @@ import javax.persistence.OneToMany; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.validation.constraints.NotNull; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -63,7 +64,7 @@ public class Event extends BaseEntity { private String description; @ManyToMany(fetch = FetchType.EAGER) - private List recipients; + private List recipients = new ArrayList(); @ManyToOne @JoinColumn(name = "child_id") diff --git a/src/main/java/ru/ulstu/timeline/service/EventService.java b/src/main/java/ru/ulstu/timeline/service/EventService.java index f4e2873..d2fa510 100644 --- a/src/main/java/ru/ulstu/timeline/service/EventService.java +++ b/src/main/java/ru/ulstu/timeline/service/EventService.java @@ -159,7 +159,7 @@ public class EventService { newEvent.setDescription("Дедлайн '" + deadline.getDescription() + "' конференции '" + newConference.getTitle() + "'"); newConference.getUsers().forEach(conferenceUser -> newEvent.getRecipients().add(conferenceUser.getUser())); newEvent.setConference(newConference); - eventRepository.save(newEvent); + save(newEvent); timeline.getEvents().add(newEvent); timelineService.save(timeline); From 6350e1c0d635ec76a6fe949ec01ab45c71c1b4b7 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Thu, 2 May 2019 14:51:22 +0400 Subject: [PATCH 11/11] #70 add hashCode for deadline --- src/main/java/ru/ulstu/deadline/model/Deadline.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/ru/ulstu/deadline/model/Deadline.java b/src/main/java/ru/ulstu/deadline/model/Deadline.java index 199427c..148697c 100644 --- a/src/main/java/ru/ulstu/deadline/model/Deadline.java +++ b/src/main/java/ru/ulstu/deadline/model/Deadline.java @@ -9,6 +9,7 @@ import javax.persistence.Entity; import javax.persistence.Temporal; import javax.persistence.TemporalType; import java.util.Date; +import java.util.Objects; @Entity public class Deadline extends BaseEntity { @@ -68,4 +69,9 @@ public class Deadline extends BaseEntity { description.equals(deadline.description) && date.equals(deadline.date); } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), description, date); + } }