2 * Copyright (C) 2016 Roland Haeder
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.jcontacts.contact.ContactSessionBeanRemote;
37 import org.mxchange.jjobs.beans.BaseJobsController;
38 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
39 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
40 import org.mxchange.jjobs.beans.localization.JobsLocalizationSessionController;
41 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
42 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
43 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
44 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
45 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
46 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
47 import org.mxchange.jusercore.events.user.locked.AdminLockedUserEvent;
48 import org.mxchange.jusercore.events.user.password_change.UpdatedUserPasswordEvent;
49 import org.mxchange.jusercore.events.user.unlocked.AdminUnlockedUserEvent;
50 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
51 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
52 import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
53 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
54 import org.mxchange.jusercore.exceptions.UserNotFoundException;
55 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
56 import org.mxchange.jusercore.model.user.LoginUser;
57 import org.mxchange.jusercore.model.user.User;
58 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
59 import org.mxchange.jusercore.model.user.UserUtils;
60 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
63 * A user controller (bean)
65 * @author Roland Haeder<roland@mxchange.org>
67 @Named ("userController")
69 public class JobsUserWebSessionBean extends BaseJobsController implements JobsUserWebSessionController {
74 private static final long serialVersionUID = 542_145_347_916L;
79 private ContactSessionBeanRemote contactBean;
82 * General contact controller
85 private JobsContactWebSessionController contactController;
91 private JobsFeaturesWebApplicationController featureController;
94 * Localization controller
97 private JobsLocalizationSessionController localizationController;
100 * Event being fired when user updated personal data
104 private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
109 private final UserSessionBeanRemote userBean;
117 * A list of all user profiles
119 private List<User> userList;
122 * Login controller (bean)
125 private JobsUserLoginWebSessionController userLoginController;
130 private String userName;
135 private List<String> userNameList;
138 * User password (unencrypted from web form)
140 private String userPassword;
143 * User password repeated (unencrypted from web form)
145 private String userPasswordRepeat;
148 * Whether the user wants a public profile
150 private ProfileMode userProfileMode;
153 * A list of all public user profiles
155 * @TODO Hmm, makes that sense? Having visible user list in current
156 * (session-scoped) user's visible user list?
158 private List<User> visibleUserList;
161 * Default constructor
163 public JobsUserWebSessionBean () {
166 // Get initial context
167 Context context = new InitialContext();
170 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
173 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
174 } catch (final NamingException e) {
176 throw new FaceletException(e);
181 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
183 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
185 // event should not be null
188 throw new NullPointerException("event is null"); //NOI18N
189 } else if (event.getAddedUser() == null) {
191 throw new NullPointerException("event.addedUser is null"); //NOI18N
192 } else if (event.getAddedUser().getUserId() == null) {
194 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
195 } else if (event.getAddedUser().getUserId() < 1) {
197 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
201 this.updateList(event.getAddedUser());
207 this.setUserId(event.getAddedUser().getUserId());
211 public void afterAdminLinkedUserEvent (@Observes final AdminLinkedUserEvent event) {
212 // event should not be null
215 throw new NullPointerException("event is null"); //NOI18N
216 } else if (event.getLinkedUser() == null) {
218 throw new NullPointerException("event.linkedUser is null"); //NOI18N
219 } else if (event.getLinkedUser().getUserId() == null) {
221 throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
222 } else if (event.getLinkedUser().getUserId() < 1) {
224 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
228 this.updateList(event.getLinkedUser());
234 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
238 public void afterAdminLockedUserEvent (@Observes final AdminLockedUserEvent event) {
239 // event should not be null
242 throw new NullPointerException("event is null"); //NOI18N
243 } else if (event.getLockedUser() == null) {
245 throw new NullPointerException("event.lockedUser is null"); //NOI18N
246 } else if (event.getLockedUser().getUserId() == null) {
248 throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
249 } else if (event.getLockedUser().getUserId() < 1) {
251 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
255 this.updateList(event.getLockedUser());
259 public void afterAdminUnlockedUserEvent (@Observes final AdminUnlockedUserEvent event) {
260 // event should not be null
263 throw new NullPointerException("event is null"); //NOI18N
264 } else if (event.getUnlockedUser() == null) {
266 throw new NullPointerException("event.unlockedUser is null"); //NOI18N
267 } else if (event.getUnlockedUser().getUserId() == null) {
269 throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
270 } else if (event.getUnlockedUser().getUserId() < 1) {
272 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
276 this.updateList(event.getUnlockedUser());
280 public void afterAdminUpdatedUserDataEvent (@Observes final AdminUpdatedUserDataEvent event) {
282 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminUpdatedUserEvent: event={0} - CALLED!", event)); //NOI18N
284 // event should not be null
287 throw new NullPointerException("event is null"); //NOI18N
288 } else if (event.getUpdatedUser() == null) {
290 throw new NullPointerException("event.updatedUser is null"); //NOI18N
291 } else if (event.getUpdatedUser().getUserId() == null) {
293 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
294 } else if (event.getUpdatedUser().getUserId() < 1) {
296 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
300 this.updateList(event.getUpdatedUser());
306 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminUpdatedUserEvent: EXIT!"); //NOI18N
310 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
312 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
314 // event should not be null
317 throw new NullPointerException("event is null"); //NOI18N
318 } else if (event.getRegisteredUser() == null) {
320 throw new NullPointerException("event.registeredUser is null"); //NOI18N
321 } else if (event.getRegisteredUser().getUserId() == null) {
323 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
324 } else if (event.getRegisteredUser().getUserId() < 1) {
326 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
330 User registeredUser = event.getRegisteredUser();
333 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
335 // Copy all data from registered->user
336 this.copyUser(registeredUser);
342 this.updateList(registeredUser);
345 this.addUserName(registeredUser);
347 // Is the account public?
348 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
349 // Also add it to this list
350 this.visibleUserList.add(registeredUser);
354 this.setUserId(registeredUser.getUserId());
357 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
361 public void afterUserConfirmedAccount (@Observes final UserConfirmedAccountEvent event) {
363 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
365 // event should not be null
368 throw new NullPointerException("event is null"); //NOI18N
369 } else if (event.getConfirmedUser() == null) {
371 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
372 } else if (event.getConfirmedUser().getUserId() == null) {
374 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
375 } else if (event.getConfirmedUser().getUserId() < 1) {
377 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
381 this.updateList(event.getConfirmedUser());
385 public void afterUserLogin (@Observes final UserLoggedInEvent event) {
387 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
389 // event should not be null
392 throw new NullPointerException("event is null"); //NOI18N
393 } else if (event.getLoggedInUser() == null) {
395 throw new NullPointerException("event.registeredUser is null"); //NOI18N
396 } else if (event.getLoggedInUser().getUserId() == null) {
398 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
399 } else if (event.getLoggedInUser().getUserId() < 1) {
401 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
404 // "Cache" user instance
405 User loggedInUser = event.getLoggedInUser();
407 // Copy all data to this bean
408 this.copyUser(loggedInUser);
410 // Is the user visible?
411 if (Objects.equals(loggedInUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
412 // Yes, then add user
413 this.visibleUserList.add(loggedInUser);
418 public void afterUserUpdatedPasswordEvent (@Observes final UpdatedUserPasswordEvent event) {
422 throw new NullPointerException("event is null"); //NOI18N
423 } else if (event.getPasswordHistory() == null) {
425 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
426 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
428 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
429 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
431 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
435 this.updateList(event.getPasswordHistory().getUserPasswordHistoryUser());
439 public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
443 throw new NullPointerException("event is null"); //NOI18N
444 } else if (event.getUpdatedUser() == null) {
446 throw new NullPointerException("event.updatedUser is null"); //NOI18N
447 } else if (event.getUpdatedUser().getUserId() == null) {
449 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
450 } else if (event.getUpdatedUser().getUserId() < 1) {
452 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
456 this.updateList(event.getUpdatedUser());
460 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
461 public List<User> allUsers () {
463 return this.userList;
467 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
468 public List<User> allVisibleUsers () {
470 return this.visibleUserList;
474 public User createUserInstance (final boolean createContactData) {
476 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
478 // Required personal data must be set
479 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
481 // Create new user instance
482 User user = new LoginUser();
484 // Is user name required?
485 if (!this.isUserNameRequired()) {
486 // Generate pseudo-random user name
487 String randomName = this.userBean.generateRandomUserName();
489 // Set it and inivisible profile
490 this.setUserName(randomName);
491 this.setUserProfileMode(ProfileMode.INVISIBLE);
493 // Generate random password
494 String randomPassword = UserUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
496 // Set random password
497 this.setUserPassword(randomPassword);
498 this.setUserPasswordRepeat(randomPassword);
501 // Set local, user name and mode
502 user.setUserLocale(this.localizationController.getLocale());
503 user.setUserName(this.getUserName());
504 user.setUserProfileMode(this.getUserProfileMode());
506 // Is multiple registration page
507 if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
508 // Create contact instance
509 Contact contact = this.contactController.createContactInstance();
512 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
514 // Set contact in user
515 user.setUserContact(contact);
519 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
526 public User createUserLogin () {
528 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
531 if (this.getUserName() == null) {
533 throw new NullPointerException("recruiterName is null"); //NOI18N
534 } else if (this.getUserName().isEmpty()) {
536 throw new IllegalStateException("recruiterName is empty."); //NOI18N
539 // Create new recruiter instance
540 User recruiter = new LoginUser();
542 // Update all data ...
543 recruiter.setUserName(this.getUserName());
546 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: recruiter={1} - EXIT!", this.getClass().getSimpleName(), recruiter));
548 // Return the new instance
553 public String doChangePersonalData () {
554 // This method shall only be called if the user is logged-in
555 if (!this.userLoginController.isUserLoggedIn()) {
557 throw new IllegalStateException("User is not logged-in"); //NOI18N
558 } else if (!this.isRequiredChangePersonalDataSet()) {
559 // Not all required fields are set
560 throw new FaceletException("Not all required fields are set."); //NOI18N
561 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
562 // Password not matching
563 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
564 } else if (!this.featureController.isFeatureEnabled("edit_user_data")) {
565 // Editing is not allowed
566 throw new IllegalStateException("User tried to edit personal data."); //NOI18N
570 User user = this.userLoginController.getLoggedInUser();
572 // Copy contact data to contact instance
573 this.contactController.updateContactDataFromController(user.getUserContact());
575 // It should be there, so run some tests on it
576 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
577 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
578 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
579 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
580 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
581 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
584 user.setUserProfileMode(this.getUserProfileMode());
586 // Send it to the EJB
587 User updatedUser = this.userBean.updateUserPersonalData(user);
590 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
593 return "contact_data_saved"; //NOI18N
597 public Long getUserId () {
602 public void setUserId (final Long userId) {
603 this.userId = userId;
607 public String getUserName () {
608 return this.userName;
612 public void setUserName (final String userName) {
613 this.userName = userName;
617 public String getUserPassword () {
618 return this.userPassword;
622 public void setUserPassword (final String userPassword) {
623 this.userPassword = userPassword;
627 public String getUserPasswordRepeat () {
628 return this.userPasswordRepeat;
632 public void setUserPasswordRepeat (final String userPasswordRepeat) {
633 this.userPasswordRepeat = userPasswordRepeat;
637 public ProfileMode getUserProfileMode () {
638 return this.userProfileMode;
642 public void setUserProfileMode (final ProfileMode userProfileMode) {
643 this.userProfileMode = userProfileMode;
647 * Post-initialization of this class
650 public void init () {
651 // Initialize user list
652 this.userList = this.userBean.allUsers();
654 // Get full user name list for reducing EJB calls
655 this.userNameList = this.userBean.getUserNameList();
657 // Is the user logged-in?
658 if (this.userLoginController.isUserLoggedIn()) {
659 // Is logged-in, so load also users visible to memebers
660 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
662 // Initialize user list
663 this.visibleUserList = this.userBean.allPublicUsers();
668 public boolean isContactFound (final Contact contact) {
669 // The contact must be valid
670 if (null == contact) {
672 throw new NullPointerException("contact is null"); //NOI18N
673 } else if (contact.getContactId() == null) {
675 throw new NullPointerException("contact.contactId is null"); //NOI18N
676 } else if (contact.getContactId() < 1) {
678 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
681 // Default is not found
682 boolean isFound = false;
685 Iterator<User> iterator = this.allUsers().iterator();
687 // Loop through all entries
688 while (iterator.hasNext()) {
690 User next = iterator.next();
692 // Compare both objects
693 if (Objects.equals(contact, next.getUserContact())) {
705 public boolean isPublicUserProfileEnabled () {
706 // Get context parameter
707 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_public_profile_enabled"); //NOI18N
710 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
712 // This requires user names being enabled, too.
713 if ((isEnabled) && (!this.isUserNameRequired())) {
714 // Not valid state, users must be able to modify their profile, especially when it is public
715 throw new IllegalStateException("Public user profiles are enabled but user name requirement is disabled, this is not possible."); //NOI18N
723 public boolean isRequiredChangePersonalDataSet () {
724 return ((this.getUserProfileMode() != null) &&
725 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
726 (this.contactController.isRequiredChangePersonalDataSet()));
730 public boolean isRequiredPersonalDataSet () {
731 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
732 // Multiple registration page
733 return this.contactController.isRequiredPersonalDataSet();
735 // Single registration page
736 return (((this.getUserName() != null) || (!this.isUserNameRequired())) &&
737 (this.getUserProfileMode() != null) &&
738 (this.contactController.isRequiredPersonalDataSet()) &&
739 (this.getUserPassword() != null) &&
740 (this.getUserPasswordRepeat() != null));
745 public boolean isSamePasswordEntered () {
746 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
750 public boolean isUserIdEmpty () {
751 return ((this.getUserId() == null) || (this.getUserId() == 0));
755 public boolean isUserNameRegistered (final User user) {
756 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
760 public boolean isUserNameRequired () {
761 // Get context parameter
762 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_name_required"); //NOI18N
765 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
772 public boolean isVisibleUserFound () {
773 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
777 public User lookupUserById (final Long userId) throws UserNotFoundException {
778 // Parameter must be valid
779 if (null == userId) {
781 throw new NullPointerException("userId is null"); //NOI18N
782 } else if (userId < 1) {
784 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
790 // Try to lookup it in visible user list
791 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
793 User next = iterator.next();
795 // Is the user id found?
796 if (Objects.equals(next.getUserId(), userId)) {
797 // Copy to other variable
805 // Not visible for the current user
806 throw new UserNotFoundException(userId);
814 public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException {
815 // Parameter must be valid
816 if (null == emailAddress) {
818 throw new NullPointerException("emailAddress is null"); //NOI18N
819 } else if (emailAddress.isEmpty()) {
821 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
827 // Try to lookup it in visible user list
828 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
830 User next = iterator.next();
832 // Contact should be set
833 if (next.getUserContact() == null) {
835 throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getUserId())); //NOI18N
836 } else if (next.getUserContact().getContactEmailAddress() == null) {
837 // Email address should be set
838 throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getUserId())); //NOI18N //NOI18N
841 // Is the email address found?
842 if (Objects.equals(next.getUserContact().getContactEmailAddress(), emailAddress)) {
843 // Copy to other variable
851 // Not visible for the current user
852 throw new UserEmailAddressNotFoundException(emailAddress);
860 * Adds user's name to bean's internal list. It also updates the public user
861 * list if the user has decided to have a public account,
863 * @param user User instance
865 private void addUserName (final User user) {
866 // Make sure the entry is not added yet
867 if (this.userNameList.contains(user.getUserName())) {
869 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
870 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
872 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
876 this.userNameList.add(user.getUserName());
882 private void clear () {
885 this.setUserId(null);
886 this.setUserProfileMode(null);
889 this.setUserName(null);
890 this.setUserPassword(null);
891 this.setUserPasswordRepeat(null);
895 * Copies given user into the controller
897 * @param user User instance
899 private void copyUser (final User user) {
900 // Make sure the instance is valid
903 throw new NullPointerException("user is null"); //NOI18N
904 } else if (user.getUserContact() == null) {
906 throw new NullPointerException("user.userContact is null"); //NOI18N
911 this.setUserId(user.getUserId());
912 this.setUserProfileMode(user.getUserProfileMode());
916 * Uniquely adds given user instance to user list. First an existing
917 * instance will be removed, then the given instance is being added.
919 * @param user User instance to add
921 private void uniqueAddUser (final User user) {
922 // Make sure the instance is valid
925 throw new NullPointerException("user is null"); //NOI18N
926 } else if (user.getUserContact() == null) {
928 throw new NullPointerException("user.userContact is null"); //NOI18N
931 // Get iterator from list
932 Iterator<User> iterator = this.userList.iterator();
934 // "Walk" through all entries
935 while (iterator.hasNext()) {
937 User next = iterator.next();
939 // Is id number the same?
940 if (Objects.equals(user.getUserId(), next.getUserId())) {
941 // Found entry, so remove it and abort
942 this.userList.remove(next);
947 // Add contact to list
948 this.userList.add(user);
952 * Updates list with given user instance
954 * @param user User instance
956 private void updateList (final User user) {
957 // The user should be valid
960 throw new NullPointerException("user is null"); //NOI18N
961 } else if (user.getUserContact() == null) {
963 throw new NullPointerException("user.userContact is null"); //NOI18N
964 } else if (user.getUserId() == null) {
966 throw new NullPointerException("user.userId is null"); //NOI18N
967 } else if (user.getUserId() < 1) {
969 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
972 // Get iterator from list
973 Iterator<User> iterator = this.userList.iterator();
975 // "Walk" through all entries
976 while (iterator.hasNext()) {
978 User next = iterator.next();
980 // Is user id number the same?
981 if (Objects.equals(user.getUserId(), next.getUserId())) {
982 // Found entry, so remove it and abort
983 this.userList.remove(next);
989 this.userList.add(user);