From d67faac8b2bfff11c5dbc49ce2cf3f2ec23d1cab Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 15 Feb 2016 23:30:36 +0100 Subject: [PATCH] As converters and validators cannot fire events, the controller has to load the object and fire the event. --- .../AddressbookWebSessionBean.java | 53 +++++++++- .../AddressbookWebSessionController.java | 7 ++ .../profile/UserProfileWebRequestBean.java | 97 ++++++------------- .../UserProfileWebRequestController.java | 21 ---- .../templates/generic/user_profile_link.tpl | 10 +- web/login/login_shared_addressbooks.xhtml | 9 +- .../login_start_sharing_addressbook.xhtml | 4 +- web/user/show_addressbook.xhtml | 11 +-- web/user/user_list.xhtml | 12 ++- 9 files changed, 108 insertions(+), 116 deletions(-) diff --git a/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionBean.java index d01fbbef..0d85c24c 100644 --- a/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionBean.java +++ b/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionBean.java @@ -27,7 +27,9 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; +import javax.enterprise.event.Event; import javax.enterprise.event.Observes; +import javax.enterprise.inject.Any; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; @@ -36,7 +38,9 @@ import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.addressbook.beans.login.UserLoginWebSessionController; import org.mxchange.addressbook.events.addressbook.AddressbookLoadedEvent; +import org.mxchange.addressbook.events.addressbook.LoadedAddressbookEvent; import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException; +import org.mxchange.addressbook.exceptions.AddressbookNotFoundException; import org.mxchange.addressbook.model.addressbook.Addressbook; import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote; import org.mxchange.addressbook.model.addressbook.UserAddressbook; @@ -99,6 +103,13 @@ public class AddressbookWebSessionBean implements AddressbookWebSessionControlle @Inject private UserLoginWebSessionController loginController; + /** + * Event fired when user has logged in + */ + @Inject + @Any + private Event loadedEvent; + /** * A list of all user's address books */ @@ -352,6 +363,14 @@ public class AddressbookWebSessionBean implements AddressbookWebSessionControlle //this.addressbookBean.getUserCountMap() } + @Override + public boolean isAddressbookLoaded () { + return ((this.getAddressbookId() instanceof Long) + && (this.getAddressbookName() instanceof String) + && (!this.getAddressbookName().isEmpty()) + && (this.getAddressbookUser() instanceof User)); + } + @Override public boolean isAddressbookNameUsed (final String addressbookName) { // Is it zero size? @@ -399,11 +418,35 @@ public class AddressbookWebSessionBean implements AddressbookWebSessionControlle } @Override - public boolean isAddressbookLoaded () { - return ((this.getAddressbookId() instanceof Long) - && (this.getAddressbookName() instanceof String) - && (!this.getAddressbookName().isEmpty()) - && (this.getAddressbookUser() instanceof User)); + public boolean loadAddressbook () { + // Check if the id is set + if (this.getAddressbookId() == null) { + // Throw NPE + throw new NullPointerException("this.addressbookId is null"); + } else if (this.getAddressbookId() < 1) { + // Not valid id + throw new IllegalStateException(MessageFormat.format("this.addressbook={0} is invalid", this.getAddressbookId())); + } + + // Default is not found + boolean isFound = false; + + try { + // Then try to look it up + Addressbook a = this.addressbookBean.getAddressbookById(this.getAddressbookId()); + + // Fire event here + this.loadedEvent.fire(new LoadedAddressbookEvent(a)); + + // Found it + isFound = true; + } catch (final AddressbookNotFoundException ex) { + // Not found! + throw new FaceletException(ex); + } + + // Return status + return isFound; } /** diff --git a/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionController.java b/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionController.java index af97a86b..662c0fdb 100644 --- a/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionController.java +++ b/src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebSessionController.java @@ -208,4 +208,11 @@ public interface AddressbookWebSessionController extends Serializable { * @return Whether the address book is loaded */ boolean isAddressbookLoaded (); + + /** + * Loads address book from current id + *

+ * @return Whether the address book was found + */ + boolean loadAddressbook (); } diff --git a/src/java/org/mxchange/addressbook/beans/profile/UserProfileWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/profile/UserProfileWebRequestBean.java index c10dff0c..8473c49b 100644 --- a/src/java/org/mxchange/addressbook/beans/profile/UserProfileWebRequestBean.java +++ b/src/java/org/mxchange/addressbook/beans/profile/UserProfileWebRequestBean.java @@ -47,11 +47,6 @@ public class UserProfileWebRequestBean implements UserProfileWebRequestControlle @Inject private UserLoginWebSessionController loginController; - /** - * User instance - */ - private User user; - /** * User controller */ @@ -59,97 +54,63 @@ public class UserProfileWebRequestBean implements UserProfileWebRequestControlle private UserWebSessionController userController; @Override - public User getUser () { - return this.user; - } + public boolean isProfileLinkVisibleById (final Long userId) { + // Init user instance + User u = null; - @Override - public void setUser (final User user) { - this.user = user; + try { + // Try to get it + u = userController.lookupUserById(userId); + } catch (final UserNotFoundException ex) { + // Throw again + throw new FaceletException(ex); + } + + // Is it null? + if (null == u) { + // Not found, not visible. + return false; + } + + // Ask other method + return this.isProfileLinkVisibleByUser(u); } @Override - public boolean isProfileLinkVisible () { + public boolean isProfileLinkVisibleByUser (final User user) { // Check on user - if (this.getUser() == null) { + if (user == null) { /* * Not set, means wrong invocation of this method as the user * instance needs to be set first. */ - throw new NullPointerException("this.user is null"); //NOI18N - } else if (this.getUser().getUserId() == null) { + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { /* * If the id number is not set it means that the user instance has * not been persisted and the JPA has not been flushed. Or a * "virgin" instance (e.g. from registration) has been used. */ - throw new NullPointerException("this.user.userId is null"); //NOI18N - } else if (this.getUser().getUserId() < 1) { + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { /* * The id number is set invalid for an unknown reason. */ - throw new IllegalArgumentException(MessageFormat.format("this.user.userId={0} is invalid", this.getUser().getUserId())); //NOI18N - } else if (this.getUser().getUserProfileMode() == null) { + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N + } else if (user.getUserProfileMode() == null) { /* * Possibly an out-dated user profile is being used. This should not * happen. */ - throw new NullPointerException("this.user.userProfileMode is null"); //NOI18N + throw new NullPointerException("user.userProfileMode is null"); //NOI18N } // Get profile mode from user instance (safe now) - ProfileMode profileMode = this.getUser().getUserProfileMode(); + ProfileMode profileMode = user.getUserProfileMode(); // Check all conditions (except for admin) // TODO: Add admin role somehow? return ((profileMode.equals(ProfileMode.PUBLIC)) || (this.loginController.isUserLoggedIn()) && (profileMode.equals(ProfileMode.MEMBERS))); } - - @Override - public boolean isProfileLinkVisibleById (final Long userId) { - // Init user instance - User u = null; - - try { - // Try to get it - u = this.userController.lookupUserById(userId); - } catch (final UserNotFoundException ex) { - // Throw again - throw new FaceletException(ex); - } - - // Set it here - this.setUser(u); - - // Is it null? - if (null == u) { - // Not found, not visible. - return false; - } - - // Ask other method - return this.isProfileLinkVisible(); - } - - @Override - public boolean isProfileLinkVisibleByUser (final User user) { - // Is it correctly set? - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); - } else if (user.getUserId() == null) { - // Throw NPE again - throw new NullPointerException("user.userId is null"); - } else if (user.getUserId() < 1) { - // Invalid user id set - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); - } - - // Set user here - this.setUser(user); - - // Ask other method - return this.isProfileLinkVisible(); - } } diff --git a/src/java/org/mxchange/addressbook/beans/profile/UserProfileWebRequestController.java b/src/java/org/mxchange/addressbook/beans/profile/UserProfileWebRequestController.java index e7306887..ea9e8c91 100644 --- a/src/java/org/mxchange/addressbook/beans/profile/UserProfileWebRequestController.java +++ b/src/java/org/mxchange/addressbook/beans/profile/UserProfileWebRequestController.java @@ -26,13 +26,6 @@ import org.mxchange.jusercore.model.user.User; */ public interface UserProfileWebRequestController extends Serializable { - /** - * Checks if the current user profile link is visible - *

- * @return Whether the profile link is visible - */ - boolean isProfileLinkVisible (); - /** * Checks if the user profile link is visible *

@@ -50,18 +43,4 @@ public interface UserProfileWebRequestController extends Serializable { * @return Whether the user's profile is visible */ boolean isProfileLinkVisibleByUser (final User user); - - /** - * Getter for user instance - *

- * @return User instance - */ - User getUser (); - - /** - * Setter for user instance - *

- * @param user User instance - */ - void setUser (final User user); } diff --git a/web/WEB-INF/templates/generic/user_profile_link.tpl b/web/WEB-INF/templates/generic/user_profile_link.tpl index c142209e..037b83a4 100644 --- a/web/WEB-INF/templates/generic/user_profile_link.tpl +++ b/web/WEB-INF/templates/generic/user_profile_link.tpl @@ -5,12 +5,12 @@ xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets"> - + - + - - - + + + diff --git a/web/login/login_shared_addressbooks.xhtml b/web/login/login_shared_addressbooks.xhtml index e531374f..824c0e88 100644 --- a/web/login/login_shared_addressbooks.xhtml +++ b/web/login/login_shared_addressbooks.xhtml @@ -3,8 +3,7 @@ + xmlns:f="http://xmlns.jcp.org/jsf/core"> #{msg.PAGE_TITLE_LOGIN_SHARED_ADDRESSBOOKS} @@ -21,8 +20,6 @@

- - #{msg.SHARED_ADDRESSBOOK} @@ -34,7 +31,9 @@ #{msg.SHAREE_USER_NAME} - + + + diff --git a/web/login/login_start_sharing_addressbook.xhtml b/web/login/login_start_sharing_addressbook.xhtml index f86110d8..491ad98c 100644 --- a/web/login/login_start_sharing_addressbook.xhtml +++ b/web/login/login_start_sharing_addressbook.xhtml @@ -32,7 +32,9 @@ #{msg.USER_NAME} - + + + diff --git a/web/user/show_addressbook.xhtml b/web/user/show_addressbook.xhtml index 7000aaed..e7137614 100644 --- a/web/user/show_addressbook.xhtml +++ b/web/user/show_addressbook.xhtml @@ -3,8 +3,7 @@ + xmlns:f="http://xmlns.jcp.org/jsf/core"> @@ -22,9 +21,7 @@ - - - + #{msg.TABLE_HEADER_SHOW_ADDRESSBOOK} #{msg.ADDRESSBOOK_ID} @@ -34,7 +31,9 @@ #{msg.ADDRESSBOOK_OWNER} - + + + #{msg.ADDRESSBOOK_CREATED} diff --git a/web/user/user_list.xhtml b/web/user/user_list.xhtml index 12f18f92..8f5f4b63 100644 --- a/web/user/user_list.xhtml +++ b/web/user/user_list.xhtml @@ -3,8 +3,7 @@ + xmlns:f="http://xmlns.jcp.org/jsf/core"> #{msg.PAGE_TITLE_USER_LIST} @@ -20,15 +19,18 @@
- #{msg.USER_ID} - + + + #{msg.USER_NAME} - + + + -- 2.39.5