From 67881262beb515d7eb6fb4df9a96a3f53f0ef8fe Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 9 Nov 2018 10:04:50 +0400 Subject: [PATCH] add notification templates --- .../ru/ulstu/core/model/UserContainer.java | 4 +- .../ru/ulstu/core/util/StreamApiUtils.java | 9 ++- .../paper/controller/PaperController.java | 5 +- src/main/java/ru/ulstu/paper/model/Paper.java | 22 ++++++-- .../java/ru/ulstu/paper/model/PaperDto.java | 16 ++++-- .../paper/service/DeadlineScheduler.java | 11 ++-- .../service/PaperNotificationService.java | 40 +++++++------ .../ru/ulstu/paper/service/PaperService.java | 56 ++++++++++--------- .../ru/ulstu/user/service/UserService.java | 4 ++ .../paperDeadlineNotification.html | 24 ++++++++ .../paperStatusChangeNotification.html | 22 ++++++++ 11 files changed, 152 insertions(+), 61 deletions(-) create mode 100644 src/main/resources/mail_templates/paperDeadlineNotification.html create mode 100644 src/main/resources/mail_templates/paperStatusChangeNotification.html diff --git a/src/main/java/ru/ulstu/core/model/UserContainer.java b/src/main/java/ru/ulstu/core/model/UserContainer.java index 87dc97d..7f83cd3 100644 --- a/src/main/java/ru/ulstu/core/model/UserContainer.java +++ b/src/main/java/ru/ulstu/core/model/UserContainer.java @@ -2,8 +2,8 @@ package ru.ulstu.core.model; import ru.ulstu.user.model.User; -import java.util.List; +import java.util.Set; public interface UserContainer { - List getUsers(); + Set getUsers(); } diff --git a/src/main/java/ru/ulstu/core/util/StreamApiUtils.java b/src/main/java/ru/ulstu/core/util/StreamApiUtils.java index e1fb55a..ab8d66f 100644 --- a/src/main/java/ru/ulstu/core/util/StreamApiUtils.java +++ b/src/main/java/ru/ulstu/core/util/StreamApiUtils.java @@ -2,6 +2,7 @@ package ru.ulstu.core.util; import java.util.Collections; import java.util.List; +import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; @@ -9,7 +10,13 @@ public class StreamApiUtils { public static List convert(List entities, Function converter) { return entities == null - ? Collections.EMPTY_LIST + ? Collections.emptyList() : entities.stream().map(e -> converter.apply(e)).collect(Collectors.toList()); } + + public static Set convert(Set entities, Function converter) { + return entities == null + ? Collections.emptySet() + : entities.stream().map(e -> converter.apply(e)).collect(Collectors.toSet()); + } } diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index ccf0dda..e5acd23 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -14,6 +14,7 @@ import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperStatusDto; import ru.ulstu.paper.service.PaperService; +import javax.validation.Valid; import java.io.IOException; import java.util.List; @@ -36,12 +37,12 @@ public class PaperController { } @PostMapping - public Response createPaper(@RequestBody PaperDto paperDto) throws IOException { + public Response createPaper(@RequestBody @Valid PaperDto paperDto) throws IOException { return new Response(paperService.create(paperDto)); } @PutMapping - public Response updatePaper(@RequestBody PaperDto paperDto) throws IOException { + public Response updatePaper(@RequestBody @Valid PaperDto paperDto) throws IOException { return new Response(paperService.update(paperDto)); } diff --git a/src/main/java/ru/ulstu/paper/model/Paper.java b/src/main/java/ru/ulstu/paper/model/Paper.java index 6b16ee3..e9cc92c 100644 --- a/src/main/java/ru/ulstu/paper/model/Paper.java +++ b/src/main/java/ru/ulstu/paper/model/Paper.java @@ -6,10 +6,19 @@ import ru.ulstu.core.model.UserContainer; import ru.ulstu.file.model.FileData; import ru.ulstu.user.model.User; -import javax.persistence.*; -import java.util.ArrayList; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.FetchType; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; +import javax.validation.constraints.NotNull; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; @Entity public class Paper extends BaseEntity implements UserContainer { @@ -40,6 +49,7 @@ public class Paper extends BaseEntity implements UserContainer { private Date updateDate = new Date(); @Column(name = "deadline_date") + @NotNull private Date deadlineDate; private String comment; @@ -51,7 +61,7 @@ public class Paper extends BaseEntity implements UserContainer { private FileData fileData; @ManyToMany(fetch = FetchType.EAGER) - private List authors = new ArrayList<>(); + private Set authors = new HashSet<>(); public PaperStatus getStatus() { return status; @@ -117,16 +127,16 @@ public class Paper extends BaseEntity implements UserContainer { this.title = title; } - public List getAuthors() { + public Set getAuthors() { return authors; } - public void setAuthors(List authors) { + public void setAuthors(Set authors) { this.authors = authors; } @Override - public List getUsers() { + public Set getUsers() { return getAuthors(); } } diff --git a/src/main/java/ru/ulstu/paper/model/PaperDto.java b/src/main/java/ru/ulstu/paper/model/PaperDto.java index aeb4e3f..dc48fcd 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperDto.java @@ -2,19 +2,23 @@ package ru.ulstu.paper.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; +import org.hibernate.validator.constraints.NotEmpty; import ru.ulstu.user.model.UserDto; +import javax.validation.constraints.NotNull; import java.util.Date; -import java.util.List; +import java.util.Set; import static ru.ulstu.core.util.StreamApiUtils.convert; public class PaperDto { private final Integer id; + @NotEmpty private final String title; private final Paper.PaperStatus status; private final Date createDate; private final Date updateDate; + @NotNull private final Date deadlineDate; private final String comment; private final Boolean locked; @@ -22,7 +26,7 @@ public class PaperDto { private final Integer fileId; private final String fileName; private final Date fileCreateDate; - private final List authors; + private final Set authors; @JsonCreator public PaperDto(@JsonProperty("id") Integer id, @@ -34,7 +38,7 @@ public class PaperDto { @JsonProperty("comment") String comment, @JsonProperty("locked") Boolean locked, @JsonProperty("tmpFileName") String tmpFileName, - @JsonProperty("authors") List authors) { + @JsonProperty("authors") Set authors) { this.id = id; this.title = title; this.status = status; @@ -86,7 +90,9 @@ public class PaperDto { return updateDate; } - public Date getDeadlineDate() {return deadlineDate;} + public Date getDeadlineDate() { + return deadlineDate; + } public String getComment() { return comment; @@ -112,7 +118,7 @@ public class PaperDto { return fileCreateDate; } - public List getAuthors() { + public Set getAuthors() { return authors; } } diff --git a/src/main/java/ru/ulstu/paper/service/DeadlineScheduler.java b/src/main/java/ru/ulstu/paper/service/DeadlineScheduler.java index af850a6..d6d9852 100644 --- a/src/main/java/ru/ulstu/paper/service/DeadlineScheduler.java +++ b/src/main/java/ru/ulstu/paper/service/DeadlineScheduler.java @@ -12,23 +12,26 @@ public class DeadlineScheduler { private final Logger log = LoggerFactory.getLogger(DeadlineScheduler.class); private final PaperNotificationService paperNotificationService; + private final PaperService paperService; - public DeadlineScheduler(PaperNotificationService paperNotificationService) { + public DeadlineScheduler(PaperNotificationService paperNotificationService, + PaperService paperService) { this.paperNotificationService = paperNotificationService; + this.paperService = paperService; } @Scheduled(cron = "0 0 8 * 1 ?") public void checkDeadlineBeforeWeek() { log.debug("DeadlineScheduler.checkDeadlineBeforeWeek started"); - paperNotificationService.sendDeadlineNotifications(!IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); + paperNotificationService.sendDeadlineNotifications(paperService.findAll(), !IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); log.debug("DeadlineScheduler.checkDeadlineBeforeWeek finished"); } - @Scheduled(cron = "0 */2 9 * * ?") + @Scheduled(cron = "0 0 8 * * ?") public void checkDeadlineAfterWeek() { log.debug("DeadlineScheduler.checkDeadlineAfterWeek started"); - paperNotificationService.sendDeadlineNotifications(IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); + paperNotificationService.sendDeadlineNotifications(paperService.findAll(), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); log.debug("DeadlineScheduler.checkDeadlineAfterWeek finished"); } } diff --git a/src/main/java/ru/ulstu/paper/service/PaperNotificationService.java b/src/main/java/ru/ulstu/paper/service/PaperNotificationService.java index fc5dd27..736ae87 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperNotificationService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperNotificationService.java @@ -1,13 +1,14 @@ package ru.ulstu.paper.service; +import com.google.common.collect.ImmutableMap; import org.springframework.stereotype.Service; import ru.ulstu.core.util.DateUtils; import ru.ulstu.paper.model.Paper; -import ru.ulstu.user.model.User; import ru.ulstu.user.service.MailService; import java.util.Date; -import java.util.stream.Collectors; +import java.util.List; +import java.util.Map; @Service public class PaperNotificationService { @@ -15,22 +16,16 @@ public class PaperNotificationService { private final MailService mailService; - private final PaperService paperService; - - public PaperNotificationService(MailService mailService, - PaperService paperService) { + public PaperNotificationService(MailService mailService) { this.mailService = mailService; - this.paperService = paperService; } - public void sendDeadlineNotifications(boolean isDeadlineBeforeWeek) { + public void sendDeadlineNotifications(List papers, boolean isDeadlineBeforeWeek) { Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION); - paperService - .findAll() + papers .stream() .filter(paper -> needToSendDeadlineNotification(paper, now, isDeadlineBeforeWeek)) - .map(paper -> sendMessageDeadline(paper)) - .collect(Collectors.toList()); + .forEach(paper -> sendMessageDeadline(paper)); } private boolean needToSendDeadlineNotification(Paper paper, Date compareDate, boolean isDeadlineBeforeWeek) { @@ -39,11 +34,24 @@ public class PaperNotificationService { || compareDate.before(paper.getDeadlineDate()) && !isDeadlineBeforeWeek); } - private Paper sendMessageDeadline(Paper paper) { - for (User user : paper.getAuthors()) { + private void sendMessageDeadline(Paper paper) { + paper.getAuthors().forEach(user -> { mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи", "Срок сдачи статьи " + paper.getTitle() + " " + paper.getDeadlineDate().toString()); - } - return paper; + }); + } + + public void sendCreateNotification(Paper paper) { + Map variables = ImmutableMap.of("paper", paper); + paper.getAuthors().forEach(author -> { + mailService.sendEmailFromTemplate(variables, author, "paperCreateNotification", "Создана статья"); + }); + } + + public void statusChangeNotification(Paper paper, Paper.PaperStatus oldStatus) { + Map variables = ImmutableMap.of("paper", paper, "oldStatus", oldStatus); + paper.getAuthors().forEach(author -> { + mailService.sendEmailFromTemplate(variables, author, "paperStatusChangeNotification", "Изменился статус статьи"); + }); } } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 6c4cb1d..a5b5237 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -1,6 +1,5 @@ package ru.ulstu.paper.service; -import com.google.common.collect.ImmutableMap; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import ru.ulstu.file.service.FileService; @@ -9,30 +8,32 @@ import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperStatusDto; import ru.ulstu.paper.repository.PaperRepository; import ru.ulstu.user.model.User; -import ru.ulstu.user.model.UserDto; -import ru.ulstu.user.service.MailService; +import ru.ulstu.user.service.UserService; import java.io.IOException; import java.util.Arrays; import java.util.Date; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; import static ru.ulstu.core.util.StreamApiUtils.convert; @Service public class PaperService { - public final MailService mailService; + private final PaperNotificationService paperNotificationService; private final PaperRepository paperRepository; + private final UserService userService; private final FileService fileService; public PaperService(PaperRepository paperRepository, - FileService fileService, MailService mailService) { + FileService fileService, + PaperNotificationService paperNotificationService, + UserService userService) { this.paperRepository = paperRepository; this.fileService = fileService; - this.mailService = mailService; + this.paperNotificationService = paperNotificationService; + this.userService = userService; } public List findAll() { @@ -53,32 +54,45 @@ public class PaperService { @Transactional public int create(PaperDto paperDto) throws IOException { - return paperRepository.save(copyFromDto(new Paper(), paperDto)).getId(); + Paper newPaper = copyFromDto(new Paper(), paperDto); + newPaper = paperRepository.save(newPaper); + paperNotificationService.sendCreateNotification(newPaper); + return newPaper.getId(); } private Paper copyFromDto(Paper paper, PaperDto paperDto) throws IOException { paper.setComment(paperDto.getComment()); - paper.setCreateDate(paperDto.getCreateDate()); + paper.setCreateDate(paper.getCreateDate() == null ? new Date() : paper.getCreateDate()); paper.setLocked(paperDto.getLocked()); - paper.setStatus(paperDto.getStatus()); + paper.setStatus(paperDto.getStatus() == null ? Paper.PaperStatus.DRAFT : paperDto.getStatus()); paper.setTitle(paperDto.getTitle()); - paper.setUpdateDate(paperDto.getUpdateDate()); + paper.setUpdateDate(new Date()); + paper.setDeadlineDate(paperDto.getDeadlineDate()); if (paperDto.getTmpFileName() != null) { paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName())); } + if (paperDto.getAuthors() != null && !paperDto.getAuthors().isEmpty()) { + paperDto.getAuthors().forEach(authorIds -> { + paper.getAuthors().add(userService.findById(authorIds.getId())); + }); + } return paper; } @Transactional public Integer update(PaperDto paperDto) throws IOException { Paper paper = paperRepository.findOne(paperDto.getId()); - if (paper != null && paper.getStatus() != paperDto.getStatus()) { - sendMessageAboutStatusChange(paper.getStatus(), paperDto); - } + Paper.PaperStatus oldStatus = paper.getStatus(); if (paperDto.getTmpFileName() != null && paper.getFileData() != null) { fileService.deleteFile(paper.getFileData()); } - return paperRepository.save(copyFromDto(paper, paperDto)).getId(); + paperRepository.save(copyFromDto(paper, paperDto)); + + if (paper.getStatus() != oldStatus) { + paperNotificationService.statusChangeNotification(paper, oldStatus); + } + + return paper.getId(); } @Transactional @@ -94,14 +108,6 @@ public class PaperService { return convert(Arrays.asList(Paper.PaperStatus.values()), status -> new PaperStatusDto(status)); } - private void sendMessageAboutStatusChange(Paper.PaperStatus oldStatus, PaperDto paper) { - for (UserDto user : paper.getAuthors()) { - mailService.sendEmail(user.getEmail(), "Обновление статуса статьи", - "Статус статьи " + paper.getTitle() + " сменился с " + oldStatus.getName() - + " на " + paper.getStatus().getName()); - } - } - @Transactional public Paper create(String title, User user, Date deadlineDate) { Paper paper = new Paper(); @@ -113,8 +119,8 @@ public class PaperService { paper.setStatus(Paper.PaperStatus.DRAFT); paper = paperRepository.save(paper); - Map variables = ImmutableMap.of("paper", paper); - mailService.sendEmailFromTemplate(variables, user, "paperCreateNotification", "Создана статья"); + paperNotificationService.sendCreateNotification(paper); + return paper; } } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index 61fe72b..e6fca9f 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -291,4 +291,8 @@ public class UserService implements UserDetailsService { public List findByIds(List ids) { return userRepository.findAll(ids); } + + public User findById(Integer id) { + return userRepository.findOne(id); + } } diff --git a/src/main/resources/mail_templates/paperDeadlineNotification.html b/src/main/resources/mail_templates/paperDeadlineNotification.html new file mode 100644 index 0000000..ee63f3b --- /dev/null +++ b/src/main/resources/mail_templates/paperDeadlineNotification.html @@ -0,0 +1,24 @@ + + + + Уведомление дедлайне статьи + + + + +

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

+

+ Приближается срок сдачи статьи "Title". +

+

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

+

+ Regards, +
+ NG-tracker. +

+ + diff --git a/src/main/resources/mail_templates/paperStatusChangeNotification.html b/src/main/resources/mail_templates/paperStatusChangeNotification.html new file mode 100644 index 0000000..15db20a --- /dev/null +++ b/src/main/resources/mail_templates/paperStatusChangeNotification.html @@ -0,0 +1,22 @@ + + + + Уведомление о смене статуса статьи + + + + +

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

+

+ Статус статьи "Title" сменился с + "oldStatus" на "newStatus". +

+

+ Regards, +
+ NG-tracker. +

+ +