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.util.Collections;
20 import java.util.Date;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.SessionScoped;
26 import javax.enterprise.event.Event;
27 import javax.enterprise.inject.Any;
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.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.contact.UserContact;
36 import org.mxchange.jcontacts.contact.gender.Gender;
37 import org.mxchange.jcountry.data.Country;
38 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
39 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
40 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
41 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
42 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
43 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
44 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
45 import org.mxchange.jusercore.events.user.AdminAddedUserEvent;
46 import org.mxchange.jusercore.events.user.AdminUserAddedEvent;
47 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
48 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
49 import org.mxchange.jusercore.exceptions.UserNotFoundException;
50 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
51 import org.mxchange.jusercore.model.user.LoginUser;
52 import org.mxchange.jusercore.model.user.User;
53 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
54 import org.mxchange.jusercore.model.user.UserUtils;
55 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
56 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
59 * A user bean (controller)
61 * @author Roland Haeder<roland@mxchange.org>
63 @Named ("adminUserController")
65 public class AddressbookAdminUserWebSessionBean implements AddressbookAdminUserWebSessionController {
70 private static final long serialVersionUID = 542_145_347_916L;
73 * An event fired when the administrator has added a new user
77 private Event<AdminAddedUserEvent> addedUserEvent;
82 private Date birthday;
85 * Cellphone number's carrier
87 private MobileProvider cellphoneCarrier;
92 private Long cellphoneNumber;
102 private String comment;
107 private Country country;
112 private String emailAddress;
117 private String familyName;
120 * Fax number's area code
122 private Integer faxAreaCode;
125 * Country instance for fax number
127 private Country faxCountry;
132 private Long faxNumber;
137 private String firstName;
142 private Gender gender;
147 private Short houseNumber;
150 * Phone number area code
152 private Integer phoneAreaCode;
155 * Country instance for phone number
157 private Country phoneCountry;
162 private Long phoneNumber;
167 private String street;
177 private final UserSessionBeanRemote userBean;
180 * A list of all user profiles
182 private List<User> userList;
187 private String userName;
190 * User password (unencrypted from web form)
192 private String userPassword;
195 * User password repeated (unencrypted from web form)
197 private String userPasswordRepeat;
202 private Integer zipCode;
205 * Regular user controller
208 private AddressbookUserWebSessionController userController;
211 * Default constructor
213 public AddressbookAdminUserWebSessionBean () {
214 // Set gender to UNKNOWN
215 this.gender = Gender.UNKNOWN;
219 // Get initial context
220 Context context = new InitialContext();
223 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
224 } catch (final NamingException e) {
226 throw new FaceletException(e);
231 public void addUser () {
232 // Create new user instance
233 User localUser = new LoginUser();
235 // Set user name, CONFIRMED and INVISIBLE
236 localUser.setUserName(this.getUserName());
237 localUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
238 localUser.setUserProfileMode(ProfileMode.INVISIBLE);
240 // Generate phone number
241 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
242 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
243 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
245 // Create new contact
246 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
247 contact.setContactStreet(this.getStreet());
248 contact.setContactHouseNumber(this.getHouseNumber());
249 contact.setContactZipCode(this.getZipCode());
250 contact.setContactCity(this.getCity());
251 contact.setContactCountry(this.getCountry());
252 contact.setContactEmailAddress(this.getEmailAddress());
254 // Don't set null or wrong references
255 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
256 // Now the number must be given
257 if (phone.getPhoneAreaCode() == null) {
259 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
260 } else if (phone.getPhoneAreaCode() < 1) {
262 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
263 } else if (phone.getPhoneNumber() == null) {
265 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
266 } else if (phone.getPhoneNumber() < 1) {
268 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
272 contact.setContactLandLineNumber(phone);
275 // Don't set null or wrong references
276 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
277 // Now the number must be given
278 if (fax.getPhoneAreaCode() == null) {
280 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
281 } else if (fax.getPhoneAreaCode() < 1) {
283 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
284 } else if (fax.getPhoneNumber() == null) {
286 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
287 } else if (fax.getPhoneNumber() < 1) {
289 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
293 contact.setContactFaxNumber(fax);
296 // Is the provider set?
297 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
298 // Is the number set?
299 if (cellphone.getPhoneNumber() == null) {
301 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
302 } else if (cellphone.getPhoneNumber() < 1) {
304 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
307 // Set cellphone number
308 contact.setContactCellphoneNumber(cellphone);
311 contact.setContactBirthday(this.getBirthday());
312 contact.setContactComment(this.getComment());
314 // Set contact in user
315 localUser.setUserContact(contact);
317 // Init variable for password
318 String password = null;
320 // Is the user name or email address used already?
321 // @TODO Add password length check
322 if (this.userController.isUserNameRegistered(localUser)) {
323 // User name is already used
324 throw new FaceletException(new UserNameAlreadyRegisteredException(localUser));
325 } else if (this.userController.isEmailAddressRegistered(localUser)) {
326 // Email address is already used
327 throw new FaceletException(new EmailAddressAlreadyRegisteredException(localUser));
328 } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
329 // Empty password entered, then generate one
330 password = UserUtils.createRandomPassword(AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
331 } else if (!this.isSamePasswordEntered()) {
332 // Both passwords don't match
333 throw new FaceletException(new UserPasswordMismatchException(localUser));
335 // Both match, so get it from this bean
336 password = this.getUserPassword();
339 // The password should not be null and at least 5 characters long
340 assert (password != null) : "password is null"; //NOI18N
341 assert (password.length() >= AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
343 // Encrypt password and set it
344 localUser.setUserEncryptedPassword(UserUtils.encryptPassword(password));
346 // Init updated user instance
347 User updatedUser = null;
350 // Now, that all is set, call EJB
351 updatedUser = this.userBean.addUser(localUser);
352 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
354 throw new FaceletException(ex);
358 this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
360 // Add user to local list
361 this.userList.add(updatedUser);
368 public List<User> allUsers () {
370 return Collections.unmodifiableList(this.userList);
374 public Date getBirthday () {
375 return this.birthday;
379 public void setBirthday (final Date birthday) {
380 this.birthday = birthday;
384 public MobileProvider getCellphoneCarrier () {
385 return this.cellphoneCarrier;
389 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
390 this.cellphoneCarrier = cellphoneCarrier;
394 public Long getCellphoneNumber () {
395 return this.cellphoneNumber;
399 public void setCellphoneNumber (Long cellphoneNumber) {
400 this.cellphoneNumber = cellphoneNumber;
404 public String getCity () {
409 public void setCity (final String city) {
414 public String getComment () {
419 public void setComment (final String comment) {
420 this.comment = comment;
424 public Country getCountry () {
429 public void setCountry (final Country country) {
430 this.country = country;
434 public String getEmailAddress () {
435 return this.emailAddress;
439 public void setEmailAddress (final String emailAddress) {
440 this.emailAddress = emailAddress;
444 public String getFamilyName () {
445 return this.familyName;
449 public void setFamilyName (final String familyName) {
450 this.familyName = familyName;
454 public Integer getFaxAreaCode () {
455 return this.faxAreaCode;
459 public void setFaxAreaCode (final Integer faxAreaCode) {
460 this.faxAreaCode = faxAreaCode;
464 public Country getFaxCountry () {
465 return this.faxCountry;
469 public void setFaxCountry (final Country faxCountry) {
470 this.faxCountry = faxCountry;
474 public Long getFaxNumber () {
475 return this.faxNumber;
479 public void setFaxNumber (final Long faxNumber) {
480 this.faxNumber = faxNumber;
484 public String getFirstName () {
485 return this.firstName;
489 public void setFirstName (final String firstName) {
490 this.firstName = firstName;
494 public Gender getGender () {
499 public void setGender (final Gender gender) {
500 this.gender = gender;
504 public Short getHouseNumber () {
505 return this.houseNumber;
509 public void setHouseNumber (final Short houseNumber) {
510 this.houseNumber = houseNumber;
514 public Integer getPhoneAreaCode () {
515 return this.phoneAreaCode;
519 public void setPhoneAreaCode (final Integer phoneAreaCode) {
520 this.phoneAreaCode = phoneAreaCode;
524 public Country getPhoneCountry () {
525 return this.phoneCountry;
529 public void setPhoneCountry (final Country phoneCountry) {
530 this.phoneCountry = phoneCountry;
534 public Long getPhoneNumber () {
535 return this.phoneNumber;
539 public void setPhoneNumber (final Long phoneNumber) {
540 this.phoneNumber = phoneNumber;
544 public String getStreet () {
549 public void setStreet (final String street) {
550 this.street = street;
554 public User getUser () {
559 public void setUser (final User user) {
564 public String getUserName () {
565 return this.userName;
569 public void setUserName (final String userName) {
570 this.userName = userName;
574 public String getUserPassword () {
575 return this.userPassword;
579 public void setUserPassword (final String userPassword) {
580 this.userPassword = userPassword;
584 public String getUserPasswordRepeat () {
585 return this.userPasswordRepeat;
589 public void setUserPasswordRepeat (final String userPasswordRepeat) {
590 this.userPasswordRepeat = userPasswordRepeat;
594 public Integer getZipCode () {
599 public void setZipCode (final Integer zipCode) {
600 this.zipCode = zipCode;
604 public boolean hasUsers () {
605 return (!this.allUsers().isEmpty());
609 * Post-initialization of this class
612 public void init () {
613 // Initialize user list
614 this.userList = this.userBean.allUsers();
618 public User lookupUserById (final Long userId) throws UserNotFoundException {
622 // Try to lookup it in visible user list
623 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
625 User next = iterator.next();
627 // Is the user id found?
628 if (Objects.equals(next.getUserId(), userId)) {
629 // Copy to other variable
637 // Not visible for the current user
638 throw new UserNotFoundException(userId);
648 private void clear () {
650 this.setBirthday(null);
651 this.setCellphoneCarrier(null);
652 this.setCellphoneNumber(null);
654 this.setComment(null);
655 this.setCountry(null);
656 this.setEmailAddress(null);
657 this.setFamilyName(null);
658 this.setFaxAreaCode(null);
659 this.setFaxCountry(null);
660 this.setFaxNumber(null);
661 this.setFirstName(null);
662 this.setGender(null);
663 this.setHouseNumber(null);
664 this.setPhoneAreaCode(null);
665 this.setPhoneCountry(null);
666 this.setPhoneNumber(null);
667 this.setStreet(null);
668 this.setUserName(null);
669 this.setUserPassword(null);
670 this.setUserPasswordRepeat(null);
671 this.setZipCode(null);
678 * Checks if same password is entered and that they are not empty.
680 * @return Whether the same password was entered
682 private boolean isSamePasswordEntered () {
683 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));