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