fix filer

This commit is contained in:
Anton Romanov 2018-12-18 22:15:21 +04:00
parent ec1e3ff052
commit 468f440d9d
8 changed files with 98 additions and 39 deletions

View File

@ -12,11 +12,14 @@ import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.deadline.model.DeadlineDto; import ru.ulstu.deadline.model.DeadlineDto;
import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.model.PaperFilterDto;
import ru.ulstu.paper.service.PaperService; import ru.ulstu.paper.service.PaperService;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import javax.validation.Valid; import javax.validation.Valid;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -34,7 +37,14 @@ public class PaperController {
@GetMapping("/papers") @GetMapping("/papers")
public void getPapers(ModelMap modelMap) { 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") @GetMapping("/dashboard")
@ -90,6 +100,15 @@ public class PaperController {
return paperService.getPaperAuthors(); return paperService.getPaperAuthors();
} }
@ModelAttribute("allYears")
public List<Integer> getAllYears() {
List<Integer> years = new ArrayList<>();
for (int i = Calendar.getInstance().get(Calendar.YEAR); i > 2010; i-- ) {
years.add(i);
}
return years;
}
private void filterEmptyDeadlines(PaperDto paperDto) { private void filterEmptyDeadlines(PaperDto paperDto) {
paperDto.setDeadlines(paperDto.getDeadlines().stream() paperDto.setDeadlines(paperDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))

View File

@ -63,7 +63,7 @@ public class Paper extends BaseEntity implements UserContainer {
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
private Date updateDate = new Date(); private Date updateDate = new Date();
@OneToMany(cascade = CascadeType.ALL) @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "paper_id") @JoinColumn(name = "paper_id")
private List<Deadline> deadlines = new ArrayList<>(); private List<Deadline> deadlines = new ArrayList<>();

View File

@ -33,6 +33,7 @@ public class PaperDto {
private Date fileCreateDate; private Date fileCreateDate;
private Set<Integer> authorIds; private Set<Integer> authorIds;
private Set<UserDto> authors; private Set<UserDto> authors;
private Integer filterAuthorId;
public PaperDto() { public PaperDto() {
deadlines.add(new DeadlineDto()); deadlines.add(new DeadlineDto());
@ -200,4 +201,12 @@ public class PaperDto {
.map(author -> author.getLastName()) .map(author -> author.getLastName())
.collect(Collectors.joining(", ")); .collect(Collectors.joining(", "));
} }
public Integer getFilterAuthorId() {
return filterAuthorId;
}
public void setFilterAuthorId(Integer filterAuthorId) {
this.filterAuthorId = filterAuthorId;
}
} }

View File

@ -1,24 +1,42 @@
package ru.ulstu.paper.model; package ru.ulstu.paper.model;
import com.fasterxml.jackson.annotation.JsonCreator; import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
public class PaperFilterDto { public class PaperFilterDto {
private final Integer authorId; private List<PaperDto> papers;
private final Integer year; private Integer filterAuthorId;
private Integer year;
@JsonCreator public PaperFilterDto() {
public PaperFilterDto(@JsonProperty("authorId") Integer authorId, }
@JsonProperty("year") Integer year) {
this.authorId = authorId; public PaperFilterDto(List<PaperDto> paperDtos, Integer filterAuthorId, Integer year) {
this.papers = paperDtos;
this.filterAuthorId = filterAuthorId;
this.year = year; this.year = year;
} }
public Integer getAuthorId() { public List<PaperDto> getPapers() {
return authorId; return papers;
}
public void setPapers(List<PaperDto> papers) {
this.papers = papers;
}
public Integer getFilterAuthorId() {
return filterAuthorId;
}
public void setFilterAuthorId(Integer filterAuthorId) {
this.filterAuthorId = filterAuthorId;
} }
public Integer getYear() { public Integer getYear() {
return year; return year;
} }
public void setYear(Integer year) {
this.year = year;
}
} }

View File

@ -10,6 +10,6 @@ import java.util.List;
public interface PaperRepository extends JpaRepository<Paper, Integer> { public interface PaperRepository extends JpaRepository<Paper, Integer> {
@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<Paper> filter(@Param("author") User author, @Param("year") Integer year); List<Paper> filter(@Param("author") User author, @Param("year") Integer year);
} }

View File

@ -7,7 +7,7 @@ import org.springframework.stereotype.Service;
@Service @Service
public class PaperScheduler { 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); private final Logger log = LoggerFactory.getLogger(PaperScheduler.class);
@ -24,14 +24,14 @@ public class PaperScheduler {
@Scheduled(cron = "0 0 8 * 1 ?") @Scheduled(cron = "0 0 8 * 1 ?")
public void checkDeadlineBeforeWeek() { public void checkDeadlineBeforeWeek() {
log.debug("PaperScheduler.checkDeadlineBeforeWeek started"); 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"); log.debug("PaperScheduler.checkDeadlineBeforeWeek finished");
} }
@Scheduled(cron = "0 0 8 * * ?") @Scheduled(cron = "0 0 8 * * ?")
public void checkDeadlineAfterWeek() { public void checkDeadlineAfterWeek() {
log.debug("PaperScheduler.checkDeadlineAfterWeek started"); 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"); log.debug("PaperScheduler.checkDeadlineAfterWeek finished");
} }

View File

@ -15,7 +15,9 @@ import ru.ulstu.user.service.UserService;
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.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.springframework.util.ObjectUtils.isEmpty; import static org.springframework.util.ObjectUtils.isEmpty;
@ -83,10 +85,9 @@ public class PaperService {
if (paperDto.getTmpFileName() != null) { if (paperDto.getTmpFileName() != null) {
paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName())); paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName()));
} }
paper.getAuthors().clear();
if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) { if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) {
paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds))); paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds)));
} else {
paper.getAuthors().clear();
} }
return paper; return paper;
} }
@ -95,11 +96,18 @@ public class PaperService {
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());
Paper.PaperStatus oldStatus = paper.getStatus(); Paper.PaperStatus oldStatus = paper.getStatus();
Set<User> oldAuthors = new HashSet<>(paper.getAuthors());
if (paperDto.getTmpFileName() != null && paper.getFileData() != null) { if (paperDto.getTmpFileName() != null && paper.getFileData() != null) {
fileService.deleteFile(paper.getFileData()); fileService.deleteFile(paper.getFileData());
} }
paperRepository.save(copyFromDto(paper, paperDto)); paperRepository.save(copyFromDto(paper, paperDto));
paper.getAuthors().forEach(author -> {
if (!oldAuthors.contains(author)) {
paperNotificationService.sendCreateNotification(paper);
}
});
if (paper.getStatus() != oldStatus) { if (paper.getStatus() != oldStatus) {
paperNotificationService.statusChangeNotification(paper, oldStatus); paperNotificationService.statusChangeNotification(paper, oldStatus);
} }
@ -137,10 +145,12 @@ 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(
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)); return new PaperDto(paperRepository.getOne(id));
} }

View File

@ -7,39 +7,42 @@
<body> <body>
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<form id="papers-form" method="get" th:action="@{'/papers/papers'}"> <form id="papers-form" method="post" th:action="@{'/papers/papers'}">
<input th:type="hidden" name="paperDeleteId" id="paperDeleteId"/> <input th:type="hidden" name="paperDeleteId" id="paperDeleteId"/>
<section id="papers"> <section id="papers">
<div class="container"> <div class="container">
<div class="row" id="paper-list"> <div class="row" id="paper-list">
<div class="col-lg-12 text-center"> <div class="col-lg-12 text-center">
<h2 class="section-heading text-uppercase">Статьи</h2> <h2 class="section-heading text-uppercase">Статьи</h2>
<div th:replace="papers/fragments/paperNavigationFragment"/> <div th:replace="papers/fragments/paperNavigationFragment"/>
<div class="filter">
<h5>Фильтровать по:</h5>
<select id="author">
<option value="1">Алёна</option>
<option value="2">Маша</option>
<option value="3">Петя</option>
</select>
<select id="index score">
<option>РИНЦ</option>
<option>Scopus</option>
<option>Web of science</option>
</select>
<select id="year">
<option>2018</option>
<option>2017</option>
<option>2016</option>
</select>
</div> </div>
</div> </div>
</div> <div class="row">
<th:block th:each="paper : ${papers}"> <div class="col-md-9 col-sm-12">
<th:block th:each="paper : ${filteredPapers.papers}">
<div th:replace="papers/fragments/paperLineFragment :: titleLine(paper=${paper})"/> <div th:replace="papers/fragments/paperLineFragment :: titleLine(paper=${paper})"/>
</th:block> </th:block>
</div> </div>
<div class="col-md-3 col-sm-12">
<div class="filter">
<h5>Фильтр:</h5>
<select class="form-control" th:field="${filteredPapers.filterAuthorId}" id="author"
onchange="this.form.submit();">
<option value="">Все авторы </option>
<option th:each="author: ${allAuthors}" th:value="${author.id}"
th:text="${author.lastName}">lastName
</option>
</select>
<select class="form-control" id="year" th:field="${filteredPapers.year}" onchange="this.form.submit();">
<option value="">Все годы</option>
<option th:each="year: ${allYears}" th:value="${year}"
th:text="${year}">year
</option>
</select>
</div>
</div>
</div>
</div>
</section> </section>
<div id="modalDelete"/> <div id="modalDelete"/>