package ru.ulstu.project.model; import com.fasterxml.jackson.annotation.JsonCreator; 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.file.model.FileDataDto; import ru.ulstu.grant.model.GrantDto; import ru.ulstu.name.NameContainer; import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserDto; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import static ru.ulstu.core.util.StreamApiUtils.convert; public class ProjectDto extends NameContainer { private Integer id; @NotEmpty private String title; private Project.ProjectStatus status; private String description; private List deadlines = new ArrayList<>(); private GrantDto grant; private String repository; private List files = new ArrayList<>(); 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; public ProjectDto() { } public ProjectDto(String title) { this.title = title; } @JsonCreator public ProjectDto(@JsonProperty("id") Integer id, @JsonProperty("title") String title, @JsonProperty("status") Project.ProjectStatus status, @JsonProperty("description") String description, @JsonProperty("grant") GrantDto grant, @JsonProperty("repository") String repository, @JsonProperty("files") List files, @JsonProperty("deadlines") List deadlines, @JsonProperty("executorIds") Set executorIds, @JsonProperty("executors") List executors, @JsonProperty("grantIds") List grantIds, @JsonProperty("grants") List grants) { this.id = id; this.title = title; this.status = status; this.description = description; this.grant = grant; this.repository = repository; this.deadlines = deadlines; this.files = files; this.executorIds = executorIds; this.executors = executors; this.grantIds = grantIds; this.grants = grants; } public ProjectDto(Project project) { Set users = new HashSet(project.getExecutors()); this.id = project.getId(); this.title = project.getTitle(); this.status = project.getStatus(); this.description = project.getDescription(); this.files = convert(project.getFiles(), FileDataDto::new); this.grant = project.getGrant() == null ? null : new GrantDto(project.getGrant()); this.repository = project.getRepository(); 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() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Project.ProjectStatus getStatus() { return status; } public void setStatus(Project.ProjectStatus status) { this.status = status; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public GrantDto getGrant() { return grant; } public void setGrant(GrantDto 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 getRemovedDeadlineIds() { return removedDeadlineIds; } public void setRemovedDeadlineIds(List removedDeadlineIds) { this.removedDeadlineIds = removedDeadlineIds; } public Set getExecutorIds() { return executorIds; } public void setExecutorIds(Set executorIds) { this.executorIds = executorIds; } public List getExecutors() { return executors; } public void setExecutors(List executors) { this.executors = executors; } public String getExecutorsString() { return StringUtils.abbreviate(executors .stream() .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; } }