From 84a67513898d5d8d524b34eced3ee81651445ad9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 3 May 2016 17:25:29 +0200 Subject: [PATCH] Rewrites: - moved a lot unrelated user-only methods to proper admin controller MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../JobsUserProfileWebRequestBean.java | 9 +- .../JobsUserRegisterWebSessionBean.java | 9 +- .../user/JobsAdminUserWebRequestBean.java | 195 +++++++++++++++++- .../JobsAdminUserWebRequestController.java | 48 +++++ .../beans/user/JobsUserWebSessionBean.java | 83 +------- .../user/JobsUserWebSessionController.java | 25 --- web/admin/user/admin_user_delete.xhtml | 1 + web/admin/user/admin_user_list.xhtml | 2 +- web/admin/user/admin_user_show.xhtml | 1 + web/admin/user/admin_user_unlock.xhtml | 1 + 10 files changed, 267 insertions(+), 107 deletions(-) diff --git a/src/java/org/mxchange/jjobs/beans/profile/JobsUserProfileWebRequestBean.java b/src/java/org/mxchange/jjobs/beans/profile/JobsUserProfileWebRequestBean.java index 84c9cf6b..73527305 100644 --- a/src/java/org/mxchange/jjobs/beans/profile/JobsUserProfileWebRequestBean.java +++ b/src/java/org/mxchange/jjobs/beans/profile/JobsUserProfileWebRequestBean.java @@ -22,6 +22,7 @@ import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController; +import org.mxchange.jjobs.beans.user.JobsAdminUserWebRequestController; import org.mxchange.jjobs.beans.user.JobsUserWebSessionController; import org.mxchange.jusercore.exceptions.UserNotFoundException; import org.mxchange.jusercore.model.user.User; @@ -41,6 +42,12 @@ public class JobsUserProfileWebRequestBean implements JobsUserProfileWebRequestC */ private static final long serialVersionUID = 187_687_145_286_710L; + /** + * Administrative user controller + */ + @Inject + private JobsAdminUserWebRequestController adminUserController; + /** * User controller */ @@ -60,7 +67,7 @@ public class JobsUserProfileWebRequestBean implements JobsUserProfileWebRequestC try { // Try to get it - u = this.userController.lookupUserById(userId); + u = this.adminUserController.lookupUserById(userId); } catch (final UserNotFoundException ex) { // Throw again throw new FaceletException(ex); diff --git a/src/java/org/mxchange/jjobs/beans/register/JobsUserRegisterWebSessionBean.java b/src/java/org/mxchange/jjobs/beans/register/JobsUserRegisterWebSessionBean.java index dca9fc6c..8a8d9dbe 100644 --- a/src/java/org/mxchange/jjobs/beans/register/JobsUserRegisterWebSessionBean.java +++ b/src/java/org/mxchange/jjobs/beans/register/JobsUserRegisterWebSessionBean.java @@ -27,6 +27,7 @@ import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController; +import org.mxchange.jjobs.beans.user.JobsAdminUserWebRequestController; import org.mxchange.jjobs.beans.user.JobsUserWebSessionController; import org.mxchange.jusercore.events.registration.RegisteredUserEvent; import org.mxchange.jusercore.events.registration.UserRegisteredEvent; @@ -52,6 +53,12 @@ public class JobsUserRegisterWebSessionBean implements JobsUserRegisterWebSessio */ private static final long serialVersionUID = 47_828_986_719_691_592L; + /** + * User controller + */ + @Inject + private JobsAdminUserWebRequestController adminUserController; + /** * User controller */ @@ -104,7 +111,7 @@ public class JobsUserRegisterWebSessionBean implements JobsUserRegisterWebSessio } else if (!this.userController.isRequiredPersonalDataSet()) { // Not all required fields are set throw new FaceletException("Not all required fields are set."); //NOI18N - } else if (this.userController.isUserNameRegistered(user)) { + } else if (this.adminUserController.isUserNameRegistered(user)) { // User name is already used throw new FaceletException(new UserNameAlreadyRegisteredException(user)); } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) { diff --git a/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestBean.java b/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestBean.java index ae35f286..0517694c 100644 --- a/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestBean.java +++ b/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestBean.java @@ -35,7 +35,10 @@ import javax.naming.NamingException; import org.mxchange.jcontacts.contact.Contact; import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController; import org.mxchange.jjobs.beans.helper.JobsAdminWebRequestController; +import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController; import org.mxchange.jusercore.container.login.UserLoginContainer; +import org.mxchange.jusercore.events.login.UserLoggedInEvent; +import org.mxchange.jusercore.events.registration.UserRegisteredEvent; import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent; import org.mxchange.jusercore.events.user.add.AdminUserAddedEvent; import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent; @@ -85,6 +88,11 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr @Inject private JobsContactWebSessionController contactController; + /** + * A list of all selectable contacts + */ + private List selectableContacts; + /** * An event fired when the administrator has updated a new user */ @@ -108,11 +116,22 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr */ private List userList; + /** + * Login bean (controller) + */ + @Inject + private JobsUserLoginWebSessionController userLoginController; + /** * User name */ private String userName; + /** + * User name list + */ + private List userNameList; + /** * User password (unencrypted from web form) */ @@ -123,6 +142,11 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr */ private String userPasswordRepeat; + /** + * A list of all public user profiles + */ + private List visibleUserList; + /** * Default constructor */ @@ -203,7 +227,7 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr // Is the user name or email address used already? // @TODO Add password length check - if (this.userController.isUserNameRegistered(user)) { + if (this.isUserNameRegistered(user)) { // User name is already used throw new FaceletException(new UserNameAlreadyRegisteredException(user)); } else if ((this.adminHelper.getContact() == null) && (this.contactController.isEmailAddressRegistered(user.getUserContact()))) { @@ -257,6 +281,77 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr return "admin_list_user"; //NOI18N } + @Override + public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) { + // Trace message + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N + + // event 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.user is null"); //NOI18N + } else if (event.getRegisteredUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.user.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 + User registeredUser = event.getRegisteredUser(); + + // Debug message + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N + // Add user name + this.addUserName(registeredUser); + + // Clear all data + this.clear(); + + // Add user to local list + this.userList.add(registeredUser); + + // Is the account public? + if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) { + // Also add it to this list + this.visibleUserList.add(registeredUser); + } + + // Trace message + //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N + } + + @Override + public void afterUserLogin (final @Observes UserLoggedInEvent event) { + // Trace message + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N + + // event 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.user is null"); //NOI18N + } else if (event.getLoggedInUser().getUserId() == null) { + // userId is null + throw new NullPointerException("event.user.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 + } + + // Re-initialize list + this.visibleUserList = this.userBean.allMemberPublicVisibleUsers(); + + // Trace message + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N + } + @Override public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) { // Check parameter @@ -284,6 +379,12 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr return Collections.unmodifiableList(this.userList); } + @Override + public List allVisibleUsers () { + // Return it + return Collections.unmodifiableList(this.visibleUserList); + } + @Override public String editUserData () { // Get user instance @@ -393,6 +494,52 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr public void init () { // Initialize user list this.userList = this.userBean.allUsers(); + + // Get full user name list for reducing EJB calls + this.userNameList = this.userBean.getUserNameList(); + + // Is the user logged-in? + if (this.userLoginController.isUserLoggedIn()) { + // Is logged-in, so load also users visible to memebers + this.visibleUserList = this.userBean.allMemberPublicVisibleUsers(); + } else { + // Initialize user list + this.visibleUserList = this.userBean.allPublicUsers(); + } + + // Get all users + List allUsers = this.allUsers(); + + // Get all contacts + List allContacts = this.contactController.allContacts(); + + // Get iterator + Iterator iterator = allContacts.iterator(); + + // Loop through it + while (iterator.hasNext()) { + // Get next element + Contact next = iterator.next(); + + // Get iterator + Iterator userIterator = allUsers.iterator(); + + // Loop through all users + while (userIterator.hasNext()) { + // Get user instance + User nextUser = userIterator.next(); + + // Is contact same? + if (Objects.equals(next, nextUser.getUserContact())) { + // Found same + iterator.remove(); + break; + } + } + } + + // Set contact list + this.selectableContacts = allContacts; } @Override @@ -432,6 +579,16 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr return isFound; } + @Override + public boolean isUserNameRegistered (final User user) { + return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName()))); + } + + @Override + public boolean isVisibleUserFound () { + return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0)); + } + @Override public User lookupUserById (final Long userId) throws UserNotFoundException { // Parameter must be valid @@ -469,6 +626,42 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr return user; } + @Override + public List selectableContacts () { + return Collections.unmodifiableList(this.selectableContacts); + } + + /** + * Adds user's name to bean's internal list. It also updates the public user + * list if the user has decided to have a public account, + *

+ * @param user User instance + */ + private void addUserName (final User user) { + // Make sure the entry is not added yet + if (this.userNameList.contains(user.getUserName())) { + // Abort here + throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N + } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) { + // Already added + throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N + } + + // Add user name + this.userNameList.add(user.getUserName()); + } + + /** + * Clears this bean + */ + private void clear () { + // Clear all data + // - other data + this.setUserName(null); + this.setUserPassword(null); + this.setUserPasswordRepeat(null); + } + /** * Checks if same password is entered and that they are not empty. *

diff --git a/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestController.java b/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestController.java index fe084f60..92823501 100644 --- a/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestController.java +++ b/src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestController.java @@ -19,6 +19,8 @@ package org.mxchange.jjobs.beans.user; import java.io.Serializable; import java.util.List; import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jusercore.events.login.UserLoggedInEvent; +import org.mxchange.jusercore.events.registration.UserRegisteredEvent; import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent; import org.mxchange.jusercore.exceptions.UserNotFoundException; import org.mxchange.jusercore.model.user.User; @@ -30,6 +32,20 @@ import org.mxchange.jusercore.model.user.User; */ public interface JobsAdminUserWebRequestController extends Serializable { + /** + * Event observer for new user registrations + *

+ * @param event User registration event + */ + void afterRegistrationEvent (final UserRegisteredEvent event); + + /** + * Event observer for logged-in user + *

+ * @param event Event instance + */ + void afterUserLogin (final UserLoggedInEvent event); + /** * Listens to fired event when user updated personal data *

@@ -46,6 +62,23 @@ public interface JobsAdminUserWebRequestController extends Serializable { */ boolean isContactFound (final Contact contact); + /** + * Checks whether a public user account is registered. This means that at + * least one user profile has its flag "public user profile" enabled. + *

+ * @return Whether at least one user has a public profile + */ + boolean isVisibleUserFound (); + + /** + * Checks whether given user instance's name is used + *

+ * @param user User instance's name to check + *

+ * @return Whether it is already used + */ + boolean isUserNameRegistered (final User user); + /** * Tries to lookup user by given id number. If the user is not found or the * account status is not CONFIRMED proper exceptions are thrown. @@ -65,6 +98,13 @@ public interface JobsAdminUserWebRequestController extends Serializable { */ List allUsers (); + /** + * All public user profiles + *

+ * @return A list of all public user profiles + */ + List allVisibleUsers (); + /** * Checks whether users are registered *

@@ -130,4 +170,12 @@ public interface JobsAdminUserWebRequestController extends Serializable { */ void setUserPasswordRepeat (final String userPasswordRepeat); + /** + * Returns a list of all selectable contacts for user creation. Contacts + * from already existing users are excluded in this list. + *

+ * @return A list of all selectable contacts + */ + List selectableContacts (); + } diff --git a/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java b/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java index fdfb1380..eafd48d3 100644 --- a/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java +++ b/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java @@ -17,7 +17,6 @@ package org.mxchange.jjobs.beans.user; import java.text.MessageFormat; -import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Objects; @@ -69,17 +68,6 @@ public class JobsUserWebSessionBean implements JobsUserWebSessionController { @Inject private JobsContactWebSessionController contactController; - /** - * Login bean (controller) - */ - @Inject - private JobsUserLoginWebSessionController userLoginController; - - /** - * A list of all selectable contacts - */ - private List selectableContacts; - /** * Event being fired when user updated personal data */ @@ -98,14 +86,15 @@ public class JobsUserWebSessionBean implements JobsUserWebSessionController { private Long userId; /** - * User name + * Login bean (controller) */ - private String userName; + @Inject + private JobsUserLoginWebSessionController userLoginController; /** - * User name list + * User name */ - private List userNameList; + private String userName; /** * User password (unencrypted from web form) @@ -173,21 +162,12 @@ public class JobsUserWebSessionBean implements JobsUserWebSessionController { // Copy all data from registered->user this.copyUser(registeredUser); - // Add user name and email address - this.addUserNameEmailAddress(registeredUser); - // Clear all data this.clear(); // Set user id again this.setUserId(registeredUser.getUserId()); - // Is the account public? - if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) { - // Also add it to this list - this.visibleUserList.add(registeredUser); - } - // Trace message //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N } @@ -212,9 +192,6 @@ public class JobsUserWebSessionBean implements JobsUserWebSessionController { throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N } - // Re-initialize list - this.visibleUserList = this.userBean.allMemberPublicVisibleUsers(); - // Copy all data to this bean this.copyUser(event.getLoggedInUser()); @@ -222,12 +199,6 @@ public class JobsUserWebSessionBean implements JobsUserWebSessionController { //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N } - @Override - public List allVisibleUsers () { - // Return it - return Collections.unmodifiableList(this.visibleUserList); - } - @Override public User createUserInstance () { // User message @@ -351,17 +322,6 @@ public class JobsUserWebSessionBean implements JobsUserWebSessionController { */ @PostConstruct public void init () { - // Get full user name list for reducing EJB calls - this.userNameList = this.userBean.getUserNameList(); - - // Is the user logged-in? - if (this.userLoginController.isUserLoggedIn()) { - // Is logged-in, so load also users visible to memebers - this.visibleUserList = this.userBean.allMemberPublicVisibleUsers(); - } else { - // Initialize user list - this.visibleUserList = this.userBean.allPublicUsers(); - } } @Override @@ -390,16 +350,6 @@ public class JobsUserWebSessionBean implements JobsUserWebSessionController { return ((this.getUserId() == null) || (this.getUserId() == 0)); } - @Override - public boolean isUserNameRegistered (final User user) { - return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName()))); - } - - @Override - public boolean isVisibleUserFound () { - return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0)); - } - @Override public User lookupUserById (final Long userId) throws UserNotFoundException { // Init variable @@ -434,29 +384,6 @@ public class JobsUserWebSessionBean implements JobsUserWebSessionController { return localUser; } - /** - * 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 ha } - *

- * @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 - throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N - } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) { - // Already added - throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N - } - - // Add user name - this.userNameList.add(user.getUserName()); - - // Add email addres - this.contactController.addEmailAddress(user.getUserContact().getContactEmailAddress()); - } - /** * Clears this bean */ diff --git a/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionController.java b/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionController.java index 2c28731d..d905ce5e 100644 --- a/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionController.java +++ b/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionController.java @@ -17,7 +17,6 @@ package org.mxchange.jjobs.beans.user; import java.io.Serializable; -import java.util.List; import org.mxchange.jusercore.events.login.UserLoggedInEvent; import org.mxchange.jusercore.events.registration.UserRegisteredEvent; import org.mxchange.jusercore.exceptions.UserNotFoundException; @@ -62,13 +61,6 @@ public interface JobsUserWebSessionController extends Serializable { */ void afterUserLogin (final UserLoggedInEvent event); - /** - * All public user profiles - *

- * @return A list of all public user profiles - */ - List allVisibleUsers (); - /** * Creates an instance from all properties *

@@ -167,23 +159,6 @@ public interface JobsUserWebSessionController extends Serializable { */ boolean isSamePasswordEntered (); - /** - * Checks whether given user instance's name is used - *

- * @param user User instance's name to check - *

- * @return Whether it is already used - */ - boolean isUserNameRegistered (final User user); - - /** - * Checks whether a public user account is registered. This means that at - * least one user profile has its flag "public user profile" enabled. - *

- * @return Whether at least one user has a public profile - */ - boolean isVisibleUserFound (); - /** * Checks if the user id is empty *

diff --git a/web/admin/user/admin_user_delete.xhtml b/web/admin/user/admin_user_delete.xhtml index 0fdcb199..720c9033 100644 --- a/web/admin/user/admin_user_delete.xhtml +++ b/web/admin/user/admin_user_delete.xhtml @@ -10,6 +10,7 @@ + diff --git a/web/admin/user/admin_user_list.xhtml b/web/admin/user/admin_user_list.xhtml index 11ead762..4c7d045c 100644 --- a/web/admin/user/admin_user_list.xhtml +++ b/web/admin/user/admin_user_list.xhtml @@ -88,7 +88,7 @@

- +
diff --git a/web/admin/user/admin_user_show.xhtml b/web/admin/user/admin_user_show.xhtml index 98d5d702..927dfc9b 100644 --- a/web/admin/user/admin_user_show.xhtml +++ b/web/admin/user/admin_user_show.xhtml @@ -10,6 +10,7 @@ + diff --git a/web/admin/user/admin_user_unlock.xhtml b/web/admin/user/admin_user_unlock.xhtml index fe96f551..7b33fdaa 100644 --- a/web/admin/user/admin_user_unlock.xhtml +++ b/web/admin/user/admin_user_unlock.xhtml @@ -10,6 +10,7 @@ + -- 2.39.5