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.GregorianCalendar;
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.Observes;
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.jcontacts.contact.UserContact;
37 import org.mxchange.jcontacts.contact.gender.Gender;
38 import org.mxchange.jcountry.data.Country;
39 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
40 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
41 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
42 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
43 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
44 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
45 import org.mxchange.jphone.phonenumbers.smsprovider.SmsProvider;
46 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
47 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
48 import org.mxchange.jusercore.exceptions.UserNotFoundException;
49 import org.mxchange.jusercore.model.user.LoginUser;
50 import org.mxchange.jusercore.model.user.User;
51 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
52 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
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;
69 /////////////////////// Properties /////////////////////
73 private Date birthday;
76 * Cellphone number's carrier
78 private SmsProvider cellphoneCarrier;
83 private Long cellphoneNumber;
93 private String comment;
98 private Country country;
103 private String emailAddress;
106 * Email address 1 (changing)
108 private String emailAddress1;
111 * Email address 2 (repeat in changing)
113 private String emailAddress2;
118 private List<String> emailAddressList;
121 * Email address repeated
123 private String emailAddressRepeat;
128 private String familyName;
131 * Fax number's area code
133 private Integer faxAreaCode;
136 * Country instance for fax number
138 private Country faxCountry;
143 private Long faxNumber;
148 private String firstName;
153 private Gender gender;
158 private Short houseNumber;
161 * Login bean (controller)
164 private PizzaUserLoginWebSessionController loginController;
167 * Phone number area code
169 private Integer phoneAreaCode;
172 * Country instance for phone number
174 private Country phoneCountry;
179 private Long phoneNumber;
184 private String street;
189 private final UserSessionBeanRemote userBean;
199 private String userName;
204 private List<String> userNameList;
207 * User password (unencrypted from web form)
209 private String userPassword;
212 * User password repeated (unencrypted from web form)
214 private String userPasswordRepeat;
217 * Whether the user wants a public profile
219 private ProfileMode userProfileMode;
222 * A list of all public user profiles
224 private List<User> visibleUserList;
229 private Integer zipCode;
232 * Default constructor
234 public PizzaUserWebSessionBean () {
235 // Set gender to UNKNOWN
236 this.gender = Gender.UNKNOWN;
240 // Get initial context
241 Context context = new InitialContext();
244 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
245 } catch (final NamingException e) {
247 throw new FaceletException(e);
252 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
254 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
256 // event should not be null
259 throw new NullPointerException("event is null"); //NOI18N
260 } else if (event.getUser() == null) {
262 throw new NullPointerException("event.user is null"); //NOI18N
263 } else if (event.getUser().getUserId() == null) {
265 throw new NullPointerException("event.user.userId is null"); //NOI18N
266 } else if (event.getUser().getUserId() < 1) {
268 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
272 User registeredUser = event.getUser();
275 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
277 // Copy all data from registered->user
278 this.copyUser(registeredUser);
280 // Add user name and email address
281 this.addUserNameEmailAddress(registeredUser);
287 this.setUserId(registeredUser.getUserId());
289 // Is the account public?
290 if (registeredUser.getUserProfileMode().equals(ProfileMode.PUBLIC)) {
291 // Also add it to this list
292 this.visibleUserList.add(registeredUser);
296 System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
300 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
302 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
304 // event should not be null
307 throw new NullPointerException("event is null"); //NOI18N
308 } else if (event.getUser() == null) {
310 throw new NullPointerException("event.user is null"); //NOI18N
311 } else if (event.getUser().getUserId() == null) {
313 throw new NullPointerException("event.user.userId is null"); //NOI18N
314 } else if (event.getUser().getUserId() < 1) {
316 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
319 // Re-initialize list
320 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
322 // Copy all data to this bean
323 this.copyUser(event.getUser());
326 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
330 public List<User> allVisibleUsers () {
332 return Collections.unmodifiableList(this.visibleUserList);
336 public User createUserInstance () {
338 //this.getLogger().logTrace("createUserInstance: CALLED!");
340 // Required personal data must be set
341 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
343 // Create new user instance
344 User user = new LoginUser();
345 user.setUserName(this.getUserName());
346 user.setUserProfileMode(this.getUserProfileMode());
347 user.setUserCreated(new GregorianCalendar());
349 // Generate phone number
350 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber(), new GregorianCalendar());
351 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber(), new GregorianCalendar());
352 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber(), new GregorianCalendar());
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 SmsProvider) && (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.setContactCreated(new GregorianCalendar());
425 contact.setContactOwnContact(Boolean.TRUE);
427 // Set contact in user
428 user.setUserContact(contact);
431 //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 SmsProvider getCellphoneCarrier () {
448 return this.cellphoneCarrier;
452 public void setCellphoneCarrier (final SmsProvider 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 isRequiredPersonalDataSet () {
734 return ((this.getUserName() != null) &&
735 (this.getUserProfileMode() != null) &&
736 (this.getGender() != null) &&
737 (this.getFirstName() != null) &&
738 (this.getFamilyName() != null) &&
739 (this.getStreet() != null) &&
740 (this.getHouseNumber() != null) &&
741 (this.getZipCode() != null) &&
742 (this.getCity() != null) &&
743 (this.getEmailAddress() != null) &&
744 (this.getEmailAddressRepeat() != null) &&
745 (this.getUserPassword() != null) &&
746 (this.getUserPasswordRepeat() != null));
750 public boolean isRequiredChangePersonalDataSet () {
751 return ((this.getUserProfileMode() != null) &&
752 (this.getGender() != null) &&
753 (this.getFirstName() != null) &&
754 (this.getFamilyName() != null) &&
755 (this.getStreet() != null) &&
756 (this.getHouseNumber() != null) &&
757 (this.getZipCode() != null) &&
758 (this.getCity() != null));
762 public boolean isSameEmailAddressEntered () {
763 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
767 public boolean isSamePasswordEntered () {
768 return (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 {
791 // Try to lookup it in visible user list
792 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
794 User next = iterator.next();
796 // Is the user id found?
797 if (Objects.equals(next.getUserId(), userId)) {
798 // Copy to other variable
806 // Not visible for the current user
807 throw new UserNotFoundException(userId);
815 * Adds user's name and email address to bean's internal list. It also
816 * updates the public user list if the user has decided to ha }
818 * @param user User instance
820 private void addUserNameEmailAddress (final User user) {
821 // Make sure the entry is not added yet
822 if (this.userNameList.contains(user.getUserName())) {
824 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
825 } else if (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())) {
827 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
831 this.userNameList.add(user.getUserName());
834 this.emailAddressList.add(user.getUserContact().getContactEmailAddress());
838 * Clears all data in this bean
840 private void clearData () {
843 this.setUserId(null);
844 this.setGender(Gender.UNKNOWN);
845 this.setUserProfileMode(null);
846 this.setFirstName(null);
847 this.setFamilyName(null);
848 this.setStreet(null);
849 this.setHouseNumber(null);
850 this.setZipCode(null);
852 this.setCountry(null);
855 this.setEmailAddress(null);
856 this.setEmailAddressRepeat(null);
857 this.setPhoneAreaCode(null);
858 this.setCellphoneCarrier(null);
859 this.setFaxAreaCode(null);
862 this.setBirthday(null);
863 this.setComment(null);
864 this.setUserName(null);
865 this.setUserPassword(null);
866 this.setUserPasswordRepeat(null);
870 * Copies given user into the controller
872 * @param user User instance
874 private void copyUser (final User user) {
877 this.setUserId(user.getUserId());
878 this.setUserProfileMode(user.getUserProfileMode());
879 this.setGender(user.getUserContact().getContactGender());
880 this.setFirstName(user.getUserContact().getContactFirstName());
881 this.setFamilyName(user.getUserContact().getContactFamilyName());
882 this.setStreet(user.getUserContact().getContactStreet());
883 this.setHouseNumber(user.getUserContact().getContactHouseNumber());
884 this.setZipCode(user.getUserContact().getContactZipCode());
885 this.setCity(user.getUserContact().getContactCity());
886 this.setCountry(user.getUserContact().getContactCountry());
888 // Get cellphone, phone and fax instance
889 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
890 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
891 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();
894 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
895 this.setPhoneCountry(phone.getPhoneCountry());
896 this.setPhoneAreaCode(phone.getPhoneAreaCode());
897 this.setPhoneNumber(phone.getPhoneNumber());
899 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof SmsProvider)) {
900 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
901 this.setCellphoneNumber(cellphone.getPhoneNumber());
903 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
904 this.setFaxCountry(fax.getPhoneCountry());
905 this.setFaxAreaCode(fax.getPhoneAreaCode());
906 this.setFaxNumber(fax.getPhoneNumber());
908 this.setEmailAddress(user.getUserContact().getContactEmailAddress());
911 this.setBirthday(user.getUserContact().getContactBirthday());
912 this.setComment(user.getUserContact().getContactComment());