From 8a7117d713e4d547d6b6e4e706def91f9a220b32 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Thu, 4 Apr 2019 09:38:18 +0400 Subject: [PATCH] #42 Add leader and authors --- .../grant/controller/GrantController.java | 10 +++ src/main/java/ru/ulstu/grant/model/Grant.java | 39 ++++++++- .../java/ru/ulstu/grant/model/GrantDto.java | 52 +++++++++++- .../ru/ulstu/grant/service/GrantService.java | 26 ++++-- .../db/changelog-20190402_000000-schema.xml | 26 ++++++ src/main/resources/public/css/grant.css | 12 +++ .../grants/fragments/grantLineFragment.html | 2 +- .../grants/fragments/grantStatusFragment.html | 2 +- .../resources/templates/grants/grant.html | 81 +++++++++++++++++-- 9 files changed, 233 insertions(+), 17 deletions(-) create mode 100644 src/main/resources/db/changelog-20190402_000000-schema.xml create mode 100644 src/main/resources/public/css/grant.css diff --git a/src/main/java/ru/ulstu/grant/controller/GrantController.java b/src/main/java/ru/ulstu/grant/controller/GrantController.java index c786ffa..8481c07 100644 --- a/src/main/java/ru/ulstu/grant/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/controller/GrantController.java @@ -13,6 +13,7 @@ import ru.ulstu.deadline.model.Deadline; import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.service.GrantService; +import ru.ulstu.user.model.User; import javax.validation.Valid; import java.io.IOException; @@ -57,9 +58,13 @@ public class GrantController { @PostMapping(value = "/grant", params = "save") public String save(@Valid GrantDto grantDto, Errors errors) throws IOException { filterEmptyDeadlines(grantDto); + // фильтр авторов if (grantDto.getDeadlines().isEmpty()) { errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); } +// if (grantDto.getLeader().getId().equals(null)) { +// errors.rejectValue("leader", "errorCode", "Укажите руководителя"); +// } hasErrors(errors, GRANT_PAGE); grantService.save(grantDto); return String.format(REDIRECT_TO, GRANTS_PAGE); @@ -91,6 +96,11 @@ public class GrantController { return grantService.getGrantStatuses(); } + @ModelAttribute("allAuthors") + public List getAllAuthors() { + return grantService.getGrantAuthors(); + } + private void filterEmptyDeadlines(GrantDto grantDto) { grantDto.setDeadlines(grantDto.getDeadlines().stream() .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) diff --git a/src/main/java/ru/ulstu/grant/model/Grant.java b/src/main/java/ru/ulstu/grant/model/Grant.java index 685a1d5..9b1e8fe 100644 --- a/src/main/java/ru/ulstu/grant/model/Grant.java +++ b/src/main/java/ru/ulstu/grant/model/Grant.java @@ -2,28 +2,35 @@ package ru.ulstu.grant.model; import org.hibernate.validator.constraints.NotBlank; import ru.ulstu.core.model.BaseEntity; +import ru.ulstu.core.model.UserContainer; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.file.model.FileData; import ru.ulstu.project.model.Project; +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.JoinColumn; +import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; +import javax.persistence.OrderBy; import javax.persistence.Table; import javax.validation.constraints.NotNull; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; +import java.util.HashSet; import java.util.List; import java.util.Optional; +import java.util.Set; @Entity @Table(name = "grants") -public class Grant extends BaseEntity { +public class Grant extends BaseEntity implements UserContainer { public enum GrantStatus { APPLICATION("Заявка"), ON_COMPETITION("Отправлен на конкурс"), @@ -52,6 +59,7 @@ public class Grant extends BaseEntity { @OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "grant_id") + @OrderBy("date") private List deadlines = new ArrayList<>(); //Описание гранта @@ -67,6 +75,14 @@ public class Grant extends BaseEntity { @JoinColumn(name = "project_id") private Project project; + @ManyToMany(fetch = FetchType.EAGER) + private Set authors = new HashSet<>(); + + @NotNull + @ManyToOne + @JoinColumn(name = "leader_id") + private User leader; + public GrantStatus getStatus() { return status; } @@ -115,6 +131,27 @@ public class Grant extends BaseEntity { this.project = project; } + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } + + @Override + public Set getUsers() { + return getAuthors(); + } + + public User getLeader() { + return leader; + } + + public void setLeader(User leader) { + this.leader = leader; + } + public Optional getNextDeadline() { return deadlines .stream() diff --git a/src/main/java/ru/ulstu/grant/model/GrantDto.java b/src/main/java/ru/ulstu/grant/model/GrantDto.java index fb29164..cc0f2da 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantDto.java @@ -2,14 +2,22 @@ package ru.ulstu.grant.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.commons.lang3.StringUtils; import org.hibernate.validator.constraints.NotEmpty; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.project.model.ProjectDto; +import ru.ulstu.user.model.UserDto; import java.util.ArrayList; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import static ru.ulstu.core.util.StreamApiUtils.convert; public class GrantDto { + private final static int MAX_AUTHORS_LENGTH = 60; + private Integer id; @NotEmpty private String title; @@ -18,6 +26,9 @@ public class GrantDto { private String comment; private String applicationFileName; private ProjectDto project; + private Set authorIds; + private Set authors; + private Integer leaderId; public GrantDto() { deadlines.add(new Deadline()); @@ -29,7 +40,10 @@ public class GrantDto { @JsonProperty("status") Grant.GrantStatus status, @JsonProperty("deadlines") List deadlines, @JsonProperty("comment") String comment, - @JsonProperty("project") ProjectDto project) { + @JsonProperty("project") ProjectDto project, + @JsonProperty("authorIds") Set authorIds, + @JsonProperty("authors") Set authors, + @JsonProperty("leader") Integer leaderId) { this.id = id; this.title = title; this.status = status; @@ -37,6 +51,8 @@ public class GrantDto { this.comment = comment; this.applicationFileName = null; this.project = project; + this.authors = authors; + this.leaderId = leaderId; } public GrantDto(Grant grant) { @@ -47,6 +63,9 @@ public class GrantDto { this.comment = grant.getComment(); this.project = grant.getProject() == null ? null : new ProjectDto(grant.getProject()); this.applicationFileName = grant.getApplication() == null ? null : grant.getApplication().getName(); + this.authorIds = convert(grant.getAuthors(), user -> user.getId()); + this.authors = convert(grant.getAuthors(), UserDto::new); + this.leaderId = grant.getLeader().getId(); } public Integer getId() { @@ -104,4 +123,35 @@ public class GrantDto { public void setApplicationFileName(String applicationFileName) { this.applicationFileName = applicationFileName; } + + public Set getAuthorIds() { + return authorIds; + } + + public void setAuthorIds(Set authorIds) { + this.authorIds = authorIds; + } + + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } + + public String getAuthorsString() { + return StringUtils.abbreviate(authors + .stream() + .map(author -> author.getLastName()) + .collect(Collectors.joining(", ")), MAX_AUTHORS_LENGTH); + } + + public Integer getLeaderId() { + return leaderId; + } + + public void setLeaderId(Integer leaderId) { + this.leaderId = leaderId; + } } diff --git a/src/main/java/ru/ulstu/grant/service/GrantService.java b/src/main/java/ru/ulstu/grant/service/GrantService.java index d5beec3..cbf37ce 100644 --- a/src/main/java/ru/ulstu/grant/service/GrantService.java +++ b/src/main/java/ru/ulstu/grant/service/GrantService.java @@ -12,6 +12,8 @@ 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.service.UserService; import java.io.IOException; import java.util.Arrays; @@ -30,15 +32,18 @@ public class GrantService { private final ProjectService projectService; private final DeadlineService deadlineService; private final FileService fileService; + private final UserService userService; public GrantService(GrantRepository grantRepository, FileService fileService, DeadlineService deadlineService, - ProjectService projectService) { + ProjectService projectService, + UserService userService) { this.grantRepository = grantRepository; - this.projectService = projectService; this.fileService = fileService; this.deadlineService = deadlineService; + this.projectService = projectService; + this.userService = userService; } public List findAll() { @@ -73,6 +78,13 @@ public class GrantService { if (grantDto.getApplicationFileName() != null) { grant.setApplication(fileService.createFileFromTmp(grantDto.getApplicationFileName())); } + grant.getAuthors().clear(); + if (grantDto.getAuthorIds() != null && !grantDto.getAuthorIds().isEmpty()) { + grantDto.getAuthorIds().forEach(authorIds -> grant.getAuthors().add(userService.findById(authorIds))); + } + if (grantDto.getLeaderId() != null) { + grant.setLeader(userService.findById(grantDto.getLeaderId())); + } return grant; } @@ -84,7 +96,6 @@ public class GrantService { @Transactional public Integer update(GrantDto grantDto) throws IOException { Grant grant = grantRepository.findOne(grantDto.getId()); - Grant.GrantStatus oldStatus = grant.getStatus(); if (grantDto.getApplicationFileName() != null && grant.getApplication() != null) { fileService.deleteFile(grant.getApplication()); } @@ -98,7 +109,6 @@ public class GrantService { if (grant.getApplication() != null) { fileService.deleteFile(grant.getApplication()); } - //возможно при удалении гранта будет удаляться и проект, к нему привязанный grantRepository.delete(grant); } @@ -107,13 +117,15 @@ public class GrantService { } @Transactional - public Grant create(String title, Project projectId, Date deadlineDate) { + public Grant create(String title, Project projectId, Date deadlineDate, User user) { Grant grant = new Grant(); grant.setTitle(title); grant.setComment("Комментарий к гранту 1"); grant.setProject(projectId); grant.setStatus(APPLICATION); grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн")); + grant.getAuthors().add(user); + grant.setLeader(user); grant = grantRepository.save(grant); return grant; } @@ -125,4 +137,8 @@ public class GrantService { update(grantDto); } } + + public List getGrantAuthors() { + return userService.findAll(); + } } diff --git a/src/main/resources/db/changelog-20190402_000000-schema.xml b/src/main/resources/db/changelog-20190402_000000-schema.xml new file mode 100644 index 0000000..70a7991 --- /dev/null +++ b/src/main/resources/db/changelog-20190402_000000-schema.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/public/css/grant.css b/src/main/resources/public/css/grant.css new file mode 100644 index 0000000..2c95628 --- /dev/null +++ b/src/main/resources/public/css/grant.css @@ -0,0 +1,12 @@ +.div-deadline-date { + padding-right: 0px; +} + +.form-deadline-date{ + padding-right: 3px; +} + +.div-deadline-description{ + padding-left: 5px; + padding-right: 5px; +} \ No newline at end of file diff --git a/src/main/resources/templates/grants/fragments/grantLineFragment.html b/src/main/resources/templates/grants/fragments/grantLineFragment.html index 1048897..dda0e8d 100644 --- a/src/main/resources/templates/grants/fragments/grantLineFragment.html +++ b/src/main/resources/templates/grants/fragments/grantLineFragment.html @@ -9,7 +9,7 @@ - + - +
diff --git a/src/main/resources/templates/grants/grant.html b/src/main/resources/templates/grants/grant.html index fc2878d..aae3b02 100644 --- a/src/main/resources/templates/grants/grant.html +++ b/src/main/resources/templates/grants/grant.html @@ -3,7 +3,7 @@ xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/html"> - + @@ -17,6 +17,7 @@
+