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.smsprovider.SmsProvider;
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 SmsProvider 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.getUser() == null) {
261 throw new NullPointerException("event.user is null"); //NOI18N
262 } else if (event.getUser().getUserId() == null) {
264 throw new NullPointerException("event.user.userId is null"); //NOI18N
265 } else if (event.getUser().getUserId() < 1) {
267 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
271 User registeredUser = event.getUser();
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 (registeredUser.getUserProfileMode().equals(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.getUser() == null) {
309 throw new NullPointerException("event.user is null"); //NOI18N
310 } else if (event.getUser().getUserId() == null) {
312 throw new NullPointerException("event.user.userId is null"); //NOI18N
313 } else if (event.getUser().getUserId() < 1) {
315 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
318 // Re-initialize list
319 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
321 // Copy all data to this bean
322 this.copyUser(event.getUser());
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 user = new LoginUser();
344 user.setUserName(this.getUserName());
345 user.setUserProfileMode(this.getUserProfileMode());
347 // Generate phone number
348 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
349 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
350 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
352 // Create new contact
353 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
354 contact.setContactStreet(this.getStreet());
355 contact.setContactHouseNumber(this.getHouseNumber());
356 contact.setContactZipCode(this.getZipCode());
357 contact.setContactCity(this.getCity());
358 contact.setContactCountry(this.getCountry());
359 contact.setContactEmailAddress(this.getEmailAddress());
361 // Don't set null or wrong references
362 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
363 // Now the number must be given
364 if (phone.getPhoneAreaCode() == null) {
366 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
367 } else if (phone.getPhoneAreaCode() < 1) {
369 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
370 } else if (phone.getPhoneNumber() == null) {
372 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
373 } else if (phone.getPhoneNumber() < 1) {
375 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
379 contact.setContactLandLineNumber(phone);
382 // Don't set null or wrong references
383 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
384 // Now the number must be given
385 if (fax.getPhoneAreaCode() == null) {
387 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
388 } else if (fax.getPhoneAreaCode() < 1) {
390 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
391 } else if (fax.getPhoneNumber() == null) {
393 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
394 } else if (fax.getPhoneNumber() < 1) {
396 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
400 contact.setContactFaxNumber(fax);
403 // Is the provider set?
404 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof SmsProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
405 // Is the number set?
406 if (cellphone.getPhoneNumber() == null) {
408 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
409 } else if (cellphone.getPhoneNumber() < 1) {
411 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
414 // Set cellphone number
415 contact.setContactCellphoneNumber(cellphone);
418 contact.setContactBirthday(this.getBirthday());
419 contact.setContactComment(this.getComment());
421 // Created timestamp and ownContact
422 contact.setContactOwnContact(Boolean.TRUE);
424 // Set contact in user
425 user.setUserContact(contact);
428 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
435 public Date getBirthday () {
436 return this.birthday;
440 public void setBirthday (final Date birthday) {
441 this.birthday = birthday;
445 public SmsProvider getCellphoneCarrier () {
446 return this.cellphoneCarrier;
450 public void setCellphoneCarrier (final SmsProvider cellphoneCarrier) {
451 this.cellphoneCarrier = cellphoneCarrier;
455 public Long getCellphoneNumber () {
456 return this.cellphoneNumber;
460 public void setCellphoneNumber (Long cellphoneNumber) {
461 this.cellphoneNumber = cellphoneNumber;
465 public String getCity () {
470 public void setCity (final String city) {
475 public String getComment () {
480 public void setComment (final String comment) {
481 this.comment = comment;
485 public Country getCountry () {
490 public void setCountry (final Country country) {
491 this.country = country;
495 public String getEmailAddress () {
496 return this.emailAddress;
500 public void setEmailAddress (final String emailAddress) {
501 this.emailAddress = emailAddress;
505 public String getEmailAddress1 () {
506 return this.emailAddress1;
510 public void setEmailAddress1 (final String emailAddress1) {
511 this.emailAddress1 = emailAddress1;
515 public String getEmailAddress2 () {
516 return this.emailAddress2;
520 public void setEmailAddress2 (final String emailAddress2) {
521 this.emailAddress2 = emailAddress2;
525 public String getEmailAddressRepeat () {
526 return this.emailAddressRepeat;
530 public void setEmailAddressRepeat (final String emailAddressRepeat) {
531 this.emailAddressRepeat = emailAddressRepeat;
535 public String getFamilyName () {
536 return this.familyName;
540 public void setFamilyName (final String familyName) {
541 this.familyName = familyName;
545 public Integer getFaxAreaCode () {
546 return this.faxAreaCode;
550 public void setFaxAreaCode (final Integer faxAreaCode) {
551 this.faxAreaCode = faxAreaCode;
555 public Country getFaxCountry () {
556 return this.faxCountry;
560 public void setFaxCountry (final Country faxCountry) {
561 this.faxCountry = faxCountry;
565 public Long getFaxNumber () {
566 return this.faxNumber;
570 public void setFaxNumber (final Long faxNumber) {
571 this.faxNumber = faxNumber;
575 public String getFirstName () {
576 return this.firstName;
580 public void setFirstName (final String firstName) {
581 this.firstName = firstName;
585 public Gender getGender () {
590 public void setGender (final Gender gender) {
591 this.gender = gender;
595 public Short getHouseNumber () {
596 return this.houseNumber;
600 public void setHouseNumber (final Short houseNumber) {
601 this.houseNumber = houseNumber;
605 public Integer getPhoneAreaCode () {
606 return this.phoneAreaCode;
610 public void setPhoneAreaCode (final Integer phoneAreaCode) {
611 this.phoneAreaCode = phoneAreaCode;
615 public Country getPhoneCountry () {
616 return this.phoneCountry;
620 public void setPhoneCountry (final Country phoneCountry) {
621 this.phoneCountry = phoneCountry;
625 public Long getPhoneNumber () {
626 return this.phoneNumber;
630 public void setPhoneNumber (final Long phoneNumber) {
631 this.phoneNumber = phoneNumber;
635 public String getStreet () {
640 public void setStreet (final String street) {
641 this.street = street;
645 public Long getUserId () {
650 public void setUserId (final Long userId) {
651 this.userId = userId;
655 public String getUserName () {
656 return this.userName;
660 public void setUserName (final String userName) {
661 this.userName = userName;
665 public String getUserPassword () {
666 return this.userPassword;
670 public void setUserPassword (final String userPassword) {
671 this.userPassword = userPassword;
675 public String getUserPasswordRepeat () {
676 return this.userPasswordRepeat;
680 public void setUserPasswordRepeat (final String userPasswordRepeat) {
681 this.userPasswordRepeat = userPasswordRepeat;
685 public ProfileMode getUserProfileMode () {
686 return this.userProfileMode;
690 public void setUserProfileMode (final ProfileMode userProfileMode) {
691 this.userProfileMode = userProfileMode;
695 public Integer getZipCode () {
700 public void setZipCode (final Integer zipCode) {
701 this.zipCode = zipCode;
705 * Post-initialization of this class
708 public void init () {
709 // Get full user name list for reducing EJB calls
710 this.userNameList = this.userBean.getUserNameList();
712 // Get full email address list for reducing EJB calls
713 this.emailAddressList = this.userBean.getEmailAddressList();
715 // Is the user logged-in?
716 if (this.loginController.isUserLoggedIn()) {
717 // Is logged-in, so load also users visible to memebers
718 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
720 // Initialize user list
721 this.visibleUserList = this.userBean.allPublicUsers();
726 public boolean isEmailAddressRegistered (final User user) {
727 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())));
731 public boolean isRequiredChangePersonalDataSet () {
732 return ((this.getUserProfileMode() != null) &&
733 (this.getGender() != null) &&
734 (this.getFirstName() != null) &&
735 (this.getFamilyName() != null) &&
736 (this.getStreet() != null) &&
737 (this.getHouseNumber() != null) &&
738 (this.getZipCode() != null) &&
739 (this.getCity() != null));
743 public boolean isRequiredPersonalDataSet () {
744 return ((this.getUserName() != null) &&
745 (this.getUserProfileMode() != null) &&
746 (this.getGender() != null) &&
747 (this.getFirstName() != null) &&
748 (this.getFamilyName() != null) &&
749 (this.getStreet() != null) &&
750 (this.getHouseNumber() != null) &&
751 (this.getZipCode() != null) &&
752 (this.getCity() != null) &&
753 (this.getEmailAddress() != null) &&
754 (this.getEmailAddressRepeat() != null) &&
755 (this.getUserPassword() != null) &&
756 (this.getUserPasswordRepeat() != null));
760 public boolean isSameEmailAddressEntered () {
761 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
765 public boolean isSamePasswordEntered () {
766 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
770 public boolean isUserIdEmpty () {
771 return ((this.getUserId() == null) || (this.getUserId() == 0));
775 public boolean isUserNameRegistered (final User user) {
776 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
780 public boolean isVisibleUserFound () {
781 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
785 public User lookupUserById (final Long userId) throws UserNotFoundException {
789 // Try to lookup it in visible user list
790 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
792 User next = iterator.next();
794 // Is the user id found?
795 if (Objects.equals(next.getUserId(), userId)) {
796 // Copy to other variable
804 // Not visible for the current user
805 throw new UserNotFoundException(userId);
813 * Adds user's name and email address to bean's internal list. It also
814 * updates the public user list if the user has decided to ha }
816 * @param user User instance
818 private void addUserNameEmailAddress (final User user) {
819 // Make sure the entry is not added yet
820 if (this.userNameList.contains(user.getUserName())) {
822 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
823 } else if (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())) {
825 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
829 this.userNameList.add(user.getUserName());
832 this.emailAddressList.add(user.getUserContact().getContactEmailAddress());
836 * Clears all data in this bean
838 private void clearData () {
841 this.setUserId(null);
842 this.setGender(Gender.UNKNOWN);
843 this.setUserProfileMode(null);
844 this.setFirstName(null);
845 this.setFamilyName(null);
846 this.setStreet(null);
847 this.setHouseNumber(null);
848 this.setZipCode(null);
850 this.setCountry(null);
853 this.setEmailAddress(null);
854 this.setEmailAddressRepeat(null);
855 this.setPhoneAreaCode(null);
856 this.setCellphoneCarrier(null);
857 this.setFaxAreaCode(null);
860 this.setBirthday(null);
861 this.setComment(null);
862 this.setUserName(null);
863 this.setUserPassword(null);
864 this.setUserPasswordRepeat(null);
868 * Copies given user into the controller
870 * @param user User instance
872 private void copyUser (final User user) {
875 this.setUserId(user.getUserId());
876 this.setUserProfileMode(user.getUserProfileMode());
877 this.setGender(user.getUserContact().getContactGender());
878 this.setFirstName(user.getUserContact().getContactFirstName());
879 this.setFamilyName(user.getUserContact().getContactFamilyName());
880 this.setStreet(user.getUserContact().getContactStreet());
881 this.setHouseNumber(user.getUserContact().getContactHouseNumber());
882 this.setZipCode(user.getUserContact().getContactZipCode());
883 this.setCity(user.getUserContact().getContactCity());
884 this.setCountry(user.getUserContact().getContactCountry());
886 // Get cellphone, phone and fax instance
887 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
888 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
889 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();
892 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
893 this.setPhoneCountry(phone.getPhoneCountry());
894 this.setPhoneAreaCode(phone.getPhoneAreaCode());
895 this.setPhoneNumber(phone.getPhoneNumber());
897 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof SmsProvider)) {
898 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
899 this.setCellphoneNumber(cellphone.getPhoneNumber());
901 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
902 this.setFaxCountry(fax.getPhoneCountry());
903 this.setFaxAreaCode(fax.getPhoneAreaCode());
904 this.setFaxNumber(fax.getPhoneNumber());
906 this.setEmailAddress(user.getUserContact().getContactEmailAddress());
909 this.setBirthday(user.getUserContact().getContactBirthday());
910 this.setComment(user.getUserContact().getContactComment());