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.EventSource; import ru.ulstu.core.model.UserContainer; import ru.ulstu.deadline.model.Deadline; 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.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.validation.constraints.NotNull; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @Entity public class Project extends BaseEntity implements UserContainer, EventSource { public enum ProjectStatus { TECHNICAL_TASK("Техническое задание"), OPEN("Открыт"), IN_WORK("В работе"), CERTIFICATE_ISSUED("Оформление свидетельства"), CLOSED("Закрыт"), FAILED("Провалены сроки"); private String statusName; ProjectStatus(String statusName) { this.statusName = statusName; } public String getStatusName() { return statusName; } } @NotBlank private String title; @Enumerated(value = EnumType.STRING) private ProjectStatus status = ProjectStatus.TECHNICAL_TASK; @NotNull private String description; @OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "project_id") private List deadlines = new ArrayList<>(); @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "grant_id") private Grant grant; @NotNull private String repository; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "project_id", unique = true) @Fetch(FetchMode.SUBSELECT) private List files = new ArrayList<>(); @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name = "project_id") private List events = new ArrayList<>(); @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; } @Override public List getRecipients() { return executors != null ? new ArrayList<>(executors) : Collections.emptyList(); } @Override public void addObjectToEvent(Event event) { event.setProject(this); } public void setTitle(String title) { this.title = title; } public ProjectStatus getStatus() { return status; } public void setStatus(ProjectStatus status) { this.status = status; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Grant getGrant() { return grant; } public void setGrant(Grant grant) { this.grant = grant; } public String getRepository() { return repository; } public void setRepository(String repository) { this.repository = repository; } public List getDeadlines() { return deadlines; } public void setDeadlines(List deadlines) { this.deadlines = deadlines; } public List getFiles() { return files; } public void setFiles(List files) { this.files = files; } public List getEvents() { return events; } public void setEvents(List events) { this.events = events; } public List getExecutors() { return executors; } public void setExecutors(List executors) { this.executors = executors; } @Override public Set getUsers() { Set users = new HashSet(getExecutors()); return users; } public List getGrants() { return grants; } public void setGrants(List grants) { this.grants = grants; } }