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