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