2 * Copyright (C) 2016 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.pizzaapplication.beans.user;
19 import java.text.MessageFormat;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.SessionScoped;
25 import javax.enterprise.event.Event;
26 import javax.enterprise.event.Observes;
27 import javax.enterprise.inject.Any;
28 import javax.faces.context.FacesContext;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.jcontacts.contact.Contact;
36 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
37 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
38 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
39 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
40 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
41 import org.mxchange.jusercore.events.user.password_change.UpdatedUserPasswordEvent;
42 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
43 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
44 import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
45 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
46 import org.mxchange.jusercore.exceptions.UserNotFoundException;
47 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
48 import org.mxchange.jusercore.model.user.LoginUser;
49 import org.mxchange.jusercore.model.user.User;
50 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
51 import org.mxchange.jusercore.model.user.UserUtils;
52 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
53 import org.mxchange.pizzaapplication.beans.BasePizzaController;
54 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
55 import org.mxchange.pizzaapplication.beans.features.PizzaFeaturesWebApplicationController;
56 import org.mxchange.pizzaapplication.beans.localization.PizzaLocalizationSessionController;
57 import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController;
60 * A user bean (controller)
62 * @author Roland Haeder<roland@mxchange.org>
64 @Named ("userController")
66 public class PizzaUserWebSessionBean extends BasePizzaController implements PizzaUserWebSessionController {
71 private static final long serialVersionUID = 542_145_347_916L;
76 private ContactSessionBeanRemote contactBean;
79 * General contact controller
82 private PizzaContactWebSessionController contactController;
88 private PizzaFeaturesWebApplicationController featureController;
91 * Localization controller
94 private PizzaLocalizationSessionController localizationController;
97 * Event being fired when user updated personal data
101 private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
106 private final UserSessionBeanRemote userBean;
114 * A list of all user profiles
116 private List<User> userList;
119 * Login bean (controller)
122 private PizzaUserLoginWebSessionController userLoginController;
127 private String userName;
132 private List<String> userNameList;
135 * User password (unencrypted from web form)
137 private String userPassword;
140 * User password repeated (unencrypted from web form)
142 private String userPasswordRepeat;
145 * Whether the user wants a public profile
147 private ProfileMode userProfileMode;
150 * A list of all public user profiles
152 * @TODO Hmm, makes that sense? Having visible user list in current
153 * (session-scoped) user's visible user list?
155 private List<User> visibleUserList;
158 * Default constructor
160 public PizzaUserWebSessionBean () {
163 // Get initial context
164 Context context = new InitialContext();
167 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
170 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
171 } catch (final NamingException e) {
173 throw new FaceletException(e);
178 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
180 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
182 // event should not be null
185 throw new NullPointerException("event is null"); //NOI18N
186 } else if (event.getAddedUser() == null) {
188 throw new NullPointerException("event.addedUser is null"); //NOI18N
189 } else if (event.getAddedUser().getUserId() == null) {
191 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
192 } else if (event.getAddedUser().getUserId() < 1) {
194 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
198 this.uniqueAddUser(event.getAddedUser());
204 this.setUserId(event.getAddedUser().getUserId());
207 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
211 public void afterAdminUpdatedUserDataEvent (@Observes final AdminUpdatedUserDataEvent event) {
213 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminUpdatedUserEvent: event={0} - CALLED!", event)); //NOI18N
215 // event should not be null
218 throw new NullPointerException("event is null"); //NOI18N
219 } else if (event.getUpdatedUser() == null) {
221 throw new NullPointerException("event.updatedUser is null"); //NOI18N
222 } else if (event.getUpdatedUser().getUserId() == null) {
224 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
225 } else if (event.getUpdatedUser().getUserId() < 1) {
227 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
231 this.updateList(event.getUpdatedUser());
237 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminUpdatedUserEvent: EXIT!"); //NOI18N
241 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
243 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
245 // event should not be null
248 throw new NullPointerException("event is null"); //NOI18N
249 } else if (event.getRegisteredUser() == null) {
251 throw new NullPointerException("event.registeredUser is null"); //NOI18N
252 } else if (event.getRegisteredUser().getUserId() == null) {
254 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
255 } else if (event.getRegisteredUser().getUserId() < 1) {
257 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
261 User registeredUser = event.getRegisteredUser();
264 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
266 // Copy all data from registered->user
267 this.copyUser(registeredUser);
273 this.uniqueAddUser(registeredUser);
276 this.addUserName(registeredUser);
278 // Is the account public?
279 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
280 // Also add it to this list
281 this.visibleUserList.add(registeredUser);
285 this.setUserId(registeredUser.getUserId());
288 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
292 public void afterUserConfirmedAccount (@Observes final UserConfirmedAccountEvent event) {
294 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
296 // event should not be null
299 throw new NullPointerException("event is null"); //NOI18N
300 } else if (event.getConfirmedUser() == null) {
302 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
303 } else if (event.getConfirmedUser().getUserId() == null) {
305 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
306 } else if (event.getConfirmedUser().getUserId() < 1) {
308 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
312 this.uniqueAddUser(event.getConfirmedUser());
316 public void afterUserLogin (@Observes final UserLoggedInEvent event) {
318 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
320 // event should not be null
323 throw new NullPointerException("event is null"); //NOI18N
324 } else if (event.getLoggedInUser() == null) {
326 throw new NullPointerException("event.registeredUser is null"); //NOI18N
327 } else if (event.getLoggedInUser().getUserId() == null) {
329 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
330 } else if (event.getLoggedInUser().getUserId() < 1) {
332 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
335 // "Cache" user instance
336 User loggedInUser = event.getLoggedInUser();
338 // Copy all data to this bean
339 this.copyUser(loggedInUser);
341 // Is the user visible?
342 if (Objects.equals(loggedInUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
343 // Yes, then add user
344 this.visibleUserList.add(loggedInUser);
349 public void afterUserUpdatedPasswordEvent (@Observes final UpdatedUserPasswordEvent event) {
353 throw new NullPointerException("event is null"); //NOI18N
354 } else if (event.getPasswordHistory() == null) {
356 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
357 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
359 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
360 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
362 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
365 // All fine, so update list
366 this.updateList(event.getPasswordHistory().getUserPasswordHistoryUser());
370 public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
374 throw new NullPointerException("event is null"); //NOI18N
375 } else if (event.getUpdatedUser() == null) {
377 throw new NullPointerException("event.updatedUser is null"); //NOI18N
378 } else if (event.getUpdatedUser().getUserId() == null) {
380 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
381 } else if (event.getUpdatedUser().getUserId() < 1) {
383 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
386 // All fine, so update list
387 this.updateList(event.getUpdatedUser());
391 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
392 public List<User> allUsers () {
394 return this.userList;
398 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
399 public List<User> allVisibleUsers () {
401 return this.visibleUserList;
405 public User createUserInstance (final boolean createContactData) {
407 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
409 // Required personal data must be set
410 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
412 // Create new user instance
413 User user = new LoginUser();
415 // Is user name required?
416 if (!this.isUserNameRequired()) {
417 // Generate pseudo-random user name
418 String randomName = this.userBean.generateRandomUserName();
420 // Set it and inivisible profile
421 this.setUserName(randomName);
422 this.setUserProfileMode(ProfileMode.INVISIBLE);
424 // Generate random password
425 String randomPassword = UserUtils.createRandomPassword(PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
427 // Set random password
428 this.setUserPassword(randomPassword);
429 this.setUserPasswordRepeat(randomPassword);
432 // Set local, user name and mode
433 user.setUserLocale(this.localizationController.getLocale());
434 user.setUserName(this.getUserName());
435 user.setUserProfileMode(this.getUserProfileMode());
437 // Is multiple registration page
438 if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
439 // Create contact instance
440 Contact contact = this.contactController.createContactInstance();
443 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
445 // Set contact in user
446 user.setUserContact(contact);
450 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
457 public User createUserLogin () {
459 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
462 if (this.getUserName() == null) {
464 throw new NullPointerException("recruiterName is null"); //NOI18N
465 } else if (this.getUserName().isEmpty()) {
467 throw new IllegalStateException("recruiterName is empty."); //NOI18N
470 // Create new recruiter instance
471 User recruiter = new LoginUser();
473 // Update all data ...
474 recruiter.setUserName(this.getUserName());
477 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: recruiter={1} - EXIT!", this.getClass().getSimpleName(), recruiter));
479 // Return the new instance
484 public String doChangePersonalData () {
485 // This method shall only be called if the user is logged-in
486 if (!this.userLoginController.isUserLoggedIn()) {
488 throw new IllegalStateException("User is not logged-in"); //NOI18N
489 } else if (!this.isRequiredChangePersonalDataSet()) {
490 // Not all required fields are set
491 throw new FaceletException("Not all required fields are set."); //NOI18N
492 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
493 // Password not matching
494 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
495 } else if (!this.featureController.isFeatureEnabled("edit_user_data")) {
496 // Editing is not allowed
497 throw new IllegalStateException("User tried to edit personal data."); //NOI18N
501 User user = this.userLoginController.getLoggedInUser();
503 // Copy contact data to contact instance
504 this.contactController.updateContactDataFromController(user.getUserContact());
506 // It should be there, so run some tests on it
507 assert (user instanceof User) : "Instance loginController.loggedInUser is null"; //NOI18N
508 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null"; //NOI18N
509 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
510 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
511 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
512 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
515 user.setUserProfileMode(this.getUserProfileMode());
517 // Send it to the EJB
518 User updatedUser = this.userBean.updateUserPersonalData(user);
521 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
524 return "contact_data_saved"; //NOI18N
528 public Long getUserId () {
533 public void setUserId (final Long userId) {
534 this.userId = userId;
538 public String getUserName () {
539 return this.userName;
543 public void setUserName (final String userName) {
544 this.userName = userName;
548 public String getUserPassword () {
549 return this.userPassword;
553 public void setUserPassword (final String userPassword) {
554 this.userPassword = userPassword;
558 public String getUserPasswordRepeat () {
559 return this.userPasswordRepeat;
563 public void setUserPasswordRepeat (final String userPasswordRepeat) {
564 this.userPasswordRepeat = userPasswordRepeat;
568 public ProfileMode getUserProfileMode () {
569 return this.userProfileMode;
573 public void setUserProfileMode (final ProfileMode userProfileMode) {
574 this.userProfileMode = userProfileMode;
578 * Post-initialization of this class
581 public void init () {
582 // Initialize user list
583 this.userList = this.userBean.allUsers();
585 // Get full user name list for reducing EJB calls
586 this.userNameList = this.userBean.getUserNameList();
588 // Is the user logged-in?
589 if (this.userLoginController.isUserLoggedIn()) {
590 // Is logged-in, so load also users visible to memebers
591 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
593 // Initialize user list
594 this.visibleUserList = this.userBean.allPublicUsers();
599 public boolean isContactFound (final Contact contact) {
600 // The contact must be valid
601 if (null == contact) {
603 throw new NullPointerException("contact is null"); //NOI18N
604 } else if (contact.getContactId() == null) {
606 throw new NullPointerException("contact.contactId is null"); //NOI18N
607 } else if (contact.getContactId() < 1) {
609 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
612 // Default is not found
613 boolean isFound = false;
616 Iterator<User> iterator = this.allUsers().iterator();
618 // Loop through all entries
619 while (iterator.hasNext()) {
621 User next = iterator.next();
623 // Compare both objects
624 if (Objects.equals(contact, next.getUserContact())) {
636 public boolean isPublicUserProfileEnabled () {
637 // Get context parameter
638 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_public_profile_enabled"); //NOI18N
641 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
643 // This requires user names being enabled, too.
644 if ((isEnabled) && (!this.isUserNameRequired())) {
645 // Not valid state, users must be able to modify their profile, especially when it is public
646 throw new IllegalStateException("Public user profiles are enabled but user name requirement is disabled, this is not possible."); //NOI18N
654 public boolean isRequiredChangePersonalDataSet () {
655 return ((this.getUserProfileMode() != null) &&
656 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
657 (this.contactController.isRequiredChangePersonalDataSet()));
661 public boolean isRequiredPersonalDataSet () {
662 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
663 // Multiple registration page
664 return this.contactController.isRequiredPersonalDataSet();
666 // Single registration page
667 return (((this.getUserName() != null) || (!this.isUserNameRequired())) &&
668 (this.getUserProfileMode() != null) &&
669 (this.contactController.isRequiredPersonalDataSet()) &&
670 (this.getUserPassword() != null) &&
671 (this.getUserPasswordRepeat() != null));
676 public boolean isSamePasswordEntered () {
677 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
681 public boolean isUserIdEmpty () {
682 return ((this.getUserId() == null) || (this.getUserId() == 0));
686 public boolean isUserNameRegistered (final User user) {
687 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
691 public boolean isUserNameRequired () {
692 // Get context parameter
693 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_name_required"); //NOI18N
696 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
703 public boolean isVisibleUserFound () {
704 return ((this.visibleUserList instanceof List) && (!this.visibleUserList.isEmpty()));
708 public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException {
709 // Parameter must be valid
710 if (null == emailAddress) {
712 throw new NullPointerException("emailAddress is null"); //NOI18N
713 } else if (emailAddress.isEmpty()) {
715 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
721 // Try to lookup it in visible user list
722 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
724 User next = iterator.next();
726 // Contact should be set
727 if (next.getUserContact() == null) {
729 throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getUserId())); //NOI18N
730 } else if (next.getUserContact().getContactEmailAddress() == null) {
731 // Email address should be set
732 throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getUserId())); //NOI18N //NOI18N
735 // Is the email address found?
736 if (Objects.equals(next.getUserContact().getContactEmailAddress(), emailAddress)) {
737 // Copy to other variable
745 // Not visible for the current user
746 throw new UserEmailAddressNotFoundException(emailAddress);
754 public User lookupUserById (final Long userId) throws UserNotFoundException {
755 // Parameter must be valid
756 if (null == userId) {
758 throw new NullPointerException("userId is null"); //NOI18N
759 } else if (userId < 1) {
761 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
767 // Try to lookup it in visible user list
768 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
770 User next = iterator.next();
772 // Is the user id found?
773 if (Objects.equals(next.getUserId(), userId)) {
774 // Copy to other variable
782 // Not visible for the current user
783 throw new UserNotFoundException(userId);
791 * Adds user's name to bean's internal list. It also updates the public user
792 * list if the user has decided to have a public account,
794 * @param user User instance
796 private void addUserName (final User user) {
797 // Make sure the entry is not added yet
798 if (this.userNameList.contains(user.getUserName())) {
800 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
801 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
803 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
807 this.userNameList.add(user.getUserName());
813 private void clear () {
816 this.setUserId(null);
817 this.setUserProfileMode(null);
820 this.setUserName(null);
821 this.setUserPassword(null);
822 this.setUserPasswordRepeat(null);
826 * Copies given user into the controller
828 * @param user User instance
830 private void copyUser (final User user) {
831 // Make sure the instance is valid
834 throw new NullPointerException("user is null"); //NOI18N
835 } else if (user.getUserContact() == null) {
837 throw new NullPointerException("user.userContact is null"); //NOI18N
842 this.setUserId(user.getUserId());
843 this.setUserProfileMode(user.getUserProfileMode());
847 * Uniquely adds given user instance to user list. First an existing
848 * instance will be removed, then the given instance is being added.
850 * @param user User instance to add
852 private void uniqueAddUser (final User user) {
853 // Make sure the instance is valid
856 throw new NullPointerException("user is null"); //NOI18N
857 } else if (user.getUserContact() == null) {
859 throw new NullPointerException("user.userContact is null"); //NOI18N
862 // Get iterator from list
863 Iterator<User> iterator = this.userList.iterator();
865 // "Walk" through all entries
866 while (iterator.hasNext()) {
868 User next = iterator.next();
870 // Is id number the same?
871 if (Objects.equals(user.getUserId(), next.getUserId())) {
872 // Found entry, so remove it and abort
873 this.userList.remove(next);
878 // Add contact to list
879 this.userList.add(user);
883 * Updates list with given user instance
885 * @param user User instance
887 private void updateList (final User user) {
888 // The user should be valid
891 throw new NullPointerException("user is null"); //NOI18N
892 } else if (user.getUserId() == null) {
894 throw new NullPointerException("user.userId is null"); //NOI18N
895 } else if (user.getUserId() < 1) {
897 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
901 Iterator<User> iterator = this.userList.iterator();
904 while (iterator.hasNext()) {
906 User next = iterator.next();
908 // Is the same user id?
909 if (Objects.equals(user.getUserId(), next.getUserId())) {
910 // Found it, so remove it
911 this.userList.remove(next);
917 this.userList.add(user);