#42 filter authors by leader and degree

pull/171/head
T-Midnight 5 years ago
parent 4955e9a637
commit 1756f29505

@ -12,7 +12,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.grant.model.GrantUserFilterDto;
import ru.ulstu.grant.service.GrantService;
import ru.ulstu.user.model.User;
import springfox.documentation.annotations.ApiIgnore;
@ -26,7 +25,6 @@ import static org.springframework.util.StringUtils.isEmpty;
import static ru.ulstu.core.controller.Navigation.GRANTS_PAGE;
import static ru.ulstu.core.controller.Navigation.GRANT_PAGE;
import static ru.ulstu.core.controller.Navigation.REDIRECT_TO;
import static ru.ulstu.core.controller.Navigation.hasErrors;
@Controller()
@ -50,30 +48,23 @@ public class GrantController {
}
@GetMapping("/grant")
public void getGrant(ModelMap modelMap, GrantUserFilterDto grantUserFilterDto, @RequestParam(value = "id") Integer id) {
public void getGrant(ModelMap modelMap, @RequestParam(value = "id") Integer id) {
if (id != null && id > 0) {
modelMap.put("grantDto", grantService.findOneDto(id));
} else {
modelMap.put("grantDto", new GrantDto());
}
modelMap.put("filteredAuthors", new GrantUserFilterDto(grantService.filterUsers(grantUserFilterDto), false, false, false));
}
@PostMapping(value = "/grant", params = "save")
public String save(@Valid GrantDto grantDto, GrantUserFilterDto grantUserFilterDto, Errors errors, ModelMap modelMap /*@ModelAttribute List<User> allAuthors*/)
public String save(@Valid GrantDto grantDto, Errors errors)
throws IOException {
filterEmptyDeadlines(grantDto);
modelMap.put("filteredAuthors", new GrantUserFilterDto(grantService.filterUsers(grantUserFilterDto),
grantUserFilterDto.isLeader(),
grantUserFilterDto.isBirthDate(),
grantUserFilterDto.isDegree()));
if (grantDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
}
if (grantDto.getLeaderId().equals(null)) {
errors.rejectValue("leader", "errorCode", "Укажите руководителя");
// } else {
// allAuthors.remove(grantDto.getLeaderId());
}
if (errors.hasErrors()) {
return GRANT_PAGE;
@ -82,6 +73,11 @@ public class GrantController {
return String.format(REDIRECT_TO, GRANTS_PAGE);
}
@PostMapping(value = "/grant", params = "filterUsers")
public String filterUsers() {
return GRANT_PAGE;
}
@PostMapping(value = "/grant", params = "addDeadline")
public String addDeadline(@Valid GrantDto grantDto, Errors errors) {
filterEmptyDeadlines(grantDto);
@ -101,12 +97,6 @@ public class GrantController {
return GRANT_PAGE;
}
// @PostMapping(value = "/grant", params = "updateAuthors")
// public String updateAuthors(@ModelAttribute List<User> allAuthors, @Valid GrantDto grantDto) {
// allAuthors.remove(grantDto.getLeaderId());
// return GRANT_PAGE;
// }
@GetMapping("/delete/{grant-id}")
public String delete(@PathVariable("grant-id") Integer grantId) throws IOException {
grantService.delete(grantId);
@ -119,15 +109,10 @@ public class GrantController {
}
@ModelAttribute("allAuthors")
public List<User> getAllAuthors() {
return grantService.getGrantAuthors();
public List<User> getAllAuthors(GrantDto grantDto) {
return grantService.getGrantAuthors(grantDto);
}
// @ModelAttribute("allLeaders")
// public List<User> getAllLeaders() {
// return grantService.getGrantAuthors();
// }
private void filterEmptyDeadlines(GrantDto grantDto) {
grantDto.setDeadlines(grantDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))

@ -29,6 +29,9 @@ public class GrantDto {
private Set<Integer> authorIds;
private Set<UserDto> authors;
private Integer leaderId;
private boolean leader;
private boolean birthDate;
private boolean degree;
public GrantDto() {
deadlines.add(new Deadline());
@ -43,7 +46,10 @@ public class GrantDto {
@JsonProperty("project") ProjectDto project,
@JsonProperty("authorIds") Set<Integer> authorIds,
@JsonProperty("authors") Set<UserDto> authors,
@JsonProperty("leader") Integer leaderId) {
@JsonProperty("leader") Integer leaderId,
@JsonProperty("leader") boolean leader,
@JsonProperty("birthDate") boolean birthDate,
@JsonProperty("degree") boolean degree) {
this.id = id;
this.title = title;
this.status = status;
@ -53,6 +59,9 @@ public class GrantDto {
this.project = project;
this.authors = authors;
this.leaderId = leaderId;
this.leader = leader;
this.birthDate = birthDate;
this.degree = degree;
}
public GrantDto(Grant grant) {
@ -66,6 +75,9 @@ public class GrantDto {
this.authorIds = convert(grant.getAuthors(), user -> user.getId());
this.authors = convert(grant.getAuthors(), UserDto::new);
this.leaderId = grant.getLeader().getId();
this.leader = false;
this.birthDate = false;
this.degree = false;
}
public Integer getId() {
@ -154,4 +166,28 @@ public class GrantDto {
public void setLeaderId(Integer leaderId) {
this.leaderId = leaderId;
}
public boolean isLeader() {
return leader;
}
public void setLeader(boolean leader) {
this.leader = leader;
}
public boolean isBirthDate() {
return birthDate;
}
public void setBirthDate(boolean birthDate) {
this.birthDate = birthDate;
}
public boolean isDegree() {
return degree;
}
public void setDegree(boolean degree) {
this.degree = degree;
}
}

@ -1,54 +0,0 @@
package ru.ulstu.grant.model;
import ru.ulstu.user.model.UserDto;
import java.util.List;
public class GrantUserFilterDto {
private List<UserDto> users;
private boolean leader;
private boolean birthDate;
private boolean degree;
public GrantUserFilterDto() {
}
public GrantUserFilterDto(List<UserDto> users, boolean leader, boolean birthDate, boolean degree) {
this.users = users;
this.leader = leader;
this.birthDate = birthDate;
this.degree = degree;
}
public List<UserDto> getUsers() {
return users;
}
public void setUsers(List<UserDto> users) {
this.users = users;
}
public boolean isLeader() {
return leader;
}
public void setLeader(boolean leader) {
this.leader = leader;
}
public boolean isBirthDate() {
return birthDate;
}
public void setBirthDate(boolean birthDate) {
this.birthDate = birthDate;
}
public boolean isDegree() {
return degree;
}
public void setDegree(boolean degree) {
this.degree = degree;
}
}

@ -1,10 +1,7 @@
package ru.ulstu.grant.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.user.model.User;
import java.util.List;

@ -8,13 +8,11 @@ import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.file.service.FileService;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.grant.model.GrantUserFilterDto;
import ru.ulstu.grant.repository.GrantRepository;
import ru.ulstu.project.model.Project;
import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.project.service.ProjectService;
import ru.ulstu.user.model.User;
import ru.ulstu.user.model.UserDto;
import ru.ulstu.user.service.UserService;
import java.io.IOException;
@ -141,19 +139,15 @@ public class GrantService {
}
}
public List<User> getGrantAuthors() {
return userService.findAll();
}
public List<UserDto> filterUsers(GrantUserFilterDto filterDto) {
List<User> filteredUsers = userService.filterByAgeAndDegree(filterDto.isBirthDate(), filterDto.isDegree());
if (filterDto.isLeader()) {
public List<User> getGrantAuthors(GrantDto grantDto) {
List<User> filteredUsers = userService.filterByAgeAndDegree(grantDto.isBirthDate(), grantDto.isDegree());
if (grantDto.isLeader()) {
filteredUsers = filteredUsers
.stream()
.filter(getCompletedGrantLeaders()::contains)
.collect(Collectors.toList());
}
return convert(filteredUsers, UserDto::new);
return filteredUsers;
}
private List<User> getCompletedGrantLeaders() {

@ -8,7 +8,6 @@
<body>
<div class="container" layout:fragment="content">
<section id="grant">
<div class="container">
<div class="row">
@ -95,22 +94,20 @@
<div class="row">
<div class="col">
<input class="form-check-input" type="checkbox" id="f1"
th:field="${filteredAuthors.leader}" onchange="this.form.submit();"/>
<!--path="filteredAuthors.leader"-->
th:field="*{leader}" th:onclick="|$('#filter').click();|"/>
<label class="form-check-label" for="f1">Был руководителем проекта</label>
</div>
<div class="col">
<input class="form-check-input" type="checkbox" id="f2"
th:value="${filteredAuthors.birthDate}"
onchange="this.form.submit();"/>
th:field="*{birthDate}" th:onclick="|$('#filter').click();|"/>
<label class="form-check-label" for="f2">Младше 35 лет</label>
</div>
</div>
<div class="row">
<div class="col">
<input class="form-check-input" type="checkbox" id="f3"
th:value="${filteredAuthors.degree}" onchange="this.form.submit();"/>
<label class="form-check-label" for="f3">епень к.т.н.</label> <br/>
th:field="*{degree}" th:onclick="|$('#filter').click();|"/>
<label class="form-check-label" for="f3">епень к.т.н.</label>
</div>
<div class="col">
<input class="form-check-input" type="checkbox" id="f4"/>
@ -128,6 +125,8 @@
<label class="form-check-label" for="f6">Наименьшая загруженность</label>
</div>
</div>
<input type="submit" hidden="hidden" id="filter" name="filterUsers"
value="Применить фильтр"/>
<hr/>
</div>
@ -135,7 +134,7 @@
<label>Руководитель проекта:</label>
<select class="form-control" th:field="*{leaderId}" id="leader"
onchange="updateAuthors();">
<option th:each="leader : ${filteredAuthors.users}" th:value="${leader.id}"
<option th:each="leader : ${allAuthors}" th:value="${leader.id}"
th:text="${leader.lastName}"> Руководитель
</option>
</select>
@ -146,7 +145,7 @@
<select class="selectpicker form-control" multiple="true"
title="-- Выберите участников --" id="authors"
th:field="*{authorIds}">
<option th:each="author: ${filteredAuthors.users}" th:value="${author.id}"
<option th:each="author : ${allAuthors}" th:value="${author.id}"
th:text="${author.lastName}"> Участник
</option>
</select>
@ -209,15 +208,13 @@
</script>
<script type="text/javascript">
function updateAuthors() {
//document.allAuthors.action = "${pageContext.request.contextPath}/grant";
//document.allAuthors.submit();
$("#authors").val('default');
$("#authors").selectpicker("refresh");
$("#authors").children('option:disabled').prop('disabled', false);
var lidSel = document.getElementById("leader");
var autSel = document.getElementById("authors");
autSel.options[lidSel.selectedIndex].disabled="disabled";
var lid = document.getElementById("leader");
var aut = document.getElementById("authors");
aut.options[lid.selectedIndex].disabled="disabled";
}
</script>
</div>

Loading…
Cancel
Save