From: Roland Haeder Date: Thu, 15 Oct 2015 09:58:37 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=a7ae99e6ad7a8fd9f014cd0befd062b39c1cd035;p=jfinancials-war.git Continued: - rewrote user registration to event-driven (CDI event/observer) pattern - updated jar(s) Signed-off-by:Roland Häder --- diff --git a/lib/juser-core.jar b/lib/juser-core.jar index 404aa57a..320f3709 100644 Binary files a/lib/juser-core.jar and b/lib/juser-core.jar differ diff --git a/lib/juser-lib.jar b/lib/juser-lib.jar index c961a971..2dfb221c 100644 Binary files a/lib/juser-lib.jar and b/lib/juser-lib.jar differ diff --git a/src/java/org/mxchange/addressbook/beans/register/UserRegisterWebBean.java b/src/java/org/mxchange/addressbook/beans/register/UserRegisterWebBean.java index 71995c64..d1ec119b 100644 --- a/src/java/org/mxchange/addressbook/beans/register/UserRegisterWebBean.java +++ b/src/java/org/mxchange/addressbook/beans/register/UserRegisterWebBean.java @@ -18,6 +18,8 @@ package org.mxchange.addressbook.beans.register; import java.text.MessageFormat; import javax.enterprise.context.SessionScoped; +import javax.enterprise.event.Event; +import javax.enterprise.inject.Any; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; @@ -25,6 +27,8 @@ import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.addressbook.beans.user.UserWebController; +import org.mxchange.jusercore.events.registration.RegisteredUserEvent; +import org.mxchange.jusercore.events.registration.UserRegisteredEvent; import org.mxchange.jusercore.exceptions.DataRepeatMismatchException; import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; @@ -58,6 +62,13 @@ public class UserRegisterWebBean implements UserRegisterWebController { @Inject private UserWebController userController; + /** + * An en event fireable when a new user has registered + */ + @Inject + @Any + private Event registeredEvent; + /** * Default constructor */ @@ -113,17 +124,8 @@ public class UserRegisterWebBean implements UserRegisterWebController { // Call bean User registeredUser = this.registerBean.registerUser(user); - // Copy all data from registered->user - this.userController.copyUser(registeredUser); - - // Add user name and email address - this.userController.addUserNameEmailAddress(registeredUser); - - // Clear all data - this.userController.clearData(); - - // Set user id again - this.userController.setUserId(registeredUser.getUserId()); + // Fire event + this.registeredEvent.fire(new RegisteredUserEvent(registeredUser)); // All fine, redirect to proper page return "register_done"; //NOI18N diff --git a/src/java/org/mxchange/addressbook/beans/user/UserWebBean.java b/src/java/org/mxchange/addressbook/beans/user/UserWebBean.java index 0b04027d..7c9c5f64 100644 --- a/src/java/org/mxchange/addressbook/beans/user/UserWebBean.java +++ b/src/java/org/mxchange/addressbook/beans/user/UserWebBean.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.Objects; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; +import javax.enterprise.event.Observes; import javax.faces.view.facelets.FaceletException; import javax.inject.Named; import javax.naming.Context; @@ -40,6 +41,7 @@ import org.mxchange.jphone.phonenumbers.fax.FaxNumber; import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; import org.mxchange.jphone.phonenumbers.landline.LandLineNumber; import org.mxchange.jphone.phonenumbers.smsprovider.SmsProvider; +import org.mxchange.jusercore.events.registration.UserRegisteredEvent; import org.mxchange.jusercore.model.user.LoginUser; import org.mxchange.jusercore.model.user.User; import org.mxchange.jusercore.model.user.UserSessionBeanRemote; @@ -194,7 +196,6 @@ public class UserWebBean implements UserWebController { */ private String userPasswordRepeat; - /** * Whether the user wants a public profile */ @@ -225,8 +226,14 @@ public class UserWebBean implements UserWebController { } } - @Override - public void addUserNameEmailAddress (final User user) { + /** + * Adds user's name and email address to bean's internal list. It also + * updates the public user list if the user has decided to have a public + * profile on registration. + *

+ * @param user User instance + */ + private void addUserNameEmailAddress (final User user) { // Make sure the entry is not added yet if (this.userNameList.contains(user.getUserName())) { // Abort here @@ -249,8 +256,10 @@ public class UserWebBean implements UserWebController { return Collections.unmodifiableList(this.publicUserList); } - @Override - public void clearData () { + /** + * Clears all data in this bean + */ + private void clearData () { // Clear all data // - personal data this.setUserId(null); @@ -279,8 +288,12 @@ public class UserWebBean implements UserWebController { this.setUserPasswordRepeat(null); } - @Override - public void copyUser (final User user) { + /** + * Copies given user into the controller + *

+ * @param user User instance + */ + private void copyUser (final User user) { // Copy all fields: // - base data this.setUserId(user.getUserId()); @@ -422,6 +435,39 @@ public class UserWebBean implements UserWebController { return user; } + @Override + public void afterRegistration (final @Observes UserRegisteredEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); + } else if (event.getUser() == null) { + // Throw NPE again + throw new NullPointerException("event.user is null"); + } else if (event.getUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.user.userId is null"); + } else if (event.getUser().getUserId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); + } + + // Get user instance + User registeredUser = event.getUser(); + + // Copy all data from registered->user + this.copyUser(registeredUser); + + // Add user name and email address + this.addUserNameEmailAddress(registeredUser); + + // Clear all data + this.clearData(); + + // Set user id again + this.setUserId(registeredUser.getUserId()); + } + @Override public Date getBirthday () { return this.birthday; diff --git a/src/java/org/mxchange/addressbook/beans/user/UserWebController.java b/src/java/org/mxchange/addressbook/beans/user/UserWebController.java index 92cc995f..e887607e 100644 --- a/src/java/org/mxchange/addressbook/beans/user/UserWebController.java +++ b/src/java/org/mxchange/addressbook/beans/user/UserWebController.java @@ -22,6 +22,7 @@ import java.util.List; import org.mxchange.jcontacts.contact.gender.Gender; import org.mxchange.jcountry.data.Country; import org.mxchange.jphone.phonenumbers.smsprovider.SmsProvider; +import org.mxchange.jusercore.events.registration.UserRegisteredEvent; import org.mxchange.jusercore.model.user.User; /** @@ -31,15 +32,6 @@ import org.mxchange.jusercore.model.user.User; */ public interface UserWebController extends Serializable { - /** - * Adds user's name and email address to bean's internal list. It also - * updates the public user list if the user has decided to have a public - * profile on registration. - *

- * @param user User instance - */ - void addUserNameEmailAddress (final User user); - /** * All public user profiles *

@@ -47,18 +39,6 @@ public interface UserWebController extends Serializable { */ List allPublicUsers (); - /** - * Clears all data in this bean - */ - void clearData (); - - /** - * Copies given user into the controller - *

- * @param user User instance - */ - void copyUser (final User user); - /** * Creates an instance from all properties *

@@ -462,4 +442,11 @@ public interface UserWebController extends Serializable { * @return Whether at least one user has a public profile */ boolean isPublicUserRegistered (); + + /** + * Event observer for new user registrations + *

+ * @param event User registration event + */ + void afterRegistration (final UserRegisteredEvent event); }