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