From a7ae99e6ad7a8fd9f014cd0befd062b39c1cd035 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 15 Oct 2015 11:58:37 +0200 Subject: [PATCH] =?utf8?q?Continued:=20-=20rewrote=20user=20registration?= =?utf8?q?=20to=20event-driven=20(CDI=20event/observer)=20pattern=20-=20up?= =?utf8?q?dated=20jar(s)=20Signed-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- lib/juser-core.jar | Bin 23003 -> 24733 bytes lib/juser-lib.jar | Bin 3978 -> 3978 bytes .../beans/register/UserRegisterWebBean.java | 24 +++---- .../addressbook/beans/user/UserWebBean.java | 60 ++++++++++++++++-- .../beans/user/UserWebController.java | 29 +++------ 5 files changed, 74 insertions(+), 39 deletions(-) diff --git a/lib/juser-core.jar b/lib/juser-core.jar index 404aa57a64c762198ae62f54693ad7854a9566bf..320f3709346dffe09dcb4c35042014ef71c3e1d6 100644 GIT binary patch delta 1842 zcma)6O>A355T4!l>=*yUNgUU9l4r+>+^CLI(oh7lNQ*;Af)b+mr%B8u#iCY~5VazX znp0Ic^wgp%83{O0Q6w%@l>&r{HU~;52ZT6)xPYkWp&&IXZ~zIZ6%=OHHuZ}h2wVHU z-I>{MXLe@xyA5%EOL#LAn(hW@0N9>e^(N%)xz)#BlQjP_#@}BP^u>tS&yW)b7*@q_ z%l6#V7}WiuSQPEur4yxN%jN!hPODjx&0Xg8q4029yd?sZ;tWsG|hm}z|k&-)ie8ovsL_x#T@ z9>;ZO3NM5jd%|`eEEG=^z0e2|FL=N!;2>_bg?lQOX}Kkv&A8Rws#Uj~vf09MKG>!r z7NG4(%s>~#sDl6pNN5Kq7&NCkpp^^!?1-8R^2CR&Y>j;U_sg6IOUt4i8WC`jk%{Q%^CPd)d9F zd_=II2Y<<{N8uisxHWuB?)6$ovxiKXCAmo8o5Mcr)SP(fkfPWm&7ebR{?NlLjqxjH zc{gft5WfBLMGps3unFWrKXKCQUJ+{?Q)8AAwUGbi`!{l%I;s@xPI9XI-|tfA@EEp` z&ovYI2tH9zfxkX&$}4`pXS~+f$5IveN^#8bwFbar+_K#tQpWqj4m=)+tj+j217~MU zIoVNF?7~#RuUPu}e|X153_M0GEH|Q6V;v97CYfSk)|5|ovvjS?jfbO6%)*LqNpW{~ zMG>#LJy|g2^#tWSj^_#;Y<7txo+p*$f~I5Zbj5{}i2?r#JzP8nEH3;s`j7E5&xVx0 ox1Rgt#aU`W9%+m5`%_d+N&I*`6wFLGxJ$vLe~fDW=nSv^2D<|7xBvhE delta 716 zcmbPxkn#3rMxFp~W)?061`Y-WOTURcnoJgc6V0bH0ofN6K;$P)AURo<(HKMpG1`Kt ziHy!5Dw@d-M9pThgzz*cCvb94=3?doamAQjfz;+$=9i2h+JiNR4Mb-NL@4Zok6@M;l3d1b9gX_dK)1#IR%L0Ww|EnNAiG#Y$IhD zxh8LnkOKOHZE_Vv5U7sP6)MO*`4mL(22f=ySnxui5MO{dBa;X-EDR^>N69dr0ZX2b z5}163g>Q08ln}`L8BsD!+gTd{0_+{a0dz7R>R)S<^17!{;Z@MdP=VqoB4V6d6Ut;J|P(QF2z&BXJHK;okoqs?R)MiU?z$Y=#5dl(&n zOk&N z9v2`P$S6GdFONBpWy%`>B-?m{futal@Z`6=HXwN*%a$(|$Vy}tp1h7P9mo>nF984^ C!!4Hp delta 176 zcmeB@?~>;Z@MdP=VqoB4U{IULt;MK1(QF2z+QjpUK;okoquOK{MiU?z$Y=#5dl(&n zf~?SjzG2rk0Hn;;mNf;>Ok&N z9v2`P$S6GdFONBpWy%`>B-?m{futal@Z`6=HXwN*%a$(|$Vy}tp1h7P9mo>nF985! C8Y_nY 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); } -- 2.39.5