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.getUser() == null) {
266 throw new NullPointerException("event.user is null"); //NOI18N
267 } else if (event.getUser().getUserId() == null) {
269 throw new NullPointerException("event.user.userId is null"); //NOI18N
270 } else if (event.getUser().getUserId() < 1) {
272 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
276 User registeredUser = event.getUser();
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 (registeredUser.getUserProfileMode().equals(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.getUser() == null) {
314 throw new NullPointerException("event.user is null"); //NOI18N
315 } else if (event.getUser().getUserId() == null) {
317 throw new NullPointerException("event.user.userId is null"); //NOI18N
318 } else if (event.getUser().getUserId() < 1) {
320 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
323 // Re-initialize list
324 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
326 // Copy all data to this bean
327 this.copyUser(event.getUser());
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 user = new LoginUser();
349 user.setUserName(this.getUserName());
350 user.setUserProfileMode(this.getUserProfileMode());
352 // Generate phone number
353 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
354 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
355 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
357 // Create new contact
358 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
359 contact.setContactStreet(this.getStreet());
360 contact.setContactHouseNumber(this.getHouseNumber());
361 contact.setContactZipCode(this.getZipCode());
362 contact.setContactCity(this.getCity());
363 contact.setContactCountry(this.getCountry());
364 contact.setContactEmailAddress(this.getEmailAddress());
366 // Don't set null or wrong references
367 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
368 // Now the number must be given
369 if (phone.getPhoneAreaCode() == null) {
371 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
372 } else if (phone.getPhoneAreaCode() < 1) {
374 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
375 } else if (phone.getPhoneNumber() == null) {
377 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
378 } else if (phone.getPhoneNumber() < 1) {
380 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
384 contact.setContactLandLineNumber(phone);
387 // Don't set null or wrong references
388 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
389 // Now the number must be given
390 if (fax.getPhoneAreaCode() == null) {
392 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
393 } else if (fax.getPhoneAreaCode() < 1) {
395 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
396 } else if (fax.getPhoneNumber() == null) {
398 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
399 } else if (fax.getPhoneNumber() < 1) {
401 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
405 contact.setContactFaxNumber(fax);
408 // Is the provider set?
409 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
410 // Is the number set?
411 if (cellphone.getPhoneNumber() == null) {
413 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
414 } else if (cellphone.getPhoneNumber() < 1) {
416 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
419 // Set cellphone number
420 contact.setContactCellphoneNumber(cellphone);
423 contact.setContactBirthday(this.getBirthday());
424 contact.setContactComment(this.getComment());
426 // Created timestamp and ownContact
427 contact.setContactOwnContact(Boolean.TRUE);
429 // Set contact in user
430 user.setUserContact(contact);
433 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
439 public Date getBirthday () {
440 return this.birthday;
444 public void setBirthday (final Date birthday) {
445 this.birthday = birthday;
449 public MobileProvider getCellphoneCarrier () {
450 return this.cellphoneCarrier;
454 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
455 this.cellphoneCarrier = cellphoneCarrier;
459 public Long getCellphoneNumber () {
460 return this.cellphoneNumber;
464 public void setCellphoneNumber (Long cellphoneNumber) {
465 this.cellphoneNumber = cellphoneNumber;
469 public String getCity () {
474 public void setCity (final String city) {
479 public String getComment () {
484 public void setComment (final String comment) {
485 this.comment = comment;
489 public Country getCountry () {
494 public void setCountry (final Country country) {
495 this.country = country;
499 public String getEmailAddress () {
500 return this.emailAddress;
504 public void setEmailAddress (final String emailAddress) {
505 this.emailAddress = emailAddress;
509 public String getEmailAddress1 () {
510 return this.emailAddress1;
514 public void setEmailAddress1 (final String emailAddress1) {
515 this.emailAddress1 = emailAddress1;
519 public String getEmailAddress2 () {
520 return this.emailAddress2;
524 public void setEmailAddress2 (final String emailAddress2) {
525 this.emailAddress2 = emailAddress2;
529 public String getEmailAddressRepeat () {
530 return this.emailAddressRepeat;
534 public void setEmailAddressRepeat (final String emailAddressRepeat) {
535 this.emailAddressRepeat = emailAddressRepeat;
539 public String getFamilyName () {
540 return this.familyName;
544 public void setFamilyName (final String familyName) {
545 this.familyName = familyName;
549 public Integer getFaxAreaCode () {
550 return this.faxAreaCode;
554 public void setFaxAreaCode (final Integer faxAreaCode) {
555 this.faxAreaCode = faxAreaCode;
559 public Country getFaxCountry () {
560 return this.faxCountry;
564 public void setFaxCountry (final Country faxCountry) {
565 this.faxCountry = faxCountry;
569 public Long getFaxNumber () {
570 return this.faxNumber;
574 public void setFaxNumber (final Long faxNumber) {
575 this.faxNumber = faxNumber;
579 public String getFirstName () {
580 return this.firstName;
584 public void setFirstName (final String firstName) {
585 this.firstName = firstName;
589 public Gender getGender () {
594 public void setGender (final Gender gender) {
595 this.gender = gender;
599 public Short getHouseNumber () {
600 return this.houseNumber;
604 public void setHouseNumber (final Short houseNumber) {
605 this.houseNumber = houseNumber;
609 public Integer getPhoneAreaCode () {
610 return this.phoneAreaCode;
614 public void setPhoneAreaCode (final Integer phoneAreaCode) {
615 this.phoneAreaCode = phoneAreaCode;
619 public Country getPhoneCountry () {
620 return this.phoneCountry;
624 public void setPhoneCountry (final Country phoneCountry) {
625 this.phoneCountry = phoneCountry;
629 public Long getPhoneNumber () {
630 return this.phoneNumber;
634 public void setPhoneNumber (final Long phoneNumber) {
635 this.phoneNumber = phoneNumber;
639 public String getStreet () {
644 public void setStreet (final String street) {
645 this.street = street;
649 public User getUser () {
654 public void setUser (final User user) {
659 public Long getUserId () {
664 public void setUserId (final Long userId) {
665 this.userId = userId;
669 public String getUserName () {
670 return this.userName;
674 public void setUserName (final String userName) {
675 this.userName = userName;
679 public String getUserPassword () {
680 return this.userPassword;
684 public void setUserPassword (final String userPassword) {
685 this.userPassword = userPassword;
689 public String getUserPasswordRepeat () {
690 return this.userPasswordRepeat;
694 public void setUserPasswordRepeat (final String userPasswordRepeat) {
695 this.userPasswordRepeat = userPasswordRepeat;
699 public ProfileMode getUserProfileMode () {
700 return this.userProfileMode;
704 public void setUserProfileMode (final ProfileMode userProfileMode) {
705 this.userProfileMode = userProfileMode;
709 public Integer getZipCode () {
714 public void setZipCode (final Integer zipCode) {
715 this.zipCode = zipCode;
719 * Post-initialization of this class
722 public void init () {
723 // Get full user name list for reducing EJB calls
724 this.userNameList = this.userBean.getUserNameList();
726 // Get full email address list for reducing EJB calls
727 this.emailAddressList = this.userBean.getEmailAddressList();
729 // Is the user logged-in?
730 if (this.loginController.isUserLoggedIn()) {
731 // Is logged-in, so load also users visible to memebers
732 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
734 // Initialize user list
735 this.visibleUserList = this.userBean.allPublicUsers();
740 public boolean isEmailAddressRegistered (final User user) {
741 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())));
745 public boolean isRequiredChangePersonalDataSet () {
746 return ((this.getUserProfileMode() != null) &&
747 (this.getGender() != null) &&
748 (this.getFirstName() != null) &&
749 (this.getFamilyName() != null) &&
750 (this.getStreet() != null) &&
751 (this.getHouseNumber() != null) &&
752 (this.getZipCode() != null) &&
753 (this.getCity() != null));
757 public boolean isRequiredPersonalDataSet () {
758 return ((this.getUserName() != null) &&
759 (this.getUserProfileMode() != null) &&
760 (this.getGender() != null) &&
761 (this.getFirstName() != null) &&
762 (this.getFamilyName() != null) &&
763 (this.getStreet() != null) &&
764 (this.getHouseNumber() != null) &&
765 (this.getZipCode() != null) &&
766 (this.getCity() != null) &&
767 (this.getEmailAddress() != null) &&
768 (this.getEmailAddressRepeat() != null) &&
769 (this.getUserPassword() != null) &&
770 (this.getUserPasswordRepeat() != null));
774 public boolean isSameEmailAddressEntered () {
775 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
779 public boolean isSamePasswordEntered () {
780 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
784 public boolean isUserIdEmpty () {
785 return ((this.getUserId() == null) || (this.getUserId() == 0));
789 public boolean isUserNameRegistered (final User user) {
790 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
794 public boolean isVisibleUserFound () {
795 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
799 public User lookupUserById (final Long userId) throws UserNotFoundException {
801 User localUser = null;
806 // Try to lookup it in visible user list
807 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
809 User next = iterator.next();
811 // Is the user id found?
812 if (Objects.equals(next.getUserId(), userId)) {
813 // Copy to other variable
820 if (null == localUser) {
821 // Not visible for the current user
822 throw new UserNotFoundException(userId);
825 // Copy all data to this bean
826 this.copyUser(localUser);
833 * Adds user's name and email address to bean's internal list. It also
834 * updates the public user list if the user has decided to ha }
836 * @param user User instance
838 private void addUserNameEmailAddress (final User user) {
839 // Make sure the entry is not added yet
840 if (this.userNameList.contains(user.getUserName())) {
842 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
843 } else if (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())) {
845 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
849 this.userNameList.add(user.getUserName());
852 this.emailAddressList.add(user.getUserContact().getContactEmailAddress());
858 private void clear () {
861 this.setUserId(null);
862 this.setGender(Gender.UNKNOWN);
863 this.setUserProfileMode(null);
864 this.setFirstName(null);
865 this.setFamilyName(null);
866 this.setStreet(null);
867 this.setHouseNumber(null);
868 this.setZipCode(null);
870 this.setCountry(null);
873 this.setEmailAddress(null);
874 this.setEmailAddressRepeat(null);
875 this.setPhoneAreaCode(null);
876 this.setCellphoneCarrier(null);
877 this.setFaxAreaCode(null);
880 this.setBirthday(null);
881 this.setComment(null);
882 this.setUserName(null);
883 this.setUserPassword(null);
884 this.setUserPasswordRepeat(null);
891 * Copies given user into the controller
893 * @param user User instance
895 private void copyUser (final User user) {
898 this.setUserId(user.getUserId());
899 this.setUserProfileMode(user.getUserProfileMode());
900 this.setGender(user.getUserContact().getContactGender());
901 this.setFirstName(user.getUserContact().getContactFirstName());
902 this.setFamilyName(user.getUserContact().getContactFamilyName());
903 this.setStreet(user.getUserContact().getContactStreet());
904 this.setHouseNumber(user.getUserContact().getContactHouseNumber());
905 this.setZipCode(user.getUserContact().getContactZipCode());
906 this.setCity(user.getUserContact().getContactCity());
907 this.setCountry(user.getUserContact().getContactCountry());
909 // Get cellphone, phone and fax instance
910 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
911 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
912 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();
915 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
916 this.setPhoneCountry(phone.getPhoneCountry());
917 this.setPhoneAreaCode(phone.getPhoneAreaCode());
918 this.setPhoneNumber(phone.getPhoneNumber());
920 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
921 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
922 this.setCellphoneNumber(cellphone.getPhoneNumber());
924 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
925 this.setFaxCountry(fax.getPhoneCountry());
926 this.setFaxAreaCode(fax.getPhoneAreaCode());
927 this.setFaxNumber(fax.getPhoneNumber());
929 this.setEmailAddress(user.getUserContact().getContactEmailAddress());
932 this.setBirthday(user.getUserContact().getContactBirthday());
933 this.setComment(user.getUserContact().getContactComment());