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.pizzaapplication.beans.user;
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Iterator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.cache.Cache;
29 import javax.enterprise.context.RequestScoped;
30 import javax.enterprise.event.Event;
31 import javax.enterprise.event.Observes;
32 import javax.enterprise.inject.Any;
33 import javax.faces.context.FacesContext;
34 import javax.faces.view.facelets.FaceletException;
35 import javax.inject.Inject;
36 import javax.inject.Named;
37 import org.mxchange.jcontacts.model.contact.Contact;
38 import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent;
39 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
40 import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent;
41 import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent;
42 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
43 import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent;
44 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
45 import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent;
46 import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent;
47 import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent;
48 import org.mxchange.jusercore.events.user.update.ObservableUpdatedUserPersonalDataEvent;
49 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
50 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
51 import org.mxchange.jusercore.exceptions.UserNotFoundException;
52 import org.mxchange.jusercore.model.user.LoginUser;
53 import org.mxchange.jusercore.model.user.User;
54 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
55 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
56 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
57 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
58 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
59 import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
60 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
61 import org.mxchange.juserlogincore.login.UserLoginUtils;
62 import org.mxchange.pizzaapplication.beans.BasePizzaController;
63 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebRequestController;
64 import org.mxchange.pizzaapplication.beans.features.PizzaFeaturesWebApplicationController;
65 import org.mxchange.pizzaapplication.beans.localization.PizzaLocalizationSessionController;
66 import org.mxchange.pizzaapplication.beans.user.login.PizzaUserLoginWebSessionController;
69 * A user bean (controller)
71 * @author Roland Häder<roland@mxchange.org>
73 @Named ("userController")
75 public class PizzaUserWebRequestBean extends BasePizzaController implements PizzaUserWebRequestController {
80 private static final long serialVersionUID = 542_145_347_916L;
83 * General contact controller
86 private PizzaContactWebRequestController contactController;
92 private PizzaFeaturesWebApplicationController featureController;
97 private Locale locale;
100 * Localization controller
103 private PizzaLocalizationSessionController localizationController;
106 * Event being fired when user updated personal data
110 private Event<ObservableUpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
115 @EJB (lookup = "java:global/jfinancials-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
116 private UserSessionBeanRemote userBean;
119 * A list of all user profiles
122 @NamedCache (cacheName = "userCache", managementEnabled = true)
123 private transient Cache<Long, User> userCache;
131 * Login controller (bean)
134 private PizzaUserLoginWebSessionController userLoginController;
139 private String userName;
145 @NamedCache (cacheName = "userNameCache", managementEnabled = true)
146 private transient Cache<Long, String> userNameCache;
149 * User password (clear-text from web form)
151 private String userPassword;
154 * User password repeated (clear-text from web form)
156 private String userPasswordRepeat;
159 * Whether the user wants a public profile
161 private ProfileMode userProfileMode;
164 * Default constructor
166 public PizzaUserWebRequestBean () {
167 // Call super constructor
172 * Event observer for newly added users by administrator
174 * @param event Event being fired
176 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
177 // event should not be null
180 throw new NullPointerException("event is null"); //NOI18N
181 } else if (event.getAddedUser() == null) {
183 throw new NullPointerException("event.addedUser is null"); //NOI18N
184 } else if (event.getAddedUser().getUserId() == null) {
186 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
187 } else if (event.getAddedUser().getUserId() < 1) {
189 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
193 this.updateList(event.getAddedUser());
199 this.setUserId(event.getAddedUser().getUserId());
203 * Event observer for deleted user accounts (by administrator)
205 * @param event Event being fired
207 public void afterAdminDeletedUserEvent (@Observes final ObservableAdminDeletedUserEvent event) {
208 // event should not be null
211 throw new NullPointerException("event is null"); //NOI18N
212 } else if (event.getDeletedUser() == null) {
214 throw new NullPointerException("event.deletedUser is null"); //NOI18N
215 } else if (event.getDeletedUser().getUserId() == null) {
217 throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N
218 } else if (event.getDeletedUser().getUserId() < 1) {
220 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N
224 this.removeFromList(event.getDeletedUser());
231 * Event observer for linked users with existing contact data
233 * @param event Event being fired
235 public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
236 // event should not be null
239 throw new NullPointerException("event is null"); //NOI18N
240 } else if (event.getLinkedUser() == null) {
242 throw new NullPointerException("event.linkedUser is null"); //NOI18N
243 } else if (event.getLinkedUser().getUserId() == null) {
245 throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
246 } else if (event.getLinkedUser().getUserId() < 1) {
248 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
252 this.updateList(event.getLinkedUser());
258 this.setUserId(event.getLinkedUser().getUserId());
262 * Event observer for locked users
264 * @param event Event being fired
266 public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) {
267 // event should not be null
270 throw new NullPointerException("event is null"); //NOI18N
271 } else if (event.getLockedUser() == null) {
273 throw new NullPointerException("event.lockedUser is null"); //NOI18N
274 } else if (event.getLockedUser().getUserId() == null) {
276 throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
277 } else if (event.getLockedUser().getUserId() < 1) {
279 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
283 this.updateList(event.getLockedUser());
287 * Event observer for unlocked users
289 * @param event Event being fired
291 public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) {
292 // event should not be null
295 throw new NullPointerException("event is null"); //NOI18N
296 } else if (event.getUnlockedUser() == null) {
298 throw new NullPointerException("event.unlockedUser is null"); //NOI18N
299 } else if (event.getUnlockedUser().getUserId() == null) {
301 throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
302 } else if (event.getUnlockedUser().getUserId() < 1) {
304 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
308 this.updateList(event.getUnlockedUser());
312 * Event observer for updated user data by administrator
314 * @param event Event being updated
316 public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) {
317 // event should not be null
320 throw new NullPointerException("event is null"); //NOI18N
321 } else if (event.getUpdatedUser() == null) {
323 throw new NullPointerException("event.updatedUser is null"); //NOI18N
324 } else if (event.getUpdatedUser().getUserId() == null) {
326 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
327 } else if (event.getUpdatedUser().getUserId() < 1) {
329 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
333 this.updateList(event.getUpdatedUser());
340 * Event observer for when a bean helper has successfully created a user
341 * instance, means the user exists. If the user does not exist, this event
342 * should not fire but instead a proper exception must be thrown.
344 * @param event User created event
346 public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) {
347 // Is the instance valid?
350 throw new NullPointerException("event is null"); //NOI18N
351 } else if (event.getCreatedUser() == null) {
353 throw new NullPointerException("event.createdUser is null"); //NOI18N
354 } else if (event.getCreatedUser().getUserId() == null) {
356 throw new NullPointerException("event.createdUser.userId is null"); //NOI18N
357 } else if (event.getCreatedUser().getUserId() < 1) {
359 throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N
363 User user = event.getCreatedUser();
365 // Set all fields here
370 * Observer method for events being fired when the application's locale has
373 * @param event Event being fired
375 public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) {
376 // Is the parameter valid?
379 throw new NullPointerException("event is null");
380 } else if (event.getLocale() == null) {
382 throw new NullPointerException("event.locale is null");
386 this.setLocale(event.getLocale());
390 * Event observer when user confirmed account.
392 * @param event Event being fired
394 public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) {
395 // event should not be null
398 throw new NullPointerException("event is null"); //NOI18N
399 } else if (event.getConfirmedUser() == null) {
401 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
402 } else if (event.getConfirmedUser().getUserId() == null) {
404 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
405 } else if (event.getConfirmedUser().getUserId() < 1) {
407 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
411 this.updateList(event.getConfirmedUser());
415 * Event observer for logged-in user
417 * @param event Event instance
419 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
420 // event should not be null
423 throw new NullPointerException("event is null"); //NOI18N
424 } else if (event.getLoggedInUser() == null) {
426 throw new NullPointerException("event.registeredUser is null"); //NOI18N
427 } else if (event.getLoggedInUser().getUserId() == null) {
429 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
430 } else if (event.getLoggedInUser().getUserId() < 1) {
432 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
435 // "Cache" user instance
436 User loggedInUser = event.getLoggedInUser();
438 // Copy all data to this bean
439 this.copyUser(loggedInUser);
443 * Event observer for user password changes
445 * @param event Event being fired
447 public void afterUserPasswordChangedEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
451 throw new NullPointerException("event is null"); //NOI18N
452 } else if (event.getUserPassword() == null) {
454 throw new NullPointerException("event.userPassword is null"); //NOI18N
455 } else if (event.getUserPassword().isEmpty()) {
457 throw new IllegalArgumentException("event.userPassword is empty"); //NOI18N
461 this.setUserPassword(event.getUserPassword());
462 this.setUserPasswordRepeat(event.getUserPassword());
466 * Event observer for new user registrations
468 * @param event User registration event
470 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
471 // event should not be null
474 throw new NullPointerException("event is null"); //NOI18N
475 } else if (event.getRegisteredUser() == null) {
477 throw new NullPointerException("event.registeredUser is null"); //NOI18N
478 } else if (event.getRegisteredUser().getUserId() == null) {
480 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
481 } else if (event.getRegisteredUser().getUserId() < 1) {
483 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
487 User registeredUser = event.getRegisteredUser();
489 // Copy all data from registered->user
490 this.copyUser(registeredUser);
496 this.updateList(registeredUser);
499 this.addUserName(registeredUser);
502 this.setUserId(registeredUser.getUserId());
506 * Method being call after user's password has been updated (and history
507 * entry has been created).
509 * @param event Event being observed
511 public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
515 throw new NullPointerException("event is null"); //NOI18N
516 } else if (event.getPasswordHistory() == null) {
518 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
519 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
521 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
522 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
524 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
528 this.updateList(event.getPasswordHistory().getUserPasswordHistoryUser());
532 * Listens to fired event when user updated personal data
534 * @param event Event being fired
536 public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) {
540 throw new NullPointerException("event is null"); //NOI18N
541 } else if (event.getUpdatedUser() == null) {
543 throw new NullPointerException("event.updatedUser is null"); //NOI18N
544 } else if (event.getUpdatedUser().getUserId() == null) {
546 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
547 } else if (event.getUpdatedUser().getUserId() < 1) {
549 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
553 this.updateList(event.getUpdatedUser());
557 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
558 public List<User> allUsers () {
560 List<User> list = new LinkedList<>();
563 Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator();
566 while (iterator.hasNext()) {
568 final Cache.Entry<Long, User> next = iterator.next();
571 list.add(next.getValue());
579 * Event observer for when a user name should be cleared
581 * @param event Event being fired
583 public void clearUserNameEvent (@Observes final ObservableClearUserNameEvent event) {
587 throw new NullPointerException("event is null");
591 this.clearUserName();
595 * Event observer for when both user passwords should be cleared
597 * @param event Event being fired
599 public void clearUserPasswordEvent (@Observes final ObservableClearUserPasswordEvent event) {
603 throw new NullPointerException("event is null");
607 this.clearUserPasswords();
611 public User createUserInstance (final boolean createContactData) {
613 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
615 // Required personal data must be set
616 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
618 // Create new user instance
619 User user = new LoginUser();
621 // Is user name required?
622 if (!this.isUserNameRequired()) {
623 // Generate pseudo-random user name
624 String randomName = this.userBean.generateRandomUserName();
626 // Set it and inivisible profile
627 this.setUserName(randomName);
628 this.setUserProfileMode(ProfileMode.INVISIBLE);
630 // Generate random password
631 String randomPassword = UserLoginUtils.createRandomPassword(PizzaUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
633 // Set random password
634 this.setUserPassword(randomPassword);
635 this.setUserPasswordRepeat(randomPassword);
638 // Set user name profile mode and locale
639 user.setUserName(this.getUserName());
640 user.setUserProfileMode(this.getUserProfileMode());
641 user.setUserLocale(this.getLocale());
643 // Is multiple registration page
644 if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
645 // Create contact instance
646 Contact contact = this.contactController.createContactInstance();
649 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
650 // Set contact in user
651 user.setUserContact(contact);
655 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
661 public User createUserLogin () {
663 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
666 if (this.getUserName() == null) {
668 throw new NullPointerException("userName is null"); //NOI18N
669 } else if (this.getUserName().isEmpty()) {
671 throw new IllegalStateException("userName is empty."); //NOI18N
674 // Create new user instance
675 User user = new LoginUser();
677 // Update all data ...
678 user.setUserName(this.getUserName());
681 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: user={1} - EXIT!", this.getClass().getSimpleName(), user));
682 // Return the new instance
687 public String doChangePersonalData () {
688 // This method shall only be called if the user is logged-in
689 if (!this.userLoginController.isUserLoggedIn()) {
691 throw new IllegalStateException("User is not logged-in"); //NOI18N
692 } else if (!this.isRequiredChangePersonalDataSet()) {
693 // Not all required fields are set
694 throw new FaceletException("Not all required fields are set."); //NOI18N
695 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
696 // Password not matching
697 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
698 } else if (!this.featureController.isFeatureEnabled("change_user_personal_data")) { //NOI18N
699 // Editing is not allowed
700 throw new IllegalStateException("User tried to edit personal data."); //NOI18N
704 User user = this.userLoginController.getLoggedInUser();
706 // Copy contact data to contact instance
707 this.contactController.updateContactDataFromController(user.getUserContact());
709 // It should be there, so run some tests on it
710 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
711 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
712 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
713 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
714 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
715 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
718 user.setUserProfileMode(this.getUserProfileMode());
720 // Send it to the EJB
721 User updatedUser = this.userBean.updateUserPersonalData(user);
724 this.updatedPersonalDataEvent.fire(new UpdatedUserPersonalDataEvent(updatedUser));
727 return "user_contact_data_saved"; //NOI18N
735 public Long getUserId () {
742 * @param userId User id
744 public void setUserId (final Long userId) {
745 this.userId = userId;
749 * Getter for user name
753 public String getUserName () {
754 return this.userName;
758 * Setter for user name
760 * @param userName User name
762 public void setUserName (final String userName) {
763 this.userName = userName;
767 public String getUserPassword () {
768 return this.userPassword;
772 * Setter for clear-text user password
774 * @param userPassword Clear-text user password
776 public void setUserPassword (final String userPassword) {
777 this.userPassword = userPassword;
781 * Getter for clear-text user password repeated
783 * @return Clear-text user password repeated
785 public String getUserPasswordRepeat () {
786 return this.userPasswordRepeat;
790 * Setter for clear-text user password repeated
792 * @param userPasswordRepeat Clear-text user password repeated
794 public void setUserPasswordRepeat (final String userPasswordRepeat) {
795 this.userPasswordRepeat = userPasswordRepeat;
799 * Getter for user profile mode
801 * @return User profile mode
803 public ProfileMode getUserProfileMode () {
804 return this.userProfileMode;
808 * Setter for user profile mode
810 * @param userProfileMode User profile mode
812 public void setUserProfileMode (final ProfileMode userProfileMode) {
813 this.userProfileMode = userProfileMode;
817 public boolean ifBothPasswordsEmptyAllowed () {
818 // Check feature first
819 return ((this.featureController.isFeatureEnabled("allow_user_registration_empty_password")) && //NOI18N
820 ((this.getUserPassword() == null) || (this.getUserPassword().isEmpty())) &&
821 ((this.getUserPasswordRepeat() == null) || (this.getUserPasswordRepeat().isEmpty())));
825 * Post-initialization of this class
828 public void init () {
830 if (!this.userCache.iterator().hasNext()) {
832 List<User> list = this.userBean.allUsers();
835 for (final Iterator<User> iterator = list.iterator(); iterator.hasNext();) {
837 final User next = iterator.next();
840 this.userCache.put(next.getUserId(), next);
841 this.userNameCache.put(next.getUserId(), next.getUserName());
847 public boolean isContactFound (final Contact contact) {
848 // The contact must be valid
849 if (null == contact) {
851 throw new NullPointerException("contact is null"); //NOI18N
852 } else if (contact.getContactId() == null) {
854 throw new NullPointerException("contact.contactId is null"); //NOI18N
855 } else if (contact.getContactId() < 1) {
857 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
860 // Default is not found
861 boolean isFound = false;
864 Iterator<User> iterator = this.allUsers().iterator();
866 // Loop through all entries
867 while (iterator.hasNext()) {
869 User next = iterator.next();
871 // Compare both objects
872 if (Objects.equals(contact, next.getUserContact())) {
884 public boolean isPublicUserProfileEnabled () {
885 // Get context parameter
886 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_public_profile_enabled"); //NOI18N
889 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
891 // This requires user names being enabled, too.
892 if ((isEnabled) && (!this.isUserNameRequired())) {
893 // Not valid state, users must be able to modify their profile, especially when it is public
894 throw new IllegalStateException("Public user profiles are enabled but user name requirement is disabled, this is not possible."); //NOI18N
902 public boolean isRequiredChangePersonalDataSet () {
903 return ((this.getUserProfileMode() != null) &&
904 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
905 (this.contactController.isRequiredChangePersonalDataSet()));
909 public boolean isRequiredPersonalDataSet () {
910 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
911 // Multiple registration page
912 return this.contactController.isRequiredPersonalDataSet();
914 // Single registration page
915 return (((this.getUserName() != null) || (!this.isUserNameRequired())) &&
916 (this.getUserProfileMode() != null) &&
917 (this.contactController.isRequiredPersonalDataSet()) &&
918 (this.getUserPassword() != null) &&
919 (this.getUserPasswordRepeat() != null));
924 public boolean isSamePasswordEntered () {
925 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
929 public boolean isUserIdEmpty () {
930 return ((this.getUserId() == null) || (this.getUserId() == 0));
934 public boolean isUserNameRegistered (final User user) {
935 return ((this.userNameCache instanceof List) && (this.userNameCache.containsKey(user.getUserId())));
939 public boolean isUserNameRequired () {
940 // Get context parameter
941 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_login_require_user_name"); //NOI18N
944 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
951 public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException {
952 // Parameter must be valid
953 if (null == emailAddress) {
955 throw new NullPointerException("emailAddress is null"); //NOI18N
956 } else if (emailAddress.isEmpty()) {
958 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
964 // Try to lookup it in visible user list
965 for (final Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator(); iterator.hasNext();) {
967 Cache.Entry<Long, User> next = iterator.next();
969 // Contact should be set
970 if (next.getValue().getUserContact() == null) {
972 throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getKey())); //NOI18N
973 } else if (next.getValue().getUserContact().getContactEmailAddress() == null) {
974 // Email address should be set
975 throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getKey())); //NOI18N
978 // Is the email address found?
979 if (Objects.equals(next.getValue().getUserContact().getContactEmailAddress(), emailAddress)) {
980 // Copy to other variable
981 user = next.getValue();
988 // Not visible for the current user
989 throw new UserEmailAddressNotFoundException(emailAddress);
997 public User lookupUserById (final Long userId) throws UserNotFoundException {
998 // Parameter must be valid
999 if (null == userId) {
1001 throw new NullPointerException("userId is null"); //NOI18N
1002 } else if (userId < 1) {
1004 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
1010 // Try to lookup it in visible user list
1011 for (final Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator(); iterator.hasNext();) {
1013 Cache.Entry<Long, User> next = iterator.next();
1015 // Is the user id found?
1016 if (Objects.equals(next.getKey(), userId)) {
1017 // Copy to other variable
1018 user = next.getValue();
1023 // Is it still null?
1025 // Not visible for the current user
1026 throw new UserNotFoundException(userId);
1034 * Adds user's name to bean's internal list. It also updates the public user
1035 * list if the user has decided to have a public account,
1037 * @param user User instance
1039 private void addUserName (final User user) {
1040 // Make sure the entry is not added yet
1041 if (this.userNameCache.containsKey(user.getUserId())) {
1043 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
1047 this.userNameCache.put(user.getUserId(), user.getUserName());
1053 private void clear () {
1056 this.setUserId(null);
1057 this.setUserProfileMode(null);
1060 this.clearUserName();
1061 this.clearUserPasswords();
1062 this.setLocale(null);
1068 private void clearUserName () {
1070 this.setUserName(null);
1074 * Clears both user passwords
1076 private void clearUserPasswords () {
1078 this.setUserPassword(null);
1079 this.setUserPasswordRepeat(null);
1083 * Copies given user into the controller
1085 * @param user User instance
1087 private void copyUser (final User user) {
1088 // Make sure the instance is valid
1091 throw new NullPointerException("user is null"); //NOI18N
1092 } else if (user.getUserContact() == null) {
1094 throw new NullPointerException("user.userContact is null"); //NOI18N
1099 this.setUserId(user.getUserId());
1100 this.setUserProfileMode(user.getUserProfileMode());
1104 * Getter for locale instance
1106 * @return Locale instance
1108 private Locale getLocale () {
1113 * Setter for locale instance
1115 * @param locale Locale instance
1117 private void setLocale (final Locale locale) {
1118 this.locale = locale;
1122 * Removes user from all lists
1124 * @param user User to remove
1126 private void removeFromList (final User user) {
1127 // The user should be valid
1130 throw new NullPointerException("user is null"); //NOI18N
1131 } else if (user.getUserId() == null) {
1133 throw new NullPointerException("user.userId is null"); //NOI18N
1134 } else if (user.getUserId() < 1) {
1136 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
1139 // Remove it from lists
1140 this.userCache.remove(user.getUserId());
1142 // Remove name from list
1143 this.userNameCache.remove(user.getUserId());
1147 * Updates list with given user instance
1149 * @param user User instance
1151 private void updateList (final User user) {
1152 // The user should be valid
1155 throw new NullPointerException("user is null"); //NOI18N
1156 } else if (user.getUserId() == null) {
1158 throw new NullPointerException("user.userId is null"); //NOI18N
1159 } else if (user.getUserId() < 1) {
1161 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
1162 } else if (user.getUserContact() == null) {
1164 throw new NullPointerException("user.userContact is null"); //NOI18N
1165 } else if (user.getUserContact().getContactId() == null) {
1167 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
1168 } else if (user.getUserContact().getContactId() < 1) {
1170 throw new NullPointerException(MessageFormat.format("user.userContact.contactId={0} is invalid.", user.getUserContact().getContactId())); //NOI18N
1174 this.userCache.put(user.getUserId(), user);