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

@ -29,6 +29,9 @@ public class GrantDto {
private Set<Integer> authorIds; private Set<Integer> authorIds;
private Set<UserDto> authors; private Set<UserDto> authors;
private Integer leaderId; private Integer leaderId;
private boolean leader;
private boolean birthDate;
private boolean degree;
public GrantDto() { public GrantDto() {
deadlines.add(new Deadline()); deadlines.add(new Deadline());
@ -43,7 +46,10 @@ public class GrantDto {
@JsonProperty("project") ProjectDto project, @JsonProperty("project") ProjectDto project,
@JsonProperty("authorIds") Set<Integer> authorIds, @JsonProperty("authorIds") Set<Integer> authorIds,
@JsonProperty("authors") Set<UserDto> authors, @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.id = id;
this.title = title; this.title = title;
this.status = status; this.status = status;
@ -53,6 +59,9 @@ public class GrantDto {
this.project = project; this.project = project;
this.authors = authors; this.authors = authors;
this.leaderId = leaderId; this.leaderId = leaderId;
this.leader = leader;
this.birthDate = birthDate;
this.degree = degree;
} }
public GrantDto(Grant grant) { public GrantDto(Grant grant) {
@ -66,6 +75,9 @@ public class GrantDto {
this.authorIds = convert(grant.getAuthors(), user -> user.getId()); this.authorIds = convert(grant.getAuthors(), user -> user.getId());
this.authors = convert(grant.getAuthors(), UserDto::new); this.authors = convert(grant.getAuthors(), UserDto::new);
this.leaderId = grant.getLeader().getId(); this.leaderId = grant.getLeader().getId();
this.leader = false;
this.birthDate = false;
this.degree = false;
} }
public Integer getId() { public Integer getId() {
@ -154,4 +166,28 @@ public class GrantDto {
public void setLeaderId(Integer leaderId) { public void setLeaderId(Integer leaderId) {
this.leaderId = 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; package ru.ulstu.grant.repository;
import org.springframework.data.jpa.repository.JpaRepository; 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.grant.model.Grant;
import ru.ulstu.user.model.User;
import java.util.List; import java.util.List;

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

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

Loading…
Cancel
Save