From 34c5e79cbb4298976e32c9a8c2dab81a9aed8f01 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 22 Feb 2020 09:03:18 +0400 Subject: [PATCH] edit user --- .../java/ru/ulstu/core/util/FacesUtil.java | 6 + .../ru/ulstu/paper/controller/PaperView.java | 6 +- .../user/controller/UserDegreeConverter.java | 21 ++++ .../ru/ulstu/user/controller/UserView.java | 39 +++++++ .../UsersView.java} | 4 +- .../META-INF/resources/admin/user.xhtml | 105 ++++++++++++++++++ .../META-INF/resources/admin/users.xhtml | 2 +- 7 files changed, 175 insertions(+), 8 deletions(-) create mode 100644 src/main/java/ru/ulstu/user/controller/UserDegreeConverter.java create mode 100644 src/main/java/ru/ulstu/user/controller/UserView.java rename src/main/java/ru/ulstu/user/{component/UserView.java => controller/UsersView.java} (86%) create mode 100644 src/main/resources/META-INF/resources/admin/user.xhtml diff --git a/src/main/java/ru/ulstu/core/util/FacesUtil.java b/src/main/java/ru/ulstu/core/util/FacesUtil.java index 5cd2211..74279dd 100644 --- a/src/main/java/ru/ulstu/core/util/FacesUtil.java +++ b/src/main/java/ru/ulstu/core/util/FacesUtil.java @@ -5,6 +5,7 @@ import javax.faces.application.FacesMessage; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import java.io.IOException; +import java.util.Map; public class FacesUtil { public static void redirectToPage(FacesContext context, String page, String params) { @@ -26,6 +27,11 @@ public class FacesUtil { redirectToPage(FacesContext.getCurrentInstance(), page, params); } + public static Map getRequestParams() { + return FacesContext.getCurrentInstance(). + getExternalContext().getRequestParameterMap(); + } + public static void showInfoMessage(String summary, String detail) { FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail); FacesContext fc = FacesContext.getCurrentInstance(); diff --git a/src/main/java/ru/ulstu/paper/controller/PaperView.java b/src/main/java/ru/ulstu/paper/controller/PaperView.java index c00f9cf..a6bc9eb 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperView.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperView.java @@ -8,14 +8,12 @@ import ru.ulstu.user.model.User; import ru.ulstu.user.service.UserService; import javax.annotation.PostConstruct; -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 @@ -30,9 +28,7 @@ public class PaperView implements Serializable { @PostConstruct public void init() { - Map params = FacesContext.getCurrentInstance(). - getExternalContext().getRequestParameterMap(); - paper = paperService.findPaperById(Integer.valueOf(params.get("id"))); + paper = paperService.findPaperById(Integer.valueOf(FacesUtil.getRequestParams().get("id"))); FacesUtil.showInfoMessage("Статья открыта", ""); } diff --git a/src/main/java/ru/ulstu/user/controller/UserDegreeConverter.java b/src/main/java/ru/ulstu/user/controller/UserDegreeConverter.java new file mode 100644 index 0000000..db8ae77 --- /dev/null +++ b/src/main/java/ru/ulstu/user/controller/UserDegreeConverter.java @@ -0,0 +1,21 @@ +package ru.ulstu.user.controller; + +import ru.ulstu.user.model.User; + +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.convert.Converter; +import javax.faces.convert.FacesConverter; + +@FacesConverter(value = "userDegreeConverter") +public class UserDegreeConverter implements Converter { + @Override + public Object getAsObject(FacesContext context, UIComponent component, String value) { + return User.UserDegree.valueOf(value); + } + + @Override + public String getAsString(FacesContext context, UIComponent component, Object value) { + return ((User.UserDegree) value).name(); + } +} diff --git a/src/main/java/ru/ulstu/user/controller/UserView.java b/src/main/java/ru/ulstu/user/controller/UserView.java new file mode 100644 index 0000000..4a07346 --- /dev/null +++ b/src/main/java/ru/ulstu/user/controller/UserView.java @@ -0,0 +1,39 @@ +package ru.ulstu.user.controller; + +import ru.ulstu.core.util.FacesUtil; +import ru.ulstu.user.model.User; +import ru.ulstu.user.service.UserService; + +import javax.annotation.PostConstruct; +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; + +@Named +@ViewScoped +public class UserView implements Serializable { + @Inject + private UserService userService; + + private User user; + + @PostConstruct + public void init() { + user = userService.findById(Integer.valueOf(FacesUtil.getRequestParams().get("id"))); + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public List getDegrees() { + return Arrays.asList(User.UserDegree.values()); + } +} diff --git a/src/main/java/ru/ulstu/user/component/UserView.java b/src/main/java/ru/ulstu/user/controller/UsersView.java similarity index 86% rename from src/main/java/ru/ulstu/user/component/UserView.java rename to src/main/java/ru/ulstu/user/controller/UsersView.java index 90e234a..31320c9 100644 --- a/src/main/java/ru/ulstu/user/component/UserView.java +++ b/src/main/java/ru/ulstu/user/controller/UsersView.java @@ -1,4 +1,4 @@ -package ru.ulstu.user.component; +package ru.ulstu.user.controller; import ru.ulstu.user.model.User; import ru.ulstu.user.service.UserService; @@ -12,7 +12,7 @@ import java.util.List; @Named @ViewScoped -public class UserView implements Serializable { +public class UsersView implements Serializable { @Inject private UserService userService; diff --git a/src/main/resources/META-INF/resources/admin/user.xhtml b/src/main/resources/META-INF/resources/admin/user.xhtml new file mode 100644 index 0000000..d145ea9 --- /dev/null +++ b/src/main/resources/META-INF/resources/admin/user.xhtml @@ -0,0 +1,105 @@ + + + + + + Редактирование статьи + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/META-INF/resources/admin/users.xhtml b/src/main/resources/META-INF/resources/admin/users.xhtml index 1b03dfc..ba394a0 100644 --- a/src/main/resources/META-INF/resources/admin/users.xhtml +++ b/src/main/resources/META-INF/resources/admin/users.xhtml @@ -10,7 +10,7 @@ text-align: right !important; } -