diff --git a/src/main/java/ru/ulstu/grant/repository/GrantRepository.java b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java index 876a8c1..50fad13 100644 --- a/src/main/java/ru/ulstu/grant/repository/GrantRepository.java +++ b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java @@ -14,6 +14,8 @@ public interface GrantRepository extends JpaRepository, BaseRepo Grant findByTitle(String title); + Grant findGrantById(Integer grantId); + @Override @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); diff --git a/src/main/java/ru/ulstu/grant/service/GrantService.java b/src/main/java/ru/ulstu/grant/service/GrantService.java index 86f8f1b..09da77f 100644 --- a/src/main/java/ru/ulstu/grant/service/GrantService.java +++ b/src/main/java/ru/ulstu/grant/service/GrantService.java @@ -336,4 +336,8 @@ public class GrantService extends BaseService { private List findAllActive() { return grantRepository.findAllActive(); } + + public Grant findGrantById(Integer grantId) { + return grantRepository.findOne(grantId); + } } diff --git a/src/main/java/ru/ulstu/project/controller/ProjectController.java b/src/main/java/ru/ulstu/project/controller/ProjectController.java index 88f0c7a..87ae8df 100644 --- a/src/main/java/ru/ulstu/project/controller/ProjectController.java +++ b/src/main/java/ru/ulstu/project/controller/ProjectController.java @@ -10,6 +10,8 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; 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.project.model.Project; import ru.ulstu.project.model.ProjectDto; import ru.ulstu.project.service.ProjectService; @@ -46,6 +48,8 @@ public class ProjectController { @GetMapping("/project") public void getProject(ModelMap modelMap, @RequestParam(value = "id") Integer id) { if (id != null && id > 0) { + ProjectDto projectDto = projectService.findOneDto(id); + attachGrant(projectDto); modelMap.put("projectDto", projectService.findOneDto(id)); } else { modelMap.put("projectDto", new ProjectDto()); @@ -70,6 +74,12 @@ public class ProjectController { 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") public String addDeadline(@Valid ProjectDto projectDto, Errors errors) { filterEmptyDeadlines(projectDto); @@ -98,6 +108,11 @@ public class ProjectController { return projectService.getProjectExecutors(projectDto); } + @ModelAttribute("allGrants") + public List getAllGrants() { + return projectService.getAllGrants(); + } + private void filterEmptyDeadlines(ProjectDto projectDto) { projectDto.setDeadlines(projectDto.getDeadlines().stream() .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) diff --git a/src/main/java/ru/ulstu/project/model/Project.java b/src/main/java/ru/ulstu/project/model/Project.java index cb53645..27a4ae6 100644 --- a/src/main/java/ru/ulstu/project/model/Project.java +++ b/src/main/java/ru/ulstu/project/model/Project.java @@ -1,5 +1,7 @@ package ru.ulstu.project.model; +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; import org.hibernate.validator.constraints.NotBlank; import ru.ulstu.core.model.BaseEntity; 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.timeline.model.Event; import ru.ulstu.user.model.User; -import javax.persistence.CascadeType; + import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; -import javax.persistence.FetchType; +import javax.persistence.OneToMany; +import javax.persistence.CascadeType; import javax.persistence.JoinColumn; -import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; -import javax.persistence.OneToMany; +import javax.persistence.FetchType; +import javax.persistence.ManyToMany; +import javax.persistence.JoinTable; import javax.validation.constraints.NotNull; import java.util.ArrayList; import java.util.HashSet; @@ -76,6 +80,13 @@ public class Project extends BaseEntity implements UserContainer { @ManyToMany(fetch = FetchType.LAZY) private List 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 grants = new ArrayList<>(); + public String getTitle() { return title; } @@ -153,4 +164,12 @@ public class Project extends BaseEntity implements UserContainer { Set users = new HashSet(getExecutors()); return users; } + + public List getGrants() { + return grants; + } + + public void setGrants(List grants) { + this.grants = grants; + } } diff --git a/src/main/java/ru/ulstu/project/model/ProjectDto.java b/src/main/java/ru/ulstu/project/model/ProjectDto.java index affee4e..7da4ac4 100644 --- a/src/main/java/ru/ulstu/project/model/ProjectDto.java +++ b/src/main/java/ru/ulstu/project/model/ProjectDto.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.hibernate.validator.constraints.NotEmpty; import org.thymeleaf.util.StringUtils; import ru.ulstu.deadline.model.Deadline; +import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.GrantDto; import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserDto; @@ -31,6 +32,8 @@ public class ProjectDto { private List removedDeadlineIds = new ArrayList<>(); private Set executorIds; private List executors; + private List grantIds; + private List grants; private final static int MAX_EXECUTORS_LENGTH = 40; @@ -50,7 +53,9 @@ public class ProjectDto { @JsonProperty("repository") String repository, @JsonProperty("deadlines") List deadlines, @JsonProperty("executorIds") Set executorIds, - @JsonProperty("executors") List executors) { + @JsonProperty("executors") List executors, + @JsonProperty("grantIds") List grantIds, + @JsonProperty("grants") List grants) { this.id = id; this.title = title; this.status = status; @@ -61,6 +66,8 @@ public class ProjectDto { this.applicationFileName = null; this.executorIds = executorIds; this.executors = executors; + this.grantIds = grantIds; + this.grants = grants; } @@ -76,6 +83,8 @@ public class ProjectDto { this.deadlines = project.getDeadlines(); this.executorIds = convert(users, user -> user.getId()); 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() { @@ -172,4 +181,20 @@ public class ProjectDto { .map(executor -> executor.getLastName()) .collect(Collectors.joining(", ")), MAX_EXECUTORS_LENGTH); } + + public List getGrantIds() { + return grantIds; + } + + public void setGrantIds(List grantIds) { + this.grantIds = grantIds; + } + + public List getGrants() { + return grants; + } + + public void setGrants(List grants) { + this.grants = grants; + } } diff --git a/src/main/java/ru/ulstu/project/service/ProjectService.java b/src/main/java/ru/ulstu/project/service/ProjectService.java index 30b5f01..fe0671c 100644 --- a/src/main/java/ru/ulstu/project/service/ProjectService.java +++ b/src/main/java/ru/ulstu/project/service/ProjectService.java @@ -5,6 +5,7 @@ import org.springframework.transaction.annotation.Transactional; import org.thymeleaf.util.StringUtils; import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.file.service.FileService; +import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.repository.GrantRepository; import ru.ulstu.project.model.Project; import ru.ulstu.project.model.ProjectDto; @@ -16,6 +17,7 @@ import ru.ulstu.user.service.UserService; import java.io.IOException; import java.util.Arrays; import java.util.List; +import java.util.Set; import static org.springframework.util.ObjectUtils.isEmpty; import static ru.ulstu.core.util.StreamApiUtils.convert; @@ -104,6 +106,10 @@ public class ProjectService { if (projectDto.getApplicationFileName() != null) { 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; } @@ -131,4 +137,22 @@ public class ProjectService { return users; } + public List getAllGrants() { + List grants = convert(grantRepository.findAll(), GrantDto::new); + return grants; + } + + public List getProjectGrants(List 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(); + } + } + } diff --git a/src/main/java/ru/ulstu/timeline/service/EventService.java b/src/main/java/ru/ulstu/timeline/service/EventService.java index eef38eb..8f5376e 100644 --- a/src/main/java/ru/ulstu/timeline/service/EventService.java +++ b/src/main/java/ru/ulstu/timeline/service/EventService.java @@ -225,7 +225,7 @@ public class EventService { if (newProject.getExecutors() != null) { newEvent.setRecipients(new ArrayList(newProject.getExecutors())); } - newEvent.getRecipients().add((User) newProject.getExecutors()); +// newEvent.getRecipients().add((User) newProject.getExecutors()); newEvent.setProject(newProject); eventRepository.save(newEvent); diff --git a/src/main/resources/db/changelog-20190529_000000-schema.xml b/src/main/resources/db/changelog-20190529_000000-schema.xml new file mode 100644 index 0000000..40079ff --- /dev/null +++ b/src/main/resources/db/changelog-20190529_000000-schema.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml index 1cede5f..6dadded 100644 --- a/src/main/resources/db/changelog-master.xml +++ b/src/main/resources/db/changelog-master.xml @@ -50,4 +50,5 @@ + \ No newline at end of file diff --git a/src/main/resources/templates/projects/project.html b/src/main/resources/templates/projects/project.html index 2c4a67b..67dac69 100644 --- a/src/main/resources/templates/projects/project.html +++ b/src/main/resources/templates/projects/project.html @@ -51,12 +51,18 @@
- -
- + +
+
+ +
-