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.addressbook.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.addressbook.beans.login.AddressbookUserLoginWebSessionController;
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.mobileprovider.MobileProvider;
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;
55 * A user bean (controller)
57 * @author Roland Haeder<roland@mxchange.org>
59 @Named ("userController")
61 public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionController {
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 AddressbookUserLoginWebSessionController 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;
193 private final UserSessionBeanRemote userBean;
203 private String userName;
208 private List<String> userNameList;
211 * User password (unencrypted from web form)
213 private String userPassword;
216 * User password repeated (unencrypted from web form)
218 private String userPasswordRepeat;
221 * Whether the user wants a public profile
223 private ProfileMode userProfileMode;
226 * A list of all public user profiles
228 private List<User> visibleUserList;
233 private Integer zipCode;
236 * Default constructor
238 public AddressbookUserWebSessionBean () {
239 // Set gender to UNKNOWN
240 this.gender = Gender.UNKNOWN;
244 // Get initial context
245 Context context = new InitialContext();
248 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
249 } catch (final NamingException e) {
251 throw new FaceletException(e);
256 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
258 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
260 // event should not be null
263 throw new NullPointerException("event is null"); //NOI18N
264 } else if (event.getRegisteredUser() == null) {
266 throw new NullPointerException("event.user is null"); //NOI18N
267 } else if (event.getRegisteredUser().getUserId() == null) {
269 throw new NullPointerException("event.user.userId is null"); //NOI18N
270 } else if (event.getRegisteredUser().getUserId() < 1) {
272 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
276 User registeredUser = event.getRegisteredUser();
279 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
281 // Copy all data from registered->user
282 this.copyUser(registeredUser);
284 // Add user name and email address
285 this.addUserNameEmailAddress(registeredUser);
291 this.setUserId(registeredUser.getUserId());
293 // Is the account public?
294 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
295 // Also add it to this list
296 this.visibleUserList.add(registeredUser);
300 System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
304 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
306 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
308 // event should not be null
311 throw new NullPointerException("event is null"); //NOI18N
312 } else if (event.getLoggedInUser() == null) {
314 throw new NullPointerException("event.user is null"); //NOI18N
315 } else if (event.getLoggedInUser().getUserId() == null) {
317 throw new NullPointerException("event.user.userId is null"); //NOI18N
318 } else if (event.getLoggedInUser().getUserId() < 1) {
320 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
323 // Re-initialize list
324 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
326 // Copy all data to this bean
327 this.copyUser(event.getLoggedInUser());
330 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
334 public List<User> allVisibleUsers () {
336 return Collections.unmodifiableList(this.visibleUserList);
340 public User createUserInstance () {
342 //this.getLogger().logTrace("createUserInstance: CALLED!");
344 // Required personal data must be set
345 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
347 // Create new user instance
348 User localUser = new LoginUser();
350 // Update all data ...
351 localUser.setUserName(this.getUserName());
352 localUser.setUserProfileMode(this.getUserProfileMode());
354 // Generate phone number
355 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
356 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
357 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
359 // Create new contact
360 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
361 contact.setContactStreet(this.getStreet());
362 contact.setContactHouseNumber(this.getHouseNumber());
363 contact.setContactZipCode(this.getZipCode());
364 contact.setContactCity(this.getCity());
365 contact.setContactCountry(this.getCountry());
366 contact.setContactEmailAddress(this.getEmailAddress());
368 // Don't set null or wrong references
369 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
370 // Now the number must be given
371 if (phone.getPhoneAreaCode() == null) {
373 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
374 } else if (phone.getPhoneAreaCode() < 1) {
376 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
377 } else if (phone.getPhoneNumber() == null) {
379 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
380 } else if (phone.getPhoneNumber() < 1) {
382 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
386 contact.setContactLandLineNumber(phone);
389 // Don't set null or wrong references
390 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
391 // Now the number must be given
392 if (fax.getPhoneAreaCode() == null) {
394 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
395 } else if (fax.getPhoneAreaCode() < 1) {
397 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
398 } else if (fax.getPhoneNumber() == null) {
400 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
401 } else if (fax.getPhoneNumber() < 1) {
403 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
407 contact.setContactFaxNumber(fax);
410 // Is the provider set?
411 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
412 // Is the number set?
413 if (cellphone.getPhoneNumber() == null) {
415 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
416 } else if (cellphone.getPhoneNumber() < 1) {
418 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
421 // Set cellphone number
422 contact.setContactCellphoneNumber(cellphone);
425 contact.setContactBirthday(this.getBirthday());
426 contact.setContactComment(this.getComment());
428 // Created timestamp and ownContact
429 contact.setContactOwnContact(Boolean.TRUE);
431 // Set contact in user
432 localUser.setUserContact(contact);
435 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
442 public Date getBirthday () {
443 return this.birthday;
447 public void setBirthday (final Date birthday) {
448 this.birthday = birthday;
452 public MobileProvider getCellphoneCarrier () {
453 return this.cellphoneCarrier;
457 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
458 this.cellphoneCarrier = cellphoneCarrier;
462 public Long getCellphoneNumber () {
463 return this.cellphoneNumber;
467 public void setCellphoneNumber (Long cellphoneNumber) {
468 this.cellphoneNumber = cellphoneNumber;
472 public String getCity () {
477 public void setCity (final String city) {
482 public String getComment () {
487 public void setComment (final String comment) {
488 this.comment = comment;
492 public Country getCountry () {
497 public void setCountry (final Country country) {
498 this.country = country;
502 public String getEmailAddress () {
503 return this.emailAddress;
507 public void setEmailAddress (final String emailAddress) {
508 this.emailAddress = emailAddress;
512 public String getEmailAddress1 () {
513 return this.emailAddress1;
517 public void setEmailAddress1 (final String emailAddress1) {
518 this.emailAddress1 = emailAddress1;
522 public String getEmailAddress2 () {
523 return this.emailAddress2;
527 public void setEmailAddress2 (final String emailAddress2) {
528 this.emailAddress2 = emailAddress2;
532 public String getEmailAddressRepeat () {
533 return this.emailAddressRepeat;
537 public void setEmailAddressRepeat (final String emailAddressRepeat) {
538 this.emailAddressRepeat = emailAddressRepeat;
542 public String getFamilyName () {
543 return this.familyName;
547 public void setFamilyName (final String familyName) {
548 this.familyName = familyName;
552 public Integer getFaxAreaCode () {
553 return this.faxAreaCode;
557 public void setFaxAreaCode (final Integer faxAreaCode) {
558 this.faxAreaCode = faxAreaCode;
562 public Country getFaxCountry () {
563 return this.faxCountry;
567 public void setFaxCountry (final Country faxCountry) {
568 this.faxCountry = faxCountry;
572 public Long getFaxNumber () {
573 return this.faxNumber;
577 public void setFaxNumber (final Long faxNumber) {
578 this.faxNumber = faxNumber;
582 public String getFirstName () {
583 return this.firstName;
587 public void setFirstName (final String firstName) {
588 this.firstName = firstName;
592 public Gender getGender () {
597 public void setGender (final Gender gender) {
598 this.gender = gender;
602 public Short getHouseNumber () {
603 return this.houseNumber;
607 public void setHouseNumber (final Short houseNumber) {
608 this.houseNumber = houseNumber;
612 public Integer getPhoneAreaCode () {
613 return this.phoneAreaCode;
617 public void setPhoneAreaCode (final Integer phoneAreaCode) {
618 this.phoneAreaCode = phoneAreaCode;
622 public Country getPhoneCountry () {
623 return this.phoneCountry;
627 public void setPhoneCountry (final Country phoneCountry) {
628 this.phoneCountry = phoneCountry;
632 public Long getPhoneNumber () {
633 return this.phoneNumber;
637 public void setPhoneNumber (final Long phoneNumber) {
638 this.phoneNumber = phoneNumber;
642 public String getStreet () {
647 public void setStreet (final String street) {
648 this.street = street;
652 public User getUser () {
657 public void setUser (final User user) {
662 public Long getUserId () {
667 public void setUserId (final Long userId) {
668 this.userId = userId;
672 public String getUserName () {
673 return this.userName;
677 public void setUserName (final String userName) {
678 this.userName = userName;
682 public String getUserPassword () {
683 return this.userPassword;
687 public void setUserPassword (final String userPassword) {
688 this.userPassword = userPassword;
692 public String getUserPasswordRepeat () {
693 return this.userPasswordRepeat;
697 public void setUserPasswordRepeat (final String userPasswordRepeat) {
698 this.userPasswordRepeat = userPasswordRepeat;
702 public ProfileMode getUserProfileMode () {
703 return this.userProfileMode;
707 public void setUserProfileMode (final ProfileMode userProfileMode) {
708 this.userProfileMode = userProfileMode;
712 public Integer getZipCode () {
717 public void setZipCode (final Integer zipCode) {
718 this.zipCode = zipCode;
722 * Post-initialization of this class
725 public void init () {
726 // Get full user name list for reducing EJB calls
727 this.userNameList = this.userBean.getUserNameList();
729 // Get full email address list for reducing EJB calls
730 this.emailAddressList = this.userBean.getEmailAddressList();
732 // Is the user logged-in?
733 if (this.loginController.isUserLoggedIn()) {
734 // Is logged-in, so load also users visible to memebers
735 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
737 // Initialize user list
738 this.visibleUserList = this.userBean.allPublicUsers();
743 public boolean isEmailAddressRegistered (final User user) {
744 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())));
748 public boolean isRequiredChangePersonalDataSet () {
749 return ((this.getUserProfileMode() != null) &&
750 (this.getGender() != null) &&
751 (this.getFirstName() != null) &&
752 (this.getFamilyName() != null) &&
753 (this.getStreet() != null) &&
754 (this.getHouseNumber() != null) &&
755 (this.getZipCode() != null) &&
756 (this.getCity() != null));
760 public boolean isRequiredPersonalDataSet () {
761 return ((this.getUserName() != null) &&
762 (this.getUserProfileMode() != null) &&
763 (this.getGender() != null) &&
764 (this.getFirstName() != null) &&
765 (this.getFamilyName() != null) &&
766 (this.getStreet() != null) &&
767 (this.getHouseNumber() != null) &&
768 (this.getZipCode() != null) &&
769 (this.getCity() != null) &&
770 (this.getEmailAddress() != null) &&
771 (this.getEmailAddressRepeat() != null) &&
772 (this.getUserPassword() != null) &&
773 (this.getUserPasswordRepeat() != null));
777 public boolean isSameEmailAddressEntered () {
778 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
782 public boolean isSamePasswordEntered () {
783 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
787 public boolean isUserIdEmpty () {
788 return ((this.getUserId() == null) || (this.getUserId() == 0));
792 public boolean isUserNameRegistered (final User user) {
793 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
797 public boolean isVisibleUserFound () {
798 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
802 public User lookupUserById (final Long userId) throws UserNotFoundException {
804 User localUser = null;
809 // Try to lookup it in visible user list
810 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
812 User next = iterator.next();
814 // Is the user id found?
815 if (Objects.equals(next.getUserId(), userId)) {
816 // Copy to other variable
823 if (null == localUser) {
824 // Not visible for the current user
825 throw new UserNotFoundException(userId);
828 // Copy all data to this bean
829 this.copyUser(localUser);
836 * Adds user's name and email address to bean's internal list. It also
837 * updates the public user list if the user has decided to ha }
839 * @param user User instance
841 private void addUserNameEmailAddress (final User user) {
842 // Make sure the entry is not added yet
843 if (this.userNameList.contains(user.getUserName())) {
845 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
846 } else if (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())) {
848 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
852 this.userNameList.add(user.getUserName());
855 this.emailAddressList.add(user.getUserContact().getContactEmailAddress());
861 private void clear () {
864 this.setUserId(null);
865 this.setGender(Gender.UNKNOWN);
866 this.setUserProfileMode(null);
867 this.setFirstName(null);
868 this.setFamilyName(null);
869 this.setStreet(null);
870 this.setHouseNumber(null);
871 this.setZipCode(null);
873 this.setCountry(null);
876 this.setEmailAddress(null);
877 this.setEmailAddressRepeat(null);
878 this.setPhoneAreaCode(null);
879 this.setCellphoneCarrier(null);
880 this.setFaxAreaCode(null);
883 this.setBirthday(null);
884 this.setComment(null);
885 this.setUserName(null);
886 this.setUserPassword(null);
887 this.setUserPasswordRepeat(null);
894 * Copies given user into the controller
896 * @param user User instance
898 private void copyUser (final User user) {
901 this.setUserId(user.getUserId());
902 this.setUserProfileMode(user.getUserProfileMode());
903 this.setGender(user.getUserContact().getContactGender());
904 this.setFirstName(user.getUserContact().getContactFirstName());
905 this.setFamilyName(user.getUserContact().getContactFamilyName());
906 this.setStreet(user.getUserContact().getContactStreet());
907 this.setHouseNumber(user.getUserContact().getContactHouseNumber());
908 this.setZipCode(user.getUserContact().getContactZipCode());
909 this.setCity(user.getUserContact().getContactCity());
910 this.setCountry(user.getUserContact().getContactCountry());
912 // Get cellphone, phone and fax instance
913 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
914 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
915 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();
918 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
919 this.setPhoneCountry(phone.getPhoneCountry());
920 this.setPhoneAreaCode(phone.getPhoneAreaCode());
921 this.setPhoneNumber(phone.getPhoneNumber());
923 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
924 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
925 this.setCellphoneNumber(cellphone.getPhoneNumber());
927 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
928 this.setFaxCountry(fax.getPhoneCountry());
929 this.setFaxAreaCode(fax.getPhoneAreaCode());
930 this.setFaxNumber(fax.getPhoneNumber());
932 this.setEmailAddress(user.getUserContact().getContactEmailAddress());
935 this.setBirthday(user.getUserContact().getContactBirthday());
936 this.setComment(user.getUserContact().getContactComment());