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 General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (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 General Public License for more details.
14 * You should have received a copy of the GNU 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.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.addressbook.beans.login.UserLoginWebSessionController;
36 import org.mxchange.jcontacts.contact.Contact;
37 import org.mxchange.jcontacts.contact.UserContact;
38 import org.mxchange.jcontacts.contact.gender.Gender;
39 import org.mxchange.jcountry.data.Country;
40 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
41 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
42 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
43 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
44 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
45 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
46 import org.mxchange.jphone.phonenumbers.smsprovider.SmsProvider;
47 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
48 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
49 import org.mxchange.jusercore.exceptions.UserNotFoundException;
50 import org.mxchange.jusercore.model.user.LoginUser;
51 import org.mxchange.jusercore.model.user.User;
52 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
53 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
56 * A user bean (controller)
58 * @author Roland Haeder<roland@mxchange.org>
60 @Named ("userController")
62 public class UserWebSessionBean implements UserWebSessionController {
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;
108 private List<String> emailAddressList;
111 * Email address repeated
113 private String emailAddressRepeat;
118 private String familyName;
121 * Fax number's area code
123 private Integer faxAreaCode;
126 * Country instance for fax number
128 private Country faxCountry;
133 private Long faxNumber;
138 private String firstName;
143 private Gender gender;
148 private Short houseNumber;
151 * Login bean (controller)
154 private UserLoginWebSessionController loginController;
157 * Phone number area code
159 private Integer phoneAreaCode;
162 * Country instance for phone number
164 private Country phoneCountry;
169 private Long phoneNumber;
174 private String street;
179 private final UserSessionBeanRemote userBean;
189 private String userName;
194 private List<String> userNameList;
197 * User password (unencrypted from web form)
199 private String userPassword;
202 * User password repeated (unencrypted from web form)
204 private String userPasswordRepeat;
207 * Whether the user wants a public profile
209 private ProfileMode userProfileMode;
212 * A list of all public user profiles
214 private List<User> visibleUserList;
219 private Integer zipCode;
222 * Default constructor
224 public UserWebSessionBean () {
225 // Set gender to UNKNOWN
226 this.gender = Gender.UNKNOWN;
230 // Get initial context
231 Context context = new InitialContext();
234 this.userBean = (UserSessionBeanRemote) context.lookup("ejb/stateless-addressbook-user"); //NOI18N
235 } catch (final NamingException e) {
237 throw new FaceletException(e);
242 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
244 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
246 // event should not be null
249 throw new NullPointerException("event is null"); //NOI18N
250 } else if (event.getUser() == null) {
252 throw new NullPointerException("event.user is null"); //NOI18N
253 } else if (event.getUser().getUserId() == null) {
255 throw new NullPointerException("event.user.userId is null"); //NOI18N
256 } else if (event.getUser().getUserId() < 1) {
258 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
262 User registeredUser = event.getUser();
265 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
267 // Copy all data from registered->user
268 this.copyUser(registeredUser);
270 // Add user name and email address
271 this.addUserNameEmailAddress(registeredUser);
277 this.setUserId(registeredUser.getUserId());
279 // Is the account public?
280 if (registeredUser.getUserProfileMode().equals(ProfileMode.PUBLIC)) {
281 // Also add it to this list
282 this.visibleUserList.add(registeredUser);
286 System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
290 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
292 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
294 // event should not be null
297 throw new NullPointerException("event is null"); //NOI18N
298 } else if (event.getUser() == null) {
300 throw new NullPointerException("event.user is null"); //NOI18N
301 } else if (event.getUser().getUserId() == null) {
303 throw new NullPointerException("event.user.userId is null"); //NOI18N
304 } else if (event.getUser().getUserId() < 1) {
306 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
309 // Re-initialize list
310 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
312 // Copy all data to this bean
313 this.copyUser(event.getUser());
316 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
320 public List<User> allVisibleUsers () {
322 return Collections.unmodifiableList(this.visibleUserList);
326 public User createUserInstance () {
328 //this.getLogger().logTrace("createUserInstance: CALLED!");
330 // Required personal data must be set
331 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
333 // Create new user instance
334 User user = new LoginUser();
335 user.setUserName(this.getUserName());
336 user.setUserProfileMode(this.getUserProfileMode());
337 user.setUserCreated(new GregorianCalendar());
339 // Generate phone number
340 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber(), new GregorianCalendar());
341 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber(), new GregorianCalendar());
342 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber(), new GregorianCalendar());
344 // Create new contact
345 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
346 contact.setContactStreet(this.getStreet());
347 contact.setContactHouseNumber(this.getHouseNumber());
348 contact.setContactZipCode(this.getZipCode());
349 contact.setContactCity(this.getCity());
350 contact.setContactCountry(this.getCountry());
351 contact.setContactEmailAddress(this.getEmailAddress());
353 // Don't set null or wrong references
354 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
355 // Now the number must be given
356 if (phone.getPhoneAreaCode() == null) {
358 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
359 } else if (phone.getPhoneAreaCode() < 1) {
361 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
362 } else if (phone.getPhoneNumber() == null) {
364 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
365 } else if (phone.getPhoneNumber() < 1) {
367 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
371 contact.setContactPhoneNumber(phone);
374 // Don't set null or wrong references
375 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
376 // Now the number must be given
377 if (fax.getPhoneAreaCode() == null) {
379 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
380 } else if (fax.getPhoneAreaCode() < 1) {
382 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
383 } else if (fax.getPhoneNumber() == null) {
385 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
386 } else if (fax.getPhoneNumber() < 1) {
388 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
392 contact.setContactFaxNumber(fax);
395 // Is the provider set?
396 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof SmsProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
397 // Is the number set?
398 if (cellphone.getPhoneNumber() == null) {
400 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
401 } else if (cellphone.getPhoneNumber() < 1) {
403 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
406 // Set cellphone number
407 contact.setContactCellphoneNumber(cellphone);
410 contact.setContactBirthday(this.getBirthday());
411 contact.setContactComment(this.getComment());
413 // Created timestamp and ownContact
414 contact.setContactCreated(new GregorianCalendar());
415 contact.setContactOwnContact(Boolean.TRUE);
417 // Set contact in user
418 user.setUserContact(contact);
421 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
427 public Date getBirthday () {
428 return this.birthday;
432 public void setBirthday (final Date birthday) {
433 this.birthday = birthday;
437 public SmsProvider getCellphoneCarrier () {
438 return this.cellphoneCarrier;
442 public void setCellphoneCarrier (final SmsProvider cellphoneCarrier) {
443 this.cellphoneCarrier = cellphoneCarrier;
447 public Long getCellphoneNumber () {
448 return this.cellphoneNumber;
452 public void setCellphoneNumber (Long cellphoneNumber) {
453 this.cellphoneNumber = cellphoneNumber;
457 public String getCity () {
462 public void setCity (final String city) {
467 public String getComment () {
472 public void setComment (final String comment) {
473 this.comment = comment;
477 public Country getCountry () {
482 public void setCountry (final Country country) {
483 this.country = country;
487 public String getEmailAddress () {
488 return this.emailAddress;
492 public void setEmailAddress (final String emailAddress) {
493 this.emailAddress = emailAddress;
497 public String getEmailAddressRepeat () {
498 return this.emailAddressRepeat;
502 public void setEmailAddressRepeat (final String emailAddressRepeat) {
503 this.emailAddressRepeat = emailAddressRepeat;
507 public String getFamilyName () {
508 return this.familyName;
512 public void setFamilyName (final String familyName) {
513 this.familyName = familyName;
517 public Integer getFaxAreaCode () {
518 return this.faxAreaCode;
522 public void setFaxAreaCode (final Integer faxAreaCode) {
523 this.faxAreaCode = faxAreaCode;
527 public Country getFaxCountry () {
528 return this.faxCountry;
532 public void setFaxCountry (final Country faxCountry) {
533 this.faxCountry = faxCountry;
537 public Long getFaxNumber () {
538 return this.faxNumber;
542 public void setFaxNumber (final Long faxNumber) {
543 this.faxNumber = faxNumber;
547 public String getFirstName () {
548 return this.firstName;
552 public void setFirstName (final String firstName) {
553 this.firstName = firstName;
557 public Gender getGender () {
562 public void setGender (final Gender gender) {
563 this.gender = gender;
567 public Short getHouseNumber () {
568 return this.houseNumber;
572 public void setHouseNumber (final Short houseNumber) {
573 this.houseNumber = houseNumber;
577 public Integer getPhoneAreaCode () {
578 return this.phoneAreaCode;
582 public void setPhoneAreaCode (final Integer phoneAreaCode) {
583 this.phoneAreaCode = phoneAreaCode;
587 public Country getPhoneCountry () {
588 return this.phoneCountry;
592 public void setPhoneCountry (final Country phoneCountry) {
593 this.phoneCountry = phoneCountry;
597 public Long getPhoneNumber () {
598 return this.phoneNumber;
602 public void setPhoneNumber (final Long phoneNumber) {
603 this.phoneNumber = phoneNumber;
607 public String getStreet () {
612 public void setStreet (final String street) {
613 this.street = street;
617 public Long getUserId () {
622 public void setUserId (final Long userId) {
623 this.userId = userId;
627 public String getUserName () {
628 return this.userName;
632 public void setUserName (final String userName) {
633 this.userName = userName;
637 public String getUserPassword () {
638 return this.userPassword;
642 public void setUserPassword (final String userPassword) {
643 this.userPassword = userPassword;
647 public String getUserPasswordRepeat () {
648 return this.userPasswordRepeat;
652 public void setUserPasswordRepeat (final String userPasswordRepeat) {
653 this.userPasswordRepeat = userPasswordRepeat;
657 public ProfileMode getUserProfileMode () {
658 return this.userProfileMode;
662 public void setUserProfileMode (final ProfileMode userProfileMode) {
663 this.userProfileMode = userProfileMode;
667 public Integer getZipCode () {
672 public void setZipCode (final Integer zipCode) {
673 this.zipCode = zipCode;
677 * Post-initialization of this class
680 public void init () {
681 // Get full user name list for reducing EJB calls
682 this.userNameList = this.userBean.getUserNameList();
684 // Get full email address list for reducing EJB calls
685 this.emailAddressList = this.userBean.getEmailAddressList();
687 // Is the user logged-in?
688 if (this.loginController.isUserLoggedIn()) {
689 // Is logged-in, so load also users visible to memebers
690 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
692 // Initialize user list
693 this.visibleUserList = this.userBean.allPublicUsers();
698 public boolean isEmailAddressRegistered (final User user) {
699 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())));
703 public boolean isRequiredPersonalDataSet () {
704 return ((this.getUserName() != null) &&
705 (this.getUserProfileMode() != null) &&
706 (this.getGender() != null) &&
707 (this.getFirstName() != null) &&
708 (this.getFamilyName() != null) &&
709 (this.getStreet() != null) &&
710 (this.getHouseNumber() != null) &&
711 (this.getZipCode() != null) &&
712 (this.getCity() != null) &&
713 (this.getEmailAddress() != null) &&
714 (this.getEmailAddressRepeat() != null) &&
715 (this.getUserPassword() != null) &&
716 (this.getUserPasswordRepeat() != null));
720 public boolean isSameEmailAddressEntered () {
721 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
725 public boolean isSamePasswordEntered () {
726 return (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat()));
730 public boolean isUserIdEmpty () {
731 return ((this.getUserId() == null) || (this.getUserId() == 0));
735 public boolean isUserNameRegistered (final User user) {
736 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
740 public boolean isVisibleUserFound () {
741 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
745 public User lookupUserById (final Long userId) throws UserNotFoundException {
749 // Try to lookup it in visible user list
750 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
752 User next = iterator.next();
754 // Is the user id found?
755 if (Objects.equals(next.getUserId(), userId)) {
756 // Copy to other variable
764 // Not visible for the current user
765 throw new UserNotFoundException(userId);
773 * Adds user's name and email address to bean's internal list. It also
774 * updates the public user list if the user has decided to ha }
776 * @param user User instance
778 private void addUserNameEmailAddress (final User user) {
779 // Make sure the entry is not added yet
780 if (this.userNameList.contains(user.getUserName())) {
782 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
783 } else if (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())) {
785 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
789 this.userNameList.add(user.getUserName());
792 this.emailAddressList.add(user.getUserContact().getContactEmailAddress());
796 * Clears all data in this bean
798 private void clearData () {
801 this.setUserId(null);
802 this.setGender(Gender.UNKNOWN);
803 this.setUserProfileMode(null);
804 this.setFirstName(null);
805 this.setFamilyName(null);
806 this.setStreet(null);
807 this.setHouseNumber(null);
808 this.setZipCode(null);
810 this.setCountry(null);
813 this.setEmailAddress(null);
814 this.setEmailAddressRepeat(null);
815 this.setPhoneAreaCode(null);
816 this.setCellphoneCarrier(null);
817 this.setFaxAreaCode(null);
820 this.setBirthday(null);
821 this.setComment(null);
822 this.setUserName(null);
823 this.setUserPassword(null);
824 this.setUserPasswordRepeat(null);
828 * Copies given user into the controller
830 * @param user User instance
832 private void copyUser (final User user) {
835 this.setUserId(user.getUserId());
836 this.setUserProfileMode(user.getUserProfileMode());
837 this.setGender(user.getUserContact().getContactGender());
838 this.setFirstName(user.getUserContact().getContactFirstName());
839 this.setFamilyName(user.getUserContact().getContactFamilyName());
840 this.setStreet(user.getUserContact().getContactStreet());
841 this.setHouseNumber(user.getUserContact().getContactHouseNumber());
842 this.setZipCode(user.getUserContact().getContactZipCode());
843 this.setCity(user.getUserContact().getContactCity());
844 this.setCountry(user.getUserContact().getContactCountry());
846 // Get cellphone, phone and fax instance
847 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
848 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
849 DialableLandLineNumber phone = user.getUserContact().getContactPhoneNumber();
852 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
853 this.setPhoneCountry(phone.getPhoneCountry());
854 this.setPhoneAreaCode(phone.getPhoneAreaCode());
855 this.setPhoneNumber(phone.getPhoneNumber());
857 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof SmsProvider)) {
858 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
859 this.setCellphoneNumber(cellphone.getPhoneNumber());
861 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
862 this.setFaxCountry(fax.getPhoneCountry());
863 this.setFaxAreaCode(fax.getPhoneAreaCode());
864 this.setFaxNumber(fax.getPhoneNumber());
866 this.setEmailAddress(user.getUserContact().getContactEmailAddress());
869 this.setBirthday(user.getUserContact().getContactBirthday());
870 this.setComment(user.getUserContact().getContactComment());