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 de.chotime.jratecalc.beans.contact.RateCalcContactWebSessionController;
20 import de.chotime.jratecalc.beans.login.RateCalcUserLoginWebSessionController;
21 import java.text.MessageFormat;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
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.view.facelets.FaceletException;
32 import javax.inject.Inject;
33 import javax.inject.Named;
34 import javax.naming.Context;
35 import javax.naming.InitialContext;
36 import javax.naming.NamingException;
37 import org.mxchange.jcontacts.contact.Contact;
38 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
39 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
40 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
41 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
42 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
43 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
44 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
45 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
46 import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
47 import org.mxchange.jusercore.exceptions.UserNotFoundException;
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.login.PizzaUserLoginWebSessionController;
53 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
54 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
57 * A user bean (controller)
59 * @author Roland Haeder<roland@mxchange.org>
61 @Named ("userController")
63 public class PizzaUserWebSessionBean implements PizzaUserWebSessionController {
68 private static final long serialVersionUID = 542_145_347_916L;
71 * Administrative user controller
74 private RateCalcAdminUserWebRequestController adminController;
79 private final ContactSessionBeanRemote contactBean;
82 * General contact controller
85 private PizzaContactWebSessionController contactController;
88 * Login bean (controller)
91 private PizzaUserLoginWebSessionController loginController;
94 * A list of all selectable contacts
96 private List<Contact> selectableContacts;
99 * Event being fired when user updated personal data
103 private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
108 private final UserSessionBeanRemote userBean;
118 private String userName;
123 private List<String> userNameList;
126 * User password (unencrypted from web form)
128 private String userPassword;
131 * User password repeated (unencrypted from web form)
133 private String userPasswordRepeat;
136 * Whether the user wants a public profile
138 private ProfileMode userProfileMode;
141 * A list of all public user profiles
143 private List<User> visibleUserList;
146 * Default constructor
148 public PizzaUserWebSessionBean () {
151 // Get initial context
152 Context context = new InitialContext();
155 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
158 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
159 } catch (final NamingException e) {
161 throw new FaceletException(e);
166 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
168 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
170 // event should not be null
173 throw new NullPointerException("event is null"); //NOI18N
174 } else if (event.getRegisteredUser() == null) {
176 throw new NullPointerException("event.user is null"); //NOI18N
177 } else if (event.getRegisteredUser().getUserId() == null) {
179 throw new NullPointerException("event.user.userId is null"); //NOI18N
180 } else if (event.getRegisteredUser().getUserId() < 1) {
182 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
186 User registeredUser = event.getRegisteredUser();
189 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
191 // Copy all data from registered->user
192 this.copyUser(registeredUser);
194 // Add user name and email address
195 this.addUserNameEmailAddress(registeredUser);
201 this.setUserId(registeredUser.getUserId());
203 // Is the account public?
204 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
205 // Also add it to this list
206 this.visibleUserList.add(registeredUser);
210 System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
214 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
216 System.out.println(MessageFormat.format("ContactWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
218 // The event must be valid
221 throw new NullPointerException("event is null"); //NOI18N
222 } else if (event.getAddedUser() == null) {
224 throw new NullPointerException("event.addedUser is null"); //NOI18N
225 } else if (event.getAddedUser().getUserId() == null) {
227 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
228 } else if (event.getAddedUser().getUserId() < 1) {
230 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
231 } else if (event.getAddedUser().getUserContact() == null) {
233 throw new NullPointerException("event.addedUser.userContact is null"); //NOI18N
234 } else if (event.getAddedUser().getUserContact().getContactId() == null) {
236 throw new NullPointerException("event.addedUser.userContact.contactId is null"); //NOI18N
237 } else if (event.getAddedUser().getUserContact().getContactId() < 1) {
239 throw new IllegalArgumentException(MessageFormat.format("event.addedUser.userContact.contactId={0} is not valid", event.getAddedUser().getUserContact().getContactId())); //NOI18N
242 // Remove this contact from selectable list
243 assert(this.selectableContacts.remove(event.getAddedUser().getUserContact())) : "contact was not removed"; //NOI18N
247 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
249 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
251 // event should not be null
254 throw new NullPointerException("event is null"); //NOI18N
255 } else if (event.getLoggedInUser() == null) {
257 throw new NullPointerException("event.user is null"); //NOI18N
258 } else if (event.getLoggedInUser().getUserId() == null) {
260 throw new NullPointerException("event.user.userId is null"); //NOI18N
261 } else if (event.getLoggedInUser().getUserId() < 1) {
263 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
266 // Re-initialize list
267 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
269 // Copy all data to this bean
270 this.copyUser(event.getLoggedInUser());
273 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
277 public List<User> allVisibleUsers () {
279 return Collections.unmodifiableList(this.visibleUserList);
283 public User createUserInstance () {
285 //this.getLogger().logTrace("createUserInstance: CALLED!");
287 // Required personal data must be set
288 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
290 // Create new user instance
291 User localUser = new LoginUser();
293 // Update all data ...
294 localUser.setUserName(this.getUserName());
295 localUser.setUserProfileMode(this.getUserProfileMode());
297 // Create contact instance
298 Contact contact = this.contactController.createContactInstance();
300 // Set contact in user
301 localUser.setUserContact(contact);
304 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
310 public String doChangePersonalData () {
311 // This method shall only be called if the user is logged-in
312 if (!this.loginController.isUserLoggedIn()) {
314 throw new IllegalStateException("User is not logged-in"); //NOI18N
315 } else if (!this.isRequiredChangePersonalDataSet()) {
316 // Not all required fields are set
317 throw new FaceletException("Not all required fields are set."); //NOI18N
318 } else if (!this.loginController.ifCurrentPasswordMatches()) {
319 // Password not matching
320 throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
324 User user = this.loginController.getLoggedInUser();
326 // Copy contact data to contact instance
327 this.contactController.updateContactDataFromController(user.getUserContact());
329 // It should be there, so run some tests on it
330 assert (user instanceof User) : "Instance loginController.loggedInUser is null"; //NOI18N
331 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null"; //NOI18N
332 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
333 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
334 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
335 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
338 user.setUserProfileMode(this.getUserProfileMode());
340 // Send it to the EJB
341 User updatedUser = this.userBean.updateUserPersonalData(user);
344 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
347 return "user_data_saved"; //NOI18N
351 public Long getUserId () {
356 public void setUserId (final Long userId) {
357 this.userId = userId;
361 public String getUserName () {
362 return this.userName;
366 public void setUserName (final String userName) {
367 this.userName = userName;
371 public String getUserPassword () {
372 return this.userPassword;
376 public void setUserPassword (final String userPassword) {
377 this.userPassword = userPassword;
381 public String getUserPasswordRepeat () {
382 return this.userPasswordRepeat;
386 public void setUserPasswordRepeat (final String userPasswordRepeat) {
387 this.userPasswordRepeat = userPasswordRepeat;
391 public ProfileMode getUserProfileMode () {
392 return this.userProfileMode;
396 public void setUserProfileMode (final ProfileMode userProfileMode) {
397 this.userProfileMode = userProfileMode;
401 * Post-initialization of this class
404 public void init () {
405 // Get full user name list for reducing EJB calls
406 this.userNameList = this.userBean.getUserNameList();
408 // Is the user logged-in?
409 if (this.loginController.isUserLoggedIn()) {
410 // Is logged-in, so load also users visible to memebers
411 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
413 // Initialize user list
414 this.visibleUserList = this.userBean.allPublicUsers();
418 List<User> allUsers = this.adminController.allUsers();
421 List<Contact> allContacts = this.contactBean.getAllContacts();
424 Iterator<Contact> iterator = allContacts.iterator();
427 while (iterator.hasNext()) {
429 Contact next = iterator.next();
432 Iterator<User> userIterator = allUsers.iterator();
434 // Loop through all users
435 while (userIterator.hasNext()) {
437 User nextUser = userIterator.next();
440 if (Objects.equals(next, nextUser.getUserContact())) {
449 this.selectableContacts = allContacts;
453 public boolean isRequiredChangePersonalDataSet () {
454 return ((this.getUserProfileMode() != null) &&
455 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
456 (this.contactController.isRequiredChangePersonalDataSet()));
460 public boolean isRequiredPersonalDataSet () {
461 return ((this.getUserName() != null) &&
462 (this.getUserProfileMode() != null) &&
463 (this.contactController.isRequiredPersonalDataSet()) &&
464 (this.getUserPassword() != null) &&
465 (this.getUserPasswordRepeat() != null));
469 public boolean isSamePasswordEntered () {
470 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
474 public boolean isUserIdEmpty () {
475 return ((this.getUserId() == null) || (this.getUserId() == 0));
479 public boolean isUserNameRegistered (final User user) {
480 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
484 public boolean isVisibleUserFound () {
485 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
489 public User lookupUserById (final Long userId) throws UserNotFoundException {
491 User localUser = null;
496 // Try to lookup it in visible user list
497 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
499 User next = iterator.next();
501 // Is the user id found?
502 if (Objects.equals(next.getUserId(), userId)) {
503 // Copy to other variable
510 if (null == localUser) {
511 // Not visible for the current user
512 throw new UserNotFoundException(userId);
515 // Copy all data to this bean
516 this.copyUser(localUser);
523 public List<Contact> selectableContacts () {
524 return Collections.unmodifiableList(this.selectableContacts);
528 * Adds user's name and email address to bean's internal list. It also
529 * updates the public user list if the user has decided to ha }
531 * @param user User instance
533 private void addUserNameEmailAddress (final User user) {
534 // Make sure the entry is not added yet
535 if (this.userNameList.contains(user.getUserName())) {
537 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
538 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
540 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
544 this.userNameList.add(user.getUserName());
547 this.contactController.addEmailAddress(user.getUserContact().getContactEmailAddress());
553 private void clear () {
556 this.setUserId(null);
557 this.setUserProfileMode(null);
560 this.setUserName(null);
561 this.setUserPassword(null);
562 this.setUserPasswordRepeat(null);
566 * Copies given user into the controller
568 * @param user User instance
570 private void copyUser (final User user) {
573 this.setUserId(user.getUserId());
574 this.setUserProfileMode(user.getUserProfileMode());
576 // Get cellphone, phone and fax instance
577 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
578 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
579 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();