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.pizzaapplication.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 PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionController {
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;
172 private final UserSessionBeanRemote userBean;
175 * Regular user controller
178 private PizzaUserWebSessionController userController;
181 * A list of all user profiles
183 private List<User> userList;
188 private String userName;
191 * User password (unencrypted from web form)
193 private String userPassword;
196 * User password repeated (unencrypted from web form)
198 private String userPasswordRepeat;
203 private Integer zipCode;
206 * Default constructor
208 public PizzaAdminUserWebSessionBean () {
209 // Set gender to UNKNOWN
210 this.gender = Gender.UNKNOWN;
214 // Get initial context
215 Context context = new InitialContext();
218 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
219 } catch (final NamingException e) {
221 throw new FaceletException(e);
226 public void addUser () {
227 // Create new user instance
228 User localUser = new LoginUser();
230 // Set user name, CONFIRMED and INVISIBLE
231 localUser.setUserName(this.getUserName());
232 localUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
233 localUser.setUserProfileMode(ProfileMode.INVISIBLE);
235 // Generate phone number
236 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
237 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
238 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
240 // Create new contact
241 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
242 contact.setContactStreet(this.getStreet());
243 contact.setContactHouseNumber(this.getHouseNumber());
244 contact.setContactZipCode(this.getZipCode());
245 contact.setContactCity(this.getCity());
246 contact.setContactCountry(this.getCountry());
247 contact.setContactEmailAddress(this.getEmailAddress());
249 // Don't set null or wrong references
250 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
251 // Now the number must be given
252 if (phone.getPhoneAreaCode() == null) {
254 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
255 } else if (phone.getPhoneAreaCode() < 1) {
257 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
258 } else if (phone.getPhoneNumber() == null) {
260 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
261 } else if (phone.getPhoneNumber() < 1) {
263 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
267 contact.setContactLandLineNumber(phone);
270 // Don't set null or wrong references
271 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
272 // Now the number must be given
273 if (fax.getPhoneAreaCode() == null) {
275 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
276 } else if (fax.getPhoneAreaCode() < 1) {
278 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
279 } else if (fax.getPhoneNumber() == null) {
281 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
282 } else if (fax.getPhoneNumber() < 1) {
284 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
288 contact.setContactFaxNumber(fax);
291 // Is the provider set?
292 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
293 // Is the number set?
294 if (cellphone.getPhoneNumber() == null) {
296 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
297 } else if (cellphone.getPhoneNumber() < 1) {
299 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
302 // Set cellphone number
303 contact.setContactCellphoneNumber(cellphone);
306 contact.setContactBirthday(this.getBirthday());
307 contact.setContactComment(this.getComment());
309 // Set contact in user
310 localUser.setUserContact(contact);
312 // Init variable for password
313 String password = null;
315 // Is the user name or email address used already?
316 // @TODO Add password length check
317 if (this.userController.isUserNameRegistered(localUser)) {
318 // User name is already used
319 throw new FaceletException(new UserNameAlreadyRegisteredException(localUser));
320 } else if (this.userController.isEmailAddressRegistered(localUser)) {
321 // Email address is already used
322 throw new FaceletException(new EmailAddressAlreadyRegisteredException(localUser));
323 } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
324 // Empty password entered, then generate one
325 password = UserUtils.createRandomPassword(PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
326 } else if (!this.isSamePasswordEntered()) {
327 // Both passwords don't match
328 throw new FaceletException(new UserPasswordMismatchException(localUser));
330 // Both match, so get it from this bean
331 password = this.getUserPassword();
334 // The password should not be null and at least 5 characters long
335 assert (password != null) : "password is null"; //NOI18N
336 assert (password.length() >= PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
338 // Encrypt password and set it
339 localUser.setUserEncryptedPassword(UserUtils.encryptPassword(password));
341 // Init updated user instance
342 User updatedUser = null;
345 // Now, that all is set, call EJB
346 updatedUser = this.userBean.addUser(localUser);
347 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
349 throw new FaceletException(ex);
353 this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
355 // Add user to local list
356 this.userList.add(updatedUser);
363 public List<User> allUsers () {
365 return Collections.unmodifiableList(this.userList);
369 public Date getBirthday () {
370 return this.birthday;
374 public void setBirthday (final Date birthday) {
375 this.birthday = birthday;
379 public MobileProvider getCellphoneCarrier () {
380 return this.cellphoneCarrier;
384 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
385 this.cellphoneCarrier = cellphoneCarrier;
389 public Long getCellphoneNumber () {
390 return this.cellphoneNumber;
394 public void setCellphoneNumber (Long cellphoneNumber) {
395 this.cellphoneNumber = cellphoneNumber;
399 public String getCity () {
404 public void setCity (final String city) {
409 public String getComment () {
414 public void setComment (final String comment) {
415 this.comment = comment;
419 public Country getCountry () {
424 public void setCountry (final Country country) {
425 this.country = country;
429 public String getEmailAddress () {
430 return this.emailAddress;
434 public void setEmailAddress (final String emailAddress) {
435 this.emailAddress = emailAddress;
439 public String getFamilyName () {
440 return this.familyName;
444 public void setFamilyName (final String familyName) {
445 this.familyName = familyName;
449 public Integer getFaxAreaCode () {
450 return this.faxAreaCode;
454 public void setFaxAreaCode (final Integer faxAreaCode) {
455 this.faxAreaCode = faxAreaCode;
459 public Country getFaxCountry () {
460 return this.faxCountry;
464 public void setFaxCountry (final Country faxCountry) {
465 this.faxCountry = faxCountry;
469 public Long getFaxNumber () {
470 return this.faxNumber;
474 public void setFaxNumber (final Long faxNumber) {
475 this.faxNumber = faxNumber;
479 public String getFirstName () {
480 return this.firstName;
484 public void setFirstName (final String firstName) {
485 this.firstName = firstName;
489 public Gender getGender () {
494 public void setGender (final Gender gender) {
495 this.gender = gender;
499 public Short getHouseNumber () {
500 return this.houseNumber;
504 public void setHouseNumber (final Short houseNumber) {
505 this.houseNumber = houseNumber;
509 public Integer getPhoneAreaCode () {
510 return this.phoneAreaCode;
514 public void setPhoneAreaCode (final Integer phoneAreaCode) {
515 this.phoneAreaCode = phoneAreaCode;
519 public Country getPhoneCountry () {
520 return this.phoneCountry;
524 public void setPhoneCountry (final Country phoneCountry) {
525 this.phoneCountry = phoneCountry;
529 public Long getPhoneNumber () {
530 return this.phoneNumber;
534 public void setPhoneNumber (final Long phoneNumber) {
535 this.phoneNumber = phoneNumber;
539 public String getStreet () {
544 public void setStreet (final String street) {
545 this.street = street;
549 public String getUserName () {
550 return this.userName;
554 public void setUserName (final String userName) {
555 this.userName = userName;
559 public String getUserPassword () {
560 return this.userPassword;
564 public void setUserPassword (final String userPassword) {
565 this.userPassword = userPassword;
569 public String getUserPasswordRepeat () {
570 return this.userPasswordRepeat;
574 public void setUserPasswordRepeat (final String userPasswordRepeat) {
575 this.userPasswordRepeat = userPasswordRepeat;
579 public Integer getZipCode () {
584 public void setZipCode (final Integer zipCode) {
585 this.zipCode = zipCode;
589 public boolean hasUsers () {
590 return (!this.allUsers().isEmpty());
594 * Post-initialization of this class
597 public void init () {
598 // Initialize user list
599 this.userList = this.userBean.allUsers();
603 public User lookupUserById (final Long userId) throws UserNotFoundException {
604 if (null == userId) {
606 throw new NullPointerException("userId is null"); //NOI18N
607 } else if (userId < 1) {
609 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
615 // Try to lookup it in visible user list
616 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
618 User next = iterator.next();
620 // Is the user id found?
621 if (Objects.equals(next.getUserId(), userId)) {
622 // Copy to other variable
630 // Not visible for the current user
631 throw new UserNotFoundException(userId);
641 private void clear () {
643 this.setBirthday(null);
644 this.setCellphoneCarrier(null);
645 this.setCellphoneNumber(null);
647 this.setComment(null);
648 this.setCountry(null);
649 this.setEmailAddress(null);
650 this.setFamilyName(null);
651 this.setFaxAreaCode(null);
652 this.setFaxCountry(null);
653 this.setFaxNumber(null);
654 this.setFirstName(null);
655 this.setGender(null);
656 this.setHouseNumber(null);
657 this.setPhoneAreaCode(null);
658 this.setPhoneCountry(null);
659 this.setPhoneNumber(null);
660 this.setStreet(null);
661 this.setUserName(null);
662 this.setUserPassword(null);
663 this.setUserPasswordRepeat(null);
664 this.setZipCode(null);
668 * Checks if same password is entered and that they are not empty.
670 * @return Whether the same password was entered
672 private boolean isSamePasswordEntered () {
673 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));