This commit is contained in:
Nightblade73 2019-05-02 14:35:40 +04:00
parent 0e062f1337
commit 60bdd9d233
8 changed files with 77 additions and 68 deletions

View File

@ -13,7 +13,6 @@ import ru.ulstu.conference.model.ConferenceDto;
import ru.ulstu.conference.model.ConferenceFilterDto; import ru.ulstu.conference.model.ConferenceFilterDto;
import ru.ulstu.conference.model.ConferenceUser; import ru.ulstu.conference.model.ConferenceUser;
import ru.ulstu.conference.service.ConferenceService; import ru.ulstu.conference.service.ConferenceService;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
@ -22,9 +21,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; 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.CONFERENCES_PAGE;
import static ru.ulstu.core.controller.Navigation.CONFERENCE_PAGE; import static ru.ulstu.core.controller.Navigation.CONFERENCE_PAGE;
import static ru.ulstu.core.controller.Navigation.REDIRECT_TO; import static ru.ulstu.core.controller.Navigation.REDIRECT_TO;
@ -77,13 +74,8 @@ public class ConferenceController {
@PostMapping(value = "/conference", params = "save") @PostMapping(value = "/conference", params = "save")
public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException {
filterEmptyDeadlines(conferenceDto); conferenceService.filterEmptyDeadlines(conferenceDto);
for (Deadline deadline : conferenceDto.getDeadlines()) { conferenceService.checkEmptyFieldsOfDeadline(conferenceDto, errors);
if (deadline.getDate() == null || deadline.getDescription().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Все поля дедлайна должны быть заполнены");
return CONFERENCE_PAGE;
}
}
if (errors.hasErrors()) { if (errors.hasErrors()) {
return CONFERENCE_PAGE; return CONFERENCE_PAGE;
} }
@ -93,11 +85,11 @@ public class ConferenceController {
@PostMapping(value = "/conference", params = "addDeadline") @PostMapping(value = "/conference", params = "addDeadline")
public String addDeadline(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { public String addDeadline(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException {
filterEmptyDeadlines(conferenceDto); conferenceService.filterEmptyDeadlines(conferenceDto);
if (errors.hasErrors()) { if (errors.hasErrors()) {
return CONFERENCE_PAGE; return CONFERENCE_PAGE;
} }
conferenceDto.getDeadlines().add(new Deadline()); conferenceService.addDeadline(conferenceDto);
return CONFERENCE_PAGE; return CONFERENCE_PAGE;
} }
@ -173,9 +165,4 @@ public class ConferenceController {
return years; return years;
} }
private void filterEmptyDeadlines(ConferenceDto conferenceDto) {
conferenceDto.setDeadlines(conferenceDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))
.collect(Collectors.toList()));
}
} }

View File

@ -8,6 +8,7 @@ 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;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -15,7 +16,7 @@ import java.util.Map;
@Service @Service
public class ConferenceNotificationService { 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 int DAYS_TO_DEADLINE_NOTIFICATION = 7;
private final static String TEMPLATE_PING = "conferencePingNotification"; private final static String TEMPLATE_PING = "conferencePingNotification";
private final static String TEMPLATE_DEADLINE = "conferenceDeadlineNotification"; private final static String TEMPLATE_DEADLINE = "conferenceDeadlineNotification";
@ -41,24 +42,23 @@ public class ConferenceNotificationService {
this.pingService = pingService; this.pingService = pingService;
} }
public void sendDeadlineNotifications(List<Conference> conferences, boolean isDeadlineBeforeWeek) { public void sendDeadlineNotifications(List<Conference> conferences) {
Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION); Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION);
conferences conferences
.stream() .stream()
.filter(conference -> needToSendDeadlineNotification(conference, now, isDeadlineBeforeWeek)) .filter(conference -> needToSendDeadlineNotification(conference, now))
.forEach(this::sendMessageDeadline); .forEach(this::sendMessageDeadline);
} }
private boolean needToSendDeadlineNotification(Conference conference, Date compareDate, boolean isDeadlineBeforeWeek) { private boolean needToSendDeadlineNotification(Conference conference, Date compareDate) {
return (conference.getNextDeadline().isPresent()) return (conference.getNextDeadline().isPresent())
&& (compareDate.before(conference.getNextDeadline().get().getDate()) && isDeadlineBeforeWeek && conference.getNextDeadline().get().getDate().after(new Date())
|| compareDate.after(conference.getNextDeadline().get().getDate()) && !isDeadlineBeforeWeek) && conference.getNextDeadline().get().getDate().before(compareDate);
&& conference.getNextDeadline().get().getDate().after(new Date());
} }
private void sendMessageDeadline(Conference conference) { private void sendMessageDeadline(Conference conference) {
Map<String, Object> variables = ImmutableMap.of("conference", conference); Map<String, Object> 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) { public void sendCreateNotification(Conference conference) {
@ -68,29 +68,31 @@ public class ConferenceNotificationService {
public void updateDeadlineNotification(Conference conference) { public void updateDeadlineNotification(Conference conference) {
Map<String, Object> variables = ImmutableMap.of("conference", conference); Map<String, Object> 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) { public void updateConferencesDatesNotification(Conference conference, Date oldBeginDate, Date oldEndDate) {
Map<String, Object> variables = ImmutableMap.of("conference", conference, "oldBeginDate", oldBeginDate, "oldEndDate", oldEndDate); Map<String, Object> 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<String, Object> variables, String template, String title) { private void sendForAllUsers(Map<String, Object> variables, String template, String title) {
userService.findAll().forEach(user -> mailService.sendEmailFromTemplate(variables, user, template, title)); userService.findAll().forEach(user -> mailService.sendEmailFromTemplate(variables, user, template, title));
} }
private void sendForAllParticipals(Map<String, Object> variables, Conference conference, String template, String title) { private void sendForAllParticipants(Map<String, Object> variables, Conference conference, String template, String title) {
conference.getUsers().forEach(conferenceUser -> mailService.sendEmailFromTemplate(variables, conferenceUser.getUser(), template, title)); conference.getUsers().forEach(conferenceUser -> mailService.sendEmailFromTemplate(variables, conferenceUser.getUser(), template, title));
} }
public void sendPingNotifications(List<Conference> conferences) { public void sendPingNotifications(List<Conference> conferences) {
Date yesterday = DateUtils.addDays(new Date(), YESTERDAY); Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_MONTH, YESTERDAY);
conferences conferences
.stream() .stream()
.filter(conference -> { .filter(conference -> {
Integer pingCount = pingService.countPingYesterday(conference, yesterday); Integer pingCount = pingService.countPingYesterday(conference, calendar);
return needToSendPingNotification(conference, pingCount); return needToSendPingNotification(conference, pingCount);
}) })
.forEach(this::sendMessagePing); .forEach(this::sendMessagePing);
@ -106,6 +108,6 @@ public class ConferenceNotificationService {
private void sendMessagePing(Conference conference) { private void sendMessagePing(Conference conference) {
Map<String, Object> variables = ImmutableMap.of("conference", conference); Map<String, Object> 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()));
} }
} }

View File

@ -24,17 +24,10 @@ public class ConferenceScheduler {
@Scheduled(cron = "0 0 8 * * MON", 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());
log.debug("ConferenceScheduler.checkDeadlineBeforeWeek finished"); 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") @Scheduled(cron = "0 0 8 * * *", zone = "Europe/Samara")
public void checkNewPing() { public void checkNewPing() {
log.debug("ConferenceScheduler.checkPing started"); log.debug("ConferenceScheduler.checkPing started");

View File

@ -5,6 +5,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import ru.ulstu.conference.model.Conference; import ru.ulstu.conference.model.Conference;
import ru.ulstu.conference.model.ConferenceDto; import ru.ulstu.conference.model.ConferenceDto;
import ru.ulstu.conference.model.ConferenceFilterDto; import ru.ulstu.conference.model.ConferenceFilterDto;
@ -115,7 +116,10 @@ public class ConferenceService {
Date oldEndDate = conference.getEndDate(); Date oldEndDate = conference.getEndDate();
conferenceRepository.save(copyFromDto(conference, conferenceDto)); conferenceRepository.save(copyFromDto(conference, conferenceDto));
eventService.updateConferenceDeadlines(conference); 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); conferenceDto.getRemovedDeadlineIds().forEach(deadlineService::remove);
return conference.getId(); 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 { public void removeDeadline(ConferenceDto conferenceDto, Integer deadlineIndex) throws IOException {
if (conferenceDto.getDeadlines().get(deadlineIndex).getId() != null) { if (conferenceDto.getDeadlines().get(deadlineIndex).getId() != null) {
conferenceDto.getRemovedDeadlineIds().add(conferenceDto.getDeadlines().get(deadlineIndex).getId()); conferenceDto.getRemovedDeadlineIds().add(conferenceDto.getDeadlines().get(deadlineIndex).getId());
@ -243,29 +252,17 @@ public class ConferenceService {
modelMap.addAttribute("offshoreSales", offshoreSales); modelMap.addAttribute("offshoreSales", offshoreSales);
} }
public void sendNotificationAfterUpdate(Conference conference, List<Deadline> oldDeadlines, Date oldBeginDate, Date oldEndDate) { public void sendNotificationAfterUpdateDeadlines(Conference conference, List<Deadline> oldDeadlines) {
boolean isSendNotificationAboutDeadlines = false;
if (oldDeadlines.size() != conference.getDeadlines().size()) { 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); 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()); newDeadline.setId(oldDeadline.getId());
return newDeadline; 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()));
}
} }

View File

@ -51,4 +51,21 @@ public class Deadline extends BaseEntity {
public void setDate(Date date) { public void setDate(Date date) {
this.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);
}
} }

View File

@ -29,11 +29,8 @@ public class PingService {
pingRepository.save(newPing); pingRepository.save(newPing);
} }
public Integer countPingYesterday(Conference conference, Date yesterday) { public Integer countPingYesterday(Conference conference, Calendar calendar) {
Calendar cal = Calendar.getInstance(); return Math.toIntExact(pingRepository.countByConferenceAndDate(conference, calendar.get(Calendar.DAY_OF_MONTH),
cal.setTime(yesterday); calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.YEAR)));
return Math.toIntExact(pingRepository.countByConferenceAndDate(conference, cal.get(Calendar.DAY_OF_MONTH),
cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR)));
} }
} }

View File

@ -19,6 +19,7 @@ import javax.persistence.OneToMany;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -63,7 +64,7 @@ public class Event extends BaseEntity {
private String description; private String description;
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.EAGER)
private List<User> recipients; private List<User> recipients = new ArrayList<User>();
@ManyToOne @ManyToOne
@JoinColumn(name = "child_id") @JoinColumn(name = "child_id")

View File

@ -159,7 +159,7 @@ public class EventService {
newEvent.setDescription("Дедлайн '" + deadline.getDescription() + "' конференции '" + newConference.getTitle() + "'"); newEvent.setDescription("Дедлайн '" + deadline.getDescription() + "' конференции '" + newConference.getTitle() + "'");
newConference.getUsers().forEach(conferenceUser -> newEvent.getRecipients().add(conferenceUser.getUser())); newConference.getUsers().forEach(conferenceUser -> newEvent.getRecipients().add(conferenceUser.getUser()));
newEvent.setConference(newConference); newEvent.setConference(newConference);
eventRepository.save(newEvent); save(newEvent);
timeline.getEvents().add(newEvent); timeline.getEvents().add(newEvent);
timelineService.save(timeline); timelineService.save(timeline);