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();
313 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
317 public List<User> allVisibleUsers () {
319 return Collections.unmodifiableList(this.visibleUserList);
323 public User createUserInstance () {
325 //this.getLogger().logTrace("createUserInstance: CALLED!");
327 // Required personal data must be set
328 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
330 // Create new user instance
331 User user = new LoginUser();
332 user.setUserName(this.getUserName());
333 user.setUserProfileMode(this.getUserProfileMode());
334 user.setUserCreated(new GregorianCalendar());
336 // Generate phone number
337 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber(), new GregorianCalendar());
338 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber(), new GregorianCalendar());
339 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber(), new GregorianCalendar());
341 // Create new contact
342 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
343 contact.setContactStreet(this.getStreet());
344 contact.setContactHouseNumber(this.getHouseNumber());
345 contact.setContactZipCode(this.getZipCode());
346 contact.setContactCity(this.getCity());
347 contact.setContactCountry(this.getCountry());
348 contact.setContactEmailAddress(this.getEmailAddress());
350 // Don't set null or wrong references
351 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
352 // Now the number must be given
353 if (phone.getPhoneAreaCode() == null) {
355 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
356 } else if (phone.getPhoneAreaCode() < 1) {
358 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
359 } else if (phone.getPhoneNumber() == null) {
361 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
362 } else if (phone.getPhoneNumber() < 1) {
364 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
368 contact.setContactPhoneNumber(phone);
371 // Don't set null or wrong references
372 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
373 // Now the number must be given
374 if (fax.getPhoneAreaCode() == null) {
376 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
377 } else if (fax.getPhoneAreaCode() < 1) {
379 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
380 } else if (fax.getPhoneNumber() == null) {
382 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
383 } else if (fax.getPhoneNumber() < 1) {
385 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
389 contact.setContactFaxNumber(fax);
392 // Is the provider set?
393 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof SmsProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
394 // Is the number set?
395 if (cellphone.getPhoneNumber() == null) {
397 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
398 } else if (cellphone.getPhoneNumber() < 1) {
400 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
403 // Set cellphone number
404 contact.setContactCellphoneNumber(cellphone);
407 contact.setContactBirthday(this.getBirthday());
408 contact.setContactComment(this.getComment());
410 // Created timestamp and ownContact
411 contact.setContactCreated(new GregorianCalendar());
412 contact.setContactOwnContact(Boolean.TRUE);
414 // Set contact in user
415 user.setUserContact(contact);
418 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
424 public Date getBirthday () {
425 return this.birthday;
429 public void setBirthday (final Date birthday) {
430 this.birthday = birthday;
434 public SmsProvider getCellphoneCarrier () {
435 return this.cellphoneCarrier;
439 public void setCellphoneCarrier (final SmsProvider cellphoneCarrier) {
440 this.cellphoneCarrier = cellphoneCarrier;
444 public Long getCellphoneNumber () {
445 return this.cellphoneNumber;
449 public void setCellphoneNumber (Long cellphoneNumber) {
450 this.cellphoneNumber = cellphoneNumber;
454 public String getCity () {
459 public void setCity (final String city) {
464 public String getComment () {
469 public void setComment (final String comment) {
470 this.comment = comment;
474 public Country getCountry () {
479 public void setCountry (final Country country) {
480 this.country = country;
484 public String getEmailAddress () {
485 return this.emailAddress;
489 public void setEmailAddress (final String emailAddress) {
490 this.emailAddress = emailAddress;
494 public String getEmailAddressRepeat () {
495 return this.emailAddressRepeat;
499 public void setEmailAddressRepeat (final String emailAddressRepeat) {
500 this.emailAddressRepeat = emailAddressRepeat;
504 public String getFamilyName () {
505 return this.familyName;
509 public void setFamilyName (final String familyName) {
510 this.familyName = familyName;
514 public Integer getFaxAreaCode () {
515 return this.faxAreaCode;
519 public void setFaxAreaCode (final Integer faxAreaCode) {
520 this.faxAreaCode = faxAreaCode;
524 public Country getFaxCountry () {
525 return this.faxCountry;
529 public void setFaxCountry (final Country faxCountry) {
530 this.faxCountry = faxCountry;
534 public Long getFaxNumber () {
535 return this.faxNumber;
539 public void setFaxNumber (final Long faxNumber) {
540 this.faxNumber = faxNumber;
544 public String getFirstName () {
545 return this.firstName;
549 public void setFirstName (final String firstName) {
550 this.firstName = firstName;
554 public Gender getGender () {
559 public void setGender (final Gender gender) {
560 this.gender = gender;
564 public Short getHouseNumber () {
565 return this.houseNumber;
569 public void setHouseNumber (final Short houseNumber) {
570 this.houseNumber = houseNumber;
574 public Integer getPhoneAreaCode () {
575 return this.phoneAreaCode;
579 public void setPhoneAreaCode (final Integer phoneAreaCode) {
580 this.phoneAreaCode = phoneAreaCode;
584 public Country getPhoneCountry () {
585 return this.phoneCountry;
589 public void setPhoneCountry (final Country phoneCountry) {
590 this.phoneCountry = phoneCountry;
594 public Long getPhoneNumber () {
595 return this.phoneNumber;
599 public void setPhoneNumber (final Long phoneNumber) {
600 this.phoneNumber = phoneNumber;
604 public String getStreet () {
609 public void setStreet (final String street) {
610 this.street = street;
614 public Long getUserId () {
619 public void setUserId (final Long userId) {
620 this.userId = userId;
624 public String getUserName () {
625 return this.userName;
629 public void setUserName (final String userName) {
630 this.userName = userName;
634 public String getUserPassword () {
635 return this.userPassword;
639 public void setUserPassword (final String userPassword) {
640 this.userPassword = userPassword;
644 public String getUserPasswordRepeat () {
645 return this.userPasswordRepeat;
649 public void setUserPasswordRepeat (final String userPasswordRepeat) {
650 this.userPasswordRepeat = userPasswordRepeat;
654 public ProfileMode getUserProfileMode () {
655 return this.userProfileMode;
659 public void setUserProfileMode (final ProfileMode userProfileMode) {
660 this.userProfileMode = userProfileMode;
664 public Integer getZipCode () {
669 public void setZipCode (final Integer zipCode) {
670 this.zipCode = zipCode;
674 * Post-initialization of this class
677 public void init () {
678 // Get full user name list for reducing EJB calls
679 this.userNameList = this.userBean.getUserNameList();
681 // Get full email address list for reducing EJB calls
682 this.emailAddressList = this.userBean.getEmailAddressList();
684 // Is the user logged-in?
685 if (this.loginController.isUserLoggedIn()) {
686 // Is logged-in, so load also users visible to memebers
687 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
689 // Initialize user list
690 this.visibleUserList = this.userBean.allPublicUsers();
695 public boolean isEmailAddressRegistered (final User user) {
696 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())));
700 public boolean isRequiredPersonalDataSet () {
701 return ((this.getUserName() != null) &&
702 (this.getUserProfileMode() != null) &&
703 (this.getGender() != null) &&
704 (this.getFirstName() != null) &&
705 (this.getFamilyName() != null) &&
706 (this.getStreet() != null) &&
707 (this.getHouseNumber() != null) &&
708 (this.getZipCode() != null) &&
709 (this.getCity() != null) &&
710 (this.getEmailAddress() != null) &&
711 (this.getEmailAddressRepeat() != null) &&
712 (this.getUserPassword() != null) &&
713 (this.getUserPasswordRepeat() != null));
717 public boolean isSameEmailAddressEntered () {
718 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
722 public boolean isSamePasswordEntered () {
723 return (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat()));
727 public boolean isUserIdEmpty () {
728 return ((this.getUserId() == null) || (this.getUserId() == 0));
732 public boolean isUserNameRegistered (final User user) {
733 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
737 public boolean isVisibleUserFound () {
738 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
742 public User lookupUserById (final Long userId) throws UserNotFoundException {
746 // Try to lookup it in visible user list
747 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
749 User next = iterator.next();
751 // Is the user id found?
752 if (Objects.equals(next.getUserId(), userId)) {
753 // Copy to other variable
761 // Not visible for the current user
762 throw new UserNotFoundException(userId);
770 * Adds user's name and email address to bean's internal list. It also
771 * updates the public user list if the user has decided to ha }
773 * @param user User instance
775 private void addUserNameEmailAddress (final User user) {
776 // Make sure the entry is not added yet
777 if (this.userNameList.contains(user.getUserName())) {
779 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
780 } else if (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())) {
782 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
786 this.userNameList.add(user.getUserName());
789 this.emailAddressList.add(user.getUserContact().getContactEmailAddress());
793 * Clears all data in this bean
795 private void clearData () {
798 this.setUserId(null);
799 this.setGender(Gender.UNKNOWN);
800 this.setUserProfileMode(null);
801 this.setFirstName(null);
802 this.setFamilyName(null);
803 this.setStreet(null);
804 this.setHouseNumber(null);
805 this.setZipCode(null);
807 this.setCountry(null);
810 this.setEmailAddress(null);
811 this.setEmailAddressRepeat(null);
812 this.setPhoneAreaCode(null);
813 this.setCellphoneCarrier(null);
814 this.setFaxAreaCode(null);
817 this.setBirthday(null);
818 this.setComment(null);
819 this.setUserName(null);
820 this.setUserPassword(null);
821 this.setUserPasswordRepeat(null);
825 * Copies given user into the controller
827 * @param user User instance
829 private void copyUser (final User user) {
832 this.setUserId(user.getUserId());
833 this.setUserProfileMode(user.getUserProfileMode());
834 this.setGender(user.getUserContact().getContactGender());
835 this.setFirstName(user.getUserContact().getContactFirstName());
836 this.setFamilyName(user.getUserContact().getContactFamilyName());
837 this.setStreet(user.getUserContact().getContactStreet());
838 this.setHouseNumber(user.getUserContact().getContactHouseNumber());
839 this.setZipCode(user.getUserContact().getContactZipCode());
840 this.setCity(user.getUserContact().getContactCity());
841 this.setCountry(user.getUserContact().getContactCountry());
843 // Get cellphone, phone and fax instance
844 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
845 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
846 DialableLandLineNumber phone = user.getUserContact().getContactPhoneNumber();
849 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
850 this.setPhoneCountry(phone.getPhoneCountry());
851 this.setPhoneAreaCode(phone.getPhoneAreaCode());
852 this.setPhoneNumber(phone.getPhoneNumber());
854 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof SmsProvider)) {
855 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
856 this.setCellphoneNumber(cellphone.getPhoneNumber());
858 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
859 this.setFaxCountry(fax.getPhoneCountry());
860 this.setFaxAreaCode(fax.getPhoneAreaCode());
861 this.setFaxNumber(fax.getPhoneNumber());
863 this.setEmailAddress(user.getUserContact().getContactEmailAddress());
866 this.setBirthday(user.getUserContact().getContactBirthday());
867 this.setComment(user.getUserContact().getContactComment());