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