#101 grant attach

This commit is contained in:
a.vasin 2019-05-29 23:40:30 +04:00
parent 192adbfa0b
commit 9013d79228
10 changed files with 125 additions and 12 deletions

View File

@ -14,6 +14,8 @@ public interface GrantRepository extends JpaRepository<Grant, Integer>, BaseRepo
Grant findByTitle(String title); Grant findByTitle(String title);
Grant findGrantById(Integer grantId);
@Override @Override
@Query("SELECT title FROM Grant g WHERE (g.title = :name) AND (:id IS NULL OR g.id != :id) ") @Query("SELECT title FROM Grant g WHERE (g.title = :name) AND (:id IS NULL OR g.id != :id) ")
String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id); String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);

View File

@ -336,4 +336,8 @@ public class GrantService extends BaseService {
private List<Grant> findAllActive() { private List<Grant> findAllActive() {
return grantRepository.findAllActive(); return grantRepository.findAllActive();
} }
public Grant findGrantById(Integer grantId) {
return grantRepository.findOne(grantId);
}
} }

View File

@ -10,6 +10,8 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; 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.GrantDto;
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;
@ -46,6 +48,8 @@ public class ProjectController {
@GetMapping("/project") @GetMapping("/project")
public void getProject(ModelMap modelMap, @RequestParam(value = "id") Integer id) { public void getProject(ModelMap modelMap, @RequestParam(value = "id") Integer id) {
if (id != null && id > 0) { if (id != null && id > 0) {
ProjectDto projectDto = projectService.findOneDto(id);
attachGrant(projectDto);
modelMap.put("projectDto", projectService.findOneDto(id)); modelMap.put("projectDto", projectService.findOneDto(id));
} else { } else {
modelMap.put("projectDto", new ProjectDto()); modelMap.put("projectDto", new ProjectDto());
@ -70,6 +74,12 @@ public class ProjectController {
return String.format("redirect:%s", "/projects/projects"); return String.format("redirect:%s", "/projects/projects");
} }
@PostMapping(value = "/project", params = "attachGrant")
public String attachGrant(ProjectDto projectDto) {
projectService.attachGrant(projectDto);
return "/projects/project";
}
@PostMapping(value = "/project", params = "addDeadline") @PostMapping(value = "/project", params = "addDeadline")
public String addDeadline(@Valid ProjectDto projectDto, Errors errors) { public String addDeadline(@Valid ProjectDto projectDto, Errors errors) {
filterEmptyDeadlines(projectDto); filterEmptyDeadlines(projectDto);
@ -98,6 +108,11 @@ public class ProjectController {
return projectService.getProjectExecutors(projectDto); return projectService.getProjectExecutors(projectDto);
} }
@ModelAttribute("allGrants")
public List<GrantDto> getAllGrants() {
return projectService.getAllGrants();
}
private void filterEmptyDeadlines(ProjectDto projectDto) { private void filterEmptyDeadlines(ProjectDto projectDto) {
projectDto.setDeadlines(projectDto.getDeadlines().stream() projectDto.setDeadlines(projectDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))

View File

@ -1,5 +1,7 @@
package ru.ulstu.project.model; package ru.ulstu.project.model;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity; import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.core.model.UserContainer; import ru.ulstu.core.model.UserContainer;
@ -8,15 +10,17 @@ import ru.ulstu.file.model.FileData;
import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.Grant;
import ru.ulstu.timeline.model.Event; import ru.ulstu.timeline.model.Event;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EnumType; import javax.persistence.EnumType;
import javax.persistence.Enumerated; import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.CascadeType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.JoinTable;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
@ -76,6 +80,13 @@ public class Project extends BaseEntity implements UserContainer {
@ManyToMany(fetch = FetchType.LAZY) @ManyToMany(fetch = FetchType.LAZY)
private List<User> executors = new ArrayList<>(); private List<User> executors = new ArrayList<>();
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "project_grants",
joinColumns = {@JoinColumn(name = "project_id")},
inverseJoinColumns = {@JoinColumn(name = "grants_id")})
@Fetch(FetchMode.SUBSELECT)
private List<Grant> grants = new ArrayList<>();
public String getTitle() { public String getTitle() {
return title; return title;
} }
@ -153,4 +164,12 @@ public class Project extends BaseEntity implements UserContainer {
Set<User> users = new HashSet<User>(getExecutors()); Set<User> users = new HashSet<User>(getExecutors());
return users; return users;
} }
public List<Grant> getGrants() {
return grants;
}
public void setGrants(List<Grant> grants) {
this.grants = grants;
}
} }

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty; import org.hibernate.validator.constraints.NotEmpty;
import org.thymeleaf.util.StringUtils; import org.thymeleaf.util.StringUtils;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserDto;
@ -31,6 +32,8 @@ public class ProjectDto {
private List<Integer> removedDeadlineIds = new ArrayList<>(); private List<Integer> removedDeadlineIds = new ArrayList<>();
private Set<Integer> executorIds; private Set<Integer> executorIds;
private List<UserDto> executors; private List<UserDto> executors;
private List<Integer> grantIds;
private List<GrantDto> grants;
private final static int MAX_EXECUTORS_LENGTH = 40; private final static int MAX_EXECUTORS_LENGTH = 40;
@ -50,7 +53,9 @@ public class ProjectDto {
@JsonProperty("repository") String repository, @JsonProperty("repository") String repository,
@JsonProperty("deadlines") List<Deadline> deadlines, @JsonProperty("deadlines") List<Deadline> deadlines,
@JsonProperty("executorIds") Set<Integer> executorIds, @JsonProperty("executorIds") Set<Integer> executorIds,
@JsonProperty("executors") List<UserDto> executors) { @JsonProperty("executors") List<UserDto> executors,
@JsonProperty("grantIds") List<Integer> grantIds,
@JsonProperty("grants") List<GrantDto> grants) {
this.id = id; this.id = id;
this.title = title; this.title = title;
this.status = status; this.status = status;
@ -61,6 +66,8 @@ public class ProjectDto {
this.applicationFileName = null; this.applicationFileName = null;
this.executorIds = executorIds; this.executorIds = executorIds;
this.executors = executors; this.executors = executors;
this.grantIds = grantIds;
this.grants = grants;
} }
@ -76,6 +83,8 @@ public class ProjectDto {
this.deadlines = project.getDeadlines(); this.deadlines = project.getDeadlines();
this.executorIds = convert(users, user -> user.getId()); this.executorIds = convert(users, user -> user.getId());
this.executors = convert(project.getExecutors(), UserDto::new); this.executors = convert(project.getExecutors(), UserDto::new);
this.grantIds = convert(project.getGrants(), grant -> grant.getId());
this.grants = convert(project.getGrants(), GrantDto::new);
} }
public Integer getId() { public Integer getId() {
@ -172,4 +181,20 @@ public class ProjectDto {
.map(executor -> executor.getLastName()) .map(executor -> executor.getLastName())
.collect(Collectors.joining(", ")), MAX_EXECUTORS_LENGTH); .collect(Collectors.joining(", ")), MAX_EXECUTORS_LENGTH);
} }
public List<Integer> getGrantIds() {
return grantIds;
}
public void setGrantIds(List<Integer> grantIds) {
this.grantIds = grantIds;
}
public List<GrantDto> getGrants() {
return grants;
}
public void setGrants(List<GrantDto> grants) {
this.grants = grants;
}
} }

View File

@ -5,6 +5,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils; import org.thymeleaf.util.StringUtils;
import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.file.service.FileService; import ru.ulstu.file.service.FileService;
import ru.ulstu.grant.model.GrantDto;
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;
@ -16,6 +17,7 @@ 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.List; import java.util.List;
import java.util.Set;
import static org.springframework.util.ObjectUtils.isEmpty; import static org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.core.util.StreamApiUtils.convert;
@ -104,6 +106,10 @@ public class ProjectService {
if (projectDto.getApplicationFileName() != null) { if (projectDto.getApplicationFileName() != null) {
project.setApplication(fileService.createFileFromTmp(projectDto.getApplicationFileName())); project.setApplication(fileService.createFileFromTmp(projectDto.getApplicationFileName()));
} }
project.getGrants().clear();
if (projectDto.getGrantIds() != null && !projectDto.getGrantIds().isEmpty()) {
projectDto.getGrantIds().forEach(grantIds -> project.getGrants().add(grantRepository.findGrantById(grantIds)));
}
return project; return project;
} }
@ -131,4 +137,22 @@ public class ProjectService {
return users; return users;
} }
public List<GrantDto> getAllGrants() {
List<GrantDto> grants = convert(grantRepository.findAll(), GrantDto::new);
return grants;
}
public List<GrantDto> getProjectGrants(List<Integer> grantIds) {
return convert(grantRepository.findAll(grantIds), GrantDto::new);
}
public void attachGrant(ProjectDto projectDto) {
if (!projectDto.getGrantIds().isEmpty()) {
projectDto.getGrants().clear();
projectDto.setGrants(getProjectGrants(projectDto.getGrantIds()));
} else {
projectDto.getGrants().clear();
}
}
} }

View File

@ -225,7 +225,7 @@ public class EventService {
if (newProject.getExecutors() != null) { if (newProject.getExecutors() != null) {
newEvent.setRecipients(new ArrayList(newProject.getExecutors())); newEvent.setRecipients(new ArrayList(newProject.getExecutors()));
} }
newEvent.getRecipients().add((User) newProject.getExecutors()); // newEvent.getRecipients().add((User) newProject.getExecutors());
newEvent.setProject(newProject); newEvent.setProject(newProject);
eventRepository.save(newEvent); eventRepository.save(newEvent);

View File

@ -0,0 +1,17 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet author="anton" id="20190528_000000-3">
<createTable tableName="project_grants">
<column name="project_id" type="integer"/>
<column name="grants_id" type="integer"/>
</createTable>
<addForeignKeyConstraint baseTableName="project_grants" baseColumnNames="project_id"
constraintName="fk_project_project_grants" referencedTableName="project"
referencedColumnNames="id"/>
<addForeignKeyConstraint baseTableName="project_grants" baseColumnNames="grants_id"
constraintName="fk_grant_project_grants" referencedTableName="grants"
referencedColumnNames="id"/>
</changeSet>
</databaseChangeLog>

View File

@ -50,4 +50,5 @@
<include file="db/changelog-20190523_000000-schema.xml"/> <include file="db/changelog-20190523_000000-schema.xml"/>
<include file="db/changelog-20190528_000000-schema.xml"/> <include file="db/changelog-20190528_000000-schema.xml"/>
<include file="db/changelog-20190528_000002-schema.xml"/> <include file="db/changelog-20190528_000002-schema.xml"/>
<include file="db/changelog-20190529_000000-schema.xml"/>
</databaseChangeLog> </databaseChangeLog>

View File

@ -51,12 +51,18 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="createGrant">Добавить грант:</label> <label>Добавить грант:</label>
<div th:if="*{grant} == null"> <div class="row">
<input type="submit" id="createGrant" name="createGrant" class="btn btn-primary" <div class="col-10">
value="Добавить грант"/> <select class="selectpicker form-control" data-live-search="true"
title="-- Прикрепить грант --" id="allGrants"
th:field="*{grantIds}" data-size="5">
<option th:each="grant : ${allGrants}" th:value="${grant.id}"
th:text="${grant.title}"> Грант для прикрепления
</option>
</select>
</div>
</div> </div>
<input type="hidden" th:field="*{grant.id}"/>
</div> </div>
<div class="form-group"> <div class="form-group">