2 * Copyright (C) 2016, 2017 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.jjobs.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.jcontacts.contact.Contact;
36 import org.mxchange.jjobs.beans.BaseJobsController;
37 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
38 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
39 import org.mxchange.jjobs.beans.localization.JobsLocalizationSessionController;
40 import org.mxchange.jjobs.beans.user.login.JobsUserLoginWebSessionController;
41 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
42 import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent;
43 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
44 import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent;
45 import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent;
46 import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent;
47 import org.mxchange.jusercore.events.user.update.ObservableUpdatedUserPersonalDataEvent;
48 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
49 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
50 import org.mxchange.jusercore.exceptions.UserNotFoundException;
51 import org.mxchange.jusercore.model.user.LoginUser;
52 import org.mxchange.jusercore.model.user.User;
53 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
54 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
55 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
56 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
57 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
58 import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
59 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
60 import org.mxchange.juserlogincore.login.UserLoginUtils;
63 * A user controller (bean)
65 * @author Roland Häder<roland@mxchange.org>
67 @Named ("userController")
69 public class JobsUserWebSessionBean extends BaseJobsController implements JobsUserWebSessionController {
74 private static final long serialVersionUID = 542_145_347_916L;
77 * General contact controller
80 private JobsContactWebSessionController contactController;
86 private JobsFeaturesWebApplicationController featureController;
89 * Localization controller
92 private JobsLocalizationSessionController localizationController;
95 * Event being fired when user updated personal data
99 private Event<ObservableUpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
104 private UserSessionBeanRemote userBean;
112 * A list of all user profiles
114 private List<User> userList;
117 * Login controller (bean)
120 private JobsUserLoginWebSessionController userLoginController;
125 private String userName;
130 private List<String> userNameList;
133 * User password (clear-text from web form)
135 private String userPassword;
138 * User password repeated (clear-text from web form)
140 private String userPasswordRepeat;
143 * Whether the user wants a public profile
145 private ProfileMode userProfileMode;
148 * A list of all public user profiles
150 * @TODO Hmm, makes that sense? Having visible user list in current
151 * (session-scoped) user's visible user list?
153 private List<User> visibleUserList;
156 * Default constructor
158 public JobsUserWebSessionBean () {
159 // Call super constructor
164 * Event observer for newly added users by administrator
166 * @param event Event being fired
168 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
169 // event should not be null
172 throw new NullPointerException("event is null"); //NOI18N
173 } else if (event.getAddedUser() == null) {
175 throw new NullPointerException("event.addedUser is null"); //NOI18N
176 } else if (event.getAddedUser().getUserId() == null) {
178 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
179 } else if (event.getAddedUser().getUserId() < 1) {
181 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
185 this.updateList(event.getAddedUser());
191 this.setUserId(event.getAddedUser().getUserId());
195 * Event observer for deleted user accounts (by administrator)
197 * @param event Event being fired
199 public void afterAdminDeletedUserEvent (@Observes final ObservableAdminDeletedUserEvent event) {
200 // event should not be null
203 throw new NullPointerException("event is null"); //NOI18N
204 } else if (event.getDeletedUser() == null) {
206 throw new NullPointerException("event.deletedUser is null"); //NOI18N
207 } else if (event.getDeletedUser().getUserId() == null) {
209 throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N
210 } else if (event.getDeletedUser().getUserId() < 1) {
212 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N
216 this.removeFromList(event.getDeletedUser());
223 * Event observer for linked users with existing contact data
225 * @param event Event being fired
227 public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
228 // event should not be null
231 throw new NullPointerException("event is null"); //NOI18N
232 } else if (event.getLinkedUser() == null) {
234 throw new NullPointerException("event.linkedUser is null"); //NOI18N
235 } else if (event.getLinkedUser().getUserId() == null) {
237 throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
238 } else if (event.getLinkedUser().getUserId() < 1) {
240 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
244 this.updateList(event.getLinkedUser());
250 this.setUserId(event.getLinkedUser().getUserId());
254 * Event observer for locked users
256 * @param event Event being fired
258 public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) {
259 // event should not be null
262 throw new NullPointerException("event is null"); //NOI18N
263 } else if (event.getLockedUser() == null) {
265 throw new NullPointerException("event.lockedUser is null"); //NOI18N
266 } else if (event.getLockedUser().getUserId() == null) {
268 throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
269 } else if (event.getLockedUser().getUserId() < 1) {
271 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
275 this.updateList(event.getLockedUser());
279 * Event observer for unlocked users
281 * @param event Event being fired
283 public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) {
284 // event should not be null
287 throw new NullPointerException("event is null"); //NOI18N
288 } else if (event.getUnlockedUser() == null) {
290 throw new NullPointerException("event.unlockedUser is null"); //NOI18N
291 } else if (event.getUnlockedUser().getUserId() == null) {
293 throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
294 } else if (event.getUnlockedUser().getUserId() < 1) {
296 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
300 this.updateList(event.getUnlockedUser());
304 * Event observer for updated user data by administrator
306 * @param event Event being updated
308 public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) {
309 // event should not be null
312 throw new NullPointerException("event is null"); //NOI18N
313 } else if (event.getUpdatedUser() == null) {
315 throw new NullPointerException("event.updatedUser is null"); //NOI18N
316 } else if (event.getUpdatedUser().getUserId() == null) {
318 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
319 } else if (event.getUpdatedUser().getUserId() < 1) {
321 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
325 this.updateList(event.getUpdatedUser());
332 * Event observer when user confirmed account.
334 * @param event Event being fired
336 public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) {
337 // event should not be null
340 throw new NullPointerException("event is null"); //NOI18N
341 } else if (event.getConfirmedUser() == null) {
343 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
344 } else if (event.getConfirmedUser().getUserId() == null) {
346 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
347 } else if (event.getConfirmedUser().getUserId() < 1) {
349 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
353 this.updateList(event.getConfirmedUser());
357 * Event observer for logged-in user
359 * @param event Event instance
361 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
362 // event should not be null
365 throw new NullPointerException("event is null"); //NOI18N
366 } else if (event.getLoggedInUser() == null) {
368 throw new NullPointerException("event.registeredUser is null"); //NOI18N
369 } else if (event.getLoggedInUser().getUserId() == null) {
371 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
372 } else if (event.getLoggedInUser().getUserId() < 1) {
374 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
377 // "Cache" user instance
378 User loggedInUser = event.getLoggedInUser();
380 // Copy all data to this bean
381 this.copyUser(loggedInUser);
383 // Is the user visible?
384 if (Objects.equals(loggedInUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
385 // Yes, then add user
386 this.visibleUserList.add(loggedInUser);
391 * Event observer for new user registrations
393 * @param event User registration event
395 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
396 // event should not be null
399 throw new NullPointerException("event is null"); //NOI18N
400 } else if (event.getRegisteredUser() == null) {
402 throw new NullPointerException("event.registeredUser is null"); //NOI18N
403 } else if (event.getRegisteredUser().getUserId() == null) {
405 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
406 } else if (event.getRegisteredUser().getUserId() < 1) {
408 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
412 User registeredUser = event.getRegisteredUser();
414 // Copy all data from registered->user
415 this.copyUser(registeredUser);
421 this.updateList(registeredUser);
424 this.addUserName(registeredUser);
426 // Is the account public?
427 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
428 // Also add it to this list
429 this.visibleUserList.add(registeredUser);
433 this.setUserId(registeredUser.getUserId());
437 * Method being call after user's password has been updated (and history
438 * entry has been created).
440 * @param event Event being observed
442 public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
446 throw new NullPointerException("event is null"); //NOI18N
447 } else if (event.getPasswordHistory() == null) {
449 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
450 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
452 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
453 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
455 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
459 this.updateList(event.getPasswordHistory().getUserPasswordHistoryUser());
463 * Listens to fired event when user updated personal data
465 * @param event Event being fired
467 public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) {
471 throw new NullPointerException("event is null"); //NOI18N
472 } else if (event.getUpdatedUser() == null) {
474 throw new NullPointerException("event.updatedUser is null"); //NOI18N
475 } else if (event.getUpdatedUser().getUserId() == null) {
477 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
478 } else if (event.getUpdatedUser().getUserId() < 1) {
480 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
484 this.updateList(event.getUpdatedUser());
488 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
489 public List<User> allUsers () {
491 return this.userList;
495 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
496 public List<User> allVisibleUsers () {
498 return this.visibleUserList;
502 public User createUserInstance (final boolean createContactData) {
504 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
506 // Required personal data must be set
507 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
509 // Create new user instance
510 User user = new LoginUser();
512 // Is user name required?
513 if (!this.isUserNameRequired()) {
514 // Generate pseudo-random user name
515 String randomName = this.userBean.generateRandomUserName();
517 // Set it and inivisible profile
518 this.setUserName(randomName);
519 this.setUserProfileMode(ProfileMode.INVISIBLE);
521 // Generate random password
522 String randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
524 // Set random password
525 this.setUserPassword(randomPassword);
526 this.setUserPasswordRepeat(randomPassword);
529 // Set user name profile mode and locale
530 user.setUserName(this.getUserName());
531 user.setUserProfileMode(this.getUserProfileMode());
532 user.setUserLocale(this.localizationController.getLocale());
534 // Is multiple registration page
535 if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
536 // Create contact instance
537 Contact contact = this.contactController.createContactInstance();
540 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
541 // Set contact in user
542 user.setUserContact(contact);
546 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
552 public User createUserLogin () {
554 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
557 if (this.getUserName() == null) {
559 throw new NullPointerException("userName is null"); //NOI18N
560 } else if (this.getUserName().isEmpty()) {
562 throw new IllegalStateException("userName is empty."); //NOI18N
565 // Create new user instance
566 User user = new LoginUser();
568 // Update all data ...
569 user.setUserName(this.getUserName());
572 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: user={1} - EXIT!", this.getClass().getSimpleName(), user));
573 // Return the new instance
578 public String doChangePersonalData () {
579 // This method shall only be called if the user is logged-in
580 if (!this.userLoginController.isUserLoggedIn()) {
582 throw new IllegalStateException("User is not logged-in"); //NOI18N
583 } else if (!this.isRequiredChangePersonalDataSet()) {
584 // Not all required fields are set
585 throw new FaceletException("Not all required fields are set."); //NOI18N
586 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
587 // Password not matching
588 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
589 } else if (!this.featureController.isFeatureEnabled("change_user_personal_data")) {
590 // Editing is not allowed
591 throw new IllegalStateException("User tried to edit personal data."); //NOI18N
595 User user = this.userLoginController.getLoggedInUser();
597 // Copy contact data to contact instance
598 this.contactController.updateContactDataFromController(user.getUserContact());
600 // It should be there, so run some tests on it
601 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
602 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
603 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
604 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
605 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
606 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
609 user.setUserProfileMode(this.getUserProfileMode());
611 // Send it to the EJB
612 User updatedUser = this.userBean.updateUserPersonalData(user);
615 this.updatedPersonalDataEvent.fire(new UpdatedUserPersonalDataEvent(updatedUser));
618 return "user_contact_data_saved"; //NOI18N
622 public Long getUserId () {
627 public void setUserId (final Long userId) {
628 this.userId = userId;
632 public String getUserName () {
633 return this.userName;
637 public void setUserName (final String userName) {
638 this.userName = userName;
642 public String getUserPassword () {
643 return this.userPassword;
647 public void setUserPassword (final String userPassword) {
648 this.userPassword = userPassword;
652 public String getUserPasswordRepeat () {
653 return this.userPasswordRepeat;
657 public void setUserPasswordRepeat (final String userPasswordRepeat) {
658 this.userPasswordRepeat = userPasswordRepeat;
662 public ProfileMode getUserProfileMode () {
663 return this.userProfileMode;
667 public void setUserProfileMode (final ProfileMode userProfileMode) {
668 this.userProfileMode = userProfileMode;
672 public boolean ifBothPasswordsEmptyAllowed () {
673 // Check feature first
674 return ((this.featureController.isFeatureEnabled("allow_user_registration_empty_password")) &&
675 ((this.getUserPassword() == null) || (this.getUserPassword().isEmpty())) &&
676 ((this.getUserPasswordRepeat() == null) || (this.getUserPasswordRepeat().isEmpty())));
680 * Post-initialization of this class
683 public void init () {
686 // Get initial context
687 Context context = new InitialContext();
690 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
691 } catch (final NamingException e) {
693 throw new FaceletException(e);
696 // Initialize user list
697 this.userList = this.userBean.allUsers();
699 // Get full user name list for reducing EJB calls
700 this.userNameList = this.userBean.getUserNameList();
702 // Is the user logged-in?
703 if (this.userLoginController.isUserLoggedIn()) {
704 // Is logged-in, so load also users visible to memebers
705 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
707 // Initialize user list
708 this.visibleUserList = this.userBean.allPublicUsers();
713 public boolean isContactFound (final Contact contact) {
714 // The contact must be valid
715 if (null == contact) {
717 throw new NullPointerException("contact is null"); //NOI18N
718 } else if (contact.getContactId() == null) {
720 throw new NullPointerException("contact.contactId is null"); //NOI18N
721 } else if (contact.getContactId() < 1) {
723 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
726 // Default is not found
727 boolean isFound = false;
730 Iterator<User> iterator = this.allUsers().iterator();
732 // Loop through all entries
733 while (iterator.hasNext()) {
735 User next = iterator.next();
737 // Compare both objects
738 if (Objects.equals(contact, next.getUserContact())) {
750 public boolean isPublicUserProfileEnabled () {
751 // Get context parameter
752 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_public_profile_enabled"); //NOI18N
755 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
757 // This requires user names being enabled, too.
758 if ((isEnabled) && (!this.isUserNameRequired())) {
759 // Not valid state, users must be able to modify their profile, especially when it is public
760 throw new IllegalStateException("Public user profiles are enabled but user name requirement is disabled, this is not possible."); //NOI18N
768 public boolean isRequiredChangePersonalDataSet () {
769 return ((this.getUserProfileMode() != null) &&
770 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
771 (this.contactController.isRequiredChangePersonalDataSet()));
775 public boolean isRequiredPersonalDataSet () {
776 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
777 // Multiple registration page
778 return this.contactController.isRequiredPersonalDataSet();
780 // Single registration page
781 return (((this.getUserName() != null) || (!this.isUserNameRequired())) &&
782 (this.getUserProfileMode() != null) &&
783 (this.contactController.isRequiredPersonalDataSet()) &&
784 (this.getUserPassword() != null) &&
785 (this.getUserPasswordRepeat() != null));
790 public boolean isSamePasswordEntered () {
791 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
795 public boolean isUserIdEmpty () {
796 return ((this.getUserId() == null) || (this.getUserId() == 0));
800 public boolean isUserNameRegistered (final User user) {
801 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
805 public boolean isUserNameRequired () {
806 // Get context parameter
807 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_name_required"); //NOI18N
810 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
817 public boolean isVisibleUserFound () {
818 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
822 public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException {
823 // Parameter must be valid
824 if (null == emailAddress) {
826 throw new NullPointerException("emailAddress is null"); //NOI18N
827 } else if (emailAddress.isEmpty()) {
829 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
835 // Try to lookup it in visible user list
836 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
838 User next = iterator.next();
840 // Contact should be set
841 if (next.getUserContact() == null) {
843 throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getUserId())); //NOI18N
844 } else if (next.getUserContact().getContactEmailAddress() == null) {
845 // Email address should be set
846 throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getUserId())); //NOI18N
849 // Is the email address found?
850 if (Objects.equals(next.getUserContact().getContactEmailAddress(), emailAddress)) {
851 // Copy to other variable
859 // Not visible for the current user
860 throw new UserEmailAddressNotFoundException(emailAddress);
868 public User lookupUserById (final Long userId) throws UserNotFoundException {
869 // Parameter must be valid
870 if (null == userId) {
872 throw new NullPointerException("userId is null"); //NOI18N
873 } else if (userId < 1) {
875 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
881 // Try to lookup it in visible user list
882 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
884 User next = iterator.next();
886 // Is the user id found?
887 if (Objects.equals(next.getUserId(), userId)) {
888 // Copy to other variable
896 // Not visible for the current user
897 throw new UserNotFoundException(userId);
905 * Adds user's name to bean's internal list. It also updates the public user
906 * list if the user has decided to have a public account,
908 * @param user User instance
910 private void addUserName (final User user) {
911 // Make sure the entry is not added yet
912 if (this.userNameList.contains(user.getUserName())) {
914 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
918 this.userNameList.add(user.getUserName());
924 private void clear () {
927 this.setUserId(null);
928 this.setUserProfileMode(null);
931 this.setUserName(null);
932 this.setUserPassword(null);
933 this.setUserPasswordRepeat(null);
937 * Copies given user into the controller
939 * @param user User instance
941 private void copyUser (final User user) {
942 // Make sure the instance is valid
945 throw new NullPointerException("user is null"); //NOI18N
946 } else if (user.getUserContact() == null) {
948 throw new NullPointerException("user.userContact is null"); //NOI18N
953 this.setUserId(user.getUserId());
954 this.setUserProfileMode(user.getUserProfileMode());
958 * Removes user from all lists
960 * @param user User to remove
962 private void removeFromList (final User user) {
963 // The user should be valid
966 throw new NullPointerException("user is null"); //NOI18N
967 } else if (user.getUserId() == null) {
969 throw new NullPointerException("user.userId is null"); //NOI18N
970 } else if (user.getUserId() < 1) {
972 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
975 // Remove it from lists
976 this.userList.remove(user);
977 this.visibleUserList.remove(user);
979 // Remove name from list
980 this.userNameList.remove(user.getUserName());
984 * Updates list with given user instance
986 * @param user User instance
988 private void updateList (final User user) {
989 // The user should be valid
992 throw new NullPointerException("user is null"); //NOI18N
993 } else if (user.getUserId() == null) {
995 throw new NullPointerException("user.userId is null"); //NOI18N
996 } else if (user.getUserId() < 1) {
998 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
999 } else if (user.getUserContact() == null) {
1001 throw new NullPointerException("user.userContact is null"); //NOI18N
1002 } else if (user.getUserContact().getContactId() == null) {
1004 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
1005 } else if (user.getUserContact().getContactId() < 1) {
1007 throw new NullPointerException(MessageFormat.format("user.userContact.contactId={0} is invalid.", user.getUserContact().getContactId())); //NOI18N
1010 // Get iterator from list
1011 Iterator<User> iterator = this.userList.iterator();
1013 // "Walk" through all entries
1014 while (iterator.hasNext()) {
1016 User next = iterator.next();
1018 // Is user id number the same?
1019 if (Objects.equals(user.getUserId(), next.getUserId())) {
1020 // Found entry, so remove it and abort
1021 this.userList.remove(next);
1027 this.userList.add(user);