#41 create simple attach of articles

This commit is contained in:
T-Midnight 2019-04-19 17:49:36 +04:00
parent f29d31b840
commit eec739a49f
5 changed files with 76 additions and 6 deletions

View File

@ -13,6 +13,7 @@ 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.service.GrantService; import ru.ulstu.grant.service.GrantService;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
@ -113,6 +114,11 @@ public class GrantController {
return grantService.getGrantAuthors(grantDto); return grantService.getGrantAuthors(grantDto);
} }
@ModelAttribute("allPapers")
public List<Paper> getGrantPapers() {
return grantService.getGrantPapers();
}
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()))

View File

@ -5,6 +5,7 @@ import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.core.model.UserContainer; import ru.ulstu.core.model.UserContainer;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileData; import ru.ulstu.file.model.FileData;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.project.model.Project; import ru.ulstu.project.model.Project;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
@ -14,6 +15,7 @@ import javax.persistence.EnumType;
import javax.persistence.Enumerated; import javax.persistence.Enumerated;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany; import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
@ -83,6 +85,12 @@ public class Grant extends BaseEntity implements UserContainer {
@JoinColumn(name = "leader_id") @JoinColumn(name = "leader_id")
private User leader; private User leader;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "grants_papers",
joinColumns = {@JoinColumn(name = "grant_id")},
inverseJoinColumns = {@JoinColumn(name = "paper_id")})
private List<Paper> papers = new ArrayList<>();
public GrantStatus getStatus() { public GrantStatus getStatus() {
return status; return status;
} }
@ -152,6 +160,14 @@ public class Grant extends BaseEntity implements UserContainer {
this.leader = leader; this.leader = leader;
} }
public List<Paper> getPapers() {
return papers;
}
public void setPapers(List<Paper> papers) {
this.papers = papers;
}
public Optional<Deadline> getNextDeadline() { public Optional<Deadline> getNextDeadline() {
return deadlines return deadlines
.stream() .stream()

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotEmpty; import org.hibernate.validator.constraints.NotEmpty;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.project.model.ProjectDto; import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserDto;
@ -32,6 +33,8 @@ public class GrantDto {
private boolean wasLeader; private boolean wasLeader;
private boolean hasAge; private boolean hasAge;
private boolean hasDegree; private boolean hasDegree;
private List<Integer> paperIds = new ArrayList<>();
private List<PaperDto> papers = new ArrayList<>();
public GrantDto() { public GrantDto() {
deadlines.add(new Deadline()); deadlines.add(new Deadline());
@ -49,7 +52,9 @@ public class GrantDto {
@JsonProperty("leader") Integer leaderId, @JsonProperty("leader") Integer leaderId,
@JsonProperty("wasLeader") boolean wasLeader, @JsonProperty("wasLeader") boolean wasLeader,
@JsonProperty("hasAge") boolean hasAge, @JsonProperty("hasAge") boolean hasAge,
@JsonProperty("hasDegree") boolean hasDegree) { @JsonProperty("hasDegree") boolean hasDegree,
@JsonProperty("paperIds") List<Integer> paperIds,
@JsonProperty("papers") List<PaperDto> papers) {
this.id = id; this.id = id;
this.title = title; this.title = title;
this.status = status; this.status = status;
@ -62,6 +67,8 @@ public class GrantDto {
this.wasLeader = wasLeader; this.wasLeader = wasLeader;
this.hasAge = hasAge; this.hasAge = hasAge;
this.hasDegree = hasDegree; this.hasDegree = hasDegree;
this.paperIds = paperIds;
this.papers = papers;
} }
public GrantDto(Grant grant) { public GrantDto(Grant grant) {
@ -78,6 +85,8 @@ public class GrantDto {
this.wasLeader = false; this.wasLeader = false;
this.hasAge = false; this.hasAge = false;
this.hasDegree = false; this.hasDegree = false;
this.paperIds = convert(grant.getPapers(), paper -> paper.getId());
this.papers = convert(grant.getPapers(), PaperDto::new);
} }
public Integer getId() { public Integer getId() {
@ -190,4 +199,20 @@ public class GrantDto {
public void setHasDegree(boolean hasDegree) { public void setHasDegree(boolean hasDegree) {
this.hasDegree = hasDegree; this.hasDegree = hasDegree;
} }
public List<Integer> getPaperIds() {
return paperIds;
}
public void setPaperIds(List<Integer> paperIds) {
this.paperIds = paperIds;
}
public List<PaperDto> getPapers() {
return papers;
}
public void setPapers(List<PaperDto> papers) {
this.papers = papers;
}
} }

View File

@ -9,6 +9,8 @@ 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.repository.GrantRepository; import ru.ulstu.grant.repository.GrantRepository;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.service.PaperService;
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;
@ -34,17 +36,20 @@ public class GrantService {
private final DeadlineService deadlineService; private final DeadlineService deadlineService;
private final FileService fileService; private final FileService fileService;
private final UserService userService; private final UserService userService;
private final PaperService paperService;
public GrantService(GrantRepository grantRepository, public GrantService(GrantRepository grantRepository,
FileService fileService, FileService fileService,
DeadlineService deadlineService, DeadlineService deadlineService,
ProjectService projectService, ProjectService projectService,
UserService userService) { UserService userService,
PaperService paperService) {
this.grantRepository = grantRepository; this.grantRepository = grantRepository;
this.fileService = fileService; this.fileService = fileService;
this.deadlineService = deadlineService; this.deadlineService = deadlineService;
this.projectService = projectService; this.projectService = projectService;
this.userService = userService; this.userService = userService;
this.paperService = paperService;
} }
public List<Grant> findAll() { public List<Grant> findAll() {
@ -86,6 +91,11 @@ public class GrantService {
if (grantDto.getLeaderId() != null) { if (grantDto.getLeaderId() != null) {
grant.setLeader(userService.findById(grantDto.getLeaderId())); grant.setLeader(userService.findById(grantDto.getLeaderId()));
} }
grant.getPapers().clear();
if (grantDto.getPaperIds() != null && !grantDto.getAuthorIds().isEmpty()) {
grantDto.getPaperIds().forEach(paperIds ->
grant.getPapers().add(paperService.findEntityById(paperIds)));
}
return grant; return grant;
} }
@ -118,7 +128,7 @@ public class GrantService {
} }
@Transactional @Transactional
public Grant create(String title, Project projectId, Date deadlineDate, User user) { public Grant create(String title, Project projectId, Date deadlineDate, User user, Paper paper) {
Grant grant = new Grant(); Grant grant = new Grant();
grant.setTitle(title); grant.setTitle(title);
grant.setComment("Комментарий к гранту 1"); grant.setComment("Комментарий к гранту 1");
@ -127,6 +137,7 @@ public class GrantService {
grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн")); grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн"));
grant.getAuthors().add(user); grant.getAuthors().add(user);
grant.setLeader(user); grant.setLeader(user);
grant.getPapers().add(paper);
grant = grantRepository.save(grant); grant = grantRepository.save(grant);
return grant; return grant;
} }
@ -156,4 +167,9 @@ public class GrantService {
.map(Grant::getLeader) .map(Grant::getLeader)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public List<Paper> getGrantPapers() {
return paperService.findAll();
}
} }

View File

@ -144,7 +144,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label>Участники гранта:</label> <label>Участники гранта:</label>
<select class="selectpicker form-control" multiple="true" <select class="selectpicker form-control" multiple="true" data-live-search="true"
title="-- Выберите участников --" id="authors" title="-- Выберите участников --" id="authors"
th:field="*{authorIds}"> th:field="*{authorIds}">
<option th:each="author : ${allAuthors}" th:value="${author.id}" <option th:each="author : ${allAuthors}" th:value="${author.id}"
@ -154,9 +154,16 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label>Список статей:</label> <label>Список статей:</label>
<p><a href="./#" class="btn btn-primary"><i class="fa fa-plus-circle" <select class="selectpicker form-control" multiple="true" data-live-search="true"
title="Прикрепить статью" data-style="btn-primary" id="papers"
th:field="*{paperIds}">
<option th:each="paper : ${allPapers}" th:value="${paper.id}"
th:text="${paper.title}"> Статья
</option>
</select>
<!-- <p><a href="./#" class="btn btn-primary"><i class="fa fa-plus-circle"
aria-hidden="true"> aria-hidden="true">
</i> Добавить статью</a></p> </i> Добавить статью</a></p> -->
</div> </div>
<div class="form-group"> <div class="form-group">
<div th:if="*{project} == null"> <div th:if="*{project} == null">