From d7721ce19d4733e7b75f89ac939b5a527d376beb Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 24 Dec 2018 15:20:57 +0400 Subject: [PATCH] Update classes --- src/main/java/ru/ulstu/grant/model/Grant.java | 5 +- .../java/ru/ulstu/grant/model/GrantDto.java | 3 +- .../model/controller/GrantController.java | 22 ++++----- .../grant/model/service/GrantService.java | 27 ++++++++--- .../ru/ulstu/project/model/ProjectDto.java | 10 +++- .../ulstu/project/service/ProjectService.java | 48 ++++++++----------- 6 files changed, 62 insertions(+), 53 deletions(-) diff --git a/src/main/java/ru/ulstu/grant/model/Grant.java b/src/main/java/ru/ulstu/grant/model/Grant.java index 1bd7ff9..e3d60b1 100644 --- a/src/main/java/ru/ulstu/grant/model/Grant.java +++ b/src/main/java/ru/ulstu/grant/model/Grant.java @@ -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; } diff --git a/src/main/java/ru/ulstu/grant/model/GrantDto.java b/src/main/java/ru/ulstu/grant/model/GrantDto.java index aeff302..9070337 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantDto.java @@ -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 deadlines; + private List deadlines = new ArrayList<>(); private String comment; private String applicationFileName; private ProjectDto project; diff --git a/src/main/java/ru/ulstu/grant/model/controller/GrantController.java b/src/main/java/ru/ulstu/grant/model/controller/GrantController.java index acfc27a..350611d 100644 --- a/src/main/java/ru/ulstu/grant/model/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/model/controller/GrantController.java @@ -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 getGrantStatuses() { + return grantService.getGrantStatuses(); + } + private void filterEmptyDeadlines(GrantDto grantDto) { grantDto.setDeadlines(grantDto.getDeadlines().stream() .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) diff --git a/src/main/java/ru/ulstu/grant/model/service/GrantService.java b/src/main/java/ru/ulstu/grant/model/service/GrantService.java index e052198..b753009 100644 --- a/src/main/java/ru/ulstu/grant/model/service/GrantService.java +++ b/src/main/java/ru/ulstu/grant/model/service/GrantService.java @@ -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 findAll() { - return sortGrants(grantRepository.findAll()); + return grantRepository.findAll(); } public List findAllDto() { - List grants = convert(findAll(), ru.ulstu.grant.model.GrantDto::new); + List 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)); } diff --git a/src/main/java/ru/ulstu/project/model/ProjectDto.java b/src/main/java/ru/ulstu/project/model/ProjectDto.java index 81c3c91..7c2664e 100644 --- a/src/main/java/ru/ulstu/project/model/ProjectDto.java +++ b/src/main/java/ru/ulstu/project/model/ProjectDto.java @@ -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 deadlines; + + private List deadlines = new ArrayList<>(); public ProjectDto() {} + public ProjectDto(String title) { + this.title = title; + } + @JsonCreator public ProjectDto(@JsonProperty("id") Integer id, @JsonProperty("title") String title, diff --git a/src/main/java/ru/ulstu/project/service/ProjectService.java b/src/main/java/ru/ulstu/project/service/ProjectService.java index 8576e9b..43802df 100644 --- a/src/main/java/ru/ulstu/project/service/ProjectService.java +++ b/src/main/java/ru/ulstu/project/service/ProjectService.java @@ -41,30 +41,35 @@ public class ProjectService { return projectRepository.findAll(); } - public List findAllDto() { - List 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 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));