From 60bdd9d233292b7f778b271d10cfa95b1fb70ddf Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Thu, 2 May 2019 14:35:40 +0400 Subject: [PATCH] #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);