Update classes

This commit is contained in:
T-Midnight 2018-12-24 15:20:57 +04:00
parent 2f68339ba2
commit d7721ce19d
6 changed files with 62 additions and 53 deletions

View File

@ -2,17 +2,16 @@ 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.*;
import javax.validation.constraints.NotNull;
import java.util.*;
@Entity
@Table(name = "grants")
public class Grant extends BaseEntity {
public enum GrantStatus {
APPLICATION("Заявка"),
@ -23,11 +22,9 @@ public class Grant extends BaseEntity {
FAILED("Провалены сроки");
private String name;
GrantStatus(String name) {
this.name = name;
}
public String getName() {
return name;
}

View File

@ -6,6 +6,7 @@ import org.hibernate.validator.constraints.NotEmpty;
import ru.ulstu.deadline.model.DeadlineDto;
import ru.ulstu.project.model.ProjectDto;
import java.util.ArrayList;
import java.util.List;
import static ru.ulstu.core.util.StreamApiUtils.convert;
@ -15,7 +16,7 @@ public class GrantDto {
@NotEmpty
private String title;
private Grant.GrantStatus status;
private List<DeadlineDto> deadlines;
private List<DeadlineDto> deadlines = new ArrayList<>();
private String comment;
private String applicationFileName;
private ProjectDto project;

View File

@ -5,19 +5,12 @@ import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.deadline.model.DeadlineDto;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.grant.model.service.GrantService;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.model.PaperFilterDto;
import ru.ulstu.paper.service.PaperService;
import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.user.model.User;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;
@ -40,7 +33,7 @@ public class GrantController {
@GetMapping("/dashboard")
public void getDashboard(ModelMap modelMap) {
modelMap.put("dashboard", grantService.findAllDto());
modelMap.put("grants", grantService.findAllDto());
}
@GetMapping("/grant")
@ -75,12 +68,12 @@ public class GrantController {
return "/grants/grant";
}
@PostMapping(value = "/grant", params = "addProject")
public String addProject(@Valid GrantDto grantDto, Errors errors) {
@PostMapping(value = "/grant", params = "createProject")
public String createProject(@Valid GrantDto grantDto, Errors errors) {
if (errors.hasErrors()) {
return "/grants/grant";
}
grantDto.setProject(new ProjectDto());
grantService.createProject(grantDto);
return "/grants/grant";
}
@ -90,6 +83,11 @@ public class GrantController {
return "redirect:/grants/grants";
}
@ModelAttribute("allStatuses")
public List<Grant.GrantStatus> getGrantStatuses() {
return grantService.getGrantStatuses();
}
private void filterEmptyDeadlines(GrantDto grantDto) {
grantDto.setDeadlines(grantDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))

View File

@ -9,8 +9,10 @@ import ru.ulstu.file.service.FileService;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.grant.model.repository.GrantRepository;
import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.project.model.Project;
import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.project.repository.ProjectRepository;
import ru.ulstu.project.service.ProjectService;
import java.io.IOException;
import java.util.*;
@ -26,29 +28,32 @@ public class GrantService {
private final static int MAX_DISPLAY_SIZE = 40;
private final GrantRepository grantRepository;
private final ProjectService projectService;
private final DeadlineService deadlineService;
private final FileService fileService;
public GrantService(GrantRepository grantRepository,
FileService fileService,
DeadlineService deadlineService) {
DeadlineService deadlineService,
ProjectService projectService) {
this.grantRepository = grantRepository;
this.projectService = projectService;
this.fileService = fileService;
this.deadlineService = deadlineService;
}
public List<Grant> findAll() {
return sortGrants(grantRepository.findAll());
return grantRepository.findAll();
}
public List<GrantDto> findAllDto() {
List<GrantDto> grants = convert(findAll(), ru.ulstu.grant.model.GrantDto::new);
List<GrantDto> grants = convert(findAll(), GrantDto::new);
grants.forEach(grantDto -> grantDto.setTitle(StringUtils.abbreviate(grantDto.getTitle(), MAX_DISPLAY_SIZE)));
return grants;
}
public GrantDto findOneDto(Integer id) {
return new ru.ulstu.grant.model.GrantDto(grantRepository.findOne(id));
return new GrantDto(grantRepository.findOne(id));
}
@Transactional
@ -63,7 +68,10 @@ public class GrantService {
grant.setComment(grantDto.getComment());
grant.setStatus(grantDto.getStatus() == null ? APPLICATION : grantDto.getStatus());
grant.setTitle(grantDto.getTitle());
//grant.setProject(grantDto.getProject()); //TODO: Исправить!
if (grantDto.getProject() != null) {
grant.setProject(projectService.findById(grantDto.getProject().getId()));
}
//grant. setdeadlineDate(grant.getdeadlineDate() == null ? new Date() : grant.getdeadlineDate());
grant.setDeadlines(deadlineService.saveOrCreate(grantDto.getDeadlines()));
if (grantDto.getApplicationFileName() != null) {
grant.setApplication(fileService.createFileFromTmp(grantDto.getApplicationFileName()));
@ -71,6 +79,11 @@ public class GrantService {
return grant;
}
public void createProject(GrantDto grantDto) {
grantDto.setProject(
new ProjectDto(projectService.save(new ProjectDto(grantDto.getTitle()))));
}
@Transactional
public Integer update(GrantDto grantDto) throws IOException {
Grant grant = grantRepository.findOne(grantDto.getId());
@ -130,7 +143,7 @@ public class GrantService {
}).collect(Collectors.toList());
}
public ru.ulstu.grant.model.GrantDto findGrant(int id) {
public GrantDto findGrant(int id) {
return new ru.ulstu.grant.model.GrantDto(grantRepository.getOne(id));
}

View File

@ -4,19 +4,27 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;
import ru.ulstu.deadline.model.DeadlineDto;
import ru.ulstu.project.model.Project;
import java.util.ArrayList;
import java.util.List;
import static ru.ulstu.core.util.StreamApiUtils.convert;
public class ProjectDto {
private Integer id;
@NotEmpty
private String title;
private List<DeadlineDto> deadlines;
private List<DeadlineDto> deadlines = new ArrayList<>();
public ProjectDto() {}
public ProjectDto(String title) {
this.title = title;
}
@JsonCreator
public ProjectDto(@JsonProperty("id") Integer id,
@JsonProperty("title") String title,

View File

@ -41,30 +41,35 @@ public class ProjectService {
return projectRepository.findAll();
}
public List<ProjectDto> findAllDto() {
List<ProjectDto> projects = convert(findAll(), ProjectDto::new);
projects.forEach(projectDto -> projectDto.setTitle(StringUtils.abbreviate(projectDto.getTitle(), MAX_DISPLAY_SIZE)));
return projects;
}
// public GrantDto findOneDto(Integer id) {
// return new GrantDto(grantRepository.findOne(id));
// }
@Transactional
public Integer create(ProjectDto projectDto) throws IOException {
public Project create(ProjectDto projectDto) {
Project newProject = copyFromDto(new Project(), projectDto);
newProject = projectRepository.save(newProject);
//paperNotificationService.sendCreateNotification(newGrant);
return newProject.getId();
return newProject;
}
private Project copyFromDto(Project project, ProjectDto projectDto) throws IOException {
private Project copyFromDto(Project project, ProjectDto projectDto) {
project.setTitle(projectDto.getTitle());
project.setDeadlines(deadlineService.saveOrCreate(projectDto.getDeadlines()));
return project;
}
public Project save(ProjectDto projectDto) {
if (isEmpty(projectDto.getId())) {
return create(projectDto);
} else {
return update(projectDto);
}
}
private Project update(ProjectDto projectDto) {
return null;
}
public Project findById(Integer id) {
return projectRepository.findOne(id);
}
// public void createProject(GrantDto grantDto) {
// grantDto.setProject(new ProjectDto(grantDto.getTitle()));
// }
@ -97,14 +102,7 @@ public class ProjectService {
// return Arrays.asList(Grant.GrantStatus.values());
// }
@Transactional
public Project create(String title, Date deadlineDate) {
Project project = new Project();
project.setTitle(title);
project.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн у проекта"));
project = projectRepository.save(project);
return project;
}
// public List<PaperDto> filter(PaperFilterDto filterDto) {
// return convert(sortPapers(paperRepository.filter(
@ -144,13 +142,7 @@ public class ProjectService {
// });
// }
public void save(ProjectDto projectDto) throws IOException {
if (isEmpty(projectDto.getId())) {
create(projectDto);
} else {
//update(grantDto);
}
}
// public GrantDto findById(Integer grantId) {
// return new GrantDto(grantRepository.findOne(grantId));