]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java
No, putting these methods into admin (request-scoped) controller is not good as no...
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / user / AddressbookUserWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.beans.user;
18
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.SessionScoped;
26 import javax.enterprise.event.Event;
27 import javax.enterprise.event.Observes;
28 import javax.enterprise.inject.Any;
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.addressbook.beans.contact.AddressbookContactWebSessionController;
36 import org.mxchange.addressbook.beans.login.AddressbookUserLoginWebSessionController;
37 import org.mxchange.jcontacts.contact.Contact;
38 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
39 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
40 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
41 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
42 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
43 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
44 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
45 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
46 import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
47 import org.mxchange.jusercore.exceptions.UserNotFoundException;
48 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
49 import org.mxchange.jusercore.model.user.LoginUser;
50 import org.mxchange.jusercore.model.user.User;
51 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
52 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
53
54 /**
55  * A user bean (controller)
56  * <p>
57  * @author Roland Haeder<roland@mxchange.org>
58  */
59 @Named ("userController")
60 @SessionScoped
61 public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionController {
62
63         /**
64          * Serial number
65          */
66         private static final long serialVersionUID = 542_145_347_916L;
67
68         /**
69          * General contact controller
70          */
71         @Inject
72         private AddressbookContactWebSessionController contactController;
73
74         /**
75          * Login bean (controller)
76          */
77         @Inject
78         private AddressbookUserLoginWebSessionController loginController;
79
80         /**
81          * A list of all selectable contacts
82          */
83         private List<Contact> selectableContacts;
84
85         /**
86          * Event being fired when user updated personal data
87          */
88         @Inject
89         @Any
90         private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
91
92         /**
93          * Remote user bean
94          */
95         private final UserSessionBeanRemote userBean;
96
97         /**
98          * User id
99          */
100         private Long userId;
101
102         /**
103          * A list of all user profiles
104          */
105         private List<User> userList;
106
107         /**
108          * Login bean (controller)
109          */
110         @Inject
111         private AddressbookUserLoginWebSessionController userLoginController;
112
113         /**
114          * User name
115          */
116         private String userName;
117
118         /**
119          * User name list
120          */
121         private List<String> userNameList;
122
123         /**
124          * User password (unencrypted from web form)
125          */
126         private String userPassword;
127
128         /**
129          * User password repeated (unencrypted from web form)
130          */
131         private String userPasswordRepeat;
132
133         /**
134          * Whether the user wants a public profile
135          */
136         private ProfileMode userProfileMode;
137
138         /**
139          * A list of all public user profiles
140          */
141         private List<User> visibleUserList;
142
143         /**
144          * Default constructor
145          */
146         public AddressbookUserWebSessionBean () {
147                 // Try it
148                 try {
149                         // Get initial context
150                         Context context = new InitialContext();
151
152                         // Try to lookup
153                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
154                 } catch (final NamingException e) {
155                         // Throw again
156                         throw new FaceletException(e);
157                 }
158         }
159
160         @Override
161         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
162                 // Trace message
163                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
164
165                 // event should not be null
166                 if (null == event) {
167                         // Throw NPE
168                         throw new NullPointerException("event is null"); //NOI18N
169                 } else if (event.getAddedUser() == null) {
170                         // Throw NPE again
171                         throw new NullPointerException("event.addedUser is null"); //NOI18N
172                 } else if (event.getAddedUser().getUserId() == null) {
173                         // userId is null
174                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
175                 } else if (event.getAddedUser().getUserId() < 1) {
176                         // Not avalid id
177                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
178                 }
179
180                 // Add user to local list
181                 this.userList.add(event.getAddedUser());
182
183                 // Clear all data
184                 this.clear();
185
186                 // Set user id again
187                 this.setUserId(event.getAddedUser().getUserId());
188
189                 // Trace message
190                 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
191         }
192
193         @Override
194         public void afterAdminUpdatedUserDataEvent (@Observes final AdminUpdatedUserDataEvent event) {
195                 // Trace message
196                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminUpdatedUserEvent: event={0} - CALLED!", event)); //NOI18N
197
198                 // event should not be null
199                 if (null == event) {
200                         // Throw NPE
201                         throw new NullPointerException("event is null"); //NOI18N
202                 } else if (event.getUpdatedUser() == null) {
203                         // Throw NPE again
204                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
205                 } else if (event.getUpdatedUser().getUserId() == null) {
206                         // userId is null
207                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
208                 } else if (event.getUpdatedUser().getUserId() < 1) {
209                         // Not avalid id
210                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
211                 }
212
213                 // Update list
214                 this.updateList(event.getUpdatedUser());
215
216                 // Clear all data
217                 this.clear();
218
219                 // Trace message
220                 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminUpdatedUserEvent: EXIT!"); //NOI18N
221         }
222
223         @Override
224         public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
225                 // Trace message
226                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
227
228                 // event should not be null
229                 if (null == event) {
230                         // Throw NPE
231                         throw new NullPointerException("event is null"); //NOI18N
232                 } else if (event.getRegisteredUser() == null) {
233                         // Throw NPE again
234                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
235                 } else if (event.getRegisteredUser().getUserId() == null) {
236                         // userId is null
237                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
238                 } else if (event.getRegisteredUser().getUserId() < 1) {
239                         // Not avalid id
240                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
241                 }
242
243                 // Get user instance
244                 User registeredUser = event.getRegisteredUser();
245
246                 // Debug message
247                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
248
249                 // Copy all data from registered->user
250                 this.copyUser(registeredUser);
251
252                 // Clear all data
253                 this.clear();
254
255                 // Add user to local list
256                 this.userList.add(registeredUser);
257
258                 // Add user name
259                 this.addUserName(registeredUser);
260
261                 // Is the account public?
262                 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
263                         // Also add it to this list
264                         this.visibleUserList.add(registeredUser);
265                 }
266
267                 // Set user id again
268                 this.setUserId(registeredUser.getUserId());
269
270                 // Trace message
271                 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
272         }
273
274         @Override
275         public void afterUserLogin (final @Observes UserLoggedInEvent event) {
276                 // Trace message
277                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
278
279                 // event should not be null
280                 if (null == event) {
281                         // Throw NPE
282                         throw new NullPointerException("event is null"); //NOI18N
283                 } else if (event.getLoggedInUser() == null) {
284                         // Throw NPE again
285                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
286                 } else if (event.getLoggedInUser().getUserId() == null) {
287                         // userId is null
288                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
289                 } else if (event.getLoggedInUser().getUserId() < 1) {
290                         // Not avalid id
291                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
292                 }
293
294                 // Copy all data to this bean
295                 this.copyUser(event.getLoggedInUser());
296
297                 // Re-initialize list
298                 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
299
300                 // Trace message
301         }
302
303         @Override
304         public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
305                 // Check parameter
306                 if (null == event) {
307                         // Throw NPE
308                         throw new NullPointerException("event is null"); //NOI18N
309                 } else if (event.getUpdatedUser() == null) {
310                         // Throw NPE again
311                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
312                 } else if (event.getUpdatedUser().getUserId() == null) {
313                         // ... and again
314                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
315                 } else if (event.getUpdatedUser().getUserId() < 1) {
316                         // Invalid value
317                         throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
318                 }
319
320                 // All fine, so update list
321                 this.updateList(event.getUpdatedUser());
322         }
323
324         @Override
325         public List<User> allUsers () {
326                 // Return it
327                 return Collections.unmodifiableList(this.userList);
328         }
329
330         @Override
331         public List<User> allVisibleUsers () {
332                 // Return it
333                 return Collections.unmodifiableList(this.visibleUserList);
334         }
335
336         @Override
337         public User createUserInstance () {
338                 // Trace message
339                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
340
341                 // Required personal data must be set
342                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
343
344                 // Create new user instance
345                 User localUser = new LoginUser();
346
347                 // Update all data ...
348                 localUser.setUserName(this.getUserName());
349                 localUser.setUserProfileMode(this.getUserProfileMode());
350
351                 // Create contact instance
352                 Contact contact = this.contactController.createContactInstance();
353
354                 // Debug message
355                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
356
357                 // Set contact in user
358                 localUser.setUserContact(contact);
359
360                 // Trace message
361                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
362
363                 // Return it
364                 return localUser;
365         }
366
367         @Override
368         public String doChangePersonalData () {
369                 // This method shall only be called if the user is logged-in
370                 if (!this.loginController.isUserLoggedIn()) {
371                         // Not logged-in
372                         throw new IllegalStateException("User is not logged-in"); //NOI18N
373                 } else if (!this.isRequiredChangePersonalDataSet()) {
374                         // Not all required fields are set
375                         throw new FaceletException("Not all required fields are set."); //NOI18N
376                 } else if (!this.loginController.ifCurrentPasswordMatches()) {
377                         // Password not matching
378                         throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
379                 }
380
381                 // Get user instance
382                 User user = this.loginController.getLoggedInUser();
383
384                 // Copy contact data to contact instance
385                 this.contactController.updateContactDataFromController(user.getUserContact());
386
387                 // It should be there, so run some tests on it
388                 assert (user instanceof User) : "Instance loginController.loggedInUser is null";
389                 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null";
390                 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId());
391                 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null";
392                 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null";
393                 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId());
394
395                 // Update all fields
396                 user.setUserProfileMode(this.getUserProfileMode());
397
398                 // Send it to the EJB
399                 User updatedUser = this.userBean.updateUserPersonalData(user);
400
401                 // Fire event
402                 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
403
404                 // All fine
405                 return "user_data_saved"; //NOI18N
406         }
407
408         @Override
409         public Long getUserId () {
410                 return this.userId;
411         }
412
413         @Override
414         public void setUserId (final Long userId) {
415                 this.userId = userId;
416         }
417
418         @Override
419         public String getUserName () {
420                 return this.userName;
421         }
422
423         @Override
424         public void setUserName (final String userName) {
425                 this.userName = userName;
426         }
427
428         @Override
429         public String getUserPassword () {
430                 return this.userPassword;
431         }
432
433         @Override
434         public void setUserPassword (final String userPassword) {
435                 this.userPassword = userPassword;
436         }
437
438         @Override
439         public String getUserPasswordRepeat () {
440                 return this.userPasswordRepeat;
441         }
442
443         @Override
444         public void setUserPasswordRepeat (final String userPasswordRepeat) {
445                 this.userPasswordRepeat = userPasswordRepeat;
446         }
447
448         @Override
449         public ProfileMode getUserProfileMode () {
450                 return this.userProfileMode;
451         }
452
453         @Override
454         public void setUserProfileMode (final ProfileMode userProfileMode) {
455                 this.userProfileMode = userProfileMode;
456         }
457
458         @Override
459         public boolean hasUsers () {
460                 return (!this.allUsers().isEmpty());
461         }
462
463         /**
464          * Post-initialization of this class
465          */
466         @PostConstruct
467         public void init () {
468                 // Initialize user list
469                 this.userList = this.userBean.allUsers();
470
471                 // Get full user name list for reducing EJB calls
472                 this.userNameList = this.userBean.getUserNameList();
473
474                 // Is the user logged-in?
475                 if (this.userLoginController.isUserLoggedIn()) {
476                         // Is logged-in, so load also users visible to memebers
477                         this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
478                 } else {
479                         // Initialize user list
480                         this.visibleUserList = this.userBean.allPublicUsers();
481                 }
482
483                 // Get all users
484                 List<User> allUsers = this.allUsers();
485
486                 // Get all contacts
487                 List<Contact> allContacts = this.contactController.allContacts();
488
489                 // Get iterator
490                 Iterator<Contact> iterator = allContacts.iterator();
491
492                 // Loop through it
493                 while (iterator.hasNext()) {
494                         // Get next element
495                         Contact next = iterator.next();
496
497                         // Get iterator
498                         Iterator<User> userIterator = allUsers.iterator();
499
500                         // Loop through all users
501                         while (userIterator.hasNext()) {
502                                 // Get user instance
503                                 User nextUser = userIterator.next();
504
505                                 // Is contact same?
506                                 if (Objects.equals(next, nextUser.getUserContact())) {
507                                         // Found same
508                                         iterator.remove();
509                                         break;
510                                 }
511                         }
512                 }
513
514                 // Set contact list
515                 this.selectableContacts = allContacts;
516         }
517
518         @Override
519         public boolean isContactFound (final Contact contact) {
520                 // The contact must be valid
521                 if (null == contact) {
522                         // Throw NPE
523                         throw new NullPointerException("contact is null"); //NOI18N
524                 } else if (contact.getContactId() == null) {
525                         // Throw again ...
526                         throw new NullPointerException("contact.contactId is null"); //NOI18N
527                 } else if (contact.getContactId() < 1) {
528                         // Not valid
529                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
530                 }
531
532                 // Default is not found
533                 boolean isFound = false;
534
535                 // Get iterator
536                 Iterator<User> iterator = this.allUsers().iterator();
537
538                 // Loop through all entries
539                 while (iterator.hasNext()) {
540                         // Get user
541                         User next = iterator.next();
542
543                         // Compare both objects
544                         if (Objects.equals(contact, next.getUserContact())) {
545                                 // Found it
546                                 isFound = true;
547                                 break;
548                         }
549                 }
550
551                 // Return status
552                 return isFound;
553         }
554
555         @Override
556         public boolean isRequiredChangePersonalDataSet () {
557                 return ((this.getUserProfileMode() != null) &&
558                                 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
559                                 (this.contactController.isRequiredChangePersonalDataSet()));
560         }
561
562         @Override
563         public boolean isRequiredPersonalDataSet () {
564                 return ((this.getUserName() != null) &&
565                                 (this.getUserProfileMode() != null) &&
566                                 (this.contactController.isRequiredPersonalDataSet()) &&
567                                 (this.getUserPassword() != null) &&
568                                 (this.getUserPasswordRepeat() != null));
569         }
570
571         @Override
572         public boolean isSamePasswordEntered () {
573                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
574         }
575
576         @Override
577         public boolean isUserIdEmpty () {
578                 return ((this.getUserId() == null) || (this.getUserId() == 0));
579         }
580
581         @Override
582         public boolean isUserNameRegistered (final User user) {
583                 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
584         }
585
586         @Override
587         public boolean isVisibleUserFound () {
588                 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
589         }
590
591         @Override
592         public User lookupUserById (final Long userId) throws UserNotFoundException {
593                 // Parameter must be valid
594                 if (null == userId) {
595                         // Throw NPE
596                         throw new NullPointerException("userId is null"); //NOI18N
597                 } else if (userId < 1) {
598                         // Not valid
599                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
600                 }
601
602                 // Init variable
603                 User user = null;
604
605                 // Try to lookup it in visible user list
606                 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
607                         // Get next user
608                         User next = iterator.next();
609
610                         // Is the user id found?
611                         if (Objects.equals(next.getUserId(), userId)) {
612                                 // Copy to other variable
613                                 user = next;
614                                 break;
615                         }
616                 }
617
618                 // Is it still null?
619                 if (null == user) {
620                         // Not visible for the current user
621                         throw new UserNotFoundException(userId);
622                 }
623
624                 // Return it
625                 return user;
626         }
627
628         @Override
629         public List<Contact> selectableContacts () {
630                 return Collections.unmodifiableList(this.selectableContacts);
631         }
632
633         /**
634          * Adds user's name to bean's internal list. It also updates the public user
635          * list if the user has decided to have a public account,
636          * <p>
637          * @param user User instance
638          */
639         private void addUserName (final User user) {
640                 // Make sure the entry is not added yet
641                 if (this.userNameList.contains(user.getUserName())) {
642                         // Abort here
643                         throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
644                 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
645                         // Already added
646                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
647                 }
648
649                 // Add user name
650                 this.userNameList.add(user.getUserName());
651         }
652
653         /**
654          * Clears this bean
655          */
656         private void clear () {
657                 // Clear all data
658                 // - personal data
659                 this.setUserId(null);
660                 this.setUserProfileMode(null);
661
662                 // - other data
663                 this.setUserName(null);
664                 this.setUserPassword(null);
665                 this.setUserPasswordRepeat(null);
666         }
667
668         /**
669          * Copies given user into the controller
670          * <p>
671          * @param user User instance
672          */
673         private void copyUser (final User user) {
674                 // Copy all fields:
675                 // - base data
676                 this.setUserId(user.getUserId());
677                 this.setUserProfileMode(user.getUserProfileMode());
678
679                 // Get cellphone, phone and fax instance
680                 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
681                 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
682                 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();
683         }
684
685         /**
686          * Updates list with given user instance
687          * <p>
688          * @param user User instance
689          */
690         private void updateList (final User user) {
691                 // The user should be valid
692                 if (null == user) {
693                         // Throw NPE
694                         throw new NullPointerException("user is null"); //NOI18N
695                 } else if (user.getUserId() == null) {
696                         // ... again NPE
697                         throw new NullPointerException("user.userId is null"); //NOI18N
698                 } else if (user.getUserId() < 1) {
699                         // Invalid id
700                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
701                 }
702
703                 // Get iterator
704                 Iterator<User> iterator = this.userList.iterator();
705
706                 // Look whole list
707                 while (iterator.hasNext()) {
708                         // Get next element
709                         User next = iterator.next();
710
711                         // Is the same user id?
712                         if (Objects.equals(user.getUserId(), next.getUserId())) {
713                                 // Found it, so remove it
714                                 this.userList.remove(next);
715                                 break;
716                         }
717                 }
718
719                 // Re-add item
720                 this.userList.add(user);
721         }
722
723 }