From f16b4e6584fc639eeb4dbfc9b40d8652bcddab92 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 --- ...AddressbookAdminCountryWebRequestBean.java | 19 ++- .../AddressbookFeatureWebApplicationBean.java | 8 -- .../AddressbookUserLoginWebSessionBean.java | 128 +++++++++++++++++- 3 files changed, 136 insertions(+), 19 deletions(-) diff --git a/src/java/org/mxchange/addressbook/beans/country/AddressbookAdminCountryWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/country/AddressbookAdminCountryWebRequestBean.java index 671af537..6c63aee1 100644 --- a/src/java/org/mxchange/addressbook/beans/country/AddressbookAdminCountryWebRequestBean.java +++ b/src/java/org/mxchange/addressbook/beans/country/AddressbookAdminCountryWebRequestBean.java @@ -107,7 +107,7 @@ public class AddressbookAdminCountryWebRequestBean extends BaseAddressbookBean i /** * 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 AddressbookAdminCountryWebRequestBean extends BaseAddressbookBean i */ 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/addressbook/beans/features/AddressbookFeatureWebApplicationBean.java b/src/java/org/mxchange/addressbook/beans/features/AddressbookFeatureWebApplicationBean.java index 07be03ef..a0212aa2 100644 --- a/src/java/org/mxchange/addressbook/beans/features/AddressbookFeatureWebApplicationBean.java +++ b/src/java/org/mxchange/addressbook/beans/features/AddressbookFeatureWebApplicationBean.java @@ -16,7 +16,6 @@ */ package org.mxchange.addressbook.beans.features; -import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.inject.Named; import org.mxchange.addressbook.beans.BaseAddressbookBean; @@ -43,13 +42,6 @@ public class AddressbookFeatureWebApplicationBean extends BaseAddressbookBean im 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/addressbook/beans/user/login/AddressbookUserLoginWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/user/login/AddressbookUserLoginWebSessionBean.java index 4b5230b1..c308d41e 100644 --- a/src/java/org/mxchange/addressbook/beans/user/login/AddressbookUserLoginWebSessionBean.java +++ b/src/java/org/mxchange/addressbook/beans/user/login/AddressbookUserLoginWebSessionBean.java @@ -164,10 +164,122 @@ public class AddressbookUserLoginWebSessionBean extends BaseAddressbookBean impl throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N } - // All fine, so update list + // Set user id again + this.setUserId(event.getLinkedUser().getUserId()); + } + + /** + * Event observer for when a bean helper has successfully created a user + * instance, means the user exists. If the user does not exist, this event + * should not fire but instead a proper exception must be thrown. + *

+ * @param event User created event + */ + public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) { + // Is the instance valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getCreatedUser() == null) { + // Throw NPE again + throw new NullPointerException("event.createdUser is null"); //NOI18N + } else if (event.getCreatedUser().getUserId() == null) { + // Throw NPE again + throw new NullPointerException("event.createdUser.userId is null"); //NOI18N + } else if (event.getCreatedUser().getUserId() < 1) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N + } + + // 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. @@ -395,6 +507,20 @@ public class AddressbookUserLoginWebSessionBean extends BaseAddressbookBean impl 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