From 468f440d9dded094ad5bf5a434f10a9e8a1cf081 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 18 Dec 2018 22:15:21 +0400 Subject: [PATCH] fix filer --- .../paper/controller/PaperController.java | 21 +++++++++- src/main/java/ru/ulstu/paper/model/Paper.java | 2 +- .../java/ru/ulstu/paper/model/PaperDto.java | 9 ++++ .../ru/ulstu/paper/model/PaperFilterDto.java | 38 ++++++++++++----- .../paper/repository/PaperRepository.java | 2 +- .../ulstu/paper/service/PaperScheduler.java | 6 +-- .../ru/ulstu/paper/service/PaperService.java | 18 ++++++-- .../resources/templates/papers/papers.html | 41 ++++++++++--------- 8 files changed, 98 insertions(+), 39 deletions(-) diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index 01e5b85..8969a06 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -12,11 +12,14 @@ import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.deadline.model.DeadlineDto; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.PaperDto; +import ru.ulstu.paper.model.PaperFilterDto; import ru.ulstu.paper.service.PaperService; import ru.ulstu.user.model.User; import javax.validation.Valid; import java.io.IOException; +import java.util.ArrayList; +import java.util.Calendar; import java.util.List; import java.util.stream.Collectors; @@ -34,7 +37,14 @@ public class PaperController { @GetMapping("/papers") public void getPapers(ModelMap modelMap) { - modelMap.put("papers", paperService.findAllDto()); + modelMap.put("filteredPapers", new PaperFilterDto(paperService.findAllDto(), null, null)); + } + + @PostMapping("/papers") + public void filterPapers(@Valid PaperFilterDto paperFilterDto, ModelMap modelMap) { + modelMap.put("filteredPapers", new PaperFilterDto(paperService.filter(paperFilterDto), + paperFilterDto.getFilterAuthorId(), + paperFilterDto.getYear())); } @GetMapping("/dashboard") @@ -90,6 +100,15 @@ public class PaperController { return paperService.getPaperAuthors(); } + @ModelAttribute("allYears") + public List getAllYears() { + List years = new ArrayList<>(); + for (int i = Calendar.getInstance().get(Calendar.YEAR); i > 2010; i-- ) { + years.add(i); + } + return years; + } + private void filterEmptyDeadlines(PaperDto paperDto) { paperDto.setDeadlines(paperDto.getDeadlines().stream() .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) diff --git a/src/main/java/ru/ulstu/paper/model/Paper.java b/src/main/java/ru/ulstu/paper/model/Paper.java index 6c75f0e..a1074c0 100644 --- a/src/main/java/ru/ulstu/paper/model/Paper.java +++ b/src/main/java/ru/ulstu/paper/model/Paper.java @@ -63,7 +63,7 @@ public class Paper extends BaseEntity implements UserContainer { @Temporal(TemporalType.TIMESTAMP) private Date updateDate = new Date(); - @OneToMany(cascade = CascadeType.ALL) + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "paper_id") private List deadlines = new ArrayList<>(); diff --git a/src/main/java/ru/ulstu/paper/model/PaperDto.java b/src/main/java/ru/ulstu/paper/model/PaperDto.java index de3c8df..139cc3a 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperDto.java @@ -33,6 +33,7 @@ public class PaperDto { private Date fileCreateDate; private Set authorIds; private Set authors; + private Integer filterAuthorId; public PaperDto() { deadlines.add(new DeadlineDto()); @@ -200,4 +201,12 @@ public class PaperDto { .map(author -> author.getLastName()) .collect(Collectors.joining(", ")); } + + public Integer getFilterAuthorId() { + return filterAuthorId; + } + + public void setFilterAuthorId(Integer filterAuthorId) { + this.filterAuthorId = filterAuthorId; + } } diff --git a/src/main/java/ru/ulstu/paper/model/PaperFilterDto.java b/src/main/java/ru/ulstu/paper/model/PaperFilterDto.java index c832bed..f7aef29 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperFilterDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperFilterDto.java @@ -1,24 +1,42 @@ package ru.ulstu.paper.model; -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.List; public class PaperFilterDto { - private final Integer authorId; - private final Integer year; + private List papers; + private Integer filterAuthorId; + private Integer year; - @JsonCreator - public PaperFilterDto(@JsonProperty("authorId") Integer authorId, - @JsonProperty("year") Integer year) { - this.authorId = authorId; + public PaperFilterDto() { + } + + public PaperFilterDto(List paperDtos, Integer filterAuthorId, Integer year) { + this.papers = paperDtos; + this.filterAuthorId = filterAuthorId; this.year = year; } - public Integer getAuthorId() { - return authorId; + public List getPapers() { + return papers; + } + + public void setPapers(List papers) { + this.papers = papers; + } + + public Integer getFilterAuthorId() { + return filterAuthorId; + } + + public void setFilterAuthorId(Integer filterAuthorId) { + this.filterAuthorId = filterAuthorId; } public Integer getYear() { return year; } + + public void setYear(Integer year) { + this.year = year; + } } diff --git a/src/main/java/ru/ulstu/paper/repository/PaperRepository.java b/src/main/java/ru/ulstu/paper/repository/PaperRepository.java index 5d0cd9a..3951f53 100644 --- a/src/main/java/ru/ulstu/paper/repository/PaperRepository.java +++ b/src/main/java/ru/ulstu/paper/repository/PaperRepository.java @@ -10,6 +10,6 @@ import java.util.List; public interface PaperRepository extends JpaRepository { - @Query("SELECT p FROM Paper p WHERE (:author IS NULL OR :author MEMBER OF p.authors) AND YEAR(p.createDate) = :year OR :year IS NULL") + @Query("SELECT p FROM Paper p WHERE (:author IS NULL OR :author MEMBER OF p.authors) AND (YEAR(p.createDate) = :year OR :year IS NULL)") List filter(@Param("author") User author, @Param("year") Integer year); } diff --git a/src/main/java/ru/ulstu/paper/service/PaperScheduler.java b/src/main/java/ru/ulstu/paper/service/PaperScheduler.java index ed035d9..c7fcf0f 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperScheduler.java +++ b/src/main/java/ru/ulstu/paper/service/PaperScheduler.java @@ -7,7 +7,7 @@ import org.springframework.stereotype.Service; @Service public class PaperScheduler { - private final static boolean IS_DEADLINE_NOTIFICATION_BEFORE_WEEK = true; + private final static boolean IS_DEADLINE_NOTIFICATION_AFTER_WEEK = true; private final Logger log = LoggerFactory.getLogger(PaperScheduler.class); @@ -24,14 +24,14 @@ public class PaperScheduler { @Scheduled(cron = "0 0 8 * 1 ?") public void checkDeadlineBeforeWeek() { log.debug("PaperScheduler.checkDeadlineBeforeWeek started"); - paperNotificationService.sendDeadlineNotifications(paperService.findAll(), !IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); + paperNotificationService.sendDeadlineNotifications(paperService.findAll(), !IS_DEADLINE_NOTIFICATION_AFTER_WEEK); log.debug("PaperScheduler.checkDeadlineBeforeWeek finished"); } @Scheduled(cron = "0 0 8 * * ?") public void checkDeadlineAfterWeek() { log.debug("PaperScheduler.checkDeadlineAfterWeek started"); - paperNotificationService.sendDeadlineNotifications(paperService.findAll(), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); + paperNotificationService.sendDeadlineNotifications(paperService.findAll(), IS_DEADLINE_NOTIFICATION_AFTER_WEEK); log.debug("PaperScheduler.checkDeadlineAfterWeek finished"); } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 0553047..88ea8c9 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -15,7 +15,9 @@ import ru.ulstu.user.service.UserService; import java.io.IOException; import java.util.Arrays; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import static org.springframework.util.ObjectUtils.isEmpty; @@ -83,10 +85,9 @@ public class PaperService { if (paperDto.getTmpFileName() != null) { paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName())); } + paper.getAuthors().clear(); if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) { paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds))); - } else { - paper.getAuthors().clear(); } return paper; } @@ -95,11 +96,18 @@ public class PaperService { public Integer update(PaperDto paperDto) throws IOException { Paper paper = paperRepository.findOne(paperDto.getId()); Paper.PaperStatus oldStatus = paper.getStatus(); + Set oldAuthors = new HashSet<>(paper.getAuthors()); if (paperDto.getTmpFileName() != null && paper.getFileData() != null) { fileService.deleteFile(paper.getFileData()); } paperRepository.save(copyFromDto(paper, paperDto)); + paper.getAuthors().forEach(author -> { + if (!oldAuthors.contains(author)) { + paperNotificationService.sendCreateNotification(paper); + } + }); + if (paper.getStatus() != oldStatus) { paperNotificationService.statusChangeNotification(paper, oldStatus); } @@ -137,10 +145,12 @@ public class PaperService { } public List filter(PaperFilterDto filterDto) { - return convert(paperRepository.filter(userService.findById(filterDto.getAuthorId()), filterDto.getYear()), PaperDto::new); + return convert(paperRepository.filter( + filterDto.getFilterAuthorId() == null ? null : userService.findById(filterDto.getFilterAuthorId()), + filterDto.getYear()), PaperDto::new); } - public PaperDto findPaper(int id){ + public PaperDto findPaper(int id) { return new PaperDto(paperRepository.getOne(id)); } diff --git a/src/main/resources/templates/papers/papers.html b/src/main/resources/templates/papers/papers.html index 856f02e..e88b413 100644 --- a/src/main/resources/templates/papers/papers.html +++ b/src/main/resources/templates/papers/papers.html @@ -7,38 +7,41 @@
-
+

Статьи

-
+
+
+
+
+ +
+ +
+
-
Фильтровать по:
- + + - - + +
- -
-