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