2 * Copyright (C) 2016 - 2018 Free Software Foundation
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 fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Comparator;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Locale;
26 import java.util.Objects;
27 import javax.annotation.PostConstruct;
28 import javax.cache.Cache;
30 import javax.enterprise.context.RequestScoped;
31 import javax.enterprise.event.Event;
32 import javax.enterprise.event.Observes;
33 import javax.enterprise.inject.Any;
34 import javax.faces.context.FacesContext;
35 import javax.faces.view.facelets.FaceletException;
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import org.mxchange.jcontacts.model.contact.Contact;
39 import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent;
40 import org.mxchange.jjobs.beans.BaseJobsBean;
41 import org.mxchange.jjobs.beans.contact.JobsContactWebRequestController;
42 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
43 import org.mxchange.jjobs.beans.user.login.JobsUserLoginWebSessionController;
44 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
45 import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent;
46 import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent;
47 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
48 import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent;
49 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
50 import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent;
51 import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent;
52 import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent;
53 import org.mxchange.jusercore.events.user.update.ObservableUpdatedUserPersonalDataEvent;
54 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
55 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
56 import org.mxchange.jusercore.exceptions.UserNotFoundException;
57 import org.mxchange.jusercore.model.user.LoginUser;
58 import org.mxchange.jusercore.model.user.User;
59 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
60 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
61 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
62 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
63 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
64 import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
65 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
66 import org.mxchange.juserlogincore.login.UserLoginUtils;
69 * A user controller (bean)
71 * @author Roland Häder<roland@mxchange.org>
73 @Named ("userController")
75 public class JobsUserWebRequestBean extends BaseJobsBean implements JobsUserWebRequestController {
80 private static final long serialVersionUID = 542_145_347_916L;
85 private final List<User> allUsers;
88 * General contact controller
91 private JobsContactWebRequestController contactController;
97 private JobsFeaturesWebApplicationController featureController;
100 * List of filtered users
102 private List<User> filteredUsers;
107 private Locale locale;
110 * Event being fired when user updated personal data
114 private Event<ObservableUpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
119 @EJB (lookup = "java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
120 private UserSessionBeanRemote userBean;
123 * A list of all user profiles
126 @NamedCache (cacheName = "userCache")
127 private Cache<Long, User> userCache;
135 * Login controller (bean)
138 private JobsUserLoginWebSessionController userLoginController;
143 private String userName;
149 @NamedCache (cacheName = "userNameCache")
150 private Cache<Long, String> userNameCache;
153 * User password (clear-text from web form)
155 private String userPassword;
158 * User password repeated (clear-text from web form)
160 private String userPasswordRepeat;
163 * Whether the user wants a public profile
165 private ProfileMode userProfileMode;
168 * Default constructor
170 public JobsUserWebRequestBean () {
171 // Call super constructor
175 this.allUsers = new LinkedList<>();
179 * Event observer for newly added users by administrator
181 * @param event Event being fired
183 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
184 // event should not be null
187 throw new NullPointerException("event is null"); //NOI18N
188 } else if (event.getAddedUser() == null) {
190 throw new NullPointerException("event.addedUser is null"); //NOI18N
191 } else if (event.getAddedUser().getUserId() == null) {
193 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
194 } else if (event.getAddedUser().getUserId() < 1) {
196 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
200 this.updateList(event.getAddedUser());
206 this.setUserId(event.getAddedUser().getUserId());
210 * Event observer for deleted user accounts (by administrator)
212 * @param event Event being fired
214 public void afterAdminDeletedUserEvent (@Observes final ObservableAdminDeletedUserEvent event) {
215 // event should not be null
218 throw new NullPointerException("event is null"); //NOI18N
219 } else if (event.getDeletedUser() == null) {
221 throw new NullPointerException("event.deletedUser is null"); //NOI18N
222 } else if (event.getDeletedUser().getUserId() == null) {
224 throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N
225 } else if (event.getDeletedUser().getUserId() < 1) {
227 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N
231 this.removeFromList(event.getDeletedUser());
238 * Event observer for linked users with existing contact data
240 * @param event Event being fired
242 public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
243 // event should not be null
246 throw new NullPointerException("event is null"); //NOI18N
247 } else if (event.getLinkedUser() == null) {
249 throw new NullPointerException("event.linkedUser is null"); //NOI18N
250 } else if (event.getLinkedUser().getUserId() == null) {
252 throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
253 } else if (event.getLinkedUser().getUserId() < 1) {
255 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
259 this.updateList(event.getLinkedUser());
265 this.setUserId(event.getLinkedUser().getUserId());
269 * Event observer for locked users
271 * @param event Event being fired
273 public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) {
274 // event should not be null
277 throw new NullPointerException("event is null"); //NOI18N
278 } else if (event.getLockedUser() == null) {
280 throw new NullPointerException("event.lockedUser is null"); //NOI18N
281 } else if (event.getLockedUser().getUserId() == null) {
283 throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
284 } else if (event.getLockedUser().getUserId() < 1) {
286 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
290 this.updateList(event.getLockedUser());
294 * Event observer for unlocked users
296 * @param event Event being fired
298 public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) {
299 // event should not be null
302 throw new NullPointerException("event is null"); //NOI18N
303 } else if (event.getUnlockedUser() == null) {
305 throw new NullPointerException("event.unlockedUser is null"); //NOI18N
306 } else if (event.getUnlockedUser().getUserId() == null) {
308 throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
309 } else if (event.getUnlockedUser().getUserId() < 1) {
311 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
315 this.updateList(event.getUnlockedUser());
319 * Event observer for updated user data by administrator
321 * @param event Event being updated
323 public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) {
324 // event should not be null
327 throw new NullPointerException("event is null"); //NOI18N
328 } else if (event.getUpdatedUser() == null) {
330 throw new NullPointerException("event.updatedUser is null"); //NOI18N
331 } else if (event.getUpdatedUser().getUserId() == null) {
333 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
334 } else if (event.getUpdatedUser().getUserId() < 1) {
336 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
340 this.updateList(event.getUpdatedUser());
347 * Event observer for when a bean helper has successfully created a user
348 * instance, means the user exists. If the user does not exist, this event
349 * should not fire but instead a proper exception must be thrown.
351 * @param event User created event
353 public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) {
354 // Is the instance valid?
357 throw new NullPointerException("event is null"); //NOI18N
358 } else if (event.getCreatedUser() == null) {
360 throw new NullPointerException("event.createdUser is null"); //NOI18N
361 } else if (event.getCreatedUser().getUserId() == null) {
363 throw new NullPointerException("event.createdUser.userId is null"); //NOI18N
364 } else if (event.getCreatedUser().getUserId() < 1) {
366 throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N
370 final User user = event.getCreatedUser();
372 // Set all fields here
377 * Observer method for events being fired when the application's locale has
380 * @param event Event being fired
382 public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) {
383 // Is the parameter valid?
386 throw new NullPointerException("event is null"); //NOI18N
387 } else if (event.getLocale() == null) {
389 throw new NullPointerException("event.locale is null"); //NOI18N
393 this.setLocale(event.getLocale());
397 * Event observer when user confirmed account.
399 * @param event Event being fired
401 public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) {
402 // event should not be null
405 throw new NullPointerException("event is null"); //NOI18N
406 } else if (event.getConfirmedUser() == null) {
408 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
409 } else if (event.getConfirmedUser().getUserId() == null) {
411 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
412 } else if (event.getConfirmedUser().getUserId() < 1) {
414 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
418 this.updateList(event.getConfirmedUser());
422 * Event observer for logged-in user
424 * @param event Event instance
426 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
427 // event should not be null
430 throw new NullPointerException("event is null"); //NOI18N
431 } else if (event.getLoggedInUser() == null) {
433 throw new NullPointerException("event.registeredUser is null"); //NOI18N
434 } else if (event.getLoggedInUser().getUserId() == null) {
436 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
437 } else if (event.getLoggedInUser().getUserId() < 1) {
439 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
442 // "Cache" user instance
443 final User loggedInUser = event.getLoggedInUser();
445 // Copy all data to this bean
446 this.copyUser(loggedInUser);
450 * Event observer for user password changes
452 * @param event Event being fired
454 public void afterUserPasswordChangedEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
458 throw new NullPointerException("event is null"); //NOI18N
459 } else if (event.getUserPassword() == null) {
461 throw new NullPointerException("event.userPassword is null"); //NOI18N
462 } else if (event.getUserPassword().isEmpty()) {
464 throw new IllegalArgumentException("event.userPassword is empty"); //NOI18N
468 this.setUserPassword(event.getUserPassword());
469 this.setUserPasswordRepeat(event.getUserPassword());
473 * Event observer for new user registrations
475 * @param event User registration event
477 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
478 // event should not be null
481 throw new NullPointerException("event is null"); //NOI18N
482 } else if (event.getRegisteredUser() == null) {
484 throw new NullPointerException("event.registeredUser is null"); //NOI18N
485 } else if (event.getRegisteredUser().getUserId() == null) {
487 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
488 } else if (event.getRegisteredUser().getUserId() < 1) {
490 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
494 final User registeredUser = event.getRegisteredUser();
496 // Copy all data from registered->user
497 this.copyUser(registeredUser);
503 this.updateList(registeredUser);
506 this.addUserName(registeredUser);
509 this.setUserId(registeredUser.getUserId());
513 * Method being call after user's password has been updated (and history
514 * entry has been created).
516 * @param event Event being observed
518 public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
522 throw new NullPointerException("event is null"); //NOI18N
523 } else if (event.getPasswordHistory() == null) {
525 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
526 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
528 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
529 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
531 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
535 this.updateList(event.getPasswordHistory().getUserPasswordHistoryUser());
539 * Listens to fired event when user updated personal data
541 * @param event Event being fired
543 public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) {
547 throw new NullPointerException("event is null"); //NOI18N
548 } else if (event.getUpdatedUser() == null) {
550 throw new NullPointerException("event.updatedUser is null"); //NOI18N
551 } else if (event.getUpdatedUser().getUserId() == null) {
553 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
554 } else if (event.getUpdatedUser().getUserId() < 1) {
556 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
560 this.updateList(event.getUpdatedUser());
564 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
565 public List<User> allUsers () {
566 return this.allUsers;
570 * Event observer for when a user name should be cleared
572 * @param event Event being fired
574 public void clearUserNameEvent (@Observes final ObservableClearUserNameEvent event) {
578 throw new NullPointerException("event is null"); //NOI18N
582 this.clearUserName();
586 * Event observer for when both user passwords should be cleared
588 * @param event Event being fired
590 public void clearUserPasswordEvent (@Observes final ObservableClearUserPasswordEvent event) {
594 throw new NullPointerException("event is null"); //NOI18N
598 this.clearUserPasswords();
602 public User createUserInstance (final boolean createContactData) {
603 // Required personal data must be set
604 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
606 // Create new user instance
607 final User user = new LoginUser();
609 // Is user name required?
610 if (!this.isUserNameRequired()) {
611 // Generate pseudo-random user name
612 // @TODO Refacture this to avoid EJB call
613 final String randomName = this.userBean.generateRandomUserName();
615 // Set it and inivisible profile
616 this.setUserName(randomName);
617 this.setUserProfileMode(ProfileMode.INVISIBLE);
619 // Generate random password
620 final String randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
622 // Set random password
623 this.setUserPassword(randomPassword);
624 this.setUserPasswordRepeat(randomPassword);
627 // Set user name profile mode and locale
628 user.setUserName(this.getUserName());
629 user.setUserProfileMode(this.getUserProfileMode());
630 user.setUserLocale(this.getLocale());
632 // Is multiple registration page
633 if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
634 // Create contact instance
635 final Contact contact = this.contactController.createContactInstance();
637 // Set contact in user
638 user.setUserContact(contact);
646 public User createUserLogin () {
648 if (this.getUserName() == null) {
650 throw new NullPointerException("userName is null"); //NOI18N
651 } else if (this.getUserName().isEmpty()) {
653 throw new IllegalStateException("userName is empty."); //NOI18N
656 // Create new user instance
657 final User user = new LoginUser();
659 // Update all data ...
660 user.setUserName(this.getUserName());
662 // Return the new instance
667 public String doChangePersonalData () {
668 // This method shall only be called if the user is logged-in
669 if (!this.userLoginController.isUserLoggedIn()) {
671 throw new IllegalStateException("User is not logged-in"); //NOI18N
672 } else if (!this.isRequiredChangePersonalDataSet()) {
673 // Not all required fields are set
674 throw new FaceletException("Not all required fields are set."); //NOI18N
675 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
676 // Password not matching
677 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
678 } else if (!this.featureController.isFeatureEnabled("change_user_personal_data")) { //NOI18N
679 // Editing is not allowed
680 throw new IllegalStateException("User tried to edit personal data."); //NOI18N
684 final User user = this.userLoginController.getLoggedInUser();
686 // Copy contact data to contact instance
687 this.contactController.updateContactDataFromController(user.getUserContact());
689 // It should be there, so run some tests on it
690 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
691 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
692 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
693 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
694 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
695 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
698 user.setUserProfileMode(this.getUserProfileMode());
700 // Send it to the EJB
701 final User updatedUser = this.userBean.updateUserPersonalData(user);
704 this.updatedPersonalDataEvent.fire(new UpdatedUserPersonalDataEvent(updatedUser));
707 return "user_contact_data_saved"; //NOI18N
711 public User findUserById (final Long userId) throws UserNotFoundException {
712 // Validate parameter
713 if (null == userId) {
715 throw new NullPointerException("userId is null"); //NOI18N
716 } else if (userId < 1) {
718 throw new IllegalArgumentException("userId=" + userId + " is invalid."); //NOI18N
719 } else if (!this.userCache.containsKey(userId)) {
721 throw new UserNotFoundException(userId);
725 final User user = this.userCache.get(userId);
732 * Getter for filtered users list
734 * @return Filtered users list
736 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
737 public List<User> getFilteredUsers () {
738 return this.filteredUsers;
742 * Setter for filtered users list
744 * @param filteredUsers Filtered users list
746 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
747 public void setFilteredUsers (final List<User> filteredUsers) {
748 this.filteredUsers = filteredUsers;
756 public Long getUserId () {
763 * @param userId User id
765 public void setUserId (final Long userId) {
766 this.userId = userId;
770 * Getter for user name
774 public String getUserName () {
775 return this.userName;
779 * Setter for user name
781 * @param userName User name
783 public void setUserName (final String userName) {
784 this.userName = userName;
788 public String getUserPassword () {
789 return this.userPassword;
793 * Setter for clear-text user password
795 * @param userPassword Clear-text user password
797 public void setUserPassword (final String userPassword) {
798 this.userPassword = userPassword;
802 * Getter for clear-text user password repeated
804 * @return Clear-text user password repeated
806 public String getUserPasswordRepeat () {
807 return this.userPasswordRepeat;
811 * Setter for clear-text user password repeated
813 * @param userPasswordRepeat Clear-text user password repeated
815 public void setUserPasswordRepeat (final String userPasswordRepeat) {
816 this.userPasswordRepeat = userPasswordRepeat;
820 * Getter for user profile mode
822 * @return User profile mode
824 public ProfileMode getUserProfileMode () {
825 return this.userProfileMode;
829 * Setter for user profile mode
831 * @param userProfileMode User profile mode
833 public void setUserProfileMode (final ProfileMode userProfileMode) {
834 this.userProfileMode = userProfileMode;
838 public boolean ifBothPasswordsEmptyAllowed () {
839 // Check feature first
840 return ((this.featureController.isFeatureEnabled("allow_user_registration_empty_password")) && //NOI18N
841 ((this.getUserPassword() == null) || (this.getUserPassword().isEmpty())) &&
842 ((this.getUserPasswordRepeat() == null) || (this.getUserPasswordRepeat().isEmpty())));
846 public boolean ifUserIdExists (final Long userId) {
847 // Validate parameter
848 if (null == userId) {
850 throw new NullPointerException("userId is null"); //NOI18N
851 } else if (userId < 1) {
853 throw new IllegalArgumentException("userId=" + userId + " is invalid."); //NOI18N
856 // Check if key is there
857 boolean isFound = this.userCache.containsKey(userId);
864 * Post-initialization of this class
867 public void init () {
869 if (!this.userCache.iterator().hasNext()) {
871 final List<User> users = this.userBean.allUsers();
874 for (final User user : users) {
876 this.userCache.put(user.getUserId(), user);
877 this.userNameCache.put(user.getUserId(), user.getUserName());
881 // Is cache filled and list is empty
882 if ((this.userCache.iterator().hasNext()) && (this.allUsers.isEmpty())) {
884 final Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator();
887 while (iterator.hasNext()) {
889 final Cache.Entry<Long, User> next = iterator.next();
892 this.allUsers.add(next.getValue());
896 this.allUsers.sort(new Comparator<User>() {
898 public int compare (final User o1, final User o2) {
899 return o1.getUserId() > o2.getUserId() ? 1 : o1.getUserId() < o2.getUserId() ? -1 : 0;
906 public boolean isContactFound (final Contact contact) {
907 // The contact must be valid
908 if (null == contact) {
910 throw new NullPointerException("contact is null"); //NOI18N
911 } else if (contact.getContactId() == null) {
913 throw new NullPointerException("contact.contactId is null"); //NOI18N
914 } else if (contact.getContactId() < 1) {
916 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
919 // Default is not found
920 boolean isFound = false;
923 final Iterator<User> iterator = this.allUsers().iterator();
925 // Loop through all entries
926 while (iterator.hasNext()) {
928 final User next = iterator.next();
930 // Compare both objects
931 if (Objects.equals(contact, next.getUserContact())) {
943 public boolean isRequiredChangePersonalDataSet () {
944 return ((this.getUserProfileMode() != null) &&
945 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
946 (this.contactController.isRequiredChangePersonalDataSet()));
950 public boolean isRequiredPersonalDataSet () {
951 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
952 // Multiple registration page
953 return this.contactController.isRequiredPersonalDataSet();
955 // Single registration page
956 return (((this.getUserName() != null) || (!this.isUserNameRequired())) &&
957 (this.getUserProfileMode() != null) &&
958 (this.contactController.isRequiredPersonalDataSet()) &&
959 (this.getUserPassword() != null) &&
960 (this.getUserPasswordRepeat() != null));
965 public boolean isSamePasswordEntered () {
966 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
970 public boolean isUserIdEmpty () {
971 return ((this.getUserId() == null) || (this.getUserId() == 0));
975 public boolean isUserNameRegistered (final User user) {
976 return ((this.userNameCache instanceof List) && (this.userNameCache.containsKey(user.getUserId())));
980 public boolean isUserNameRequired () {
981 // Get context parameter
982 final String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_login_require_user_name"); //NOI18N
985 final boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
992 public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException {
993 // Parameter must be valid
994 if (null == emailAddress) {
996 throw new NullPointerException("emailAddress is null"); //NOI18N
997 } else if (emailAddress.isEmpty()) {
999 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
1005 // Try to lookup it in visible user list
1006 for (final Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator(); iterator.hasNext();) {
1008 final Cache.Entry<Long, User> next = iterator.next();
1010 // Contact should be set
1011 if (next.getValue().getUserContact() == null) {
1013 throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getKey())); //NOI18N
1014 } else if (next.getValue().getUserContact().getContactEmailAddress() == null) {
1015 // Email address should be set
1016 throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getKey())); //NOI18N
1019 // Is the email address found?
1020 if (Objects.equals(next.getValue().getUserContact().getContactEmailAddress(), emailAddress)) {
1021 // Copy to other variable
1022 user = next.getValue();
1027 // Is it still null?
1029 // Not visible for the current user
1030 throw new UserEmailAddressNotFoundException(emailAddress);
1038 public User lookupUserById (final Long userId) throws UserNotFoundException {
1039 // Parameter must be valid
1040 if (null == userId) {
1042 throw new NullPointerException("userId is null"); //NOI18N
1043 } else if (userId < 1) {
1045 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
1051 // Try to lookup it in visible user list
1052 for (final Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator(); iterator.hasNext();) {
1054 final Cache.Entry<Long, User> next = iterator.next();
1056 // Is the user id found?
1057 if (Objects.equals(next.getKey(), userId)) {
1058 // Copy to other variable
1059 user = next.getValue();
1064 // Is it still null?
1066 // Not visible for the current user
1067 throw new UserNotFoundException(userId);
1075 * Adds user's name to bean's internal list. It also updates the public user
1076 * list if the user has decided to have a public account,
1078 * @param user User instance
1080 private void addUserName (final User user) {
1081 // Make sure the entry is not added yet
1082 if (this.userNameCache.containsKey(user.getUserId())) {
1084 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
1088 this.userNameCache.put(user.getUserId(), user.getUserName());
1094 private void clear () {
1097 this.setUserId(null);
1098 this.setUserProfileMode(null);
1101 this.clearUserName();
1102 this.clearUserPasswords();
1103 this.setLocale(null);
1109 private void clearUserName () {
1111 this.setUserName(null);
1115 * Clears both user passwords
1117 private void clearUserPasswords () {
1119 this.setUserPassword(null);
1120 this.setUserPasswordRepeat(null);
1124 * Copies given user into the controller
1126 * @param user User instance
1128 private void copyUser (final User user) {
1129 // Make sure the instance is valid
1132 throw new NullPointerException("user is null"); //NOI18N
1133 } else if (user.getUserContact() == null) {
1135 throw new NullPointerException("user.userContact is null"); //NOI18N
1140 this.setUserId(user.getUserId());
1141 this.setUserProfileMode(user.getUserProfileMode());
1145 * Getter for locale instance
1147 * @return Locale instance
1149 private Locale getLocale () {
1154 * Setter for locale instance
1156 * @param locale Locale instance
1158 private void setLocale (final Locale locale) {
1159 this.locale = locale;
1163 * Removes user from all lists
1165 * @param user User to remove
1167 private void removeFromList (final User user) {
1168 // The user should be valid
1171 throw new NullPointerException("user is null"); //NOI18N
1172 } else if (user.getUserId() == null) {
1174 throw new NullPointerException("user.userId is null"); //NOI18N
1175 } else if (user.getUserId() < 1) {
1177 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
1180 // Remove it from lists
1181 this.userCache.remove(user.getUserId());
1182 this.allUsers.remove(user);
1184 // Remove name from list
1185 this.userNameCache.remove(user.getUserId());
1189 * Updates list with given user instance
1191 * @param user User instance
1193 private void updateList (final User user) {
1194 // The user should be valid
1197 throw new NullPointerException("user is null"); //NOI18N
1198 } else if (user.getUserId() == null) {
1200 throw new NullPointerException("user.userId is null"); //NOI18N
1201 } else if (user.getUserId() < 1) {
1203 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
1204 } else if (user.getUserContact() == null) {
1206 throw new NullPointerException("user.userContact is null"); //NOI18N
1207 } else if (user.getUserContact().getContactId() == null) {
1209 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
1210 } else if (user.getUserContact().getContactId() < 1) {
1212 throw new NullPointerException(MessageFormat.format("user.userContact.contactId={0} is invalid.", user.getUserContact().getContactId())); //NOI18N
1216 this.userCache.put(user.getUserId(), user);
1217 this.allUsers.add(user);