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