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