add notification templates

This commit is contained in:
Anton Romanov 2018-11-09 10:04:50 +04:00
parent 2bf84dc286
commit 67881262be
11 changed files with 152 additions and 61 deletions

View File

@ -2,8 +2,8 @@ package ru.ulstu.core.model;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import java.util.List; import java.util.Set;
public interface UserContainer { public interface UserContainer {
List<User> getUsers(); Set<User> getUsers();
} }

View File

@ -2,6 +2,7 @@ package ru.ulstu.core.util;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -9,7 +10,13 @@ public class StreamApiUtils {
public static <T, R> List<T> convert(List<R> entities, Function<R, T> converter) { public static <T, R> List<T> convert(List<R> entities, Function<R, T> converter) {
return entities == null return entities == null
? Collections.EMPTY_LIST ? Collections.emptyList()
: entities.stream().map(e -> converter.apply(e)).collect(Collectors.toList()); : entities.stream().map(e -> converter.apply(e)).collect(Collectors.toList());
} }
public static <T, R> Set<T> convert(Set<R> entities, Function<R, T> converter) {
return entities == null
? Collections.emptySet()
: entities.stream().map(e -> converter.apply(e)).collect(Collectors.toSet());
}
} }

View File

@ -14,6 +14,7 @@ import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.model.PaperStatusDto; import ru.ulstu.paper.model.PaperStatusDto;
import ru.ulstu.paper.service.PaperService; import ru.ulstu.paper.service.PaperService;
import javax.validation.Valid;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -36,12 +37,12 @@ public class PaperController {
} }
@PostMapping @PostMapping
public Response createPaper(@RequestBody PaperDto paperDto) throws IOException { public Response createPaper(@RequestBody @Valid PaperDto paperDto) throws IOException {
return new Response(paperService.create(paperDto)); return new Response(paperService.create(paperDto));
} }
@PutMapping @PutMapping
public Response updatePaper(@RequestBody PaperDto paperDto) throws IOException { public Response updatePaper(@RequestBody @Valid PaperDto paperDto) throws IOException {
return new Response(paperService.update(paperDto)); return new Response(paperService.update(paperDto));
} }

View File

@ -6,10 +6,19 @@ import ru.ulstu.core.model.UserContainer;
import ru.ulstu.file.model.FileData; import ru.ulstu.file.model.FileData;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import javax.persistence.*; import javax.persistence.Column;
import java.util.ArrayList; 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.Date;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
@Entity @Entity
public class Paper extends BaseEntity implements UserContainer { public class Paper extends BaseEntity implements UserContainer {
@ -40,6 +49,7 @@ public class Paper extends BaseEntity implements UserContainer {
private Date updateDate = new Date(); private Date updateDate = new Date();
@Column(name = "deadline_date") @Column(name = "deadline_date")
@NotNull
private Date deadlineDate; private Date deadlineDate;
private String comment; private String comment;
@ -51,7 +61,7 @@ public class Paper extends BaseEntity implements UserContainer {
private FileData fileData; private FileData fileData;
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.EAGER)
private List<User> authors = new ArrayList<>(); private Set<User> authors = new HashSet<>();
public PaperStatus getStatus() { public PaperStatus getStatus() {
return status; return status;
@ -117,16 +127,16 @@ public class Paper extends BaseEntity implements UserContainer {
this.title = title; this.title = title;
} }
public List<User> getAuthors() { public Set<User> getAuthors() {
return authors; return authors;
} }
public void setAuthors(List<User> authors) { public void setAuthors(Set<User> authors) {
this.authors = authors; this.authors = authors;
} }
@Override @Override
public List<User> getUsers() { public Set<User> getUsers() {
return getAuthors(); return getAuthors();
} }
} }

View File

@ -2,19 +2,23 @@ package ru.ulstu.paper.model;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;
import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserDto;
import javax.validation.constraints.NotNull;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.Set;
import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.core.util.StreamApiUtils.convert;
public class PaperDto { public class PaperDto {
private final Integer id; private final Integer id;
@NotEmpty
private final String title; private final String title;
private final Paper.PaperStatus status; private final Paper.PaperStatus status;
private final Date createDate; private final Date createDate;
private final Date updateDate; private final Date updateDate;
@NotNull
private final Date deadlineDate; private final Date deadlineDate;
private final String comment; private final String comment;
private final Boolean locked; private final Boolean locked;
@ -22,7 +26,7 @@ public class PaperDto {
private final Integer fileId; private final Integer fileId;
private final String fileName; private final String fileName;
private final Date fileCreateDate; private final Date fileCreateDate;
private final List<UserDto> authors; private final Set<UserDto> authors;
@JsonCreator @JsonCreator
public PaperDto(@JsonProperty("id") Integer id, public PaperDto(@JsonProperty("id") Integer id,
@ -34,7 +38,7 @@ public class PaperDto {
@JsonProperty("comment") String comment, @JsonProperty("comment") String comment,
@JsonProperty("locked") Boolean locked, @JsonProperty("locked") Boolean locked,
@JsonProperty("tmpFileName") String tmpFileName, @JsonProperty("tmpFileName") String tmpFileName,
@JsonProperty("authors") List<UserDto> authors) { @JsonProperty("authors") Set<UserDto> authors) {
this.id = id; this.id = id;
this.title = title; this.title = title;
this.status = status; this.status = status;
@ -86,7 +90,9 @@ public class PaperDto {
return updateDate; return updateDate;
} }
public Date getDeadlineDate() {return deadlineDate;} public Date getDeadlineDate() {
return deadlineDate;
}
public String getComment() { public String getComment() {
return comment; return comment;
@ -112,7 +118,7 @@ public class PaperDto {
return fileCreateDate; return fileCreateDate;
} }
public List<UserDto> getAuthors() { public Set<UserDto> getAuthors() {
return authors; return authors;
} }
} }

View File

@ -12,23 +12,26 @@ public class DeadlineScheduler {
private final Logger log = LoggerFactory.getLogger(DeadlineScheduler.class); private final Logger log = LoggerFactory.getLogger(DeadlineScheduler.class);
private final PaperNotificationService paperNotificationService; private final PaperNotificationService paperNotificationService;
private final PaperService paperService;
public DeadlineScheduler(PaperNotificationService paperNotificationService) { public DeadlineScheduler(PaperNotificationService paperNotificationService,
PaperService paperService) {
this.paperNotificationService = paperNotificationService; this.paperNotificationService = paperNotificationService;
this.paperService = paperService;
} }
@Scheduled(cron = "0 0 8 * 1 ?") @Scheduled(cron = "0 0 8 * 1 ?")
public void checkDeadlineBeforeWeek() { public void checkDeadlineBeforeWeek() {
log.debug("DeadlineScheduler.checkDeadlineBeforeWeek started"); 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"); log.debug("DeadlineScheduler.checkDeadlineBeforeWeek finished");
} }
@Scheduled(cron = "0 */2 9 * * ?") @Scheduled(cron = "0 0 8 * * ?")
public void checkDeadlineAfterWeek() { public void checkDeadlineAfterWeek() {
log.debug("DeadlineScheduler.checkDeadlineAfterWeek started"); 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"); log.debug("DeadlineScheduler.checkDeadlineAfterWeek finished");
} }
} }

View File

@ -1,13 +1,14 @@
package ru.ulstu.paper.service; package ru.ulstu.paper.service;
import com.google.common.collect.ImmutableMap;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.core.util.DateUtils; import ru.ulstu.core.util.DateUtils;
import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.Paper;
import ru.ulstu.user.model.User;
import ru.ulstu.user.service.MailService; import ru.ulstu.user.service.MailService;
import java.util.Date; import java.util.Date;
import java.util.stream.Collectors; import java.util.List;
import java.util.Map;
@Service @Service
public class PaperNotificationService { public class PaperNotificationService {
@ -15,22 +16,16 @@ public class PaperNotificationService {
private final MailService mailService; private final MailService mailService;
private final PaperService paperService; public PaperNotificationService(MailService mailService) {
public PaperNotificationService(MailService mailService,
PaperService paperService) {
this.mailService = mailService; this.mailService = mailService;
this.paperService = paperService;
} }
public void sendDeadlineNotifications(boolean isDeadlineBeforeWeek) { public void sendDeadlineNotifications(List<Paper> papers, boolean isDeadlineBeforeWeek) {
Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION); Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION);
paperService papers
.findAll()
.stream() .stream()
.filter(paper -> needToSendDeadlineNotification(paper, now, isDeadlineBeforeWeek)) .filter(paper -> needToSendDeadlineNotification(paper, now, isDeadlineBeforeWeek))
.map(paper -> sendMessageDeadline(paper)) .forEach(paper -> sendMessageDeadline(paper));
.collect(Collectors.toList());
} }
private boolean needToSendDeadlineNotification(Paper paper, Date compareDate, boolean isDeadlineBeforeWeek) { private boolean needToSendDeadlineNotification(Paper paper, Date compareDate, boolean isDeadlineBeforeWeek) {
@ -39,11 +34,24 @@ public class PaperNotificationService {
|| compareDate.before(paper.getDeadlineDate()) && !isDeadlineBeforeWeek); || compareDate.before(paper.getDeadlineDate()) && !isDeadlineBeforeWeek);
} }
private Paper sendMessageDeadline(Paper paper) { private void sendMessageDeadline(Paper paper) {
for (User user : paper.getAuthors()) { paper.getAuthors().forEach(user -> {
mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи", mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи",
"Срок сдачи статьи " + paper.getTitle() + " " + paper.getDeadlineDate().toString()); "Срок сдачи статьи " + paper.getTitle() + " " + paper.getDeadlineDate().toString());
});
} }
return paper;
public void sendCreateNotification(Paper paper) {
Map<String, Object> variables = ImmutableMap.of("paper", paper);
paper.getAuthors().forEach(author -> {
mailService.sendEmailFromTemplate(variables, author, "paperCreateNotification", "Создана статья");
});
}
public void statusChangeNotification(Paper paper, Paper.PaperStatus oldStatus) {
Map<String, Object> variables = ImmutableMap.of("paper", paper, "oldStatus", oldStatus);
paper.getAuthors().forEach(author -> {
mailService.sendEmailFromTemplate(variables, author, "paperStatusChangeNotification", "Изменился статус статьи");
});
} }
} }

View File

@ -1,6 +1,5 @@
package ru.ulstu.paper.service; package ru.ulstu.paper.service;
import com.google.common.collect.ImmutableMap;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.file.service.FileService; 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.model.PaperStatusDto;
import ru.ulstu.paper.repository.PaperRepository; import ru.ulstu.paper.repository.PaperRepository;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import ru.ulstu.user.model.UserDto; import ru.ulstu.user.service.UserService;
import ru.ulstu.user.service.MailService;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.core.util.StreamApiUtils.convert;
@Service @Service
public class PaperService { public class PaperService {
public final MailService mailService; private final PaperNotificationService paperNotificationService;
private final PaperRepository paperRepository; private final PaperRepository paperRepository;
private final UserService userService;
private final FileService fileService; private final FileService fileService;
public PaperService(PaperRepository paperRepository, public PaperService(PaperRepository paperRepository,
FileService fileService, MailService mailService) { FileService fileService,
PaperNotificationService paperNotificationService,
UserService userService) {
this.paperRepository = paperRepository; this.paperRepository = paperRepository;
this.fileService = fileService; this.fileService = fileService;
this.mailService = mailService; this.paperNotificationService = paperNotificationService;
this.userService = userService;
} }
public List<Paper> findAll() { public List<Paper> findAll() {
@ -53,32 +54,45 @@ public class PaperService {
@Transactional @Transactional
public int create(PaperDto paperDto) throws IOException { 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 { private Paper copyFromDto(Paper paper, PaperDto paperDto) throws IOException {
paper.setComment(paperDto.getComment()); paper.setComment(paperDto.getComment());
paper.setCreateDate(paperDto.getCreateDate()); paper.setCreateDate(paper.getCreateDate() == null ? new Date() : paper.getCreateDate());
paper.setLocked(paperDto.getLocked()); paper.setLocked(paperDto.getLocked());
paper.setStatus(paperDto.getStatus()); paper.setStatus(paperDto.getStatus() == null ? Paper.PaperStatus.DRAFT : paperDto.getStatus());
paper.setTitle(paperDto.getTitle()); paper.setTitle(paperDto.getTitle());
paper.setUpdateDate(paperDto.getUpdateDate()); paper.setUpdateDate(new Date());
paper.setDeadlineDate(paperDto.getDeadlineDate());
if (paperDto.getTmpFileName() != null) { if (paperDto.getTmpFileName() != null) {
paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName())); 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; return paper;
} }
@Transactional @Transactional
public Integer update(PaperDto paperDto) throws IOException { public Integer update(PaperDto paperDto) throws IOException {
Paper paper = paperRepository.findOne(paperDto.getId()); Paper paper = paperRepository.findOne(paperDto.getId());
if (paper != null && paper.getStatus() != paperDto.getStatus()) { Paper.PaperStatus oldStatus = paper.getStatus();
sendMessageAboutStatusChange(paper.getStatus(), paperDto);
}
if (paperDto.getTmpFileName() != null && paper.getFileData() != null) { if (paperDto.getTmpFileName() != null && paper.getFileData() != null) {
fileService.deleteFile(paper.getFileData()); 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 @Transactional
@ -94,14 +108,6 @@ public class PaperService {
return convert(Arrays.asList(Paper.PaperStatus.values()), status -> new PaperStatusDto(status)); 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 @Transactional
public Paper create(String title, User user, Date deadlineDate) { public Paper create(String title, User user, Date deadlineDate) {
Paper paper = new Paper(); Paper paper = new Paper();
@ -113,8 +119,8 @@ public class PaperService {
paper.setStatus(Paper.PaperStatus.DRAFT); paper.setStatus(Paper.PaperStatus.DRAFT);
paper = paperRepository.save(paper); paper = paperRepository.save(paper);
Map<String, Object> variables = ImmutableMap.of("paper", paper); paperNotificationService.sendCreateNotification(paper);
mailService.sendEmailFromTemplate(variables, user, "paperCreateNotification", "Создана статья");
return paper; return paper;
} }
} }

View File

@ -291,4 +291,8 @@ public class UserService implements UserDetailsService {
public List<User> findByIds(List<Integer> ids) { public List<User> findByIds(List<Integer> ids) {
return userRepository.findAll(ids); return userRepository.findAll(ids);
} }
public User findById(Integer id) {
return userRepository.findOne(id);
}
} }

View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Уведомление дедлайне статьи</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" th:href="@{|${baseUrl}/favicon.ico|}"/>
</head>
<body>
<p>
Уважаемый(ая) <span th:text="${user.firstName + ' ' + user.lastName}">Ivan Ivanov</span>
</p>
<p>
Приближается срок сдачи статьи "<span th:text="${paper.title}">Title</span>".
</p>
<p>
Срок исполнения: <span th:text="${#dates.format(paper.deadlineDate, 'dd.MM.yyyy HH:mm')}"></span>.
</p>
<p>
Regards,
<br/>
<em>NG-tracker.</em>
</p>
</body>
</html>

View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Уведомление о смене статуса статьи</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" th:href="@{|${baseUrl}/favicon.ico|}"/>
</head>
<body>
<p>
Уважаемый(ая) <span th:text="${user.firstName + ' ' + user.lastName}">Ivan Ivanov</span>
</p>
<p>
Статус статьи "<span th:text="${paper.title}">Title</span>" сменился с
"<span th:text="${oldStatus.name}">oldStatus</span>" на "<span th:text="${paper.status.name}">newStatus</span>".
</p>
<p>
Regards,
<br/>
<em>NG-tracker.</em>
</p>
</body>
</html>