]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionBean.java
No, putting these methods into admin (request-scoped) controller is not good as no...
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / user / PizzaUserWebSessionBean.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.pizzaapplication.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.jcontacts.contact.Contact;
36 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
37 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
38 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
39 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
40 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
41 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
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.UserNotFoundException;
46 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
47 import org.mxchange.jusercore.model.user.LoginUser;
48 import org.mxchange.jusercore.model.user.User;
49 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
50 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
51 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
52 import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController;
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 PizzaUserWebSessionBean implements PizzaUserWebSessionController {
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 PizzaContactWebSessionController contactController;
73
74         /**
75          * Login bean (controller)
76          */
77         @Inject
78         private PizzaUserLoginWebSessionController 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 PizzaUserLoginWebSessionController 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 PizzaUserWebSessionBean () {
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/PizzaService-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                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
302         }
303
304         @Override
305         public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
306                 // Check parameter
307                 if (null == event) {
308                         // Throw NPE
309                         throw new NullPointerException("event is null"); //NOI18N
310                 } else if (event.getUpdatedUser() == null) {
311                         // Throw NPE again
312                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
313                 } else if (event.getUpdatedUser().getUserId() == null) {
314                         // ... and again
315                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
316                 } else if (event.getUpdatedUser().getUserId() < 1) {
317                         // Invalid value
318                         throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
319                 }
320
321                 // All fine, so update list
322                 this.updateList(event.getUpdatedUser());
323         }
324
325         @Override
326         public List<User> allUsers () {
327                 // Return it
328                 return Collections.unmodifiableList(this.userList);
329         }
330
331         @Override
332         public List<User> allVisibleUsers () {
333                 // Return it
334                 return Collections.unmodifiableList(this.visibleUserList);
335         }
336
337         @Override
338         public User createUserInstance () {
339                 // Trace message
340                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
341
342                 // Required personal data must be set
343                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
344
345                 // Create new user instance
346                 User localUser = new LoginUser();
347
348                 // Update all data ...
349                 localUser.setUserName(this.getUserName());
350                 localUser.setUserProfileMode(this.getUserProfileMode());
351
352                 // Create contact instance
353                 Contact contact = this.contactController.createContactInstance();
354
355                 // Debug message
356                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
357
358                 // Set contact in user
359                 localUser.setUserContact(contact);
360
361                 // Trace message
362                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
363
364                 // Return it
365                 return localUser;
366         }
367
368         @Override
369         public String doChangePersonalData () {
370                 // This method shall only be called if the user is logged-in
371                 if (!this.loginController.isUserLoggedIn()) {
372                         // Not logged-in
373                         throw new IllegalStateException("User is not logged-in"); //NOI18N
374                 } else if (!this.isRequiredChangePersonalDataSet()) {
375                         // Not all required fields are set
376                         throw new FaceletException("Not all required fields are set."); //NOI18N
377                 } else if (!this.loginController.ifCurrentPasswordMatches()) {
378                         // Password not matching
379                         throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
380                 }
381
382                 // Get user instance
383                 User user = this.loginController.getLoggedInUser();
384
385                 // Copy contact data to contact instance
386                 this.contactController.updateContactDataFromController(user.getUserContact());
387
388                 // It should be there, so run some tests on it
389                 assert (user instanceof User) : "Instance loginController.loggedInUser is null"; //NOI18N
390                 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null"; //NOI18N
391                 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
392                 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
393                 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
394                 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
395
396                 // Update all fields
397                 user.setUserProfileMode(this.getUserProfileMode());
398
399                 // Send it to the EJB
400                 User updatedUser = this.userBean.updateUserPersonalData(user);
401
402                 // Fire event
403                 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
404
405                 // All fine
406                 return "user_data_saved"; //NOI18N
407         }
408
409         @Override
410         public Long getUserId () {
411                 return this.userId;
412         }
413
414         @Override
415         public void setUserId (final Long userId) {
416                 this.userId = userId;
417         }
418
419         @Override
420         public String getUserName () {
421                 return this.userName;
422         }
423
424         @Override
425         public void setUserName (final String userName) {
426                 this.userName = userName;
427         }
428
429         @Override
430         public String getUserPassword () {
431                 return this.userPassword;
432         }
433
434         @Override
435         public void setUserPassword (final String userPassword) {
436                 this.userPassword = userPassword;
437         }
438
439         @Override
440         public String getUserPasswordRepeat () {
441                 return this.userPasswordRepeat;
442         }
443
444         @Override
445         public void setUserPasswordRepeat (final String userPasswordRepeat) {
446                 this.userPasswordRepeat = userPasswordRepeat;
447         }
448
449         @Override
450         public ProfileMode getUserProfileMode () {
451                 return this.userProfileMode;
452         }
453
454         @Override
455         public void setUserProfileMode (final ProfileMode userProfileMode) {
456                 this.userProfileMode = userProfileMode;
457         }
458
459         @Override
460         public boolean hasUsers () {
461                 return (!this.allUsers().isEmpty());
462         }
463
464         /**
465          * Post-initialization of this class
466          */
467         @PostConstruct
468         public void init () {
469                 // Initialize user list
470                 this.userList = this.userBean.allUsers();
471
472                 // Get full user name list for reducing EJB calls
473                 this.userNameList = this.userBean.getUserNameList();
474
475                 // Is the user logged-in?
476                 if (this.userLoginController.isUserLoggedIn()) {
477                         // Is logged-in, so load also users visible to memebers
478                         this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
479                 } else {
480                         // Initialize user list
481                         this.visibleUserList = this.userBean.allPublicUsers();
482                 }
483
484                 // Get all users
485                 List<User> allUsers = this.allUsers();
486
487                 // Get all contacts
488                 List<Contact> allContacts = this.contactController.allContacts();
489
490                 // Get iterator
491                 Iterator<Contact> iterator = allContacts.iterator();
492
493                 // Loop through it
494                 while (iterator.hasNext()) {
495                         // Get next element
496                         Contact next = iterator.next();
497
498                         // Get iterator
499                         Iterator<User> userIterator = allUsers.iterator();
500
501                         // Loop through all users
502                         while (userIterator.hasNext()) {
503                                 // Get user instance
504                                 User nextUser = userIterator.next();
505
506                                 // Is contact same?
507                                 if (Objects.equals(next, nextUser.getUserContact())) {
508                                         // Found same
509                                         iterator.remove();
510                                         break;
511                                 }
512                         }
513                 }
514
515                 // Set contact list
516                 this.selectableContacts = allContacts;
517         }
518
519         @Override
520         public boolean isContactFound (final Contact contact) {
521                 // The contact must be valid
522                 if (null == contact) {
523                         // Throw NPE
524                         throw new NullPointerException("contact is null"); //NOI18N
525                 } else if (contact.getContactId() == null) {
526                         // Throw again ...
527                         throw new NullPointerException("contact.contactId is null"); //NOI18N
528                 } else if (contact.getContactId() < 1) {
529                         // Not valid
530                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
531                 }
532
533                 // Default is not found
534                 boolean isFound = false;
535
536                 // Get iterator
537                 Iterator<User> iterator = this.allUsers().iterator();
538
539                 // Loop through all entries
540                 while (iterator.hasNext()) {
541                         // Get user
542                         User next = iterator.next();
543
544                         // Compare both objects
545                         if (Objects.equals(contact, next.getUserContact())) {
546                                 // Found it
547                                 isFound = true;
548                                 break;
549                         }
550                 }
551
552                 // Return status
553                 return isFound;
554         }
555
556         @Override
557         public boolean isRequiredChangePersonalDataSet () {
558                 return ((this.getUserProfileMode() != null) &&
559                                 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
560                                 (this.contactController.isRequiredChangePersonalDataSet()));
561         }
562
563         @Override
564         public boolean isRequiredPersonalDataSet () {
565                 return ((this.getUserName() != null) &&
566                                 (this.getUserProfileMode() != null) &&
567                                 (this.contactController.isRequiredPersonalDataSet()) &&
568                                 (this.getUserPassword() != null) &&
569                                 (this.getUserPasswordRepeat() != null));
570         }
571
572         @Override
573         public boolean isSamePasswordEntered () {
574                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
575         }
576
577         @Override
578         public boolean isUserIdEmpty () {
579                 return ((this.getUserId() == null) || (this.getUserId() == 0));
580         }
581
582         @Override
583         public boolean isUserNameRegistered (final User user) {
584                 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
585         }
586
587         @Override
588         public boolean isVisibleUserFound () {
589                 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
590         }
591
592         @Override
593         public User lookupUserById (final Long userId) throws UserNotFoundException {
594                 // Parameter must be valid
595                 if (null == userId) {
596                         // Throw NPE
597                         throw new NullPointerException("userId is null"); //NOI18N
598                 } else if (userId < 1) {
599                         // Not valid
600                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
601                 }
602
603                 // Init variable
604                 User user = null;
605
606                 // Try to lookup it in visible user list
607                 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
608                         // Get next user
609                         User next = iterator.next();
610
611                         // Is the user id found?
612                         if (Objects.equals(next.getUserId(), userId)) {
613                                 // Copy to other variable
614                                 user = next;
615                                 break;
616                         }
617                 }
618
619                 // Is it still null?
620                 if (null == user) {
621                         // Not visible for the current user
622                         throw new UserNotFoundException(userId);
623                 }
624
625                 // Return it
626                 return user;
627         }
628
629         @Override
630         public List<Contact> selectableContacts () {
631                 return Collections.unmodifiableList(this.selectableContacts);
632         }
633
634         /**
635          * Adds user's name to bean's internal list. It also updates the public user
636          * list if the user has decided to have a public account,
637          * <p>
638          * @param user User instance
639          */
640         private void addUserName (final User user) {
641                 // Make sure the entry is not added yet
642                 if (this.userNameList.contains(user.getUserName())) {
643                         // Abort here
644                         throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
645                 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
646                         // Already added
647                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
648                 }
649
650                 // Add user name
651                 this.userNameList.add(user.getUserName());
652         }
653
654         /**
655          * Clears this bean
656          */
657         private void clear () {
658                 // Clear all data
659                 // - personal data
660                 this.setUserId(null);
661                 this.setUserProfileMode(null);
662
663                 // - other data
664                 this.setUserName(null);
665                 this.setUserPassword(null);
666                 this.setUserPasswordRepeat(null);
667         }
668
669         /**
670          * Copies given user into the controller
671          * <p>
672          * @param user User instance
673          */
674         private void copyUser (final User user) {
675                 // Copy all fields:
676                 // - base data
677                 this.setUserId(user.getUserId());
678                 this.setUserProfileMode(user.getUserProfileMode());
679
680                 // Get cellphone, phone and fax instance
681                 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
682                 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
683                 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();
684         }
685
686         /**
687          * Updates list with given user instance
688          * <p>
689          * @param user User instance
690          */
691         private void updateList (final User user) {
692                 // The user should be valid
693                 if (null == user) {
694                         // Throw NPE
695                         throw new NullPointerException("user is null"); //NOI18N
696                 } else if (user.getUserId() == null) {
697                         // ... again NPE
698                         throw new NullPointerException("user.userId is null"); //NOI18N
699                 } else if (user.getUserId() < 1) {
700                         // Invalid id
701                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
702                 }
703
704                 // Get iterator
705                 Iterator<User> iterator = this.userList.iterator();
706
707                 // Look whole list
708                 while (iterator.hasNext()) {
709                         // Get next element
710                         User next = iterator.next();
711
712                         // Is the same user id?
713                         if (Objects.equals(user.getUserId(), next.getUserId())) {
714                                 // Found it, so remove it
715                                 this.userList.remove(next);
716                                 break;
717                         }
718                 }
719
720                 // Re-add item
721                 this.userList.add(user);
722         }
723
724 }