From 5755cf2f92b9f32ba57b13b0d359953c8d64c276 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 23 Nov 2019 10:26:05 +0400 Subject: [PATCH] save paper --- .../java/ru/ulstu/core/navigation/Page.java | 5 +++ .../controller/PaperStatusConverter.java | 2 +- .../paper/controller/PaperTypeConverter.java | 2 +- .../ru/ulstu/paper/controller/PaperView.java | 9 +++-- .../ru/ulstu/paper/service/PaperService.java | 34 ++++++++++++++++--- .../META-INF/resources/paper/paper.xhtml | 1 + src/main/resources/application.properties | 1 + 7 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 src/main/java/ru/ulstu/core/navigation/Page.java diff --git a/src/main/java/ru/ulstu/core/navigation/Page.java b/src/main/java/ru/ulstu/core/navigation/Page.java new file mode 100644 index 0000000..b43cc46 --- /dev/null +++ b/src/main/java/ru/ulstu/core/navigation/Page.java @@ -0,0 +1,5 @@ +package ru.ulstu.core.navigation; + +public class Page { + public static final String PAPER_LIST = "/paper/papers.xhtml"; +} diff --git a/src/main/java/ru/ulstu/paper/controller/PaperStatusConverter.java b/src/main/java/ru/ulstu/paper/controller/PaperStatusConverter.java index 02c729c..dce25e0 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperStatusConverter.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperStatusConverter.java @@ -16,6 +16,6 @@ public class PaperStatusConverter implements Converter { @Override public String getAsString(FacesContext context, UIComponent component, Object value) { - return ((Paper.PaperStatus) value).getStatusName(); + return ((Paper.PaperStatus) value).name(); } } diff --git a/src/main/java/ru/ulstu/paper/controller/PaperTypeConverter.java b/src/main/java/ru/ulstu/paper/controller/PaperTypeConverter.java index 5a2126b..b06fb38 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperTypeConverter.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperTypeConverter.java @@ -16,6 +16,6 @@ public class PaperTypeConverter implements Converter { @Override public String getAsString(FacesContext context, UIComponent component, Object value) { - return ((Paper.PaperType) value).getTypeName(); + return ((Paper.PaperType) value).name(); } } diff --git a/src/main/java/ru/ulstu/paper/controller/PaperView.java b/src/main/java/ru/ulstu/paper/controller/PaperView.java index 13a362a..b4b87ea 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperView.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperView.java @@ -1,5 +1,6 @@ package ru.ulstu.paper.controller; +import ru.ulstu.core.navigation.Page; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.service.PaperService; @@ -8,14 +9,13 @@ import javax.faces.context.FacesContext; import javax.faces.view.ViewScoped; import javax.inject.Inject; import javax.inject.Named; -import java.io.Serializable; import java.util.Arrays; import java.util.List; import java.util.Map; @Named @ViewScoped -public class PaperView implements Serializable { +public class PaperView { @Inject private PaperService paperService; @@ -43,4 +43,9 @@ public class PaperView implements Serializable { public List getPaperTypes() { return Arrays.asList(Paper.PaperType.values()); } + + public String save() { + paperService.save(paper); + return Page.PAPER_LIST; + } } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 2fee949..28e3274 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -103,17 +103,13 @@ public class PaperService { @Transactional public Integer create(PaperDto paperDto) throws IOException { Paper newPaper = copyFromDto(new Paper(), paperDto); - newPaper = paperRepository.save(newPaper); - paperNotificationService.sendCreateNotification(newPaper); - eventService.createFromPaper(newPaper); - return newPaper.getId(); + return create(newPaper).getId(); } @Transactional public Paper create(Paper paper) { Paper newPaper = paperRepository.save(paper); paperNotificationService.sendCreateNotification(newPaper); - eventService.createFromPaper(newPaper); return newPaper; } @@ -206,6 +202,26 @@ public class PaperService { return paper.getId(); } + @Transactional + public Integer update(Paper newPaper) { + Paper oldPaper = paperRepository.getOne(newPaper.getId()); + Paper.PaperStatus oldStatus = oldPaper.getStatus(); + Set oldAuthors = new HashSet<>(oldPaper.getAuthors()); + + newPaper = paperRepository.save(newPaper); + for (User author : newPaper.getAuthors()) { + if (!oldAuthors.contains(author)) { + paperNotificationService.sendCreateNotification(newPaper); + } + } + + if (newPaper.getStatus() != oldStatus) { + paperNotificationService.statusChangeNotification(newPaper, oldStatus); + } + + return newPaper.getId(); + } + @Transactional public void delete(Integer paperId) { Paper paper = paperRepository.getOne(paperId); @@ -292,6 +308,14 @@ public class PaperService { } } + public void save(Paper paper) { + if (isEmpty(paper.getId())) { + create(paper); + } else { + update(paper); + } + } + public PaperDto findById(Integer paperId) { return new PaperDto(paperRepository.getOne(paperId)); } diff --git a/src/main/resources/META-INF/resources/paper/paper.xhtml b/src/main/resources/META-INF/resources/paper/paper.xhtml index 12f1b92..f919332 100644 --- a/src/main/resources/META-INF/resources/paper/paper.xhtml +++ b/src/main/resources/META-INF/resources/paper/paper.xhtml @@ -30,6 +30,7 @@ itemValue="#{type}"/> + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 756c3b3..dea9a86 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -18,6 +18,7 @@ logging.level.com.gargoylesoftware.htmlunit=ERROR #jsf joinfaces.primefaces.theme=casablanca joinfaces.primefaces.font-awesome=true +joinfaces.mojarra.enable-restore-view11-compatibility=true # Mail Settings spring.mail.host=smtp.yandex.ru spring.mail.port=465