2 * Copyright (C) 2016 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.beans.user;
19 import java.text.MessageFormat;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.SessionScoped;
25 import javax.enterprise.event.Event;
26 import javax.enterprise.event.Observes;
27 import javax.enterprise.inject.Any;
28 import javax.faces.context.FacesContext;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.addressbook.beans.BaseAddressbookController;
36 import org.mxchange.addressbook.beans.contact.AddressbookContactWebSessionController;
37 import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController;
38 import org.mxchange.addressbook.beans.login.user.AddressbookUserLoginWebSessionController;
39 import org.mxchange.jcontacts.contact.Contact;
40 import org.mxchange.jusercore.events.confirmation.ObservableUserConfirmedAccountEvent;
41 import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent;
42 import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent;
43 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
44 import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent;
45 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
46 import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent;
47 import org.mxchange.jusercore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
48 import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent;
49 import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent;
50 import org.mxchange.jusercore.events.user.update.ObservableUpdatedUserPersonalDataEvent;
51 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
52 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
53 import org.mxchange.jusercore.exceptions.UserNotFoundException;
54 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
55 import org.mxchange.jusercore.model.user.LoginUser;
56 import org.mxchange.jusercore.model.user.User;
57 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
58 import org.mxchange.jusercore.model.user.UserUtils;
59 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
62 * A user bean (controller)
64 * @author Roland Häder<roland@mxchange.org>
66 @Named ("userController")
68 public class AddressbookUserWebSessionBean extends BaseAddressbookController implements AddressbookUserWebSessionController {
73 private static final long serialVersionUID = 542_145_347_916L;
76 * General contact controller
79 private AddressbookContactWebSessionController contactController;
85 private AddressbookFeaturesWebApplicationController featureController;
88 * Event being fired when user updated personal data
92 private Event<ObservableUpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
97 private UserSessionBeanRemote userBean;
105 * A list of all user profiles
107 private List<User> userList;
110 * Login controller (bean)
113 private AddressbookUserLoginWebSessionController userLoginController;
118 private String userName;
123 private List<String> userNameList;
126 * User password (clear-text from web form)
128 private String userPassword;
131 * User password repeated (clear-text from web form)
133 private String userPasswordRepeat;
136 * Whether the user wants a public profile
138 private ProfileMode userProfileMode;
141 * A list of all public user profiles
143 * @TODO Hmm, makes that sense? Having visible user list in current
144 * (session-scoped) user's visible user list?
146 private List<User> visibleUserList;
149 * Default constructor
151 public AddressbookUserWebSessionBean () {
152 // Call super constructor
157 * Event observer for newly added users by administrator
159 * @param event Event being fired
161 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
162 // event should not be null
165 throw new NullPointerException("event is null"); //NOI18N
166 } else if (event.getAddedUser() == null) {
168 throw new NullPointerException("event.addedUser is null"); //NOI18N
169 } else if (event.getAddedUser().getUserId() == null) {
171 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
172 } else if (event.getAddedUser().getUserId() < 1) {
174 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
178 this.updateList(event.getAddedUser());
184 this.setUserId(event.getAddedUser().getUserId());
188 * Event observer for deleted user accounts (by administrator)
190 * @param event Event being fired
192 public void afterAdminDeletedUserEvent (final ObservableAdminDeletedUserEvent event) {
193 // event should not be null
196 throw new NullPointerException("event is null"); //NOI18N
197 } else if (event.getDeletedUser() == null) {
199 throw new NullPointerException("event.deletedUser is null"); //NOI18N
200 } else if (event.getDeletedUser().getUserId() == null) {
202 throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N
203 } else if (event.getDeletedUser().getUserId() < 1) {
205 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N
209 this.removeFromList(event.getDeletedUser());
216 * Event observer for linked users with existing contact data
218 * @param event Event being fired
220 public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
221 // event should not be null
224 throw new NullPointerException("event is null"); //NOI18N
225 } else if (event.getLinkedUser() == null) {
227 throw new NullPointerException("event.linkedUser is null"); //NOI18N
228 } else if (event.getLinkedUser().getUserId() == null) {
230 throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
231 } else if (event.getLinkedUser().getUserId() < 1) {
233 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
237 this.updateList(event.getLinkedUser());
243 this.setUserId(event.getLinkedUser().getUserId());
247 * Event observer for locked users
249 * @param event Event being fired
251 public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) {
252 // event should not be null
255 throw new NullPointerException("event is null"); //NOI18N
256 } else if (event.getLockedUser() == null) {
258 throw new NullPointerException("event.lockedUser is null"); //NOI18N
259 } else if (event.getLockedUser().getUserId() == null) {
261 throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
262 } else if (event.getLockedUser().getUserId() < 1) {
264 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
268 this.updateList(event.getLockedUser());
272 * Event observer for unlocked users
274 * @param event Event being fired
276 public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) {
277 // event should not be null
280 throw new NullPointerException("event is null"); //NOI18N
281 } else if (event.getUnlockedUser() == null) {
283 throw new NullPointerException("event.unlockedUser is null"); //NOI18N
284 } else if (event.getUnlockedUser().getUserId() == null) {
286 throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
287 } else if (event.getUnlockedUser().getUserId() < 1) {
289 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
293 this.updateList(event.getUnlockedUser());
297 * Event observer for updated user data by administrator
299 * @param event Event being updated
301 public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) {
302 // event should not be null
305 throw new NullPointerException("event is null"); //NOI18N
306 } else if (event.getUpdatedUser() == null) {
308 throw new NullPointerException("event.updatedUser is null"); //NOI18N
309 } else if (event.getUpdatedUser().getUserId() == null) {
311 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
312 } else if (event.getUpdatedUser().getUserId() < 1) {
314 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
318 this.updateList(event.getUpdatedUser());
325 * Event observer when user confirmed account.
327 * @param event Event being fired
329 public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) {
330 // event should not be null
333 throw new NullPointerException("event is null"); //NOI18N
334 } else if (event.getConfirmedUser() == null) {
336 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
337 } else if (event.getConfirmedUser().getUserId() == null) {
339 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
340 } else if (event.getConfirmedUser().getUserId() < 1) {
342 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
346 this.updateList(event.getConfirmedUser());
350 * Event observer for logged-in user
352 * @param event Event instance
354 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
355 // event should not be null
358 throw new NullPointerException("event is null"); //NOI18N
359 } else if (event.getLoggedInUser() == null) {
361 throw new NullPointerException("event.registeredUser is null"); //NOI18N
362 } else if (event.getLoggedInUser().getUserId() == null) {
364 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
365 } else if (event.getLoggedInUser().getUserId() < 1) {
367 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
370 // "Cache" user instance
371 User loggedInUser = event.getLoggedInUser();
373 // Copy all data to this bean
374 this.copyUser(loggedInUser);
376 // Is the user visible?
377 if (Objects.equals(loggedInUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
378 // Yes, then add user
379 this.visibleUserList.add(loggedInUser);
384 * Event observer for new user registrations
386 * @param event User registration event
388 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
389 // event should not be null
392 throw new NullPointerException("event is null"); //NOI18N
393 } else if (event.getRegisteredUser() == null) {
395 throw new NullPointerException("event.registeredUser is null"); //NOI18N
396 } else if (event.getRegisteredUser().getUserId() == null) {
398 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
399 } else if (event.getRegisteredUser().getUserId() < 1) {
401 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
405 User registeredUser = event.getRegisteredUser();
407 // Copy all data from registered->user
408 this.copyUser(registeredUser);
414 this.updateList(registeredUser);
417 this.addUserName(registeredUser);
419 // Is the account public?
420 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
421 // Also add it to this list
422 this.visibleUserList.add(registeredUser);
426 this.setUserId(registeredUser.getUserId());
430 * Method being call after user's password has been updated (and history
431 * entry has been created).
433 * @param event Event being observed
435 public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
439 throw new NullPointerException("event is null"); //NOI18N
440 } else if (event.getPasswordHistory() == null) {
442 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
443 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
445 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
446 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
448 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
452 this.updateList(event.getPasswordHistory().getUserPasswordHistoryUser());
456 * Listens to fired event when user updated personal data
458 * @param event Event being fired
460 public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) {
464 throw new NullPointerException("event is null"); //NOI18N
465 } else if (event.getUpdatedUser() == null) {
467 throw new NullPointerException("event.updatedUser is null"); //NOI18N
468 } else if (event.getUpdatedUser().getUserId() == null) {
470 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
471 } else if (event.getUpdatedUser().getUserId() < 1) {
473 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
477 this.updateList(event.getUpdatedUser());
481 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
482 public List<User> allUsers () {
484 return this.userList;
488 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
489 public List<User> allVisibleUsers () {
491 return this.visibleUserList;
495 public User createUserInstance (final boolean createContactData) {
497 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
499 // Required personal data must be set
500 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
502 // Create new user instance
503 User user = new LoginUser();
505 // Is user name required?
506 if (!this.isUserNameRequired()) {
507 // Generate pseudo-random user name
508 String randomName = this.userBean.generateRandomUserName();
510 // Set it and inivisible profile
511 this.setUserName(randomName);
512 this.setUserProfileMode(ProfileMode.INVISIBLE);
514 // Generate random password
515 String randomPassword = UserUtils.createRandomPassword(AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
517 // Set random password
518 this.setUserPassword(randomPassword);
519 this.setUserPasswordRepeat(randomPassword);
522 // Set user name and mode
523 user.setUserName(this.getUserName());
524 user.setUserProfileMode(this.getUserProfileMode());
526 // Is multiple registration page
527 if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
528 // Create contact instance
529 Contact contact = this.contactController.createContactInstance();
532 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
534 // Set contact in user
535 user.setUserContact(contact);
539 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
546 public User createUserLogin () {
548 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
551 if (this.getUserName() == null) {
553 throw new NullPointerException("userName is null"); //NOI18N
554 } else if (this.getUserName().isEmpty()) {
556 throw new IllegalStateException("userName is empty."); //NOI18N
559 // Create new user instance
560 User user = new LoginUser();
562 // Update all data ...
563 user.setUserName(this.getUserName());
566 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: user={1} - EXIT!", this.getClass().getSimpleName(), user));
568 // Return the new instance
573 public String doChangePersonalData () {
574 // This method shall only be called if the user is logged-in
575 if (!this.userLoginController.isUserLoggedIn()) {
577 throw new IllegalStateException("User is not logged-in"); //NOI18N
578 } else if (!this.isRequiredChangePersonalDataSet()) {
579 // Not all required fields are set
580 throw new FaceletException("Not all required fields are set."); //NOI18N
581 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
582 // Password not matching
583 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
584 } else if (!this.featureController.isFeatureEnabled("change_user_personal_data")) {
585 // Editing is not allowed
586 throw new IllegalStateException("User tried to edit personal data."); //NOI18N
590 User user = this.userLoginController.getLoggedInUser();
592 // Copy contact data to contact instance
593 this.contactController.updateContactDataFromController(user.getUserContact());
595 // It should be there, so run some tests on it
596 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
597 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
598 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
599 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
600 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
601 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
604 user.setUserProfileMode(this.getUserProfileMode());
606 // Send it to the EJB
607 User updatedUser = this.userBean.updateUserPersonalData(user);
610 this.updatedPersonalDataEvent.fire(new UpdatedUserPersonalDataEvent(updatedUser));
613 return "user_contact_data_saved"; //NOI18N
617 public Long getUserId () {
622 public void setUserId (final Long userId) {
623 this.userId = userId;
627 public String getUserName () {
628 return this.userName;
632 public void setUserName (final String userName) {
633 this.userName = userName;
637 public String getUserPassword () {
638 return this.userPassword;
642 public void setUserPassword (final String userPassword) {
643 this.userPassword = userPassword;
647 public String getUserPasswordRepeat () {
648 return this.userPasswordRepeat;
652 public void setUserPasswordRepeat (final String userPasswordRepeat) {
653 this.userPasswordRepeat = userPasswordRepeat;
657 public ProfileMode getUserProfileMode () {
658 return this.userProfileMode;
662 public void setUserProfileMode (final ProfileMode userProfileMode) {
663 this.userProfileMode = userProfileMode;
667 * Post-initialization of this class
670 public void init () {
673 // Get initial context
674 Context context = new InitialContext();
677 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
678 } catch (final NamingException e) {
680 throw new FaceletException(e);
683 // Initialize user list
684 this.userList = this.userBean.allUsers();
686 // Get full user name list for reducing EJB calls
687 this.userNameList = this.userBean.getUserNameList();
689 // Is the user logged-in?
690 if (this.userLoginController.isUserLoggedIn()) {
691 // Is logged-in, so load also users visible to memebers
692 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
694 // Initialize user list
695 this.visibleUserList = this.userBean.allPublicUsers();
700 public boolean isContactFound (final Contact contact) {
701 // The contact must be valid
702 if (null == contact) {
704 throw new NullPointerException("contact is null"); //NOI18N
705 } else if (contact.getContactId() == null) {
707 throw new NullPointerException("contact.contactId is null"); //NOI18N
708 } else if (contact.getContactId() < 1) {
710 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
713 // Default is not found
714 boolean isFound = false;
717 Iterator<User> iterator = this.allUsers().iterator();
719 // Loop through all entries
720 while (iterator.hasNext()) {
722 User next = iterator.next();
724 // Compare both objects
725 if (Objects.equals(contact, next.getUserContact())) {
737 public boolean isPublicUserProfileEnabled () {
738 // Get context parameter
739 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_public_profile_enabled"); //NOI18N
742 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
744 // This requires user names being enabled, too.
745 if ((isEnabled) && (!this.isUserNameRequired())) {
746 // Not valid state, users must be able to modify their profile, especially when it is public
747 throw new IllegalStateException("Public user profiles are enabled but user name requirement is disabled, this is not possible."); //NOI18N
755 public boolean isRequiredChangePersonalDataSet () {
756 return ((this.getUserProfileMode() != null) &&
757 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
758 (this.contactController.isRequiredChangePersonalDataSet()));
762 public boolean isRequiredPersonalDataSet () {
763 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
764 // Multiple registration page
765 return this.contactController.isRequiredPersonalDataSet();
767 // Single registration page
768 return (((this.getUserName() != null) || (!this.isUserNameRequired())) &&
769 (this.getUserProfileMode() != null) &&
770 (this.contactController.isRequiredPersonalDataSet()) &&
771 (this.getUserPassword() != null) &&
772 (this.getUserPasswordRepeat() != null));
777 public boolean isSamePasswordEntered () {
778 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
782 public boolean isUserIdEmpty () {
783 return ((this.getUserId() == null) || (this.getUserId() == 0));
787 public boolean isUserNameRegistered (final User user) {
788 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
792 public boolean isUserNameRequired () {
793 // Get context parameter
794 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_name_required"); //NOI18N
797 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
804 public boolean isVisibleUserFound () {
805 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
809 public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException {
810 // Parameter must be valid
811 if (null == emailAddress) {
813 throw new NullPointerException("emailAddress is null"); //NOI18N
814 } else if (emailAddress.isEmpty()) {
816 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
822 // Try to lookup it in visible user list
823 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
825 User next = iterator.next();
827 // Contact should be set
828 if (next.getUserContact() == null) {
830 throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getUserId())); //NOI18N
831 } else if (next.getUserContact().getContactEmailAddress() == null) {
832 // Email address should be set
833 throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getUserId())); //NOI18N
836 // Is the email address found?
837 if (Objects.equals(next.getUserContact().getContactEmailAddress(), emailAddress)) {
838 // Copy to other variable
846 // Not visible for the current user
847 throw new UserEmailAddressNotFoundException(emailAddress);
855 public User lookupUserById (final Long userId) throws UserNotFoundException {
856 // Parameter must be valid
857 if (null == userId) {
859 throw new NullPointerException("userId is null"); //NOI18N
860 } else if (userId < 1) {
862 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
868 // Try to lookup it in visible user list
869 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
871 User next = iterator.next();
873 // Is the user id found?
874 if (Objects.equals(next.getUserId(), userId)) {
875 // Copy to other variable
883 // Not visible for the current user
884 throw new UserNotFoundException(userId);
892 * Adds user's name to bean's internal list. It also updates the public user
893 * list if the user has decided to have a public account,
895 * @param user User instance
897 private void addUserName (final User user) {
898 // Make sure the entry is not added yet
899 if (this.userNameList.contains(user.getUserName())) {
901 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
902 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
904 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
908 this.userNameList.add(user.getUserName());
914 private void clear () {
917 this.setUserId(null);
918 this.setUserProfileMode(null);
921 this.setUserName(null);
922 this.setUserPassword(null);
923 this.setUserPasswordRepeat(null);
927 * Copies given user into the controller
929 * @param user User instance
931 private void copyUser (final User user) {
932 // Make sure the instance is valid
935 throw new NullPointerException("user is null"); //NOI18N
936 } else if (user.getUserContact() == null) {
938 throw new NullPointerException("user.userContact is null"); //NOI18N
943 this.setUserId(user.getUserId());
944 this.setUserProfileMode(user.getUserProfileMode());
948 * Removes user from all lists
950 * @param user User to remove
952 private void removeFromList (final User user) {
953 // The user should be valid
956 throw new NullPointerException("user is null"); //NOI18N
957 } else if (user.getUserId() == null) {
959 throw new NullPointerException("user.userId is null"); //NOI18N
960 } else if (user.getUserId() < 1) {
962 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
965 // Remove it from lists
966 this.userList.remove(user);
967 this.visibleUserList.remove(user);
969 // Remove name from list
970 this.userNameList.remove(user.getUserName());
974 * Updates list with given user instance
976 * @param user User instance
978 private void updateList (final User user) {
979 // The user should be valid
982 throw new NullPointerException("user is null"); //NOI18N
983 } else if (user.getUserId() == null) {
985 throw new NullPointerException("user.userId is null"); //NOI18N
986 } else if (user.getUserId() < 1) {
988 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
989 } else if (user.getUserContact() == null) {
991 throw new NullPointerException("user.userContact is null"); //NOI18N
992 } else if (user.getUserContact().getContactId() == null) {
994 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
995 } else if (user.getUserContact().getContactId() < 1) {
997 throw new NullPointerException(MessageFormat.format("user.userContact.contactId={0} is invalid.", user.getUserContact().getContactId())); //NOI18N
1000 // Get iterator from list
1001 Iterator<User> iterator = this.userList.iterator();
1003 // "Walk" through all entries
1004 while (iterator.hasNext()) {
1006 User next = iterator.next();
1008 // Is user id number the same?
1009 if (Objects.equals(user.getUserId(), next.getUserId())) {
1010 // Found entry, so remove it and abort
1011 this.userList.remove(next);
1017 this.userList.add(user);