close failed papers

This commit is contained in:
Anton Romanov 2018-11-21 15:40:20 +04:00
parent 4e3e882f76
commit 29586c9313
5 changed files with 71 additions and 11 deletions

View File

@ -23,7 +23,11 @@ import java.util.Set;
@Entity @Entity
public class Paper extends BaseEntity implements UserContainer { public class Paper extends BaseEntity implements UserContainer {
public enum PaperStatus { public enum PaperStatus {
ATTENTION("Обратить внимание"), ON_PREPARATION("На подготовке"), DRAFT("Черновик"), COMPLETED("Завершена"); ATTENTION("Обратить внимание"),
ON_PREPARATION("На подготовке"),
DRAFT("Черновик"),
COMPLETED("Завершена"),
FAILED("Провалены сроки");
private String name; private String name;

View File

@ -55,4 +55,11 @@ public class PaperNotificationService {
mailService.sendEmailFromTemplate(variables, author, "paperStatusChangeNotification", "Изменился статус статьи"); mailService.sendEmailFromTemplate(variables, author, "paperStatusChangeNotification", "Изменился статус статьи");
}); });
} }
public void sendFailedNotification(Paper paper, Paper.PaperStatus oldStatus) {
Map<String, Object> variables = ImmutableMap.of("paper", paper, "oldStatus", oldStatus);
paper.getAuthors().forEach(author -> {
mailService.sendEmailFromTemplate(variables, author, "paperFailedNotification", "Статья провалена");
});
}
} }

View File

@ -6,15 +6,15 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class DeadlineScheduler { public class PaperScheduler {
private final static boolean IS_DEADLINE_NOTIFICATION_BEFORE_WEEK = true; private final static boolean IS_DEADLINE_NOTIFICATION_BEFORE_WEEK = true;
private final Logger log = LoggerFactory.getLogger(DeadlineScheduler.class); private final Logger log = LoggerFactory.getLogger(PaperScheduler.class);
private final PaperNotificationService paperNotificationService; private final PaperNotificationService paperNotificationService;
private final PaperService paperService; private final PaperService paperService;
public DeadlineScheduler(PaperNotificationService paperNotificationService, public PaperScheduler(PaperNotificationService paperNotificationService,
PaperService paperService) { PaperService paperService) {
this.paperNotificationService = paperNotificationService; this.paperNotificationService = paperNotificationService;
this.paperService = paperService; this.paperService = paperService;
@ -23,15 +23,22 @@ public class DeadlineScheduler {
@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("PaperScheduler.checkDeadlineBeforeWeek started");
paperNotificationService.sendDeadlineNotifications(paperService.findAll(), !IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); paperNotificationService.sendDeadlineNotifications(paperService.findAll(), !IS_DEADLINE_NOTIFICATION_BEFORE_WEEK);
log.debug("DeadlineScheduler.checkDeadlineBeforeWeek finished"); log.debug("PaperScheduler.checkDeadlineBeforeWeek finished");
} }
@Scheduled(cron = "0 0 8 * * ?") @Scheduled(cron = "0 0 8 * * ?")
public void checkDeadlineAfterWeek() { public void checkDeadlineAfterWeek() {
log.debug("DeadlineScheduler.checkDeadlineAfterWeek started"); log.debug("PaperScheduler.checkDeadlineAfterWeek started");
paperNotificationService.sendDeadlineNotifications(paperService.findAll(), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); paperNotificationService.sendDeadlineNotifications(paperService.findAll(), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK);
log.debug("DeadlineScheduler.checkDeadlineAfterWeek finished"); log.debug("PaperScheduler.checkDeadlineAfterWeek finished");
}
@Scheduled(cron = "0 0 6 * * ?")
public void closeFailedPapers() {
log.debug("PaperScheduler.closeFailedPapers started");
paperService.closeFailedPapers();
log.debug("PaperScheduler.closeFailedPapers finished");
} }
} }

View File

@ -18,6 +18,9 @@ import java.util.List;
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;
import static ru.ulstu.paper.model.Paper.PaperStatus.ATTENTION;
import static ru.ulstu.paper.model.Paper.PaperStatus.DRAFT;
import static ru.ulstu.paper.model.Paper.PaperStatus.ON_PREPARATION;
@Service @Service
public class PaperService { public class PaperService {
@ -65,7 +68,7 @@ public class PaperService {
paper.setComment(paperDto.getComment()); paper.setComment(paperDto.getComment());
paper.setCreateDate(paper.getCreateDate() == null ? new Date() : paper.getCreateDate()); paper.setCreateDate(paper.getCreateDate() == null ? new Date() : paper.getCreateDate());
paper.setLocked(paperDto.getLocked()); paper.setLocked(paperDto.getLocked());
paper.setStatus(paperDto.getStatus() == null ? Paper.PaperStatus.DRAFT : paperDto.getStatus()); paper.setStatus(paperDto.getStatus() == null ? DRAFT : paperDto.getStatus());
paper.setTitle(paperDto.getTitle()); paper.setTitle(paperDto.getTitle());
paper.setUpdateDate(new Date()); paper.setUpdateDate(new Date());
paper.setDeadlineDate(paperDto.getDeadlineDate()); paper.setDeadlineDate(paperDto.getDeadlineDate());
@ -115,7 +118,7 @@ public class PaperService {
paper.setDeadlineDate(deadlineDate); paper.setDeadlineDate(deadlineDate);
paper.setCreateDate(new Date()); paper.setCreateDate(new Date());
paper.setUpdateDate(new Date()); paper.setUpdateDate(new Date());
paper.setStatus(Paper.PaperStatus.DRAFT); paper.setStatus(DRAFT);
paper = paperRepository.save(paper); paper = paperRepository.save(paper);
paperNotificationService.sendCreateNotification(paper); paperNotificationService.sendCreateNotification(paper);
@ -126,4 +129,21 @@ public class PaperService {
public List<PaperDto> filter(PaperFilterDto filterDto) { public List<PaperDto> filter(PaperFilterDto filterDto) {
return convert(paperRepository.filter(userService.findById(filterDto.getAuthorId()), filterDto.getYear()), PaperDto::new); return convert(paperRepository.filter(userService.findById(filterDto.getAuthorId()), filterDto.getYear()), PaperDto::new);
} }
public void closeFailedPapers() {
List<Paper> papers = paperRepository.findAll()
.stream()
.filter(paper -> paper.getDeadlineDate() != null
&& (paper.getStatus() == ON_PREPARATION
|| paper.getStatus() == DRAFT
|| paper.getStatus() == ATTENTION)
&& paper.getDeadlineDate().before(new Date()))
.collect(Collectors.toList());
papers.forEach(paper -> {
Paper.PaperStatus oldStatus = paper.getStatus();
paper.setStatus(Paper.PaperStatus.FAILED);
paperRepository.save(paper);
paperNotificationService.sendFailedNotification(paper, oldStatus);
});
}
} }

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>".
</p>
<p>
Regards,
<br/>
<em>NG-tracker.</em>
</p>
</body>
</html>