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