]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionBean.java
Continued a bit:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / user / PizzaUserWebSessionBean.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.pizzaapplication.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.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
37 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
38 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
39 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
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 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
53 import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController;
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 PizzaUserWebSessionBean implements PizzaUserWebSessionController {
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 PizzaContactWebSessionController contactController;
74
75         /**
76          * Login bean (controller)
77          */
78         @Inject
79         private PizzaUserLoginWebSessionController 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 PizzaUserLoginWebSessionController 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 PizzaUserWebSessionBean () {
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/PizzaService-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                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
303         }
304
305         @Override
306         public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
307                 // Check parameter
308                 if (null == event) {
309                         // Throw NPE
310                         throw new NullPointerException("event is null"); //NOI18N
311                 } else if (event.getUpdatedUser() == null) {
312                         // Throw NPE again
313                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
314                 } else if (event.getUpdatedUser().getUserId() == null) {
315                         // ... and again
316                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
317                 } else if (event.getUpdatedUser().getUserId() < 1) {
318                         // Invalid value
319                         throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
320                 }
321
322                 // All fine, so update list
323                 this.updateList(event.getUpdatedUser());
324         }
325
326         @Override
327         public List<User> allUsers () {
328                 // Return it
329                 return Collections.unmodifiableList(this.userList);
330         }
331
332         @Override
333         public List<User> allVisibleUsers () {
334                 // Return it
335                 return Collections.unmodifiableList(this.visibleUserList);
336         }
337
338         @Override
339         public User createUserInstance () {
340                 // Trace message
341                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
342
343                 // Required personal data must be set
344                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
345
346                 // Create new user instance
347                 User localUser = new LoginUser();
348
349                 // Update all data ...
350                 localUser.setUserName(this.getUserName());
351                 localUser.setUserProfileMode(this.getUserProfileMode());
352
353                 // Create contact instance
354                 Contact contact = this.contactController.createContactInstance();
355
356                 // Debug message
357                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
358
359                 // Set contact in user
360                 localUser.setUserContact(contact);
361
362                 // Trace message
363                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
364
365                 // Return it
366                 return localUser;
367         }
368
369         @Override
370         public User createUserLogin () {
371                 // Trace message
372                 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
373
374                 // Is all data set?
375                 if (this.getUserName() == null) {
376                         // Throw NPE
377                         throw new NullPointerException("recruiterName is null"); //NOI18N
378                 } else if (this.getUserName().isEmpty()) {
379                         // Is empty
380                         throw new IllegalStateException("recruiterName is empty."); //NOI18N
381                 }
382
383                 // Create new recruiter instance
384                 User recruiter = new LoginUser();
385
386                 // Update all data ...
387                 recruiter.setUserName(this.getUserName());
388
389                 // Trace message
390                 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: recruiter={1} - EXIT!", this.getClass().getSimpleName(), recruiter));
391
392                 // Return the new instance
393                 return recruiter;
394         }
395
396         @Override
397         public String doChangePersonalData () {
398                 // This method shall only be called if the user is logged-in
399                 if (!this.loginController.isUserLoggedIn()) {
400                         // Not logged-in
401                         throw new IllegalStateException("User is not logged-in"); //NOI18N
402                 } else if (!this.isRequiredChangePersonalDataSet()) {
403                         // Not all required fields are set
404                         throw new FaceletException("Not all required fields are set."); //NOI18N
405                 } else if (!this.loginController.ifCurrentPasswordMatches()) {
406                         // Password not matching
407                         throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
408                 }
409
410                 // Get user instance
411                 User user = this.loginController.getLoggedInUser();
412
413                 // Copy contact data to contact instance
414                 this.contactController.updateContactDataFromController(user.getUserContact());
415
416                 // It should be there, so run some tests on it
417                 assert (user instanceof User) : "Instance loginController.loggedInUser is null"; //NOI18N
418                 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null"; //NOI18N
419                 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
420                 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
421                 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
422                 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
423
424                 // Update all fields
425                 user.setUserProfileMode(this.getUserProfileMode());
426
427                 // Send it to the EJB
428                 User updatedUser = this.userBean.updateUserPersonalData(user);
429
430                 // Fire event
431                 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
432
433                 // All fine
434                 return "user_data_saved"; //NOI18N
435         }
436
437         @Override
438         public Long getUserId () {
439                 return this.userId;
440         }
441
442         @Override
443         public void setUserId (final Long userId) {
444                 this.userId = userId;
445         }
446
447         @Override
448         public String getUserName () {
449                 return this.userName;
450         }
451
452         @Override
453         public void setUserName (final String userName) {
454                 this.userName = userName;
455         }
456
457         @Override
458         public String getUserPassword () {
459                 return this.userPassword;
460         }
461
462         @Override
463         public void setUserPassword (final String userPassword) {
464                 this.userPassword = userPassword;
465         }
466
467         @Override
468         public String getUserPasswordRepeat () {
469                 return this.userPasswordRepeat;
470         }
471
472         @Override
473         public void setUserPasswordRepeat (final String userPasswordRepeat) {
474                 this.userPasswordRepeat = userPasswordRepeat;
475         }
476
477         @Override
478         public ProfileMode getUserProfileMode () {
479                 return this.userProfileMode;
480         }
481
482         @Override
483         public void setUserProfileMode (final ProfileMode userProfileMode) {
484                 this.userProfileMode = userProfileMode;
485         }
486
487         @Override
488         public boolean hasUsers () {
489                 return (!this.allUsers().isEmpty());
490         }
491
492         /**
493          * Post-initialization of this class
494          */
495         @PostConstruct
496         public void init () {
497                 // Initialize user list
498                 this.userList = this.userBean.allUsers();
499
500                 // Get full user name list for reducing EJB calls
501                 this.userNameList = this.userBean.getUserNameList();
502
503                 // Is the user logged-in?
504                 if (this.userLoginController.isUserLoggedIn()) {
505                         // Is logged-in, so load also users visible to memebers
506                         this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
507                 } else {
508                         // Initialize user list
509                         this.visibleUserList = this.userBean.allPublicUsers();
510                 }
511
512                 // Get all users
513                 List<User> allUsers = this.allUsers();
514
515                 // Get all contacts
516                 List<Contact> allContacts = this.contactController.allContacts();
517
518                 // Get iterator
519                 Iterator<Contact> iterator = allContacts.iterator();
520
521                 // Loop through it
522                 while (iterator.hasNext()) {
523                         // Get next element
524                         Contact next = iterator.next();
525
526                         // Get iterator
527                         Iterator<User> userIterator = allUsers.iterator();
528
529                         // Loop through all users
530                         while (userIterator.hasNext()) {
531                                 // Get user instance
532                                 User nextUser = userIterator.next();
533
534                                 // Is contact same?
535                                 if (Objects.equals(next, nextUser.getUserContact())) {
536                                         // Found same
537                                         iterator.remove();
538                                         break;
539                                 }
540                         }
541                 }
542
543                 // Set contact list
544                 this.selectableContacts = allContacts;
545         }
546
547         @Override
548         public boolean isContactFound (final Contact contact) {
549                 // The contact must be valid
550                 if (null == contact) {
551                         // Throw NPE
552                         throw new NullPointerException("contact is null"); //NOI18N
553                 } else if (contact.getContactId() == null) {
554                         // Throw again ...
555                         throw new NullPointerException("contact.contactId is null"); //NOI18N
556                 } else if (contact.getContactId() < 1) {
557                         // Not valid
558                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
559                 }
560
561                 // Default is not found
562                 boolean isFound = false;
563
564                 // Get iterator
565                 Iterator<User> iterator = this.allUsers().iterator();
566
567                 // Loop through all entries
568                 while (iterator.hasNext()) {
569                         // Get user
570                         User next = iterator.next();
571
572                         // Compare both objects
573                         if (Objects.equals(contact, next.getUserContact())) {
574                                 // Found it
575                                 isFound = true;
576                                 break;
577                         }
578                 }
579
580                 // Return status
581                 return isFound;
582         }
583
584         @Override
585         public boolean isRequiredChangePersonalDataSet () {
586                 return ((this.getUserProfileMode() != null) &&
587                                 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
588                                 (this.contactController.isRequiredChangePersonalDataSet()));
589         }
590
591         @Override
592         public boolean isRequiredPersonalDataSet () {
593                 return ((this.getUserName() != null) &&
594                                 (this.getUserProfileMode() != null) &&
595                                 (this.contactController.isRequiredPersonalDataSet()) &&
596                                 (this.getUserPassword() != null) &&
597                                 (this.getUserPasswordRepeat() != null));
598         }
599
600         @Override
601         public boolean isSamePasswordEntered () {
602                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
603         }
604
605         @Override
606         public boolean isUserIdEmpty () {
607                 return ((this.getUserId() == null) || (this.getUserId() == 0));
608         }
609
610         @Override
611         public boolean isUserNameRegistered (final User user) {
612                 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
613         }
614
615         @Override
616         public boolean isVisibleUserFound () {
617                 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
618         }
619
620         @Override
621         public User lookupUserById (final Long userId) throws UserNotFoundException {
622                 // Parameter must be valid
623                 if (null == userId) {
624                         // Throw NPE
625                         throw new NullPointerException("userId is null"); //NOI18N
626                 } else if (userId < 1) {
627                         // Not valid
628                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
629                 }
630
631                 // Init variable
632                 User user = null;
633
634                 // Try to lookup it in visible user list
635                 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
636                         // Get next user
637                         User next = iterator.next();
638
639                         // Is the user id found?
640                         if (Objects.equals(next.getUserId(), userId)) {
641                                 // Copy to other variable
642                                 user = next;
643                                 break;
644                         }
645                 }
646
647                 // Is it still null?
648                 if (null == user) {
649                         // Not visible for the current user
650                         throw new UserNotFoundException(userId);
651                 }
652
653                 // Return it
654                 return user;
655         }
656
657         @Override
658         public List<Contact> selectableContacts () {
659                 return Collections.unmodifiableList(this.selectableContacts);
660         }
661
662         /**
663          * Adds user's name to bean's internal list. It also updates the public user
664          * list if the user has decided to have a public account,
665          * <p>
666          * @param user User instance
667          */
668         private void addUserName (final User user) {
669                 // Make sure the entry is not added yet
670                 if (this.userNameList.contains(user.getUserName())) {
671                         // Abort here
672                         throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
673                 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
674                         // Already added
675                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
676                 }
677
678                 // Add user name
679                 this.userNameList.add(user.getUserName());
680         }
681
682         /**
683          * Clears this bean
684          */
685         private void clear () {
686                 // Clear all data
687                 // - personal data
688                 this.setUserId(null);
689                 this.setUserProfileMode(null);
690
691                 // - other data
692                 this.setUserName(null);
693                 this.setUserPassword(null);
694                 this.setUserPasswordRepeat(null);
695         }
696
697         /**
698          * Copies given user into the controller
699          * <p>
700          * @param user User instance
701          */
702         private void copyUser (final User user) {
703                 // Make sure the instance is valid
704                 if (null == user) {
705                         // Throw NPE
706                         throw new NullPointerException("user is null"); //NOI18N
707                 } else if (user.getUserContact() == null) {
708                         // Throw again ...
709                         throw new NullPointerException("user.userContact is null"); //NOI18N
710                 }
711
712                 // Copy all fields:
713                 // - base data
714                 this.setUserId(user.getUserId());
715                 this.setUserProfileMode(user.getUserProfileMode());
716         }
717
718         /**
719          * Updates list with given user instance
720          * <p>
721          * @param user User instance
722          */
723         private void updateList (final User user) {
724                 // The user should be valid
725                 if (null == user) {
726                         // Throw NPE
727                         throw new NullPointerException("user is null"); //NOI18N
728                 } else if (user.getUserId() == null) {
729                         // ... again NPE
730                         throw new NullPointerException("user.userId is null"); //NOI18N
731                 } else if (user.getUserId() < 1) {
732                         // Invalid id
733                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
734                 }
735
736                 // Get iterator
737                 Iterator<User> iterator = this.userList.iterator();
738
739                 // Look whole list
740                 while (iterator.hasNext()) {
741                         // Get next element
742                         User next = iterator.next();
743
744                         // Is the same user id?
745                         if (Objects.equals(user.getUserId(), next.getUserId())) {
746                                 // Found it, so remove it
747                                 this.userList.remove(next);
748                                 break;
749                         }
750                 }
751
752                 // Re-add item
753                 this.userList.add(user);
754         }
755
756 }