2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.beans.user;
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.SessionScoped;
26 import javax.enterprise.event.Event;
27 import javax.enterprise.event.Observes;
28 import javax.enterprise.inject.Any;
29 import javax.faces.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.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
37 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
38 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
39 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
40 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
41 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
42 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
43 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
44 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
45 import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
46 import org.mxchange.jusercore.exceptions.UserNotFoundException;
47 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
48 import org.mxchange.jusercore.model.user.LoginUser;
49 import org.mxchange.jusercore.model.user.User;
50 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
51 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
52 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
53 import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController;
56 * A user bean (controller)
58 * @author Roland Haeder<roland@mxchange.org>
60 @Named ("userController")
62 public class PizzaUserWebSessionBean implements PizzaUserWebSessionController {
67 private static final long serialVersionUID = 542_145_347_916L;
70 * General contact controller
73 private PizzaContactWebSessionController contactController;
76 * Login bean (controller)
79 private PizzaUserLoginWebSessionController loginController;
82 * A list of all selectable contacts
84 private List<Contact> selectableContacts;
87 * Event being fired when user updated personal data
91 private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
96 private final UserSessionBeanRemote userBean;
104 * A list of all user profiles
106 private List<User> userList;
109 * Login bean (controller)
112 private PizzaUserLoginWebSessionController userLoginController;
117 private String userName;
122 private List<String> userNameList;
125 * User password (unencrypted from web form)
127 private String userPassword;
130 * User password repeated (unencrypted from web form)
132 private String userPasswordRepeat;
135 * Whether the user wants a public profile
137 private ProfileMode userProfileMode;
140 * A list of all public user profiles
142 private List<User> visibleUserList;
145 * Default constructor
147 public PizzaUserWebSessionBean () {
150 // Get initial context
151 Context context = new InitialContext();
154 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
155 } catch (final NamingException e) {
157 throw new FaceletException(e);
162 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
164 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
166 // event should not be null
169 throw new NullPointerException("event is null"); //NOI18N
170 } else if (event.getAddedUser() == null) {
172 throw new NullPointerException("event.addedUser is null"); //NOI18N
173 } else if (event.getAddedUser().getUserId() == null) {
175 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
176 } else if (event.getAddedUser().getUserId() < 1) {
178 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
181 // Add user to local list
182 this.userList.add(event.getAddedUser());
188 this.setUserId(event.getAddedUser().getUserId());
191 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
195 public void afterAdminUpdatedUserDataEvent (@Observes final AdminUpdatedUserDataEvent event) {
197 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminUpdatedUserEvent: event={0} - CALLED!", event)); //NOI18N
199 // event should not be null
202 throw new NullPointerException("event is null"); //NOI18N
203 } else if (event.getUpdatedUser() == null) {
205 throw new NullPointerException("event.updatedUser is null"); //NOI18N
206 } else if (event.getUpdatedUser().getUserId() == null) {
208 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
209 } else if (event.getUpdatedUser().getUserId() < 1) {
211 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
215 this.updateList(event.getUpdatedUser());
221 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminUpdatedUserEvent: EXIT!"); //NOI18N
225 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
227 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
229 // event should not be null
232 throw new NullPointerException("event is null"); //NOI18N
233 } else if (event.getRegisteredUser() == null) {
235 throw new NullPointerException("event.registeredUser is null"); //NOI18N
236 } else if (event.getRegisteredUser().getUserId() == null) {
238 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
239 } else if (event.getRegisteredUser().getUserId() < 1) {
241 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
245 User registeredUser = event.getRegisteredUser();
248 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
250 // Copy all data from registered->user
251 this.copyUser(registeredUser);
256 // Add user to local list
257 this.userList.add(registeredUser);
260 this.addUserName(registeredUser);
262 // Is the account public?
263 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
264 // Also add it to this list
265 this.visibleUserList.add(registeredUser);
269 this.setUserId(registeredUser.getUserId());
272 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
276 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
278 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
280 // event should not be null
283 throw new NullPointerException("event is null"); //NOI18N
284 } else if (event.getLoggedInUser() == null) {
286 throw new NullPointerException("event.registeredUser is null"); //NOI18N
287 } else if (event.getLoggedInUser().getUserId() == null) {
289 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
290 } else if (event.getLoggedInUser().getUserId() < 1) {
292 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
295 // Copy all data to this bean
296 this.copyUser(event.getLoggedInUser());
298 // Re-initialize list
299 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
302 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
306 public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
310 throw new NullPointerException("event is null"); //NOI18N
311 } else if (event.getUpdatedUser() == null) {
313 throw new NullPointerException("event.updatedUser is null"); //NOI18N
314 } else if (event.getUpdatedUser().getUserId() == null) {
316 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
317 } else if (event.getUpdatedUser().getUserId() < 1) {
319 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
322 // All fine, so update list
323 this.updateList(event.getUpdatedUser());
327 public List<User> allUsers () {
329 return Collections.unmodifiableList(this.userList);
333 public List<User> allVisibleUsers () {
335 return Collections.unmodifiableList(this.visibleUserList);
339 public User createUserInstance () {
341 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
343 // Required personal data must be set
344 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
346 // Create new user instance
347 User localUser = new LoginUser();
349 // Update all data ...
350 localUser.setUserName(this.getUserName());
351 localUser.setUserProfileMode(this.getUserProfileMode());
353 // Create contact instance
354 Contact contact = this.contactController.createContactInstance();
357 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
359 // Set contact in user
360 localUser.setUserContact(contact);
363 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
370 public User createUserLogin () {
372 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
375 if (this.getUserName() == null) {
377 throw new NullPointerException("recruiterName is null"); //NOI18N
378 } else if (this.getUserName().isEmpty()) {
380 throw new IllegalStateException("recruiterName is empty."); //NOI18N
383 // Create new recruiter instance
384 User recruiter = new LoginUser();
386 // Update all data ...
387 recruiter.setUserName(this.getUserName());
390 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: recruiter={1} - EXIT!", this.getClass().getSimpleName(), recruiter));
392 // Return the new instance
397 public String doChangePersonalData () {
398 // This method shall only be called if the user is logged-in
399 if (!this.loginController.isUserLoggedIn()) {
401 throw new IllegalStateException("User is not logged-in"); //NOI18N
402 } else if (!this.isRequiredChangePersonalDataSet()) {
403 // Not all required fields are set
404 throw new FaceletException("Not all required fields are set."); //NOI18N
405 } else if (!this.loginController.ifCurrentPasswordMatches()) {
406 // Password not matching
407 throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
411 User user = this.loginController.getLoggedInUser();
413 // Copy contact data to contact instance
414 this.contactController.updateContactDataFromController(user.getUserContact());
416 // It should be there, so run some tests on it
417 assert (user instanceof User) : "Instance loginController.loggedInUser is null"; //NOI18N
418 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null"; //NOI18N
419 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
420 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
421 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
422 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
425 user.setUserProfileMode(this.getUserProfileMode());
427 // Send it to the EJB
428 User updatedUser = this.userBean.updateUserPersonalData(user);
431 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
434 return "user_data_saved"; //NOI18N
438 public Long getUserId () {
443 public void setUserId (final Long userId) {
444 this.userId = userId;
448 public String getUserName () {
449 return this.userName;
453 public void setUserName (final String userName) {
454 this.userName = userName;
458 public String getUserPassword () {
459 return this.userPassword;
463 public void setUserPassword (final String userPassword) {
464 this.userPassword = userPassword;
468 public String getUserPasswordRepeat () {
469 return this.userPasswordRepeat;
473 public void setUserPasswordRepeat (final String userPasswordRepeat) {
474 this.userPasswordRepeat = userPasswordRepeat;
478 public ProfileMode getUserProfileMode () {
479 return this.userProfileMode;
483 public void setUserProfileMode (final ProfileMode userProfileMode) {
484 this.userProfileMode = userProfileMode;
488 public boolean hasUsers () {
489 return (!this.allUsers().isEmpty());
493 * Post-initialization of this class
496 public void init () {
497 // Initialize user list
498 this.userList = this.userBean.allUsers();
500 // Get full user name list for reducing EJB calls
501 this.userNameList = this.userBean.getUserNameList();
503 // Is the user logged-in?
504 if (this.userLoginController.isUserLoggedIn()) {
505 // Is logged-in, so load also users visible to memebers
506 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
508 // Initialize user list
509 this.visibleUserList = this.userBean.allPublicUsers();
513 List<User> allUsers = this.allUsers();
516 List<Contact> allContacts = this.contactController.allContacts();
519 Iterator<Contact> iterator = allContacts.iterator();
522 while (iterator.hasNext()) {
524 Contact next = iterator.next();
527 Iterator<User> userIterator = allUsers.iterator();
529 // Loop through all users
530 while (userIterator.hasNext()) {
532 User nextUser = userIterator.next();
535 if (Objects.equals(next, nextUser.getUserContact())) {
544 this.selectableContacts = allContacts;
548 public boolean isContactFound (final Contact contact) {
549 // The contact must be valid
550 if (null == contact) {
552 throw new NullPointerException("contact is null"); //NOI18N
553 } else if (contact.getContactId() == null) {
555 throw new NullPointerException("contact.contactId is null"); //NOI18N
556 } else if (contact.getContactId() < 1) {
558 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
561 // Default is not found
562 boolean isFound = false;
565 Iterator<User> iterator = this.allUsers().iterator();
567 // Loop through all entries
568 while (iterator.hasNext()) {
570 User next = iterator.next();
572 // Compare both objects
573 if (Objects.equals(contact, next.getUserContact())) {
585 public boolean isRequiredChangePersonalDataSet () {
586 return ((this.getUserProfileMode() != null) &&
587 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
588 (this.contactController.isRequiredChangePersonalDataSet()));
592 public boolean isRequiredPersonalDataSet () {
593 return ((this.getUserName() != null) &&
594 (this.getUserProfileMode() != null) &&
595 (this.contactController.isRequiredPersonalDataSet()) &&
596 (this.getUserPassword() != null) &&
597 (this.getUserPasswordRepeat() != null));
601 public boolean isSamePasswordEntered () {
602 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
606 public boolean isUserIdEmpty () {
607 return ((this.getUserId() == null) || (this.getUserId() == 0));
611 public boolean isUserNameRegistered (final User user) {
612 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
616 public boolean isVisibleUserFound () {
617 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
621 public User lookupUserById (final Long userId) throws UserNotFoundException {
622 // Parameter must be valid
623 if (null == userId) {
625 throw new NullPointerException("userId is null"); //NOI18N
626 } else if (userId < 1) {
628 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
634 // Try to lookup it in visible user list
635 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
637 User next = iterator.next();
639 // Is the user id found?
640 if (Objects.equals(next.getUserId(), userId)) {
641 // Copy to other variable
649 // Not visible for the current user
650 throw new UserNotFoundException(userId);
658 public List<Contact> selectableContacts () {
659 return Collections.unmodifiableList(this.selectableContacts);
663 * Adds user's name to bean's internal list. It also updates the public user
664 * list if the user has decided to have a public account,
666 * @param user User instance
668 private void addUserName (final User user) {
669 // Make sure the entry is not added yet
670 if (this.userNameList.contains(user.getUserName())) {
672 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
673 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
675 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
679 this.userNameList.add(user.getUserName());
685 private void clear () {
688 this.setUserId(null);
689 this.setUserProfileMode(null);
692 this.setUserName(null);
693 this.setUserPassword(null);
694 this.setUserPasswordRepeat(null);
698 * Copies given user into the controller
700 * @param user User instance
702 private void copyUser (final User user) {
703 // Make sure the instance is valid
706 throw new NullPointerException("user is null"); //NOI18N
707 } else if (user.getUserContact() == null) {
709 throw new NullPointerException("user.userContact is null"); //NOI18N
714 this.setUserId(user.getUserId());
715 this.setUserProfileMode(user.getUserProfileMode());
719 * Updates list with given user instance
721 * @param user User instance
723 private void updateList (final User user) {
724 // The user should be valid
727 throw new NullPointerException("user is null"); //NOI18N
728 } else if (user.getUserId() == null) {
730 throw new NullPointerException("user.userId is null"); //NOI18N
731 } else if (user.getUserId() < 1) {
733 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
737 Iterator<User> iterator = this.userList.iterator();
740 while (iterator.hasNext()) {
742 User next = iterator.next();
744 // Is the same user id?
745 if (Objects.equals(user.getUserId(), next.getUserId())) {
746 // Found it, so remove it
747 this.userList.remove(next);
753 this.userList.add(user);