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