180 lines
4.5 KiB
Java
180 lines
4.5 KiB
Java
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.paper.model.Paper;
|
||
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.JoinTable;
|
||
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 implements UserContainer {
|
||
public enum GrantStatus {
|
||
APPLICATION("Заявка"),
|
||
ON_COMPETITION("Отправлен на конкурс"),
|
||
SUCCESSFUL_PASSAGE("Успешное прохождение"),
|
||
IN_WORK("В работе"),
|
||
COMPLETED("Завершен"),
|
||
FAILED("Провалены сроки"),
|
||
LOADED_FROM_KIAS("Загружен автоматически");
|
||
|
||
private String statusName;
|
||
|
||
GrantStatus(String statusName) {
|
||
this.statusName = statusName;
|
||
}
|
||
|
||
public String getStatusName() {
|
||
return statusName;
|
||
}
|
||
}
|
||
|
||
@NotBlank
|
||
private String title;
|
||
|
||
@Enumerated(value = EnumType.STRING)
|
||
private GrantStatus status = GrantStatus.APPLICATION;
|
||
|
||
@OneToMany(cascade = CascadeType.ALL)
|
||
@JoinColumn(name = "grant_id")
|
||
@OrderBy("date")
|
||
private List<Deadline> deadlines = new ArrayList<>();
|
||
|
||
//Описание гранта
|
||
@NotNull
|
||
private String comment;
|
||
|
||
//Заявка на грант
|
||
@ManyToOne
|
||
@JoinColumn(name = "file_id")
|
||
private FileData application;
|
||
|
||
@ManyToOne(cascade = CascadeType.ALL)
|
||
@JoinColumn(name = "project_id")
|
||
private Project project;
|
||
|
||
@ManyToMany(fetch = FetchType.EAGER)
|
||
private Set<User> authors = new HashSet<>();
|
||
|
||
@NotNull
|
||
@ManyToOne
|
||
@JoinColumn(name = "leader_id")
|
||
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() {
|
||
return status;
|
||
}
|
||
|
||
public void setStatus(GrantStatus status) {
|
||
this.status = status;
|
||
}
|
||
|
||
public List<Deadline> getDeadlines() {
|
||
return deadlines;
|
||
}
|
||
|
||
public void setDeadlines(List<Deadline> deadlines) {
|
||
this.deadlines = deadlines;
|
||
}
|
||
|
||
public String getComment() {
|
||
return comment;
|
||
}
|
||
|
||
public void setComment(String comment) {
|
||
this.comment = comment;
|
||
}
|
||
|
||
public FileData getApplication() {
|
||
return application;
|
||
}
|
||
|
||
public void setApplication(FileData application) {
|
||
this.application = application;
|
||
}
|
||
|
||
public String getTitle() {
|
||
return title;
|
||
}
|
||
|
||
public void setTitle(String title) {
|
||
this.title = title;
|
||
}
|
||
|
||
public Project getProject() {
|
||
return project;
|
||
}
|
||
|
||
public void setProject(Project project) {
|
||
this.project = project;
|
||
}
|
||
|
||
public Set<User> getAuthors() {
|
||
return authors;
|
||
}
|
||
|
||
public void setAuthors(Set<User> authors) {
|
||
this.authors = authors;
|
||
}
|
||
|
||
@Override
|
||
public Set<User> getUsers() {
|
||
return getAuthors();
|
||
}
|
||
|
||
public User getLeader() {
|
||
return leader;
|
||
}
|
||
|
||
public void setLeader(User leader) {
|
||
this.leader = leader;
|
||
}
|
||
|
||
public List<Paper> getPapers() {
|
||
return papers;
|
||
}
|
||
|
||
public void setPapers(List<Paper> papers) {
|
||
this.papers = papers;
|
||
}
|
||
|
||
public Optional<Deadline> getNextDeadline() {
|
||
return deadlines
|
||
.stream()
|
||
.filter(deadline -> deadline.getDate() != null)
|
||
.sorted(Comparator.comparing(Deadline::getDate))
|
||
.filter(d -> d.getDate().after(new Date()))
|
||
.findFirst();
|
||
}
|
||
}
|