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