]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebRequestBean.java
Please rename/cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / user / PizzaUserWebRequestBean.java
1 /*
2  * Copyright (C) 2016, 2017 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.LinkedList;
22 import java.util.List;
23 import java.util.Locale;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.ejb.EJB;
27 import javax.enterprise.context.SessionScoped;
28 import javax.enterprise.event.Event;
29 import javax.enterprise.event.Observes;
30 import javax.enterprise.inject.Any;
31 import javax.faces.context.FacesContext;
32 import javax.faces.view.facelets.FaceletException;
33 import javax.inject.Inject;
34 import javax.inject.Named;
35 import org.mxchange.jcontacts.model.contact.Contact;
36 import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent;
37 import org.mxchange.jfinancials.beans.user.PizzaUserWebRequestController;
38 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
39 import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent;
40 import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent;
41 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
42 import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent;
43 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
44 import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent;
45 import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent;
46 import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent;
47 import org.mxchange.jusercore.events.user.update.ObservableUpdatedUserPersonalDataEvent;
48 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
49 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
50 import org.mxchange.jusercore.exceptions.UserNotFoundException;
51 import org.mxchange.jusercore.model.user.LoginUser;
52 import org.mxchange.jusercore.model.user.User;
53 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
54 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
55 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
56 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
57 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
58 import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
59 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
60 import org.mxchange.juserlogincore.login.UserLoginUtils;
61 import org.mxchange.pizzaapplication.beans.BasePizzaController;
62 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebRequestController;
63 import org.mxchange.pizzaapplication.beans.features.PizzaFeaturesWebApplicationController;
64 import org.mxchange.pizzaapplication.beans.localization.PizzaLocalizationSessionController;
65 import org.mxchange.pizzaapplication.beans.user.login.PizzaUserLoginWebSessionController;
66
67 /**
68  * A user bean (controller)
69  * <p>
70  * @author Roland Häder<roland@mxchange.org>
71  */
72 @Named ("userController")
73 @SessionScoped
74 public class PizzaUserWebRequestBean extends BasePizzaController implements PizzaUserWebRequestController {
75
76         /**
77          * Serial number
78          */
79         private static final long serialVersionUID = 542_145_347_916L;
80
81         /**
82          * General contact controller
83          */
84         @Inject
85         private PizzaContactWebRequestController contactController;
86
87         /**
88          * Features controller
89          */
90         @Inject
91         private PizzaFeaturesWebApplicationController featureController;
92
93         /**
94          * Locale instance
95          */
96         private Locale locale;
97
98         /**
99          * Localization controller
100          */
101         @Inject
102         private PizzaLocalizationSessionController localizationController;
103
104         /**
105          * Event being fired when user updated personal data
106          */
107         @Inject
108         @Any
109         private Event<ObservableUpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
110
111         /**
112          * Remote user bean
113          */
114         @EJB (lookup = "java:global/jfinancials-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
115         private UserSessionBeanRemote userBean;
116
117         /**
118          * User id
119          */
120         private Long userId;
121
122         /**
123          * A list of all user profiles
124          */
125         @Inject
126         @Cached (cacheName = "userCache")
127         private transient Cache<Long, User> userCache;
128
129         /**
130          * Login controller (bean)
131          */
132         @Inject
133         private PizzaUserLoginWebSessionController userLoginController;
134
135         /**
136          * User name
137          */
138         private String userName;
139
140         /**
141          * User name list
142          */
143         @Inject
144         @Cached (cacheName = "userNameCache")
145         private transient Cache<Long, String> userNameCache;
146
147         /**
148          * User password (clear-text from web form)
149          */
150         private String userPassword;
151
152         /**
153          * User password repeated (clear-text from web form)
154          */
155         private String userPasswordRepeat;
156
157         /**
158          * Whether the user wants a public profile
159          */
160         private ProfileMode userProfileMode;
161
162         /**
163          * Default constructor
164          */
165         public PizzaUserWebRequestBean () {
166                 // Call super constructor
167                 super();
168         }
169
170         /**
171          * Event observer for newly added users by administrator
172          * <p>
173          * @param event Event being fired
174          */
175         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
176                 // event should not be null
177                 if (null == event) {
178                         // Throw NPE
179                         throw new NullPointerException("event is null"); //NOI18N
180                 } else if (event.getAddedUser() == null) {
181                         // Throw NPE again
182                         throw new NullPointerException("event.addedUser is null"); //NOI18N
183                 } else if (event.getAddedUser().getUserId() == null) {
184                         // userId is null
185                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
186                 } else if (event.getAddedUser().getUserId() < 1) {
187                         // Not avalid id
188                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
189                 }
190
191                 // Update user list
192                 this.updateList(event.getAddedUser());
193
194                 // Clear all data
195                 this.clear();
196
197                 // Set user id again
198                 this.setUserId(event.getAddedUser().getUserId());
199         }
200
201         /**
202          * Event observer for deleted user accounts (by administrator)
203          * <p>
204          * @param event Event being fired
205          */
206         public void afterAdminDeletedUserEvent (@Observes final ObservableAdminDeletedUserEvent event) {
207                 // event should not be null
208                 if (null == event) {
209                         // Throw NPE
210                         throw new NullPointerException("event is null"); //NOI18N
211                 } else if (event.getDeletedUser() == null) {
212                         // Throw NPE again
213                         throw new NullPointerException("event.deletedUser is null"); //NOI18N
214                 } else if (event.getDeletedUser().getUserId() == null) {
215                         // userId is null
216                         throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N
217                 } else if (event.getDeletedUser().getUserId() < 1) {
218                         // Not avalid id
219                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N
220                 }
221
222                 // Update user list
223                 this.removeFromList(event.getDeletedUser());
224
225                 // Clear all data
226                 this.clear();
227         }
228
229         /**
230          * Event observer for linked users with existing contact data
231          * <p>
232          * @param event Event being fired
233          */
234         public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
235                 // event should not be null
236                 if (null == event) {
237                         // Throw NPE
238                         throw new NullPointerException("event is null"); //NOI18N
239                 } else if (event.getLinkedUser() == null) {
240                         // Throw NPE again
241                         throw new NullPointerException("event.linkedUser is null"); //NOI18N
242                 } else if (event.getLinkedUser().getUserId() == null) {
243                         // userId is null
244                         throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
245                 } else if (event.getLinkedUser().getUserId() < 1) {
246                         // Not avalid id
247                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
248                 }
249
250                 // Update user list
251                 this.updateList(event.getLinkedUser());
252
253                 // Clear all data
254                 this.clear();
255
256                 // Set user id again
257                 this.setUserId(event.getLinkedUser().getUserId());
258         }
259
260         /**
261          * Event observer for locked users
262          * <p>
263          * @param event Event being fired
264          */
265         public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) {
266                 // event should not be null
267                 if (null == event) {
268                         // Throw NPE
269                         throw new NullPointerException("event is null"); //NOI18N
270                 } else if (event.getLockedUser() == null) {
271                         // Throw NPE again
272                         throw new NullPointerException("event.lockedUser is null"); //NOI18N
273                 } else if (event.getLockedUser().getUserId() == null) {
274                         // userId is null
275                         throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
276                 } else if (event.getLockedUser().getUserId() < 1) {
277                         // Not avalid id
278                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
279                 }
280
281                 // Update user list
282                 this.updateList(event.getLockedUser());
283         }
284
285         /**
286          * Event observer for unlocked users
287          * <p>
288          * @param event Event being fired
289          */
290         public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) {
291                 // event should not be null
292                 if (null == event) {
293                         // Throw NPE
294                         throw new NullPointerException("event is null"); //NOI18N
295                 } else if (event.getUnlockedUser() == null) {
296                         // Throw NPE again
297                         throw new NullPointerException("event.unlockedUser is null"); //NOI18N
298                 } else if (event.getUnlockedUser().getUserId() == null) {
299                         // userId is null
300                         throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
301                 } else if (event.getUnlockedUser().getUserId() < 1) {
302                         // Not avalid id
303                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
304                 }
305
306                 // Update user list
307                 this.updateList(event.getUnlockedUser());
308         }
309
310         /**
311          * Event observer for updated user data by administrator
312          * <p>
313          * @param event Event being updated
314          */
315         public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) {
316                 // event should not be null
317                 if (null == event) {
318                         // Throw NPE
319                         throw new NullPointerException("event is null"); //NOI18N
320                 } else if (event.getUpdatedUser() == null) {
321                         // Throw NPE again
322                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
323                 } else if (event.getUpdatedUser().getUserId() == null) {
324                         // userId is null
325                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
326                 } else if (event.getUpdatedUser().getUserId() < 1) {
327                         // Not avalid id
328                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
329                 }
330
331                 // Update user list
332                 this.updateList(event.getUpdatedUser());
333
334                 // Clear all data
335                 this.clear();
336         }
337
338         /**
339          * Event observer for when a bean helper has successfully created a user
340          * instance, means the user exists. If the user does not exist, this event
341          * should not fire but instead a proper exception must be thrown.
342          * <p>
343          * @param event User created event
344          */
345         public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) {
346                 // Is the instance valid?
347                 if (null == event) {
348                         // Throw NPE
349                         throw new NullPointerException("event is null"); //NOI18N
350                 } else if (event.getCreatedUser() == null) {
351                         // Throw NPE again
352                         throw new NullPointerException("event.createdUser is null"); //NOI18N
353                 } else if (event.getCreatedUser().getUserId() == null) {
354                         // Throw NPE again
355                         throw new NullPointerException("event.createdUser.userId is null"); //NOI18N
356                 } else if (event.getCreatedUser().getUserId() < 1) {
357                         // Throw NPE again
358                         throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N
359                 }
360
361                 // Get user instance
362                 User user = event.getCreatedUser();
363
364                 // Set all fields here
365                 this.copyUser(user);
366         }
367
368         /**
369          * Observer method for events being fired when the application's locale has
370          * been changed.
371          * <p>
372          * @param event Event being fired
373          */
374         public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) {
375                 // Is the parameter valid?
376                 if (null == event) {
377                         // Throw NPE
378                         throw new NullPointerException("event is null");
379                 } else if (event.getLocale() == null) {
380                         // Throw NPE again
381                         throw new NullPointerException("event.locale is null");
382                 }
383
384                 // Set it here
385                 this.setLocale(event.getLocale());
386         }
387
388         /**
389          * Event observer when user confirmed account.
390          * <p>
391          * @param event Event being fired
392          */
393         public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) {
394                 // event should not be null
395                 if (null == event) {
396                         // Throw NPE
397                         throw new NullPointerException("event is null"); //NOI18N
398                 } else if (event.getConfirmedUser() == null) {
399                         // Throw NPE again
400                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
401                 } else if (event.getConfirmedUser().getUserId() == null) {
402                         // userId is null
403                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
404                 } else if (event.getConfirmedUser().getUserId() < 1) {
405                         // Not avalid id
406                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
407                 }
408
409                 // Update user list
410                 this.updateList(event.getConfirmedUser());
411         }
412
413         /**
414          * Event observer for logged-in user
415          * <p>
416          * @param event Event instance
417          */
418         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
419                 // event should not be null
420                 if (null == event) {
421                         // Throw NPE
422                         throw new NullPointerException("event is null"); //NOI18N
423                 } else if (event.getLoggedInUser() == null) {
424                         // Throw NPE again
425                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
426                 } else if (event.getLoggedInUser().getUserId() == null) {
427                         // userId is null
428                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
429                 } else if (event.getLoggedInUser().getUserId() < 1) {
430                         // Not avalid id
431                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
432                 }
433
434                 // "Cache" user instance
435                 User loggedInUser = event.getLoggedInUser();
436
437                 // Copy all data to this bean
438                 this.copyUser(loggedInUser);
439         }
440
441         /**
442          * Event observer for user password changes
443          * <p>
444          * @param event Event being fired
445          */
446         public void afterUserPasswordChangedEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
447                 // Is it valid?
448                 if (null == event) {
449                         // Throw NPE
450                         throw new NullPointerException("event is null"); //NOI18N
451                 } else if (event.getUserPassword() == null) {
452                         // Throw NPE
453                         throw new NullPointerException("event.userPassword is null"); //NOI18N
454                 } else if (event.getUserPassword().isEmpty()) {
455                         // Throw NPE
456                         throw new IllegalArgumentException("event.userPassword is empty"); //NOI18N
457                 }
458
459                 // Set it here
460                 this.setUserPassword(event.getUserPassword());
461                 this.setUserPasswordRepeat(event.getUserPassword());
462         }
463
464         /**
465          * Event observer for new user registrations
466          * <p>
467          * @param event User registration event
468          */
469         public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
470                 // event should not be null
471                 if (null == event) {
472                         // Throw NPE
473                         throw new NullPointerException("event is null"); //NOI18N
474                 } else if (event.getRegisteredUser() == null) {
475                         // Throw NPE again
476                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
477                 } else if (event.getRegisteredUser().getUserId() == null) {
478                         // userId is null
479                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
480                 } else if (event.getRegisteredUser().getUserId() < 1) {
481                         // Not avalid id
482                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
483                 }
484
485                 // Get user instance
486                 User registeredUser = event.getRegisteredUser();
487
488                 // Copy all data from registered->user
489                 this.copyUser(registeredUser);
490
491                 // Clear all data
492                 this.clear();
493
494                 // Update user list
495                 this.updateList(registeredUser);
496
497                 // Add user name
498                 this.addUserName(registeredUser);
499
500                 // Set user id again
501                 this.setUserId(registeredUser.getUserId());
502         }
503
504         /**
505          * Method being call after user's password has been updated (and history
506          * entry has been created).
507          * <p>
508          * @param event Event being observed
509          */
510         public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
511                 // Check parameter
512                 if (null == event) {
513                         // Throw NPE
514                         throw new NullPointerException("event is null"); //NOI18N
515                 } else if (event.getPasswordHistory() == null) {
516                         // Throw NPE again
517                         throw new NullPointerException("event.passwordHistory is null"); //NOI18N
518                 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
519                         // ... and again
520                         throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
521                 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
522                         // Invalid value
523                         throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
524                 }
525
526                 // Update user list
527                 this.updateList(event.getPasswordHistory().getUserPasswordHistoryUser());
528         }
529
530         /**
531          * Listens to fired event when user updated personal data
532          * <p>
533          * @param event Event being fired
534          */
535         public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) {
536                 // Check parameter
537                 if (null == event) {
538                         // Throw NPE
539                         throw new NullPointerException("event is null"); //NOI18N
540                 } else if (event.getUpdatedUser() == null) {
541                         // Throw NPE again
542                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
543                 } else if (event.getUpdatedUser().getUserId() == null) {
544                         // ... and again
545                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
546                 } else if (event.getUpdatedUser().getUserId() < 1) {
547                         // Invalid value
548                         throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
549                 }
550
551                 // Update user list
552                 this.updateList(event.getUpdatedUser());
553         }
554
555         @Override
556         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
557         public List<User> allUsers () {
558                 // Init list
559                 List<User> list = new LinkedList<>();
560
561                 // Get iterator
562                 Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator();
563
564                 // Loop over all
565                 while (iterator.hasNext()) {
566                         // Get next entry
567                         final Cache.Entry<Long, User> next = iterator.next();
568
569                         // Add value to list
570                         list.add(next.getValue());
571                 }
572
573                 // Return it
574                 return list;
575         }
576
577         /**
578          * Event observer for when a user name should be cleared
579          * <p>
580          * @param event Event being fired
581          */
582         public void clearUserNameEvent (@Observes final ObservableClearUserNameEvent event) {
583                 // Is it valid?
584                 if (null == event) {
585                         // Throw NPE
586                         throw new NullPointerException("event is null");
587                 }
588
589                 // Clear it
590                 this.clearUserName();
591         }
592
593         /**
594          * Event observer for when both user passwords should be cleared
595          * <p>
596          * @param event Event being fired
597          */
598         public void clearUserPasswordEvent (@Observes final ObservableClearUserPasswordEvent event) {
599                 // Is it valid?
600                 if (null == event) {
601                         // Throw NPE
602                         throw new NullPointerException("event is null");
603                 }
604
605                 // Clear it
606                 this.clearUserPasswords();
607         }
608
609         @Override
610         public User createUserInstance (final boolean createContactData) {
611                 // Trace message
612                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
613
614                 // Required personal data must be set
615                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
616
617                 // Create new user instance
618                 User user = new LoginUser();
619
620                 // Is user name required?
621                 if (!this.isUserNameRequired()) {
622                         // Generate pseudo-random user name
623                         String randomName = this.userBean.generateRandomUserName();
624
625                         // Set it and inivisible profile
626                         this.setUserName(randomName);
627                         this.setUserProfileMode(ProfileMode.INVISIBLE);
628
629                         // Generate random password
630                         String randomPassword = UserLoginUtils.createRandomPassword(PizzaUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
631
632                         // Set random password
633                         this.setUserPassword(randomPassword);
634                         this.setUserPasswordRepeat(randomPassword);
635                 }
636
637                 // Set user name profile mode and locale
638                 user.setUserName(this.getUserName());
639                 user.setUserProfileMode(this.getUserProfileMode());
640                 user.setUserLocale(this.getLocale());
641
642                 // Is multiple registration page
643                 if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
644                         // Create contact instance
645                         Contact contact = this.contactController.createContactInstance();
646
647                         // Debug message
648                         //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
649                         // Set contact in user
650                         user.setUserContact(contact);
651                 }
652
653                 // Trace message
654                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
655                 // Return it
656                 return user;
657         }
658
659         @Override
660         public User createUserLogin () {
661                 // Trace message
662                 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
663
664                 // Is all data set?
665                 if (this.getUserName() == null) {
666                         // Throw NPE
667                         throw new NullPointerException("userName is null"); //NOI18N
668                 } else if (this.getUserName().isEmpty()) {
669                         // Is empty
670                         throw new IllegalStateException("userName is empty."); //NOI18N
671                 }
672
673                 // Create new user instance
674                 User user = new LoginUser();
675
676                 // Update all data ...
677                 user.setUserName(this.getUserName());
678
679                 // Trace message
680                 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: user={1} - EXIT!", this.getClass().getSimpleName(), user));
681                 // Return the new instance
682                 return user;
683         }
684
685         @Override
686         public String doChangePersonalData () {
687                 // This method shall only be called if the user is logged-in
688                 if (!this.userLoginController.isUserLoggedIn()) {
689                         // Not logged-in
690                         throw new IllegalStateException("User is not logged-in"); //NOI18N
691                 } else if (!this.isRequiredChangePersonalDataSet()) {
692                         // Not all required fields are set
693                         throw new FaceletException("Not all required fields are set."); //NOI18N
694                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
695                         // Password not matching
696                         throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
697                 } else if (!this.featureController.isFeatureEnabled("change_user_personal_data")) { //NOI18N
698                         // Editing is not allowed
699                         throw new IllegalStateException("User tried to edit personal data."); //NOI18N
700                 }
701
702                 // Get user instance
703                 User user = this.userLoginController.getLoggedInUser();
704
705                 // Copy contact data to contact instance
706                 this.contactController.updateContactDataFromController(user.getUserContact());
707
708                 // It should be there, so run some tests on it
709                 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
710                 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
711                 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
712                 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
713                 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
714                 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
715
716                 // Update all fields
717                 user.setUserProfileMode(this.getUserProfileMode());
718
719                 // Send it to the EJB
720                 User updatedUser = this.userBean.updateUserPersonalData(user);
721
722                 // Fire event
723                 this.updatedPersonalDataEvent.fire(new UpdatedUserPersonalDataEvent(updatedUser));
724
725                 // All fine
726                 return "user_contact_data_saved"; //NOI18N
727         }
728
729         /**
730          * Getter for user id
731          * <p>
732          * @return User id
733          */
734         public Long getUserId () {
735                 return this.userId;
736         }
737
738         /**
739          * Setter for user id
740          * <p>
741          * @param userId User id
742          */
743         public void setUserId (final Long userId) {
744                 this.userId = userId;
745         }
746
747         /**
748          * Getter for user name
749          * <p>
750          * @return User name
751          */
752         public String getUserName () {
753                 return this.userName;
754         }
755
756         /**
757          * Setter for user name
758          * <p>
759          * @param userName User name
760          */
761         public void setUserName (final String userName) {
762                 this.userName = userName;
763         }
764
765         @Override
766         public String getUserPassword () {
767                 return this.userPassword;
768         }
769
770         /**
771          * Setter for clear-text user password
772          * <p>
773          * @param userPassword Clear-text user password
774          */
775         public void setUserPassword (final String userPassword) {
776                 this.userPassword = userPassword;
777         }
778
779         /**
780          * Getter for clear-text user password repeated
781          * <p>
782          * @return Clear-text user password repeated
783          */
784         public String getUserPasswordRepeat () {
785                 return this.userPasswordRepeat;
786         }
787
788         /**
789          * Setter for clear-text user password repeated
790          * <p>
791          * @param userPasswordRepeat Clear-text user password repeated
792          */
793         public void setUserPasswordRepeat (final String userPasswordRepeat) {
794                 this.userPasswordRepeat = userPasswordRepeat;
795         }
796
797         /**
798          * Getter for user profile mode
799          * <p>
800          * @return User profile mode
801          */
802         public ProfileMode getUserProfileMode () {
803                 return this.userProfileMode;
804         }
805
806         /**
807          * Setter for user profile mode
808          * <p>
809          * @param userProfileMode User profile mode
810          */
811         public void setUserProfileMode (final ProfileMode userProfileMode) {
812                 this.userProfileMode = userProfileMode;
813         }
814
815         @Override
816         public boolean ifBothPasswordsEmptyAllowed () {
817                 // Check feature first
818                 return ((this.featureController.isFeatureEnabled("allow_user_registration_empty_password")) && //NOI18N
819                                 ((this.getUserPassword() == null) || (this.getUserPassword().isEmpty())) &&
820                                 ((this.getUserPasswordRepeat() == null) || (this.getUserPasswordRepeat().isEmpty())));
821         }
822
823         /**
824          * Post-initialization of this class
825          */
826         @PostConstruct
827         public void init () {
828                 // Is cache there?
829                 if (!this.userCache.iterator().hasNext()) {
830                         // Get whole list
831                         List<User> list = this.userBean.allUsers();
832
833                         // Add all
834                         for (final Iterator<User> iterator = list.iterator(); iterator.hasNext();) {
835                                 // Get next element
836                                 final User next = iterator.next();
837
838                                 // Add it to cache
839                                 this.userCache.put(next.getUserId(), next);
840                                 this.userNameCache.put(next.getUserId(), next.getUserName());
841                         }
842                 }
843         }
844
845         @Override
846         public boolean isContactFound (final Contact contact) {
847                 // The contact must be valid
848                 if (null == contact) {
849                         // Throw NPE
850                         throw new NullPointerException("contact is null"); //NOI18N
851                 } else if (contact.getContactId() == null) {
852                         // Throw again ...
853                         throw new NullPointerException("contact.contactId is null"); //NOI18N
854                 } else if (contact.getContactId() < 1) {
855                         // Not valid
856                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
857                 }
858
859                 // Default is not found
860                 boolean isFound = false;
861
862                 // Get iterator
863                 Iterator<User> iterator = this.allUsers().iterator();
864
865                 // Loop through all entries
866                 while (iterator.hasNext()) {
867                         // Get user
868                         User next = iterator.next();
869
870                         // Compare both objects
871                         if (Objects.equals(contact, next.getUserContact())) {
872                                 // Found it
873                                 isFound = true;
874                                 break;
875                         }
876                 }
877
878                 // Return status
879                 return isFound;
880         }
881
882         @Override
883         public boolean isPublicUserProfileEnabled () {
884                 // Get context parameter
885                 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_public_profile_enabled"); //NOI18N
886
887                 // Is it set?
888                 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
889
890                 // This requires user names being enabled, too.
891                 if ((isEnabled) && (!this.isUserNameRequired())) {
892                         // Not valid state, users must be able to modify their profile, especially when it is public
893                         throw new IllegalStateException("Public user profiles are enabled but user name requirement is disabled, this is not possible."); //NOI18N
894                 }
895
896                 // Return value
897                 return isEnabled;
898         }
899
900         @Override
901         public boolean isRequiredChangePersonalDataSet () {
902                 return ((this.getUserProfileMode() != null) &&
903                                 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
904                                 (this.contactController.isRequiredChangePersonalDataSet()));
905         }
906
907         @Override
908         public boolean isRequiredPersonalDataSet () {
909                 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
910                         // Multiple registration page
911                         return this.contactController.isRequiredPersonalDataSet();
912                 } else {
913                         // Single registration page
914                         return (((this.getUserName() != null) || (!this.isUserNameRequired())) &&
915                                         (this.getUserProfileMode() != null) &&
916                                         (this.contactController.isRequiredPersonalDataSet()) &&
917                                         (this.getUserPassword() != null) &&
918                                         (this.getUserPasswordRepeat() != null));
919                 }
920         }
921
922         @Override
923         public boolean isSamePasswordEntered () {
924                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
925         }
926
927         @Override
928         public boolean isUserIdEmpty () {
929                 return ((this.getUserId() == null) || (this.getUserId() == 0));
930         }
931
932         @Override
933         public boolean isUserNameRegistered (final User user) {
934                 return ((this.userNameCache instanceof List) && (this.userNameCache.containsKey(user.getUserId())));
935         }
936
937         @Override
938         public boolean isUserNameRequired () {
939                 // Get context parameter
940                 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_login_require_user_name"); //NOI18N
941
942                 // Is it set?
943                 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
944
945                 // Return value
946                 return isRequired;
947         }
948
949         @Override
950         public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException {
951                 // Parameter must be valid
952                 if (null == emailAddress) {
953                         // Throw NPE
954                         throw new NullPointerException("emailAddress is null"); //NOI18N
955                 } else if (emailAddress.isEmpty()) {
956                         // Not valid
957                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
958                 }
959
960                 // Init variable
961                 User user = null;
962
963                 // Try to lookup it in visible user list
964                 for (final Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator(); iterator.hasNext();) {
965                         // Get next user
966                         Cache.Entry<Long, User> next = iterator.next();
967
968                         // Contact should be set
969                         if (next.getValue().getUserContact() == null) {
970                                 // Contact is null
971                                 throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getKey())); //NOI18N
972                         } else if (next.getValue().getUserContact().getContactEmailAddress() == null) {
973                                 // Email address should be set
974                                 throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getKey())); //NOI18N
975                         }
976
977                         // Is the email address found?
978                         if (Objects.equals(next.getValue().getUserContact().getContactEmailAddress(), emailAddress)) {
979                                 // Copy to other variable
980                                 user = next.getValue();
981                                 break;
982                         }
983                 }
984
985                 // Is it still null?
986                 if (null == user) {
987                         // Not visible for the current user
988                         throw new UserEmailAddressNotFoundException(emailAddress);
989                 }
990
991                 // Return it
992                 return user;
993         }
994
995         @Override
996         public User lookupUserById (final Long userId) throws UserNotFoundException {
997                 // Parameter must be valid
998                 if (null == userId) {
999                         // Throw NPE
1000                         throw new NullPointerException("userId is null"); //NOI18N
1001                 } else if (userId < 1) {
1002                         // Not valid
1003                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
1004                 }
1005
1006                 // Init variable
1007                 User user = null;
1008
1009                 // Try to lookup it in visible user list
1010                 for (final Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator(); iterator.hasNext();) {
1011                         // Get next user
1012                         Cache.Entry<Long, User> next = iterator.next();
1013
1014                         // Is the user id found?
1015                         if (Objects.equals(next.getKey(), userId)) {
1016                                 // Copy to other variable
1017                                 user = next.getValue();
1018                                 break;
1019                         }
1020                 }
1021
1022                 // Is it still null?
1023                 if (null == user) {
1024                         // Not visible for the current user
1025                         throw new UserNotFoundException(userId);
1026                 }
1027
1028                 // Return it
1029                 return user;
1030         }
1031
1032         /**
1033          * Adds user's name to bean's internal list. It also updates the public user
1034          * list if the user has decided to have a public account,
1035          * <p>
1036          * @param user User instance
1037          */
1038         private void addUserName (final User user) {
1039                 // Make sure the entry is not added yet
1040                 if (this.userNameCache.containsKey(user.getUserId())) {
1041                         // Abort here
1042                         throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
1043                 }
1044
1045                 // Add user name
1046                 this.userNameCache.put(user.getUserId(), user.getUserName());
1047         }
1048
1049         /**
1050          * Clears this bean
1051          */
1052         private void clear () {
1053                 // Clear all data
1054                 // - personal data
1055                 this.setUserId(null);
1056                 this.setUserProfileMode(null);
1057
1058                 // - other data
1059                 this.clearUserName();
1060                 this.clearUserPasswords();
1061                 this.setLocale(null);
1062         }
1063
1064         /**
1065          * Clears user name
1066          */
1067         private void clearUserName () {
1068                 // Clear it
1069                 this.setUserName(null);
1070         }
1071
1072         /**
1073          * Clears both user passwords
1074          */
1075         private void clearUserPasswords () {
1076                 // Clear both
1077                 this.setUserPassword(null);
1078                 this.setUserPasswordRepeat(null);
1079         }
1080
1081         /**
1082          * Copies given user into the controller
1083          * <p>
1084          * @param user User instance
1085          */
1086         private void copyUser (final User user) {
1087                 // Make sure the instance is valid
1088                 if (null == user) {
1089                         // Throw NPE
1090                         throw new NullPointerException("user is null"); //NOI18N
1091                 } else if (user.getUserContact() == null) {
1092                         // Throw again ...
1093                         throw new NullPointerException("user.userContact is null"); //NOI18N
1094                 }
1095
1096                 // Copy all fields:
1097                 // - base data
1098                 this.setUserId(user.getUserId());
1099                 this.setUserProfileMode(user.getUserProfileMode());
1100         }
1101
1102         /**
1103          * Getter for locale instance
1104          * <p>
1105          * @return Locale instance
1106          */
1107         private Locale getLocale () {
1108                 return this.locale;
1109         }
1110
1111         /**
1112          * Setter for locale instance
1113          * <p>
1114          * @param locale Locale instance
1115          */
1116         private void setLocale (final Locale locale) {
1117                 this.locale = locale;
1118         }
1119
1120         /**
1121          * Removes user from all lists
1122          * <p>
1123          * @param user User to remove
1124          */
1125         private void removeFromList (final User user) {
1126                 // The user should be valid
1127                 if (null == user) {
1128                         // Throw NPE
1129                         throw new NullPointerException("user is null"); //NOI18N
1130                 } else if (user.getUserId() == null) {
1131                         // ... again NPE
1132                         throw new NullPointerException("user.userId is null"); //NOI18N
1133                 } else if (user.getUserId() < 1) {
1134                         // Invalid id
1135                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
1136                 }
1137
1138                 // Remove it from lists
1139                 this.userCache.remove(user.getUserId());
1140
1141                 // Remove name from list
1142                 this.userNameCache.remove(user.getUserId());
1143         }
1144
1145         /**
1146          * Updates list with given user instance
1147          * <p>
1148          * @param user User instance
1149          */
1150         private void updateList (final User user) {
1151                 // The user should be valid
1152                 if (null == user) {
1153                         // Throw NPE
1154                         throw new NullPointerException("user is null"); //NOI18N
1155                 } else if (user.getUserId() == null) {
1156                         // ... again NPE
1157                         throw new NullPointerException("user.userId is null"); //NOI18N
1158                 } else if (user.getUserId() < 1) {
1159                         // Invalid id
1160                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
1161                 } else if (user.getUserContact() == null) {
1162                         // Throw again ...
1163                         throw new NullPointerException("user.userContact is null"); //NOI18N
1164                 } else if (user.getUserContact().getContactId() == null) {
1165                         // Throw again ...
1166                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
1167                 } else if (user.getUserContact().getContactId() < 1) {
1168                         // Throw again ...
1169                         throw new NullPointerException(MessageFormat.format("user.userContact.contactId={0} is invalid.", user.getUserContact().getContactId())); //NOI18N
1170                 }
1171
1172                 // Add/update user
1173                 this.userCache.put(user.getUserId(), user);
1174         }
1175
1176 }