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