2 * Copyright (C) 2016, 2017 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jjobs.beans.user;
19 import 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;
28 import javax.enterprise.context.SessionScoped;
29 import javax.enterprise.event.Event;
30 import javax.enterprise.event.Observes;
31 import javax.enterprise.inject.Any;
32 import javax.faces.context.FacesContext;
33 import javax.faces.view.facelets.FaceletException;
34 import javax.inject.Inject;
35 import javax.inject.Named;
36 import org.mxchange.jcontacts.contact.Contact;
37 import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent;
38 import org.mxchange.jjobs.beans.BaseJobsController;
39 import org.mxchange.jjobs.beans.contact.JobsContactWebRequestController;
40 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
41 import org.mxchange.jjobs.beans.localization.JobsLocalizationSessionController;
42 import org.mxchange.jjobs.beans.user.login.JobsUserLoginWebSessionController;
43 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
44 import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent;
45 import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent;
46 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
47 import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent;
48 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
49 import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent;
50 import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent;
51 import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent;
52 import org.mxchange.jusercore.events.user.update.ObservableUpdatedUserPersonalDataEvent;
53 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
54 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
55 import org.mxchange.jusercore.exceptions.UserNotFoundException;
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.profilemodes.ProfileMode;
60 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
61 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
62 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
63 import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
64 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
65 import org.mxchange.juserlogincore.login.UserLoginUtils;
68 * A user controller (bean)
70 * @author Roland Häder<roland@mxchange.org>
72 @Named ("userController")
74 public class JobsUserWebRequestBean extends BaseJobsController implements JobsUserWebRequestController {
79 private static final long serialVersionUID = 542_145_347_916L;
82 * General contact controller
85 private JobsContactWebRequestController contactController;
91 private JobsFeaturesWebApplicationController featureController;
96 private Locale locale;
99 * Localization controller
102 private JobsLocalizationSessionController localizationController;
105 * Event being fired when user updated personal data
109 private Event<ObservableUpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
114 @EJB (lookup = "java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
115 private UserSessionBeanRemote userBean;
118 * A list of all user profiles
121 @NamedCache (cacheName = "userCache", managementEnabled = true)
122 private transient Cache<Long, User> userCache;
130 * Login controller (bean)
133 private JobsUserLoginWebSessionController userLoginController;
138 private String userName;
144 @NamedCache (cacheName = "userNameCache", managementEnabled = true)
145 private transient Cache<Long, String> userNameCache;
148 * User password (clear-text from web form)
150 private String userPassword;
153 * User password repeated (clear-text from web form)
155 private String userPasswordRepeat;
158 * Whether the user wants a public profile
160 private ProfileMode userProfileMode;
163 * Default constructor
165 public JobsUserWebRequestBean () {
166 // Call super constructor
171 * Event observer for newly added users by administrator
173 * @param event Event being fired
175 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
176 // event should not be null
179 throw new NullPointerException("event is null"); //NOI18N
180 } else if (event.getAddedUser() == null) {
182 throw new NullPointerException("event.addedUser is null"); //NOI18N
183 } else if (event.getAddedUser().getUserId() == null) {
185 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
186 } else if (event.getAddedUser().getUserId() < 1) {
188 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
192 this.updateList(event.getAddedUser());
198 this.setUserId(event.getAddedUser().getUserId());
202 * Event observer for deleted user accounts (by administrator)
204 * @param event Event being fired
206 public void afterAdminDeletedUserEvent (@Observes final ObservableAdminDeletedUserEvent event) {
207 // event should not be null
210 throw new NullPointerException("event is null"); //NOI18N
211 } else if (event.getDeletedUser() == null) {
213 throw new NullPointerException("event.deletedUser is null"); //NOI18N
214 } else if (event.getDeletedUser().getUserId() == null) {
216 throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N
217 } else if (event.getDeletedUser().getUserId() < 1) {
219 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N
223 this.removeFromList(event.getDeletedUser());
230 * Event observer for linked users with existing contact data
232 * @param event Event being fired
234 public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
235 // event should not be null
238 throw new NullPointerException("event is null"); //NOI18N
239 } else if (event.getLinkedUser() == null) {
241 throw new NullPointerException("event.linkedUser is null"); //NOI18N
242 } else if (event.getLinkedUser().getUserId() == null) {
244 throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
245 } else if (event.getLinkedUser().getUserId() < 1) {
247 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
251 this.updateList(event.getLinkedUser());
257 this.setUserId(event.getLinkedUser().getUserId());
261 * Event observer for locked users
263 * @param event Event being fired
265 public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) {
266 // event should not be null
269 throw new NullPointerException("event is null"); //NOI18N
270 } else if (event.getLockedUser() == null) {
272 throw new NullPointerException("event.lockedUser is null"); //NOI18N
273 } else if (event.getLockedUser().getUserId() == null) {
275 throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
276 } else if (event.getLockedUser().getUserId() < 1) {
278 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
282 this.updateList(event.getLockedUser());
286 * Event observer for unlocked users
288 * @param event Event being fired
290 public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) {
291 // event should not be null
294 throw new NullPointerException("event is null"); //NOI18N
295 } else if (event.getUnlockedUser() == null) {
297 throw new NullPointerException("event.unlockedUser is null"); //NOI18N
298 } else if (event.getUnlockedUser().getUserId() == null) {
300 throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
301 } else if (event.getUnlockedUser().getUserId() < 1) {
303 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
307 this.updateList(event.getUnlockedUser());
311 * Event observer for updated user data by administrator
313 * @param event Event being updated
315 public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) {
316 // event should not be null
319 throw new NullPointerException("event is null"); //NOI18N
320 } else if (event.getUpdatedUser() == null) {
322 throw new NullPointerException("event.updatedUser is null"); //NOI18N
323 } else if (event.getUpdatedUser().getUserId() == null) {
325 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
326 } else if (event.getUpdatedUser().getUserId() < 1) {
328 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
332 this.updateList(event.getUpdatedUser());
339 * Event observer for when a bean helper has successfully created a user
340 * instance, means the user exists. If the user does not exist, this event
341 * should not fire but instead a proper exception must be thrown.
343 * @param event User created event
345 public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) {
346 // Is the instance valid?
349 throw new NullPointerException("event is null"); //NOI18N
350 } else if (event.getCreatedUser() == null) {
352 throw new NullPointerException("event.createdUser is null"); //NOI18N
353 } else if (event.getCreatedUser().getUserId() == null) {
355 throw new NullPointerException("event.createdUser.userId is null"); //NOI18N
356 } else if (event.getCreatedUser().getUserId() < 1) {
358 throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N
362 User user = event.getCreatedUser();
364 // Set all fields here
369 * Observer method for events being fired when the application's locale has
372 * @param event Event being fired
374 public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) {
375 // Is the parameter valid?
378 throw new NullPointerException("event is null");
379 } else if (event.getLocale() == null) {
381 throw new NullPointerException("event.locale is null");
385 this.setLocale(event.getLocale());
389 * Event observer when user confirmed account.
391 * @param event Event being fired
393 public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) {
394 // event should not be null
397 throw new NullPointerException("event is null"); //NOI18N
398 } else if (event.getConfirmedUser() == null) {
400 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
401 } else if (event.getConfirmedUser().getUserId() == null) {
403 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
404 } else if (event.getConfirmedUser().getUserId() < 1) {
406 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
410 this.updateList(event.getConfirmedUser());
414 * Event observer for logged-in user
416 * @param event Event instance
418 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
419 // event should not be null
422 throw new NullPointerException("event is null"); //NOI18N
423 } else if (event.getLoggedInUser() == null) {
425 throw new NullPointerException("event.registeredUser is null"); //NOI18N
426 } else if (event.getLoggedInUser().getUserId() == null) {
428 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
429 } else if (event.getLoggedInUser().getUserId() < 1) {
431 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
434 // "Cache" user instance
435 User loggedInUser = event.getLoggedInUser();
437 // Copy all data to this bean
438 this.copyUser(loggedInUser);
442 * Event observer for user password changes
444 * @param event Event being fired
446 public void afterUserPasswordChangedEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
450 throw new NullPointerException("event is null"); //NOI18N
451 } else if (event.getUserPassword() == null) {
453 throw new NullPointerException("event.userPassword is null"); //NOI18N
454 } else if (event.getUserPassword().isEmpty()) {
456 throw new IllegalArgumentException("event.userPassword is empty"); //NOI18N
460 this.setUserPassword(event.getUserPassword());
461 this.setUserPasswordRepeat(event.getUserPassword());
465 * Event observer for new user registrations
467 * @param event User registration event
469 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
470 // event should not be null
473 throw new NullPointerException("event is null"); //NOI18N
474 } else if (event.getRegisteredUser() == null) {
476 throw new NullPointerException("event.registeredUser is null"); //NOI18N
477 } else if (event.getRegisteredUser().getUserId() == null) {
479 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
480 } else if (event.getRegisteredUser().getUserId() < 1) {
482 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
486 User registeredUser = event.getRegisteredUser();
488 // Copy all data from registered->user
489 this.copyUser(registeredUser);
495 this.updateList(registeredUser);
498 this.addUserName(registeredUser);
501 this.setUserId(registeredUser.getUserId());
505 * Method being call after user's password has been updated (and history
506 * entry has been created).
508 * @param event Event being observed
510 public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
514 throw new NullPointerException("event is null"); //NOI18N
515 } else if (event.getPasswordHistory() == null) {
517 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
518 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
520 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
521 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
523 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
527 this.updateList(event.getPasswordHistory().getUserPasswordHistoryUser());
531 * Listens to fired event when user updated personal data
533 * @param event Event being fired
535 public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) {
539 throw new NullPointerException("event is null"); //NOI18N
540 } else if (event.getUpdatedUser() == null) {
542 throw new NullPointerException("event.updatedUser is null"); //NOI18N
543 } else if (event.getUpdatedUser().getUserId() == null) {
545 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
546 } else if (event.getUpdatedUser().getUserId() < 1) {
548 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
552 this.updateList(event.getUpdatedUser());
556 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
557 public List<User> allUsers () {
559 List<User> list = new LinkedList<>();
562 Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator();
565 while (iterator.hasNext()) {
567 final Cache.Entry<Long, User> next = iterator.next();
570 list.add(next.getValue());
578 * Event observer for when a user name should be cleared
580 * @param event Event being fired
582 public void clearUserNameEvent (@Observes final ObservableClearUserNameEvent event) {
586 throw new NullPointerException("event is null");
590 this.clearUserName();
594 * Event observer for when both user passwords should be cleared
596 * @param event Event being fired
598 public void clearUserPasswordEvent (@Observes final ObservableClearUserPasswordEvent event) {
602 throw new NullPointerException("event is null");
606 this.clearUserPasswords();
610 public User createUserInstance (final boolean createContactData) {
612 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
614 // Required personal data must be set
615 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
617 // Create new user instance
618 User user = new LoginUser();
620 // Is user name required?
621 if (!this.isUserNameRequired()) {
622 // Generate pseudo-random user name
623 String randomName = this.userBean.generateRandomUserName();
625 // Set it and inivisible profile
626 this.setUserName(randomName);
627 this.setUserProfileMode(ProfileMode.INVISIBLE);
629 // Generate random password
630 String randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
632 // Set random password
633 this.setUserPassword(randomPassword);
634 this.setUserPasswordRepeat(randomPassword);
637 // Set user name profile mode and locale
638 user.setUserName(this.getUserName());
639 user.setUserProfileMode(this.getUserProfileMode());
640 user.setUserLocale(this.getLocale());
642 // Is multiple registration page
643 if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
644 // Create contact instance
645 Contact contact = this.contactController.createContactInstance();
648 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
649 // Set contact in user
650 user.setUserContact(contact);
654 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
660 public User createUserLogin () {
662 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
665 if (this.getUserName() == null) {
667 throw new NullPointerException("userName is null"); //NOI18N
668 } else if (this.getUserName().isEmpty()) {
670 throw new IllegalStateException("userName is empty."); //NOI18N
673 // Create new user instance
674 User user = new LoginUser();
676 // Update all data ...
677 user.setUserName(this.getUserName());
680 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: user={1} - EXIT!", this.getClass().getSimpleName(), user));
681 // Return the new instance
686 public String doChangePersonalData () {
687 // This method shall only be called if the user is logged-in
688 if (!this.userLoginController.isUserLoggedIn()) {
690 throw new IllegalStateException("User is not logged-in"); //NOI18N
691 } else if (!this.isRequiredChangePersonalDataSet()) {
692 // Not all required fields are set
693 throw new FaceletException("Not all required fields are set."); //NOI18N
694 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
695 // Password not matching
696 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
697 } else if (!this.featureController.isFeatureEnabled("change_user_personal_data")) { //NOI18N
698 // Editing is not allowed
699 throw new IllegalStateException("User tried to edit personal data."); //NOI18N
703 User user = this.userLoginController.getLoggedInUser();
705 // Copy contact data to contact instance
706 this.contactController.updateContactDataFromController(user.getUserContact());
708 // It should be there, so run some tests on it
709 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
710 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
711 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
712 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
713 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
714 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
717 user.setUserProfileMode(this.getUserProfileMode());
719 // Send it to the EJB
720 User updatedUser = this.userBean.updateUserPersonalData(user);
723 this.updatedPersonalDataEvent.fire(new UpdatedUserPersonalDataEvent(updatedUser));
726 return "user_contact_data_saved"; //NOI18N
734 public Long getUserId () {
741 * @param userId User id
743 public void setUserId (final Long userId) {
744 this.userId = userId;
748 * Getter for user name
752 public String getUserName () {
753 return this.userName;
757 * Setter for user name
759 * @param userName User name
761 public void setUserName (final String userName) {
762 this.userName = userName;
766 public String getUserPassword () {
767 return this.userPassword;
771 * Setter for clear-text user password
773 * @param userPassword Clear-text user password
775 public void setUserPassword (final String userPassword) {
776 this.userPassword = userPassword;
780 * Getter for clear-text user password repeated
782 * @return Clear-text user password repeated
784 public String getUserPasswordRepeat () {
785 return this.userPasswordRepeat;
789 * Setter for clear-text user password repeated
791 * @param userPasswordRepeat Clear-text user password repeated
793 public void setUserPasswordRepeat (final String userPasswordRepeat) {
794 this.userPasswordRepeat = userPasswordRepeat;
798 * Getter for user profile mode
800 * @return User profile mode
802 public ProfileMode getUserProfileMode () {
803 return this.userProfileMode;
807 * Setter for user profile mode
809 * @param userProfileMode User profile mode
811 public void setUserProfileMode (final ProfileMode userProfileMode) {
812 this.userProfileMode = userProfileMode;
816 public boolean ifBothPasswordsEmptyAllowed () {
817 // Check feature first
818 return ((this.featureController.isFeatureEnabled("allow_user_registration_empty_password")) && //NOI18N
819 ((this.getUserPassword() == null) || (this.getUserPassword().isEmpty())) &&
820 ((this.getUserPasswordRepeat() == null) || (this.getUserPasswordRepeat().isEmpty())));
824 * Post-initialization of this class
827 public void init () {
829 if (!this.userCache.iterator().hasNext()) {
831 List<User> list = this.userBean.allUsers();
834 for (final Iterator<User> iterator = list.iterator(); iterator.hasNext();) {
836 final User next = iterator.next();
839 this.userCache.put(next.getUserId(), next);
840 this.userNameCache.put(next.getUserId(), next.getUserName());
846 public boolean isContactFound (final Contact contact) {
847 // The contact must be valid
848 if (null == contact) {
850 throw new NullPointerException("contact is null"); //NOI18N
851 } else if (contact.getContactId() == null) {
853 throw new NullPointerException("contact.contactId is null"); //NOI18N
854 } else if (contact.getContactId() < 1) {
856 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
859 // Default is not found
860 boolean isFound = false;
863 Iterator<User> iterator = this.allUsers().iterator();
865 // Loop through all entries
866 while (iterator.hasNext()) {
868 User next = iterator.next();
870 // Compare both objects
871 if (Objects.equals(contact, next.getUserContact())) {
883 public boolean isPublicUserProfileEnabled () {
884 // Get context parameter
885 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_public_profile_enabled"); //NOI18N
888 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
890 // This requires user names being enabled, too.
891 if ((isEnabled) && (!this.isUserNameRequired())) {
892 // Not valid state, users must be able to modify their profile, especially when it is public
893 throw new IllegalStateException("Public user profiles are enabled but user name requirement is disabled, this is not possible."); //NOI18N
901 public boolean isRequiredChangePersonalDataSet () {
902 return ((this.getUserProfileMode() != null) &&
903 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
904 (this.contactController.isRequiredChangePersonalDataSet()));
908 public boolean isRequiredPersonalDataSet () {
909 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
910 // Multiple registration page
911 return this.contactController.isRequiredPersonalDataSet();
913 // Single registration page
914 return (((this.getUserName() != null) || (!this.isUserNameRequired())) &&
915 (this.getUserProfileMode() != null) &&
916 (this.contactController.isRequiredPersonalDataSet()) &&
917 (this.getUserPassword() != null) &&
918 (this.getUserPasswordRepeat() != null));
923 public boolean isSamePasswordEntered () {
924 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
928 public boolean isUserIdEmpty () {
929 return ((this.getUserId() == null) || (this.getUserId() == 0));
933 public boolean isUserNameRegistered (final User user) {
934 return ((this.userNameCache instanceof List) && (this.userNameCache.containsKey(user.getUserId())));
938 public boolean isUserNameRequired () {
939 // Get context parameter
940 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_login_require_user_name"); //NOI18N
943 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
950 public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException {
951 // Parameter must be valid
952 if (null == emailAddress) {
954 throw new NullPointerException("emailAddress is null"); //NOI18N
955 } else if (emailAddress.isEmpty()) {
957 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
963 // Try to lookup it in visible user list
964 for (final Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator(); iterator.hasNext();) {
966 Cache.Entry<Long, User> next = iterator.next();
968 // Contact should be set
969 if (next.getValue().getUserContact() == null) {
971 throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getKey())); //NOI18N
972 } else if (next.getValue().getUserContact().getContactEmailAddress() == null) {
973 // Email address should be set
974 throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getKey())); //NOI18N
977 // Is the email address found?
978 if (Objects.equals(next.getValue().getUserContact().getContactEmailAddress(), emailAddress)) {
979 // Copy to other variable
980 user = next.getValue();
987 // Not visible for the current user
988 throw new UserEmailAddressNotFoundException(emailAddress);
996 public User lookupUserById (final Long userId) throws UserNotFoundException {
997 // Parameter must be valid
998 if (null == userId) {
1000 throw new NullPointerException("userId is null"); //NOI18N
1001 } else if (userId < 1) {
1003 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
1009 // Try to lookup it in visible user list
1010 for (final Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator(); iterator.hasNext();) {
1012 Cache.Entry<Long, User> next = iterator.next();
1014 // Is the user id found?
1015 if (Objects.equals(next.getKey(), userId)) {
1016 // Copy to other variable
1017 user = next.getValue();
1022 // Is it still null?
1024 // Not visible for the current user
1025 throw new UserNotFoundException(userId);
1033 * Adds user's name to bean's internal list. It also updates the public user
1034 * list if the user has decided to have a public account,
1036 * @param user User instance
1038 private void addUserName (final User user) {
1039 // Make sure the entry is not added yet
1040 if (this.userNameCache.containsKey(user.getUserId())) {
1042 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
1046 this.userNameCache.put(user.getUserId(), user.getUserName());
1052 private void clear () {
1055 this.setUserId(null);
1056 this.setUserProfileMode(null);
1059 this.clearUserName();
1060 this.clearUserPasswords();
1061 this.setLocale(null);
1067 private void clearUserName () {
1069 this.setUserName(null);
1073 * Clears both user passwords
1075 private void clearUserPasswords () {
1077 this.setUserPassword(null);
1078 this.setUserPasswordRepeat(null);
1082 * Copies given user into the controller
1084 * @param user User instance
1086 private void copyUser (final User user) {
1087 // Make sure the instance is valid
1090 throw new NullPointerException("user is null"); //NOI18N
1091 } else if (user.getUserContact() == null) {
1093 throw new NullPointerException("user.userContact is null"); //NOI18N
1098 this.setUserId(user.getUserId());
1099 this.setUserProfileMode(user.getUserProfileMode());
1103 * Getter for locale instance
1105 * @return Locale instance
1107 private Locale getLocale () {
1112 * Setter for locale instance
1114 * @param locale Locale instance
1116 private void setLocale (final Locale locale) {
1117 this.locale = locale;
1121 * Removes user from all lists
1123 * @param user User to remove
1125 private void removeFromList (final User user) {
1126 // The user should be valid
1129 throw new NullPointerException("user is null"); //NOI18N
1130 } else if (user.getUserId() == null) {
1132 throw new NullPointerException("user.userId is null"); //NOI18N
1133 } else if (user.getUserId() < 1) {
1135 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
1138 // Remove it from lists
1139 this.userCache.remove(user.getUserId());
1141 // Remove name from list
1142 this.userNameCache.remove(user.getUserId());
1146 * Updates list with given user instance
1148 * @param user User instance
1150 private void updateList (final User user) {
1151 // The user should be valid
1154 throw new NullPointerException("user is null"); //NOI18N
1155 } else if (user.getUserId() == null) {
1157 throw new NullPointerException("user.userId is null"); //NOI18N
1158 } else if (user.getUserId() < 1) {
1160 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
1161 } else if (user.getUserContact() == null) {
1163 throw new NullPointerException("user.userContact is null"); //NOI18N
1164 } else if (user.getUserContact().getContactId() == null) {
1166 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
1167 } else if (user.getUserContact().getContactId() < 1) {
1169 throw new NullPointerException(MessageFormat.format("user.userContact.contactId={0} is invalid.", user.getUserContact().getContactId())); //NOI18N
1173 this.userCache.put(user.getUserId(), user);