From ce78f8ad667612618df6921a9b7fde2c702ff10c Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 28 Dec 2018 13:57:20 +0400 Subject: [PATCH] some refactor --- .../grant/controller/GrantController.java | 9 ++- src/main/java/ru/ulstu/grant/model/Grant.java | 37 ++++++--- .../ru/ulstu/grant/model/GrantStatusDto.java | 4 +- .../ru/ulstu/grant/service/GrantService.java | 56 +------------ .../project/repository/ProjectRepository.java | 2 - .../ulstu/project/service/ProjectService.java | 81 +------------------ src/main/resources/db/changelog-master.xml | 1 + .../fragments/grantDashboardFragment.html | 3 +- 8 files changed, 42 insertions(+), 151 deletions(-) diff --git a/src/main/java/ru/ulstu/grant/controller/GrantController.java b/src/main/java/ru/ulstu/grant/controller/GrantController.java index 636c6e5..415dd0b 100644 --- a/src/main/java/ru/ulstu/grant/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/controller/GrantController.java @@ -3,7 +3,12 @@ package ru.ulstu.grant.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.Errors; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.deadline.model.DeadlineDto; import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.GrantDto; @@ -49,7 +54,7 @@ public class GrantController { public String save(@Valid GrantDto grantDto, Errors errors) throws IOException { filterEmptyDeadlines(grantDto); if (grantDto.getDeadlines().isEmpty()) { - errors.rejectValue("deadlines", "errorCode","Не может быть пустым"); + errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); } if (errors.hasErrors()) { return "/grants/grant"; diff --git a/src/main/java/ru/ulstu/grant/model/Grant.java b/src/main/java/ru/ulstu/grant/model/Grant.java index e3d60b1..8d40e3a 100644 --- a/src/main/java/ru/ulstu/grant/model/Grant.java +++ b/src/main/java/ru/ulstu/grant/model/Grant.java @@ -6,13 +6,24 @@ import ru.ulstu.deadline.model.Deadline; import ru.ulstu.file.model.FileData; import ru.ulstu.project.model.Project; -import javax.persistence.*; +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import javax.persistence.Table; import javax.validation.constraints.NotNull; -import java.util.*; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Date; +import java.util.List; +import java.util.Optional; @Entity @Table(name = "grants") -public class Grant extends BaseEntity { +public class Grant extends BaseEntity { public enum GrantStatus { APPLICATION("Заявка"), ON_COMPETITION("Отправлен на конкурс"), @@ -21,12 +32,14 @@ public class Grant extends BaseEntity { COMPLETED("Завершен"), FAILED("Провалены сроки"); - private String name; - GrantStatus(String name) { - this.name = name; + private String statusName; + + GrantStatus(String statusName) { + this.statusName = statusName; } - public String getName() { - return name; + + public String getStatusName() { + return statusName; } } @@ -81,13 +94,17 @@ public class Grant extends BaseEntity { return application; } - public void setApplication(FileData application) { this.application = application; } + public void setApplication(FileData application) { + this.application = application; + } public String getTitle() { return title; } - public void setTitle(String title) { this.title = title; } + public void setTitle(String title) { + this.title = title; + } public Project getProject() { return project; diff --git a/src/main/java/ru/ulstu/grant/model/GrantStatusDto.java b/src/main/java/ru/ulstu/grant/model/GrantStatusDto.java index bcd5563..34676d6 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantStatusDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantStatusDto.java @@ -1,14 +1,12 @@ package ru.ulstu.grant.model; -import ru.ulstu.grant.model.Grant; - public class GrantStatusDto { private final String id; private final String name; public GrantStatusDto(Grant.GrantStatus status) { this.id = status.name(); - this.name = status.getName(); + this.name = status.getStatusName(); } public String getId() { diff --git a/src/main/java/ru/ulstu/grant/service/GrantService.java b/src/main/java/ru/ulstu/grant/service/GrantService.java index 1c061d2..31d0a04 100644 --- a/src/main/java/ru/ulstu/grant/service/GrantService.java +++ b/src/main/java/ru/ulstu/grant/service/GrantService.java @@ -14,13 +14,13 @@ import ru.ulstu.project.model.ProjectDto; import ru.ulstu.project.service.ProjectService; import java.io.IOException; -import java.util.*; -import java.util.stream.Collectors; +import java.util.Arrays; +import java.util.Date; +import java.util.List; import static org.springframework.util.ObjectUtils.isEmpty; import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.grant.model.Grant.GrantStatus.APPLICATION; -import static ru.ulstu.grant.model.Grant.GrantStatus.IN_WORK; @Service public class GrantService { @@ -59,7 +59,6 @@ public class GrantService { public Integer create(GrantDto grantDto) throws IOException { Grant newGrant = copyFromDto(new Grant(), grantDto); newGrant = grantRepository.save(newGrant); - //paperNotificationService.sendCreateNotification(newGrant); return newGrant.getId(); } @@ -67,10 +66,9 @@ public class GrantService { grant.setComment(grantDto.getComment()); grant.setStatus(grantDto.getStatus() == null ? APPLICATION : grantDto.getStatus()); grant.setTitle(grantDto.getTitle()); - if (grantDto.getProject() != null) { + if (grantDto.getProject() != null && grantDto.getProject().getId() != 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())); @@ -90,9 +88,6 @@ public class GrantService { if (grantDto.getApplicationFileName() != null && grant.getApplication() != null) { fileService.deleteFile(grant.getApplication()); } -// if (paper.getStatus() != oldStatus) { //TODO: доделать потом все уведомления -// paperNotificationService.statusChangeNotification(paper, oldStatus); -// } grantRepository.save(copyFromDto(grant, grantDto)); return grant.getId(); } @@ -120,48 +115,9 @@ public class GrantService { grant.setStatus(APPLICATION); grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн")); grant = grantRepository.save(grant); - // paperNotificationService.sendCreateNotification(paper); return grant; } -// public List filter(PaperFilterDto filterDto) { -// return convert(sortPapers(paperRepository.filter( -// filterDto.getFilterAuthorId() == null ? null : userService.findById(filterDto.getFilterAuthorId()), -// filterDto.getYear())), PaperDto::new); -// } - - private List sortGrants(List grants) { - return grants.stream().sorted((grant1, grant2) -> { - int statusCompareResult = - Integer.valueOf(Arrays.asList(Grant.GrantStatus.values()).indexOf(grant1.getStatus())) - .compareTo(Arrays.asList(Grant.GrantStatus.values()).indexOf(grant2.getStatus())); - if (statusCompareResult != 0) { - return statusCompareResult; - } - return grant1.getTitle().compareTo(grant2.getTitle()); - }).collect(Collectors.toList()); - } - - public GrantDto findGrant(int id) { - return new GrantDto(grantRepository.getOne(id)); - } - - public void closeFailedGrants() { - List grants = grantRepository.findAll() - .stream() - .filter(grant -> grant.getNextDeadline().isPresent() - && (grant.getStatus() == APPLICATION - || grant.getStatus() == IN_WORK) - && grant.getNextDeadline().get().getDate().before(new Date())) - .collect(Collectors.toList()); - grants.forEach(grant -> { - //Grant.GrantStatus oldStatus = grant.getStatus(); - grant.setStatus(Grant.GrantStatus.FAILED); - grantRepository.save(grant); - //paperNotificationService.sendFailedNotification(grant, oldStatus); - }); - } - public void save(GrantDto grantDto) throws IOException { if (isEmpty(grantDto.getId())) { create(grantDto); @@ -169,8 +125,4 @@ public class GrantService { update(grantDto); } } - - public GrantDto findById(Integer grantId) { - return new GrantDto(grantRepository.findOne(grantId)); - } } diff --git a/src/main/java/ru/ulstu/project/repository/ProjectRepository.java b/src/main/java/ru/ulstu/project/repository/ProjectRepository.java index ac41af2..6a78075 100644 --- a/src/main/java/ru/ulstu/project/repository/ProjectRepository.java +++ b/src/main/java/ru/ulstu/project/repository/ProjectRepository.java @@ -5,6 +5,4 @@ import ru.ulstu.project.model.Project; public interface ProjectRepository extends JpaRepository { -// @Query("SELECT p FROM Paper p WHERE (:author IS NULL OR :author MEMBER OF p.authors) AND (YEAR(p.createDate) = :year OR :year IS NULL)") -// List filter(@Param("author") User author, @Param("year") Integer year); } diff --git a/src/main/java/ru/ulstu/project/service/ProjectService.java b/src/main/java/ru/ulstu/project/service/ProjectService.java index 1a4d1c7..b54a60a 100644 --- a/src/main/java/ru/ulstu/project/service/ProjectService.java +++ b/src/main/java/ru/ulstu/project/service/ProjectService.java @@ -10,11 +10,9 @@ import ru.ulstu.project.repository.ProjectRepository; import java.util.List; import static org.springframework.util.ObjectUtils.isEmpty; -import static ru.ulstu.core.util.StreamApiUtils.convert; @Service public class ProjectService { - private final static int MAX_DISPLAY_SIZE = 40; private final ProjectRepository projectRepository; private final DeadlineService deadlineService; @@ -33,7 +31,6 @@ public class ProjectService { public Project create(ProjectDto projectDto) { Project newProject = copyFromDto(new Project(), projectDto); newProject = projectRepository.save(newProject); - //paperNotificationService.sendCreateNotification(newGrant); return newProject; } @@ -52,87 +49,11 @@ public class ProjectService { } private Project update(ProjectDto projectDto) { - return null; + throw new RuntimeException("not implemented yet"); } public Project findById(Integer id) { return projectRepository.findOne(id); } -// public void createProject(GrantDto grantDto) { -// grantDto.setProject(new ProjectDto(grantDto.getTitle())); -// } -// @Transactional -// public Integer update(GrantDto grantDto) throws IOException { -// Grant grant = grantRepository.findOne(grantDto.getId()); -// Grant.GrantStatus oldStatus = grant.getStatus(); -// if (grantDto.getApplicationFileName() != null && grant.getApplication() != null) { -// fileService.deleteFile(grant.getApplication()); -// } -//// if (paper.getStatus() != oldStatus) { -//// paperNotificationService.statusChangeNotification(paper, oldStatus); -//// } -// grantRepository.save(copyFromDto(grant, grantDto)); -// return grant.getId(); -// } - -// @Transactional -// public void delete(Integer grantId) throws IOException { -// Grant grant = grantRepository.findOne(grantId); -// if (grant.getApplication() != null) { -// fileService.deleteFile(grant.getApplication()); -// } -// //возможно при удалении гранта будет удаляться и проект, к нему привязанный -// grantRepository.delete(grant); -// } - -// public List getGrantStatuses() { -// return Arrays.asList(Grant.GrantStatus.values()); -// } - - - -// public List filter(PaperFilterDto filterDto) { -// return convert(sortPapers(paperRepository.filter( -// filterDto.getFilterAuthorId() == null ? null : userService.findById(filterDto.getFilterAuthorId()), -// filterDto.getYear())), PaperDto::new); -// } - -// private List sortGrants(List grants) { -// return grants.stream().sorted((grant1, grant2) -> { -// int statusCompareResult = -// Integer.valueOf(Arrays.asList(Grant.GrantStatus.values()).indexOf(grant1.getStatus())) -// .compareTo(Arrays.asList(Grant.GrantStatus.values()).indexOf(grant2.getStatus())); -// if (statusCompareResult != 0) { -// return statusCompareResult; -// } -// return grant1.getTitle().compareTo(grant2.getTitle()); -// }).collect(Collectors.toList()); -// } -// -// public GrantDto findGrant(int id) { -// return new GrantDto(grantRepository.getOne(id)); -// } -// -// public void closeFailedGrants() { -// List grants = grantRepository.findAll() -// .stream() -// .filter(grant -> grant.getNextDeadline().isPresent() -// && (grant.getStatus() == APPLICATION -// || grant.getStatus() == IN_WORK) -// && grant.getNextDeadline().get().getDate().before(new Date())) -// .collect(Collectors.toList()); -// grants.forEach(grant -> { -// //Grant.GrantStatus oldStatus = grant.getStatus(); -// grant.setStatus(Grant.GrantStatus.FAILED); -// grantRepository.save(grant); -// //paperNotificationService.sendFailedNotification(grant, oldStatus); -// }); -// } - - - -// public GrantDto findById(Integer grantId) { -// return new GrantDto(grantRepository.findOne(grantId)); -// } } diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml index 6c7147b..c07b1df 100644 --- a/src/main/resources/db/changelog-master.xml +++ b/src/main/resources/db/changelog-master.xml @@ -17,4 +17,5 @@ + \ No newline at end of file diff --git a/src/main/resources/templates/grants/fragments/grantDashboardFragment.html b/src/main/resources/templates/grants/fragments/grantDashboardFragment.html index 5ddaca7..df3e3d0 100644 --- a/src/main/resources/templates/grants/fragments/grantDashboardFragment.html +++ b/src/main/resources/templates/grants/fragments/grantDashboardFragment.html @@ -11,8 +11,7 @@
title -

comment

-

status

+

status