From 2c0b71b90e6c2bb43074309d52fe661c7912b480 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 20 Oct 2022 18:42:21 +0200 Subject: [PATCH] Please cherry-pick: - renamed copyUser() to copyToUser() because that method "copies" (read: sets) all required class fields except "entry-created" in given user instance - also used UserContact's parameterized constructor instead of default one and then setting all on my own - removed @PostConstruct method --- .../PizzaAdminCountryWebRequestBean.java | 19 ++-- .../PizzaFeatureWebApplicationBean.java | 8 -- .../login/PizzaUserLoginWebSessionBean.java | 101 +++++++++++++++++- 3 files changed, 109 insertions(+), 19 deletions(-) diff --git a/src/java/org/mxchange/pizzaapplication/beans/country/PizzaAdminCountryWebRequestBean.java b/src/java/org/mxchange/pizzaapplication/beans/country/PizzaAdminCountryWebRequestBean.java index 344b5240..6d5291ee 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/country/PizzaAdminCountryWebRequestBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/country/PizzaAdminCountryWebRequestBean.java @@ -107,7 +107,7 @@ public class PizzaAdminCountryWebRequestBean extends BasePizzaBean implements Pi /** * Adds country to all relevant beans and sends it to the EJB. A redirect - * should happen after successfull creation. + * should happen after successful creation. *

* @return Redirect outcome *

@@ -115,15 +115,14 @@ public class PizzaAdminCountryWebRequestBean extends BasePizzaBean implements Pi */ public String addCountry () { // Create new country object - final Country country = new CountryData(); - - // Add all data - country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix()); - country.setCountryCode(this.getCountryCode()); - country.setCountryExternalDialPrefix(this.getCountryExternalDialPrefix()); - country.setCountryI18nKey(this.getCountryI18nKey()); - country.setCountryIsLocalPrefixRequired(this.getCountryIsLocalPrefixRequired()); - country.setCountryPhoneCode(this.getCountryPhoneCode()); + final Country country = new CountryData( + this.getCountryAbroadDialPrefix(), + this.getCountryCode(), + this.getCountryExternalDialPrefix(), + this.getCountryI18nKey(), + this.getCountryIsLocalPrefixRequired(), + this.getCountryPhoneCode() + ); // Does it already exist? if (this.isCountryAdded(country)) { diff --git a/src/java/org/mxchange/pizzaapplication/beans/features/PizzaFeatureWebApplicationBean.java b/src/java/org/mxchange/pizzaapplication/beans/features/PizzaFeatureWebApplicationBean.java index 931d5ae4..f520b329 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/features/PizzaFeatureWebApplicationBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/features/PizzaFeatureWebApplicationBean.java @@ -16,7 +16,6 @@ */ package org.mxchange.pizzaapplication.beans.features; -import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.inject.Named; import org.mxchange.pizzaapplication.beans.BasePizzaBean; @@ -43,13 +42,6 @@ public class PizzaFeatureWebApplicationBean extends BasePizzaBean implements Piz super(); } - /** - * Post-construction method - */ - @PostConstruct - public void init () { - } - @Override public boolean isFeatureEnabled (final String feature) { // The parameter must be set diff --git a/src/java/org/mxchange/pizzaapplication/beans/user/login/PizzaUserLoginWebSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/user/login/PizzaUserLoginWebSessionBean.java index ed0ab8cf..a4ce66f2 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/user/login/PizzaUserLoginWebSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/user/login/PizzaUserLoginWebSessionBean.java @@ -171,10 +171,95 @@ public class PizzaUserLoginWebSessionBean extends BasePizzaBean implements Pizza throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N } - // All fine, so update list + // Get user instance + final User user = event.getCreatedUser(); + + // Set all fields here + this.copyToUser(user); + } + + /** + * Event observer for logged-in user + *

+ * @param event Event instance + */ + public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) { + // Event and contained entity instance should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getLoggedInUser() == null) { + // Throw NPE again + throw new NullPointerException("event.registeredUser is null"); //NOI18N + } else if (event.getLoggedInUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N + } else if (event.getLoggedInUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N + } + + // "Cache" user instance + final User user = event.getLoggedInUser(); + + // Copy all data to this bean + this.copyToUser(user); + } + + /** + * Event observer for user password changes + *

+ * @param event Event being fired + */ + public void afterUserPasswordChangedEvent (@Observes final ObservableUpdatedUserPasswordEvent event) { + // Is it valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUserPassword() == null) { + // Throw NPE + throw new NullPointerException("event.userPassword is null"); //NOI18N + } else if (event.getUserPassword().isEmpty()) { + // Throw NPE + throw new IllegalArgumentException("event.userPassword is empty"); //NOI18N + } + + // Set it here + this.setUserPassword(event.getUserPassword()); this.updatePasswordHistory(event.getPasswordHistory()); } + /** + * Event observer for new user registrations + *

+ * @param event User registration event + */ + public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) { + // Event and contained entity instance should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getRegisteredUser() == null) { + // Throw NPE again + throw new NullPointerException("event.registeredUser is null"); //NOI18N + } else if (event.getRegisteredUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N + } else if (event.getRegisteredUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N + } + + // Get user instance + final User registeredUser = event.getRegisteredUser(); + + // Copy all data from registered->user + this.copyToUser(registeredUser); + + // Set user id again + this.setUserId(registeredUser.getUserId()); + } + /** * Logout for administrator area. If a logged-in user instance exists, it is * being logged-out, too. @@ -402,6 +487,20 @@ public class PizzaUserLoginWebSessionBean extends BasePizzaBean implements Pizza private void clear () { // Clear all fields this.setUserCurrentPassword(null); + this.setUserProfileMode(null); + } + + /** + * Copies given user into the controller + *

+ * @param user User instance + */ + private void copyToUser (final User user) { + // Copy all fields: + // - base data + this.setUserId(user.getUserId()); + this.setUserName(user.getUserName()); + this.setUserProfileMode(user.getUserProfileMode()); } /** -- 2.39.5