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.Date;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.enterprise.context.SessionScoped;
27 import javax.enterprise.event.Observes;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.contact.UserContact;
36 import org.mxchange.jcontacts.contact.gender.Gender;
37 import org.mxchange.jcountry.data.Country;
38 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
39 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
40 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
41 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
42 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
43 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
44 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
45 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
46 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
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;
55 * A user bean (controller)
57 * @author Roland Haeder<roland@mxchange.org>
59 @Named ("userController")
61 public class PizzaUserWebSessionBean implements PizzaUserWebSessionController {
66 private static final long serialVersionUID = 542_145_347_916L;
68 /////////////////////// Properties /////////////////////
72 private Date birthday;
75 * Cellphone number's carrier
77 private MobileProvider cellphoneCarrier;
82 private Long cellphoneNumber;
92 private String comment;
97 private Country country;
102 private String emailAddress;
105 * Email address 1 (changing)
107 private String emailAddress1;
110 * Email address 2 (repeat in changing)
112 private String emailAddress2;
117 private List<String> emailAddressList;
120 * Email address repeated
122 private String emailAddressRepeat;
127 private String familyName;
130 * Fax number's area code
132 private Integer faxAreaCode;
135 * Country instance for fax number
137 private Country faxCountry;
142 private Long faxNumber;
147 private String firstName;
152 private Gender gender;
157 private Short houseNumber;
160 * Login bean (controller)
163 private PizzaUserLoginWebSessionController loginController;
166 * Phone number area code
168 private Integer phoneAreaCode;
171 * Country instance for phone number
173 private Country phoneCountry;
178 private Long phoneNumber;
183 private String street;
188 private final UserSessionBeanRemote userBean;
198 private String userName;
203 private List<String> userNameList;
206 * User password (unencrypted from web form)
208 private String userPassword;
211 * User password repeated (unencrypted from web form)
213 private String userPasswordRepeat;
216 * Whether the user wants a public profile
218 private ProfileMode userProfileMode;
221 * A list of all public user profiles
223 private List<User> visibleUserList;
228 private Integer zipCode;
231 * Default constructor
233 public PizzaUserWebSessionBean () {
234 // Set gender to UNKNOWN
235 this.gender = Gender.UNKNOWN;
239 // Get initial context
240 Context context = new InitialContext();
243 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
244 } catch (final NamingException e) {
246 throw new FaceletException(e);
251 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
253 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
255 // event should not be null
258 throw new NullPointerException("event is null"); //NOI18N
259 } else if (event.getRegisteredUser() == null) {
261 throw new NullPointerException("event.user is null"); //NOI18N
262 } else if (event.getRegisteredUser().getUserId() == null) {
264 throw new NullPointerException("event.user.userId is null"); //NOI18N
265 } else if (event.getRegisteredUser().getUserId() < 1) {
267 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
271 User registeredUser = event.getRegisteredUser();
274 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
276 // Copy all data from registered->user
277 this.copyUser(registeredUser);
279 // Add user name and email address
280 this.addUserNameEmailAddress(registeredUser);
286 this.setUserId(registeredUser.getUserId());
288 // Is the account public?
289 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
290 // Also add it to this list
291 this.visibleUserList.add(registeredUser);
295 System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
299 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
301 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
303 // event should not be null
306 throw new NullPointerException("event is null"); //NOI18N
307 } else if (event.getLoggedInUser() == null) {
309 throw new NullPointerException("event.user is null"); //NOI18N
310 } else if (event.getLoggedInUser().getUserId() == null) {
312 throw new NullPointerException("event.user.userId is null"); //NOI18N
313 } else if (event.getLoggedInUser().getUserId() < 1) {
315 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
318 // Re-initialize list
319 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
321 // Copy all data to this bean
322 this.copyUser(event.getLoggedInUser());
325 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
329 public List<User> allVisibleUsers () {
331 return Collections.unmodifiableList(this.visibleUserList);
335 public User createUserInstance () {
337 //this.getLogger().logTrace("createUserInstance: CALLED!");
339 // Required personal data must be set
340 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
342 // Create new user instance
343 User localUser = new LoginUser();
345 // Update all data ...
346 localUser.setUserName(this.getUserName());
347 localUser.setUserProfileMode(this.getUserProfileMode());
349 // Generate phone number
350 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
351 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
352 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
354 // Create new contact
355 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
356 contact.setContactStreet(this.getStreet());
357 contact.setContactHouseNumber(this.getHouseNumber());
358 contact.setContactZipCode(this.getZipCode());
359 contact.setContactCity(this.getCity());
360 contact.setContactCountry(this.getCountry());
361 contact.setContactEmailAddress(this.getEmailAddress());
363 // Don't set null or wrong references
364 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
365 // Now the number must be given
366 if (phone.getPhoneAreaCode() == null) {
368 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
369 } else if (phone.getPhoneAreaCode() < 1) {
371 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
372 } else if (phone.getPhoneNumber() == null) {
374 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
375 } else if (phone.getPhoneNumber() < 1) {
377 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
381 contact.setContactLandLineNumber(phone);
384 // Don't set null or wrong references
385 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
386 // Now the number must be given
387 if (fax.getPhoneAreaCode() == null) {
389 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
390 } else if (fax.getPhoneAreaCode() < 1) {
392 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
393 } else if (fax.getPhoneNumber() == null) {
395 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
396 } else if (fax.getPhoneNumber() < 1) {
398 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
402 contact.setContactFaxNumber(fax);
405 // Is the provider set?
406 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
407 // Is the number set?
408 if (cellphone.getPhoneNumber() == null) {
410 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
411 } else if (cellphone.getPhoneNumber() < 1) {
413 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
416 // Set cellphone number
417 contact.setContactCellphoneNumber(cellphone);
420 contact.setContactBirthday(this.getBirthday());
421 contact.setContactComment(this.getComment());
423 // Created timestamp and ownContact
424 contact.setContactOwnContact(Boolean.TRUE);
426 // Set contact in user
427 localUser.setUserContact(contact);
430 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
437 public Date getBirthday () {
438 return this.birthday;
442 public void setBirthday (final Date birthday) {
443 this.birthday = birthday;
447 public MobileProvider getCellphoneCarrier () {
448 return this.cellphoneCarrier;
452 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
453 this.cellphoneCarrier = cellphoneCarrier;
457 public Long getCellphoneNumber () {
458 return this.cellphoneNumber;
462 public void setCellphoneNumber (Long cellphoneNumber) {
463 this.cellphoneNumber = cellphoneNumber;
467 public String getCity () {
472 public void setCity (final String city) {
477 public String getComment () {
482 public void setComment (final String comment) {
483 this.comment = comment;
487 public Country getCountry () {
492 public void setCountry (final Country country) {
493 this.country = country;
497 public String getEmailAddress () {
498 return this.emailAddress;
502 public void setEmailAddress (final String emailAddress) {
503 this.emailAddress = emailAddress;
507 public String getEmailAddress1 () {
508 return this.emailAddress1;
512 public void setEmailAddress1 (final String emailAddress1) {
513 this.emailAddress1 = emailAddress1;
517 public String getEmailAddress2 () {
518 return this.emailAddress2;
522 public void setEmailAddress2 (final String emailAddress2) {
523 this.emailAddress2 = emailAddress2;
527 public String getEmailAddressRepeat () {
528 return this.emailAddressRepeat;
532 public void setEmailAddressRepeat (final String emailAddressRepeat) {
533 this.emailAddressRepeat = emailAddressRepeat;
537 public String getFamilyName () {
538 return this.familyName;
542 public void setFamilyName (final String familyName) {
543 this.familyName = familyName;
547 public Integer getFaxAreaCode () {
548 return this.faxAreaCode;
552 public void setFaxAreaCode (final Integer faxAreaCode) {
553 this.faxAreaCode = faxAreaCode;
557 public Country getFaxCountry () {
558 return this.faxCountry;
562 public void setFaxCountry (final Country faxCountry) {
563 this.faxCountry = faxCountry;
567 public Long getFaxNumber () {
568 return this.faxNumber;
572 public void setFaxNumber (final Long faxNumber) {
573 this.faxNumber = faxNumber;
577 public String getFirstName () {
578 return this.firstName;
582 public void setFirstName (final String firstName) {
583 this.firstName = firstName;
587 public Gender getGender () {
592 public void setGender (final Gender gender) {
593 this.gender = gender;
597 public Short getHouseNumber () {
598 return this.houseNumber;
602 public void setHouseNumber (final Short houseNumber) {
603 this.houseNumber = houseNumber;
607 public Integer getPhoneAreaCode () {
608 return this.phoneAreaCode;
612 public void setPhoneAreaCode (final Integer phoneAreaCode) {
613 this.phoneAreaCode = phoneAreaCode;
617 public Country getPhoneCountry () {
618 return this.phoneCountry;
622 public void setPhoneCountry (final Country phoneCountry) {
623 this.phoneCountry = phoneCountry;
627 public Long getPhoneNumber () {
628 return this.phoneNumber;
632 public void setPhoneNumber (final Long phoneNumber) {
633 this.phoneNumber = phoneNumber;
637 public String getStreet () {
642 public void setStreet (final String street) {
643 this.street = street;
647 public Long getUserId () {
652 public void setUserId (final Long userId) {
653 this.userId = userId;
657 public String getUserName () {
658 return this.userName;
662 public void setUserName (final String userName) {
663 this.userName = userName;
667 public String getUserPassword () {
668 return this.userPassword;
672 public void setUserPassword (final String userPassword) {
673 this.userPassword = userPassword;
677 public String getUserPasswordRepeat () {
678 return this.userPasswordRepeat;
682 public void setUserPasswordRepeat (final String userPasswordRepeat) {
683 this.userPasswordRepeat = userPasswordRepeat;
687 public ProfileMode getUserProfileMode () {
688 return this.userProfileMode;
692 public void setUserProfileMode (final ProfileMode userProfileMode) {
693 this.userProfileMode = userProfileMode;
697 public Integer getZipCode () {
702 public void setZipCode (final Integer zipCode) {
703 this.zipCode = zipCode;
707 * Post-initialization of this class
710 public void init () {
711 // Get full user name list for reducing EJB calls
712 this.userNameList = this.userBean.getUserNameList();
714 // Get full email address list for reducing EJB calls
715 this.emailAddressList = this.userBean.getEmailAddressList();
717 // Is the user logged-in?
718 if (this.loginController.isUserLoggedIn()) {
719 // Is logged-in, so load also users visible to memebers
720 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
722 // Initialize user list
723 this.visibleUserList = this.userBean.allPublicUsers();
728 public boolean isEmailAddressRegistered (final User user) {
729 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())));
733 public boolean isRequiredChangePersonalDataSet () {
734 return ((this.getUserProfileMode() != null) &&
735 (this.getGender() != null) &&
736 (this.getFirstName() != null) &&
737 (this.getFamilyName() != null) &&
738 (this.getStreet() != null) &&
739 (this.getHouseNumber() != null) &&
740 (this.getZipCode() != null) &&
741 (this.getCity() != null));
745 public boolean isRequiredPersonalDataSet () {
746 return ((this.getUserName() != null) &&
747 (this.getUserProfileMode() != null) &&
748 (this.getGender() != null) &&
749 (this.getFirstName() != null) &&
750 (this.getFamilyName() != null) &&
751 (this.getStreet() != null) &&
752 (this.getHouseNumber() != null) &&
753 (this.getZipCode() != null) &&
754 (this.getCity() != null) &&
755 (this.getEmailAddress() != null) &&
756 (this.getEmailAddressRepeat() != null) &&
757 (this.getUserPassword() != null) &&
758 (this.getUserPasswordRepeat() != null));
762 public boolean isSameEmailAddressEntered () {
763 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
767 public boolean isSamePasswordEntered () {
768 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
772 public boolean isUserIdEmpty () {
773 return ((this.getUserId() == null) || (this.getUserId() == 0));
777 public boolean isUserNameRegistered (final User user) {
778 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
782 public boolean isVisibleUserFound () {
783 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
787 public User lookupUserById (final Long userId) throws UserNotFoundException {
789 User localUser = null;
794 // Try to lookup it in visible user list
795 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
797 User next = iterator.next();
799 // Is the user id found?
800 if (Objects.equals(next.getUserId(), userId)) {
801 // Copy to other variable
808 if (null == localUser) {
809 // Not visible for the current user
810 throw new UserNotFoundException(userId);
813 // Copy all data to this bean
814 this.copyUser(localUser);
821 * Adds user's name and email address to bean's internal list. It also
822 * updates the public user list if the user has decided to ha }
824 * @param user User instance
826 private void addUserNameEmailAddress (final User user) {
827 // Make sure the entry is not added yet
828 if (this.userNameList.contains(user.getUserName())) {
830 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
831 } else if (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())) {
833 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
837 this.userNameList.add(user.getUserName());
840 this.emailAddressList.add(user.getUserContact().getContactEmailAddress());
846 private void clear () {
849 this.setUserId(null);
850 this.setGender(Gender.UNKNOWN);
851 this.setUserProfileMode(null);
852 this.setFirstName(null);
853 this.setFamilyName(null);
854 this.setStreet(null);
855 this.setHouseNumber(null);
856 this.setZipCode(null);
858 this.setCountry(null);
861 this.setEmailAddress(null);
862 this.setEmailAddressRepeat(null);
863 this.setPhoneAreaCode(null);
864 this.setCellphoneCarrier(null);
865 this.setFaxAreaCode(null);
868 this.setBirthday(null);
869 this.setComment(null);
870 this.setUserName(null);
871 this.setUserPassword(null);
872 this.setUserPasswordRepeat(null);
876 * Copies given user into the controller
878 * @param user User instance
880 private void copyUser (final User user) {
883 this.setUserId(user.getUserId());
884 this.setUserProfileMode(user.getUserProfileMode());
885 this.setGender(user.getUserContact().getContactGender());
886 this.setFirstName(user.getUserContact().getContactFirstName());
887 this.setFamilyName(user.getUserContact().getContactFamilyName());
888 this.setStreet(user.getUserContact().getContactStreet());
889 this.setHouseNumber(user.getUserContact().getContactHouseNumber());
890 this.setZipCode(user.getUserContact().getContactZipCode());
891 this.setCity(user.getUserContact().getContactCity());
892 this.setCountry(user.getUserContact().getContactCountry());
894 // Get cellphone, phone and fax instance
895 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
896 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
897 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();
900 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
901 this.setPhoneCountry(phone.getPhoneCountry());
902 this.setPhoneAreaCode(phone.getPhoneAreaCode());
903 this.setPhoneNumber(phone.getPhoneNumber());
905 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
906 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
907 this.setCellphoneNumber(cellphone.getPhoneNumber());
909 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
910 this.setFaxCountry(fax.getPhoneCountry());
911 this.setFaxAreaCode(fax.getPhoneAreaCode());
912 this.setFaxNumber(fax.getPhoneNumber());
914 this.setEmailAddress(user.getUserContact().getContactEmailAddress());
917 this.setBirthday(user.getUserContact().getContactBirthday());
918 this.setComment(user.getUserContact().getContactComment());