From bc48a486ee390b93ecd019c04fb28e05cceef9d9 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 13 Feb 2020 23:44:48 +0400 Subject: [PATCH] quick add paper --- .../java/ru/ulstu/core/util/DateUtils.java | 6 ++++++ .../deadline/service/DeadlineService.java | 12 ++++++++++++ .../ru/ulstu/paper/controller/PapersView.java | 18 ++++++++++++++++++ .../ru/ulstu/paper/service/PaperService.java | 11 +++++++++++ .../META-INF/resources/paper/papers.xhtml | 5 ++++- 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/main/java/ru/ulstu/core/util/DateUtils.java b/src/main/java/ru/ulstu/core/util/DateUtils.java index bbcc86b..bcdb7ff 100644 --- a/src/main/java/ru/ulstu/core/util/DateUtils.java +++ b/src/main/java/ru/ulstu/core/util/DateUtils.java @@ -60,4 +60,10 @@ public class DateUtils { cal.add(Calendar.YEAR, count); return cal.getTime(); } + + public static LocalDate convertToLocalDate(Date dateToConvert) { + return dateToConvert.toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate(); + } } diff --git a/src/main/java/ru/ulstu/deadline/service/DeadlineService.java b/src/main/java/ru/ulstu/deadline/service/DeadlineService.java index 039ddbf..1611388 100644 --- a/src/main/java/ru/ulstu/deadline/service/DeadlineService.java +++ b/src/main/java/ru/ulstu/deadline/service/DeadlineService.java @@ -2,9 +2,11 @@ package ru.ulstu.deadline.service; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.core.util.DateUtils; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.repository.DeadlineRepository; +import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.List; import java.util.stream.Collectors; @@ -47,6 +49,12 @@ public class DeadlineService { return newDeadline; } + public Deadline create(Date date) { + Deadline deadline = new Deadline(); + deadline.setDate(date); + return create(deadline); + } + @Transactional public void remove(Integer deadlineId) { deadlineRepository.deleteById(deadlineId); @@ -55,4 +63,8 @@ public class DeadlineService { public Date findByGrantIdAndDate(Integer id, Date date) { return deadlineRepository.findByGrantIdAndDate(id, date); } + + public Deadline createWithOffset(Date date, long value, ChronoUnit chronoUnit) { + return create(DateUtils.localDateToDate(DateUtils.convertToLocalDate(date).plus(value, chronoUnit))); + } } diff --git a/src/main/java/ru/ulstu/paper/controller/PapersView.java b/src/main/java/ru/ulstu/paper/controller/PapersView.java index 3539f51..eca8714 100644 --- a/src/main/java/ru/ulstu/paper/controller/PapersView.java +++ b/src/main/java/ru/ulstu/paper/controller/PapersView.java @@ -1,5 +1,6 @@ package ru.ulstu.paper.controller; +import ru.ulstu.core.util.FacesUtil; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.service.PaperService; @@ -18,11 +19,20 @@ public class PapersView { private List papers; + private String newPaperTitle; + @PostConstruct public void init() { papers = paperService.findAll(); } + public void create() { + paperService.createByTitle(newPaperTitle); + FacesUtil.showInfoMessage("Статья создана", newPaperTitle); + newPaperTitle = ""; + papers = paperService.findAll(); + } + public List getPaperStatuses() { return Arrays.asList(Paper.PaperStatus.values()); } @@ -30,4 +40,12 @@ public class PapersView { public List getPapers() { return papers; } + + public String getNewPaperTitle() { + return newPaperTitle; + } + + public void setNewPaperTitle(String newPaperTitle) { + this.newPaperTitle = newPaperTitle; + } } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index b3738ce..885bc21 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -22,6 +22,7 @@ import ru.ulstu.user.service.UserService; import java.io.IOException; import java.text.MessageFormat; +import java.time.temporal.ChronoUnit; import java.util.Arrays; import java.util.Date; import java.util.HashSet; @@ -116,6 +117,7 @@ public class PaperService { @Transactional public Paper create(Paper paper) { Paper newPaper = paperRepository.save(paper); + newPaper.setCreateDate(new Date()); paperNotificationService.sendCreateNotification(newPaper); return newPaper; } @@ -426,4 +428,13 @@ public class PaperService { public void ping(int paperId) throws IOException { pingService.addPing(findPaperById(paperId)); } + + public void createByTitle(String newPaperTitle) { + Paper paper = new Paper(); + paper.setTitle(newPaperTitle); + paper.setStatus(DRAFT); + paper.getAuthors().add(userService.getCurrentUser()); + paper.getDeadlines().add(deadlineService.createWithOffset(new Date(), 1, ChronoUnit.WEEKS)); + create(paper); + } } diff --git a/src/main/resources/META-INF/resources/paper/papers.xhtml b/src/main/resources/META-INF/resources/paper/papers.xhtml index 0e7e7af..a282043 100644 --- a/src/main/resources/META-INF/resources/paper/papers.xhtml +++ b/src/main/resources/META-INF/resources/paper/papers.xhtml @@ -26,7 +26,7 @@ ' + filterMatchMode="contains"> @@ -40,6 +40,9 @@ + +