From 29586c9313f3bbbd21fa05d383e4479753db3166 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 21 Nov 2018 15:40:20 +0400 Subject: [PATCH] close failed papers --- src/main/java/ru/ulstu/paper/model/Paper.java | 6 ++++- .../service/PaperNotificationService.java | 7 ++++++ ...lineScheduler.java => PaperScheduler.java} | 23 +++++++++++------- .../ru/ulstu/paper/service/PaperService.java | 24 +++++++++++++++++-- .../paperFailedNotification.html | 22 +++++++++++++++++ 5 files changed, 71 insertions(+), 11 deletions(-) rename src/main/java/ru/ulstu/paper/service/{DeadlineScheduler.java => PaperScheduler.java} (55%) create mode 100644 src/main/resources/mail_templates/paperFailedNotification.html diff --git a/src/main/java/ru/ulstu/paper/model/Paper.java b/src/main/java/ru/ulstu/paper/model/Paper.java index e9cc92c..b7511d6 100644 --- a/src/main/java/ru/ulstu/paper/model/Paper.java +++ b/src/main/java/ru/ulstu/paper/model/Paper.java @@ -23,7 +23,11 @@ import java.util.Set; @Entity public class Paper extends BaseEntity implements UserContainer { public enum PaperStatus { - ATTENTION("Обратить внимание"), ON_PREPARATION("На подготовке"), DRAFT("Черновик"), COMPLETED("Завершена"); + ATTENTION("Обратить внимание"), + ON_PREPARATION("На подготовке"), + DRAFT("Черновик"), + COMPLETED("Завершена"), + FAILED("Провалены сроки"); private String name; diff --git a/src/main/java/ru/ulstu/paper/service/PaperNotificationService.java b/src/main/java/ru/ulstu/paper/service/PaperNotificationService.java index 05f9c2a..99e0c86 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperNotificationService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperNotificationService.java @@ -55,4 +55,11 @@ public class PaperNotificationService { mailService.sendEmailFromTemplate(variables, author, "paperStatusChangeNotification", "Изменился статус статьи"); }); } + + public void sendFailedNotification(Paper paper, Paper.PaperStatus oldStatus) { + Map variables = ImmutableMap.of("paper", paper, "oldStatus", oldStatus); + paper.getAuthors().forEach(author -> { + mailService.sendEmailFromTemplate(variables, author, "paperFailedNotification", "Статья провалена"); + }); + } } diff --git a/src/main/java/ru/ulstu/paper/service/DeadlineScheduler.java b/src/main/java/ru/ulstu/paper/service/PaperScheduler.java similarity index 55% rename from src/main/java/ru/ulstu/paper/service/DeadlineScheduler.java rename to src/main/java/ru/ulstu/paper/service/PaperScheduler.java index d6d9852..ed035d9 100644 --- a/src/main/java/ru/ulstu/paper/service/DeadlineScheduler.java +++ b/src/main/java/ru/ulstu/paper/service/PaperScheduler.java @@ -6,16 +6,16 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service -public class DeadlineScheduler { +public class PaperScheduler { 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 PaperService paperService; - public DeadlineScheduler(PaperNotificationService paperNotificationService, - PaperService paperService) { + public PaperScheduler(PaperNotificationService paperNotificationService, + PaperService paperService) { this.paperNotificationService = paperNotificationService; this.paperService = paperService; } @@ -23,15 +23,22 @@ public class DeadlineScheduler { @Scheduled(cron = "0 0 8 * 1 ?") public void checkDeadlineBeforeWeek() { - log.debug("DeadlineScheduler.checkDeadlineBeforeWeek started"); + log.debug("PaperScheduler.checkDeadlineBeforeWeek started"); paperNotificationService.sendDeadlineNotifications(paperService.findAll(), !IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); - log.debug("DeadlineScheduler.checkDeadlineBeforeWeek finished"); + log.debug("PaperScheduler.checkDeadlineBeforeWeek finished"); } @Scheduled(cron = "0 0 8 * * ?") public void checkDeadlineAfterWeek() { - log.debug("DeadlineScheduler.checkDeadlineAfterWeek started"); + log.debug("PaperScheduler.checkDeadlineAfterWeek started"); 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"); } } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index a75592b..0e1abbc 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -18,6 +18,9 @@ import java.util.List; import java.util.stream.Collectors; 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 public class PaperService { @@ -65,7 +68,7 @@ public class PaperService { paper.setComment(paperDto.getComment()); paper.setCreateDate(paper.getCreateDate() == null ? new Date() : paper.getCreateDate()); 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.setUpdateDate(new Date()); paper.setDeadlineDate(paperDto.getDeadlineDate()); @@ -115,7 +118,7 @@ public class PaperService { paper.setDeadlineDate(deadlineDate); paper.setCreateDate(new Date()); paper.setUpdateDate(new Date()); - paper.setStatus(Paper.PaperStatus.DRAFT); + paper.setStatus(DRAFT); paper = paperRepository.save(paper); paperNotificationService.sendCreateNotification(paper); @@ -126,4 +129,21 @@ public class PaperService { public List filter(PaperFilterDto filterDto) { return convert(paperRepository.filter(userService.findById(filterDto.getAuthorId()), filterDto.getYear()), PaperDto::new); } + + public void closeFailedPapers() { + List 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); + }); + } } diff --git a/src/main/resources/mail_templates/paperFailedNotification.html b/src/main/resources/mail_templates/paperFailedNotification.html new file mode 100644 index 0000000..cacc7e9 --- /dev/null +++ b/src/main/resources/mail_templates/paperFailedNotification.html @@ -0,0 +1,22 @@ + + + + Уведомление о провале статьи + + + + +

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

+

+ Статья "Title" провалена. + Предыдущий статус - "oldStatus". +

+

+ Regards, +
+ NG-tracker. +

+ +