From 42d33d1be8f2a6f203b2cf4d7b46831bfc404ae4 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Sat, 22 Dec 2018 03:31:43 +0400 Subject: [PATCH 01/17] Create setters for GrantDto --- .../java/ru/ulstu/grant/model/GrantDto.java | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/main/java/ru/ulstu/grant/model/GrantDto.java b/src/main/java/ru/ulstu/grant/model/GrantDto.java index 922df89..a96244d 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantDto.java @@ -11,14 +11,14 @@ import java.util.List; import static ru.ulstu.core.util.StreamApiUtils.convert; public class GrantDto { - private final Integer id; + private Integer id; @NotEmpty - private final String title; - private final Grant.GrantStatus status; - private final List deadlines; - private final String comment; - private final String applicationFileName; - private final ProjectDto project; + private String title; + private Grant.GrantStatus status; + private List deadlines; + private String comment; + private String applicationFileName; + private ProjectDto project; @JsonCreator public GrantDto(@JsonProperty("id") Integer id, @@ -50,27 +50,46 @@ public class GrantDto { return id; } + public void setId(Integer id) {this.id = id;} + public String getTitle() { return title; } + public void setTitle(String title) {this.title = title;} + public Grant.GrantStatus getStatus() { return status; } + public void setStatus(Grant.GrantStatus status) { + this.status = status; + } + public List getDeadlines() { return deadlines; } + public void setDeadlines(List deadlines) { + this.deadlines = deadlines; + } + public String getComment() { return comment; } + public void setComment(String comment) {this.comment = comment;} + public ProjectDto getProject() { return project; } + public void setProject(ProjectDto project) {this.project = project;} + public String getApplicationFileName() { return applicationFileName; } + + public void setApplicationFileName(String applicationFileName) { + this.applicationFileName = applicationFileName;} } From eff5344aeabf129a4a9e4755a1ccf1c9e9e2c9ce Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Sat, 22 Dec 2018 18:03:18 +0400 Subject: [PATCH 02/17] Create GrantRepository --- .../ulstu/grant/model/repository/GrantRepository.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/ru/ulstu/grant/model/repository/GrantRepository.java diff --git a/src/main/java/ru/ulstu/grant/model/repository/GrantRepository.java b/src/main/java/ru/ulstu/grant/model/repository/GrantRepository.java new file mode 100644 index 0000000..bd1067d --- /dev/null +++ b/src/main/java/ru/ulstu/grant/model/repository/GrantRepository.java @@ -0,0 +1,10 @@ +package ru.ulstu.grant.model.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.grant.model.Grant; + +public interface GrantRepository 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); +} From edd38beb67a91d616cb5640072c3b4320c4aba48 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Sun, 23 Dec 2018 02:22:06 +0400 Subject: [PATCH 03/17] Add function getNextDeadline() --- src/main/java/ru/ulstu/grant/model/Grant.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/ru/ulstu/grant/model/Grant.java b/src/main/java/ru/ulstu/grant/model/Grant.java index 092317b..1bd7ff9 100644 --- a/src/main/java/ru/ulstu/grant/model/Grant.java +++ b/src/main/java/ru/ulstu/grant/model/Grant.java @@ -10,11 +10,7 @@ import ru.ulstu.user.model.User; import javax.persistence.*; import javax.validation.constraints.NotNull; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; @Entity public class Grant extends BaseEntity { @@ -103,4 +99,13 @@ public class Grant extends BaseEntity { public void setProject(Project project) { this.project = project; } + + public Optional getNextDeadline() { + return deadlines + .stream() + .filter(deadline -> deadline.getDate() != null) + .sorted(Comparator.comparing(Deadline::getDate)) + .filter(d -> d.getDate().after(new Date())) + .findFirst(); + } } From 9e5f3d72476814782e43d3f5e3f281a2092ca62b Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Sun, 23 Dec 2018 02:22:24 +0400 Subject: [PATCH 04/17] Add constructor --- src/main/java/ru/ulstu/grant/model/GrantDto.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/ru/ulstu/grant/model/GrantDto.java b/src/main/java/ru/ulstu/grant/model/GrantDto.java index a96244d..aeff302 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantDto.java @@ -20,6 +20,10 @@ public class GrantDto { private String applicationFileName; private ProjectDto project; + public GrantDto() { + deadlines.add(new DeadlineDto()); + } + @JsonCreator public GrantDto(@JsonProperty("id") Integer id, @JsonProperty("title") String title, From c2a32d6c278f59092d7689155b5948a0528a05dc Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Sun, 23 Dec 2018 02:22:31 +0400 Subject: [PATCH 05/17] Update ProjectDto --- .../ru/ulstu/project/model/ProjectDto.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/ulstu/project/model/ProjectDto.java b/src/main/java/ru/ulstu/project/model/ProjectDto.java index 69e9b1b..81c3c91 100644 --- a/src/main/java/ru/ulstu/project/model/ProjectDto.java +++ b/src/main/java/ru/ulstu/project/model/ProjectDto.java @@ -10,12 +10,12 @@ import java.util.List; import static ru.ulstu.core.util.StreamApiUtils.convert; public class ProjectDto { - private final Integer id; - + private Integer id; @NotEmpty - private final String title; + private String title; + private List deadlines; - private final List deadlines; + public ProjectDto() {} @JsonCreator public ProjectDto(@JsonProperty("id") Integer id, @@ -26,6 +26,7 @@ public class ProjectDto { this.deadlines = deadlines; } + public ProjectDto(Project project) { this.id = project.getId(); this.title = project.getTitle(); @@ -39,4 +40,19 @@ public class ProjectDto { public String getTitle() { return title; } + + public void setId(Integer id) { + this.id = id; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setDeadlines(List deadlines) { + this.deadlines = deadlines; + } + public List getDeadlines() { + return deadlines; + } } From a2fa574888e21a22fdd42e41756541210e1c2edd Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Sun, 23 Dec 2018 02:22:41 +0400 Subject: [PATCH 06/17] Create Controller&Service --- .../model/controller/GrantController.java | 98 +++++++++++ .../grant/model/service/GrantService.java | 164 ++++++++++++++++++ 2 files changed, 262 insertions(+) create mode 100644 src/main/java/ru/ulstu/grant/model/controller/GrantController.java create mode 100644 src/main/java/ru/ulstu/grant/model/service/GrantService.java diff --git a/src/main/java/ru/ulstu/grant/model/controller/GrantController.java b/src/main/java/ru/ulstu/grant/model/controller/GrantController.java new file mode 100644 index 0000000..acfc27a --- /dev/null +++ b/src/main/java/ru/ulstu/grant/model/controller/GrantController.java @@ -0,0 +1,98 @@ +package ru.ulstu.grant.model.controller; + +import org.springframework.stereotype.Controller; +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.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; + +import static org.springframework.util.StringUtils.isEmpty; + + +@Controller() +@RequestMapping(value = "/grants") +public class GrantController { + private final GrantService grantService; + + public GrantController(GrantService grantService) { + this.grantService = grantService; + } + + @GetMapping("/grants") + public void getGrants(ModelMap modelMap) { + modelMap.put("grants", grantService.findAllDto()); + } + + @GetMapping("/dashboard") + public void getDashboard(ModelMap modelMap) { + modelMap.put("dashboard", grantService.findAllDto()); + } + + @GetMapping("/grant") + public void getGrant(ModelMap modelMap, @RequestParam(value = "id") Integer id) { + if (id != null && id > 0) { + modelMap.put("grantDto", grantService.findOneDto(id)); + } else { + modelMap.put("grantDto", new GrantDto()); + } + } + + @PostMapping(value = "/grant", params = "save") + public String save(@Valid GrantDto grantDto, Errors errors) throws IOException { + filterEmptyDeadlines(grantDto); + if (grantDto.getDeadlines().isEmpty()) { + errors.rejectValue("deadlines", "errorCode","Не может быть пустым"); + } + if (errors.hasErrors()) { + return "/grants/grant"; + } + grantService.save(grantDto); + return "redirect:/grants/grants"; + } + + @PostMapping(value = "/grant", params = "addDeadline") + public String addDeadline(@Valid GrantDto grantDto, Errors errors) { + filterEmptyDeadlines(grantDto); + if (errors.hasErrors()) { + return "/grants/grant"; + } + grantDto.getDeadlines().add(new DeadlineDto()); + return "/grants/grant"; + } + + @PostMapping(value = "/grant", params = "addProject") + public String addProject(@Valid GrantDto grantDto, Errors errors) { + if (errors.hasErrors()) { + return "/grants/grant"; + } + grantDto.setProject(new ProjectDto()); + return "/grants/grant"; + } + + @GetMapping("/delete/{grant-id}") + public String delete(@PathVariable("grant-id") Integer grantId) throws IOException { + grantService.delete(grantId); + return "redirect:/grants/grants"; + } + + private void filterEmptyDeadlines(GrantDto grantDto) { + grantDto.setDeadlines(grantDto.getDeadlines().stream() + .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) + .collect(Collectors.toList())); + } +} diff --git a/src/main/java/ru/ulstu/grant/model/service/GrantService.java b/src/main/java/ru/ulstu/grant/model/service/GrantService.java new file mode 100644 index 0000000..e052198 --- /dev/null +++ b/src/main/java/ru/ulstu/grant/model/service/GrantService.java @@ -0,0 +1,164 @@ +package ru.ulstu.grant.model.service; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.deadline.model.Deadline; +import ru.ulstu.deadline.service.DeadlineService; +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 java.io.IOException; +import java.util.*; +import java.util.stream.Collectors; + +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 { + private final static int MAX_DISPLAY_SIZE = 40; + + private final GrantRepository grantRepository; + private final DeadlineService deadlineService; + private final FileService fileService; + + public GrantService(GrantRepository grantRepository, + FileService fileService, + DeadlineService deadlineService) { + this.grantRepository = grantRepository; + this.fileService = fileService; + this.deadlineService = deadlineService; + } + + public List findAll() { + return sortGrants(grantRepository.findAll()); + } + + public List findAllDto() { + List grants = convert(findAll(), ru.ulstu.grant.model.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)); + } + + @Transactional + public Integer create(GrantDto grantDto) throws IOException { + Grant newGrant = copyFromDto(new Grant(), grantDto); + newGrant = grantRepository.save(newGrant); + //paperNotificationService.sendCreateNotification(newGrant); + return newGrant.getId(); + } + + private Grant copyFromDto(Grant grant, GrantDto grantDto) throws IOException { + grant.setComment(grantDto.getComment()); + grant.setStatus(grantDto.getStatus() == null ? APPLICATION : grantDto.getStatus()); + grant.setTitle(grantDto.getTitle()); + //grant.setProject(grantDto.getProject()); //TODO: Исправить! + grant.setDeadlines(deadlineService.saveOrCreate(grantDto.getDeadlines())); + if (grantDto.getApplicationFileName() != null) { + grant.setApplication(fileService.createFileFromTmp(grantDto.getApplicationFileName())); + } + return grant; + } + + @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) { //TODO: доделать потом все уведомления +// 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()); + } + + @Transactional + public Grant create(String title, Project project_id, Date deadlineDate) { + Grant grant = new Grant(); + grant.setTitle(title); + grant.setComment("Комментарий к гранту 1"); + grant.setProject(project_id); + 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 ru.ulstu.grant.model.GrantDto findGrant(int id) { + return new ru.ulstu.grant.model.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); + } else { + update(grantDto); + } + } + + public GrantDto findById(Integer grantId) { + return new GrantDto(grantRepository.findOne(grantId)); + } +} From 25278bcdd8054df20b428671546dce9117078dc0 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 24 Dec 2018 11:03:03 +0400 Subject: [PATCH 07/17] Create html fragments --- .../fragments/grantDashboardFragment.html | 20 ++++++++++++ .../grants/fragments/grantLineFragment.html | 22 +++++++++++++ .../fragments/grantNavigationFragment.html | 26 ++++++++++++++++ .../grants/fragments/grantStatusFragment.html | 31 +++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 src/main/resources/templates/grants/fragments/grantDashboardFragment.html create mode 100644 src/main/resources/templates/grants/fragments/grantLineFragment.html create mode 100644 src/main/resources/templates/grants/fragments/grantNavigationFragment.html create mode 100644 src/main/resources/templates/grants/fragments/grantStatusFragment.html diff --git a/src/main/resources/templates/grants/fragments/grantDashboardFragment.html b/src/main/resources/templates/grants/fragments/grantDashboardFragment.html new file mode 100644 index 0000000..5ddaca7 --- /dev/null +++ b/src/main/resources/templates/grants/fragments/grantDashboardFragment.html @@ -0,0 +1,20 @@ + + + + + + +
+
+
+ +
+
+ title +

comment

+

status

+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/grants/fragments/grantLineFragment.html b/src/main/resources/templates/grants/fragments/grantLineFragment.html new file mode 100644 index 0000000..f9d9e09 --- /dev/null +++ b/src/main/resources/templates/grants/fragments/grantLineFragment.html @@ -0,0 +1,22 @@ + + + + + + +
+
+ + + + + + + + + +
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/grants/fragments/grantNavigationFragment.html b/src/main/resources/templates/grants/fragments/grantNavigationFragment.html new file mode 100644 index 0000000..7545077 --- /dev/null +++ b/src/main/resources/templates/grants/fragments/grantNavigationFragment.html @@ -0,0 +1,26 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/grants/fragments/grantStatusFragment.html b/src/main/resources/templates/grants/fragments/grantStatusFragment.html new file mode 100644 index 0000000..a8a4d9e --- /dev/null +++ b/src/main/resources/templates/grants/fragments/grantStatusFragment.html @@ -0,0 +1,31 @@ + + + + + + + + +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+ + \ No newline at end of file From 307b640e8ef868a41ffb398e54c3a50841153bf3 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 24 Dec 2018 11:07:02 +0400 Subject: [PATCH 08/17] Create js for grants --- src/main/resources/public/js/grants.js | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/resources/public/js/grants.js diff --git a/src/main/resources/public/js/grants.js b/src/main/resources/public/js/grants.js new file mode 100644 index 0000000..c33b4df --- /dev/null +++ b/src/main/resources/public/js/grants.js @@ -0,0 +1,42 @@ +/*\n' + + ' \n' + + ' '); + } + $('#dataConfirmModal').find('#myModalLabel').text($(this).attr('data-confirm')); + $('#dataConfirmOK').attr('href', href); + $('#dataConfirmModal').modal({show:true}); + return false; + }); +}); +/*]]>*/ \ No newline at end of file From 410d0d96346c2ceeba065ad648c924e44c63aada Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 24 Dec 2018 13:05:37 +0400 Subject: [PATCH 09/17] Create service&repository for Project --- .../project/repository/ProjectRepository.java | 11 ++ .../ulstu/project/service/ProjectService.java | 158 ++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 src/main/java/ru/ulstu/project/repository/ProjectRepository.java create mode 100644 src/main/java/ru/ulstu/project/service/ProjectService.java diff --git a/src/main/java/ru/ulstu/project/repository/ProjectRepository.java b/src/main/java/ru/ulstu/project/repository/ProjectRepository.java new file mode 100644 index 0000000..5a41fb1 --- /dev/null +++ b/src/main/java/ru/ulstu/project/repository/ProjectRepository.java @@ -0,0 +1,11 @@ +package ru.ulstu.project.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.grant.model.Grant; +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 new file mode 100644 index 0000000..8576e9b --- /dev/null +++ b/src/main/java/ru/ulstu/project/service/ProjectService.java @@ -0,0 +1,158 @@ +package ru.ulstu.project.service; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.deadline.model.Deadline; +import ru.ulstu.deadline.service.DeadlineService; +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.project.model.Project; +import ru.ulstu.project.model.ProjectDto; +import ru.ulstu.project.repository.ProjectRepository; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + +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 ProjectService { + private final static int MAX_DISPLAY_SIZE = 40; + + private final ProjectRepository projectRepository; + private final DeadlineService deadlineService; + + public ProjectService(ProjectRepository projectRepository, + DeadlineService deadlineService) { + this.projectRepository = projectRepository; + this.deadlineService = deadlineService; + } + + public List findAll() { + 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 { + Project newProject = copyFromDto(new Project(), projectDto); + newProject = projectRepository.save(newProject); + //paperNotificationService.sendCreateNotification(newGrant); + return newProject.getId(); + } + + private Project copyFromDto(Project project, ProjectDto projectDto) throws IOException { + project.setTitle(projectDto.getTitle()); + project.setDeadlines(deadlineService.saveOrCreate(projectDto.getDeadlines())); + return project; + } + +// 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()); +// } + + @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( +// 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(ProjectDto projectDto) throws IOException { + if (isEmpty(projectDto.getId())) { + create(projectDto); + } else { + //update(grantDto); + } + } + +// public GrantDto findById(Integer grantId) { +// return new GrantDto(grantRepository.findOne(grantId)); +// } +} From 2f68339ba230fa08d9bf5c90970c8fd76bda69bc Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 24 Dec 2018 15:18:52 +0400 Subject: [PATCH 10/17] Add thymeleaf template --- .../resources/templates/grants/dashboard.html | 169 +----------------- .../resources/templates/grants/grant.html | 164 ++++++----------- .../resources/templates/grants/grants.html | 36 ++++ 3 files changed, 94 insertions(+), 275 deletions(-) create mode 100644 src/main/resources/templates/grants/grants.html diff --git a/src/main/resources/templates/grants/dashboard.html b/src/main/resources/templates/grants/dashboard.html index afadb51..b469875 100644 --- a/src/main/resources/templates/grants/dashboard.html +++ b/src/main/resources/templates/grants/dashboard.html @@ -1,177 +1,24 @@ + layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml"> -
-
-
- +
+

Гранты

+
-
-
-
-
- - - - -
- -
- Название гранта -

Краткое описание

-

Статус: статус

-
- -
-
-
-
-
- - - - -
-
- Название гранта -

Краткое описание

-

Статус: статус

-
-
-
-
-
-
- - - - -
-
- Название гранта -

Краткое описание

-

Статус: статус

-
-
-
-
-
-
- - - - -
-
- Название гранта -

Краткое описание

-

Статус: статус

-
-
-
-
-
-
- - - - -
-
- Название гранта -

Краткое описание

-

Статус: статус

-
-
-
-
-
-
- - - - -
-
- Название гранта -

Краткое описание

-

Статус: статус

-
-
-
-
-
-
- - - - -
-
- Название гранта -

Краткое описание

-

Статус: статус

-
-
-
-
-
-
- - - - -
-
- Название гранта -

Краткое описание

-

Статус: статус

-
-
-
-
-
-
- - - - -
-
- Название гранта -

Краткое описание

-

Статус: статус

-
-
-
+
+ +
+
-
- \ No newline at end of file diff --git a/src/main/resources/templates/grants/grant.html b/src/main/resources/templates/grants/grant.html index 2f74adb..82098a6 100644 --- a/src/main/resources/templates/grants/grant.html +++ b/src/main/resources/templates/grants/grant.html @@ -1,7 +1,7 @@ + layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/html"> @@ -14,49 +14,68 @@

Редактирование гранта

- +
-
+
+
- - + + +

Incorrect title

- +
-
- +
- - + +
+ +
+ +
+
+ +
+
+ + +
+
+

Incorrect title

+
+ +
+
@@ -70,100 +89,20 @@
- -

Добавить проект

-
-
- -

Добавить показатель

-
-
- -

Добавить показатель

-
-
- - -
-

Редактировать - участников гранта

-
- -
@@ -188,10 +127,7 @@ console.debug(response); } }); - - getFromRest(urlPaperStatuses, function (response) { - fillSelect($("#status"), response); - }); + $('.selectpicker').selectpicker(); }); /*]]>*/ diff --git a/src/main/resources/templates/grants/grants.html b/src/main/resources/templates/grants/grants.html new file mode 100644 index 0000000..83083a0 --- /dev/null +++ b/src/main/resources/templates/grants/grants.html @@ -0,0 +1,36 @@ + + + + + +
+ + +
+
+
+
+

Гранты

+
+
+
+
+
+ +
+ +
+
+
+
+
+
+ +
+ + +
+ + From d7721ce19d4733e7b75f89ac939b5a527d376beb Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 24 Dec 2018 15:20:57 +0400 Subject: [PATCH 11/17] 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)); From fcbd91760d577e09c5538cafa8d2307f4318a8ac Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 24 Dec 2018 23:39:19 +0400 Subject: [PATCH 12/17] Rename table grant to grants --- .../resources/db/changelog-20181224_000000-schema.xml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/resources/db/changelog-20181224_000000-schema.xml diff --git a/src/main/resources/db/changelog-20181224_000000-schema.xml b/src/main/resources/db/changelog-20181224_000000-schema.xml new file mode 100644 index 0000000..61f932b --- /dev/null +++ b/src/main/resources/db/changelog-20181224_000000-schema.xml @@ -0,0 +1,9 @@ + + + + + + From 18ad291e8269f0466ec9f96a3051f64a7f3063f6 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Tue, 25 Dec 2018 00:14:38 +0400 Subject: [PATCH 13/17] Made button "delete grant" visible --- .../resources/templates/grants/fragments/grantLineFragment.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/templates/grants/fragments/grantLineFragment.html b/src/main/resources/templates/grants/fragments/grantLineFragment.html index f9d9e09..1048897 100644 --- a/src/main/resources/templates/grants/fragments/grantLineFragment.html +++ b/src/main/resources/templates/grants/fragments/grantLineFragment.html @@ -12,7 +12,7 @@ - From 816d115cc87ad6312e42f768e92113e0fc0ea825 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Tue, 25 Dec 2018 02:44:50 +0400 Subject: [PATCH 14/17] Hide button "Add Project" when project already exists (not perfect) --- src/main/resources/templates/grants/grant.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/resources/templates/grants/grant.html b/src/main/resources/templates/grants/grant.html index 82098a6..2e1e281 100644 --- a/src/main/resources/templates/grants/grant.html +++ b/src/main/resources/templates/grants/grant.html @@ -89,8 +89,10 @@
- +
+ +
From bcc64cda96abc23dfc45dd067895b02a5b2918d0 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Tue, 25 Dec 2018 12:50:14 +0400 Subject: [PATCH 15/17] Create model folder for grant --- .../{model => }/controller/GrantController.java | 4 ++-- src/main/java/ru/ulstu/grant/model/GrantDto.java | 1 + .../java/ru/ulstu/grant/model/GrantStatusDto.java | 2 ++ .../{model => }/repository/GrantRepository.java | 2 +- .../grant/{model => }/service/GrantService.java | 7 +++---- .../ulstu/project/repository/ProjectRepository.java | 1 - .../ru/ulstu/project/service/ProjectService.java | 12 ------------ 7 files changed, 9 insertions(+), 20 deletions(-) rename src/main/java/ru/ulstu/grant/{model => }/controller/GrantController.java (97%) rename src/main/java/ru/ulstu/grant/{model => }/repository/GrantRepository.java (90%) rename src/main/java/ru/ulstu/grant/{model => }/service/GrantService.java (96%) diff --git a/src/main/java/ru/ulstu/grant/model/controller/GrantController.java b/src/main/java/ru/ulstu/grant/controller/GrantController.java similarity index 97% rename from src/main/java/ru/ulstu/grant/model/controller/GrantController.java rename to src/main/java/ru/ulstu/grant/controller/GrantController.java index 350611d..636c6e5 100644 --- a/src/main/java/ru/ulstu/grant/model/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/controller/GrantController.java @@ -1,4 +1,4 @@ -package ru.ulstu.grant.model.controller; +package ru.ulstu.grant.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; @@ -7,7 +7,7 @@ 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.grant.service.GrantService; import javax.validation.Valid; import java.io.IOException; diff --git a/src/main/java/ru/ulstu/grant/model/GrantDto.java b/src/main/java/ru/ulstu/grant/model/GrantDto.java index 9070337..0f1850a 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantDto.java @@ -4,6 +4,7 @@ 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.grant.model.Grant; import ru.ulstu.project.model.ProjectDto; import java.util.ArrayList; diff --git a/src/main/java/ru/ulstu/grant/model/GrantStatusDto.java b/src/main/java/ru/ulstu/grant/model/GrantStatusDto.java index dae40e2..bcd5563 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantStatusDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantStatusDto.java @@ -1,5 +1,7 @@ package ru.ulstu.grant.model; +import ru.ulstu.grant.model.Grant; + public class GrantStatusDto { private final String id; private final String name; diff --git a/src/main/java/ru/ulstu/grant/model/repository/GrantRepository.java b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java similarity index 90% rename from src/main/java/ru/ulstu/grant/model/repository/GrantRepository.java rename to src/main/java/ru/ulstu/grant/repository/GrantRepository.java index bd1067d..65f2eb1 100644 --- a/src/main/java/ru/ulstu/grant/model/repository/GrantRepository.java +++ b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java @@ -1,4 +1,4 @@ -package ru.ulstu.grant.model.repository; +package ru.ulstu.grant.repository; import org.springframework.data.jpa.repository.JpaRepository; import ru.ulstu.grant.model.Grant; diff --git a/src/main/java/ru/ulstu/grant/model/service/GrantService.java b/src/main/java/ru/ulstu/grant/service/GrantService.java similarity index 96% rename from src/main/java/ru/ulstu/grant/model/service/GrantService.java rename to src/main/java/ru/ulstu/grant/service/GrantService.java index b753009..1c061d2 100644 --- a/src/main/java/ru/ulstu/grant/model/service/GrantService.java +++ b/src/main/java/ru/ulstu/grant/service/GrantService.java @@ -1,4 +1,4 @@ -package ru.ulstu.grant.model.service; +package ru.ulstu.grant.service; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; @@ -8,10 +8,9 @@ import ru.ulstu.deadline.service.DeadlineService; 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.grant.repository.GrantRepository; 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; @@ -144,7 +143,7 @@ public class GrantService { } public GrantDto findGrant(int id) { - return new ru.ulstu.grant.model.GrantDto(grantRepository.getOne(id)); + return new GrantDto(grantRepository.getOne(id)); } public void closeFailedGrants() { diff --git a/src/main/java/ru/ulstu/project/repository/ProjectRepository.java b/src/main/java/ru/ulstu/project/repository/ProjectRepository.java index 5a41fb1..ac41af2 100644 --- a/src/main/java/ru/ulstu/project/repository/ProjectRepository.java +++ b/src/main/java/ru/ulstu/project/repository/ProjectRepository.java @@ -1,7 +1,6 @@ package ru.ulstu.project.repository; import org.springframework.data.jpa.repository.JpaRepository; -import ru.ulstu.grant.model.Grant; import ru.ulstu.project.model.Project; public interface ProjectRepository extends JpaRepository { diff --git a/src/main/java/ru/ulstu/project/service/ProjectService.java b/src/main/java/ru/ulstu/project/service/ProjectService.java index 43802df..1a4d1c7 100644 --- a/src/main/java/ru/ulstu/project/service/ProjectService.java +++ b/src/main/java/ru/ulstu/project/service/ProjectService.java @@ -1,28 +1,16 @@ package ru.ulstu.project.service; -import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.service.DeadlineService; -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.project.model.Project; import ru.ulstu.project.model.ProjectDto; import ru.ulstu.project.repository.ProjectRepository; -import java.io.IOException; -import java.util.Arrays; -import java.util.Date; import java.util.List; -import java.util.stream.Collectors; 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 ProjectService { From ce78f8ad667612618df6921a9b7fde2c702ff10c Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 28 Dec 2018 13:57:20 +0400 Subject: [PATCH 16/17] 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

From ebb036ee715e721e1c119a8fd5a57d74e3742434 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 28 Dec 2018 14:00:09 +0400 Subject: [PATCH 17/17] some refactor --- src/main/java/ru/ulstu/grant/repository/GrantRepository.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/ru/ulstu/grant/repository/GrantRepository.java b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java index 65f2eb1..92dec43 100644 --- a/src/main/java/ru/ulstu/grant/repository/GrantRepository.java +++ b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java @@ -5,6 +5,4 @@ import ru.ulstu.grant.model.Grant; public interface GrantRepository 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); }