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.faces.view.facelets.FaceletException;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.jcontacts.contact.Contact;
33 import org.mxchange.jcontacts.contact.UserContact;
34 import org.mxchange.jcontacts.contact.gender.Gender;
35 import org.mxchange.jcountry.data.Country;
36 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
37 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
38 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
39 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
40 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
41 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
42 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
43 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
44 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
45 import org.mxchange.jusercore.exceptions.UserNotFoundException;
46 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
47 import org.mxchange.jusercore.model.user.LoginUser;
48 import org.mxchange.jusercore.model.user.User;
49 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
50 import org.mxchange.jusercore.model.user.UserUtils;
51 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
52 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
55 * A user bean (controller)
57 * @author Roland Haeder<roland@mxchange.org>
59 @Named ("adminUserController")
61 public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionController {
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;
107 private String familyName;
110 * Fax number's area code
112 private Integer faxAreaCode;
115 * Country instance for fax number
117 private Country faxCountry;
122 private Long faxNumber;
127 private String firstName;
132 private Gender gender;
137 private Short houseNumber;
140 * Phone number area code
142 private Integer phoneAreaCode;
145 * Country instance for phone number
147 private Country phoneCountry;
152 private Long phoneNumber;
157 private String street;
162 private final UserSessionBeanRemote userBean;
165 * Regular user controller
168 private PizzaUserWebSessionController userController;
171 * A list of all user profiles
173 private List<User> userList;
178 private String userName;
181 * User password (unencrypted from web form)
183 private String userPassword;
186 * User password repeated (unencrypted from web form)
188 private String userPasswordRepeat;
193 private Integer zipCode;
196 * Default constructor
198 public PizzaAdminUserWebSessionBean () {
199 // Set gender to UNKNOWN
200 this.gender = Gender.UNKNOWN;
204 // Get initial context
205 Context context = new InitialContext();
208 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
209 } catch (final NamingException e) {
211 throw new FaceletException(e);
216 public void addUser () {
217 // Create new user instance
218 User user = new LoginUser();
219 user.setUserName(this.getUserName());
220 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
221 user.setUserProfileMode(ProfileMode.INVISIBLE);
223 // Generate phone number
224 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
225 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
226 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
228 // Create new contact
229 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
230 contact.setContactStreet(this.getStreet());
231 contact.setContactHouseNumber(this.getHouseNumber());
232 contact.setContactZipCode(this.getZipCode());
233 contact.setContactCity(this.getCity());
234 contact.setContactCountry(this.getCountry());
235 contact.setContactEmailAddress(this.getEmailAddress());
237 // Don't set null or wrong references
238 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
239 // Now the number must be given
240 if (phone.getPhoneAreaCode() == null) {
242 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
243 } else if (phone.getPhoneAreaCode() < 1) {
245 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
246 } else if (phone.getPhoneNumber() == null) {
248 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
249 } else if (phone.getPhoneNumber() < 1) {
251 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
255 contact.setContactLandLineNumber(phone);
258 // Don't set null or wrong references
259 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
260 // Now the number must be given
261 if (fax.getPhoneAreaCode() == null) {
263 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
264 } else if (fax.getPhoneAreaCode() < 1) {
266 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
267 } else if (fax.getPhoneNumber() == null) {
269 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
270 } else if (fax.getPhoneNumber() < 1) {
272 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
276 contact.setContactFaxNumber(fax);
279 // Is the provider set?
280 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
281 // Is the number set?
282 if (cellphone.getPhoneNumber() == null) {
284 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
285 } else if (cellphone.getPhoneNumber() < 1) {
287 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
290 // Set cellphone number
291 contact.setContactCellphoneNumber(cellphone);
294 contact.setContactBirthday(this.getBirthday());
295 contact.setContactComment(this.getComment());
297 // Set contact in user
298 user.setUserContact(contact);
300 // Init variable for password
301 String password = null;
303 // Is the user name or email address used already?
304 // @TODO Add password length check
305 if (this.userController.isUserNameRegistered(user)) {
306 // User name is already used
307 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
308 } else if (this.userController.isEmailAddressRegistered(user)) {
309 // Email address is already used
310 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
311 } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
312 // Empty password entered, then generate one
313 password = UserUtils.createRandomPassword(PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
314 } else if (!this.isSamePasswordEntered()) {
315 // Both passwords don't match
316 throw new FaceletException(new UserPasswordMismatchException(user));
318 // Both match, so get it from this bean
319 password = this.getUserPassword();
322 // The password should not be null and at least 5 characters long
323 assert (password != null) : "password is null";
324 assert (password.length() >= PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough.";
326 // Encrypt password and set it
327 user.setUserEncryptedPassword(UserUtils.encryptPassword(password));
330 // Now, that all is set, call EJB
331 this.userBean.addUser(user);
332 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
334 throw new FaceletException(ex);
342 public List<User> allUsers () {
344 return Collections.unmodifiableList(this.userList);
348 public Date getBirthday () {
349 return this.birthday;
353 public void setBirthday (final Date birthday) {
354 this.birthday = birthday;
358 public MobileProvider getCellphoneCarrier () {
359 return this.cellphoneCarrier;
363 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
364 this.cellphoneCarrier = cellphoneCarrier;
368 public Long getCellphoneNumber () {
369 return this.cellphoneNumber;
373 public void setCellphoneNumber (Long cellphoneNumber) {
374 this.cellphoneNumber = cellphoneNumber;
378 public String getCity () {
383 public void setCity (final String city) {
388 public String getComment () {
393 public void setComment (final String comment) {
394 this.comment = comment;
398 public Country getCountry () {
403 public void setCountry (final Country country) {
404 this.country = country;
408 public String getEmailAddress () {
409 return this.emailAddress;
413 public void setEmailAddress (final String emailAddress) {
414 this.emailAddress = emailAddress;
418 public String getFamilyName () {
419 return this.familyName;
423 public void setFamilyName (final String familyName) {
424 this.familyName = familyName;
428 public Integer getFaxAreaCode () {
429 return this.faxAreaCode;
433 public void setFaxAreaCode (final Integer faxAreaCode) {
434 this.faxAreaCode = faxAreaCode;
438 public Country getFaxCountry () {
439 return this.faxCountry;
443 public void setFaxCountry (final Country faxCountry) {
444 this.faxCountry = faxCountry;
448 public Long getFaxNumber () {
449 return this.faxNumber;
453 public void setFaxNumber (final Long faxNumber) {
454 this.faxNumber = faxNumber;
458 public String getFirstName () {
459 return this.firstName;
463 public void setFirstName (final String firstName) {
464 this.firstName = firstName;
468 public Gender getGender () {
473 public void setGender (final Gender gender) {
474 this.gender = gender;
478 public Short getHouseNumber () {
479 return this.houseNumber;
483 public void setHouseNumber (final Short houseNumber) {
484 this.houseNumber = houseNumber;
488 public Integer getPhoneAreaCode () {
489 return this.phoneAreaCode;
493 public void setPhoneAreaCode (final Integer phoneAreaCode) {
494 this.phoneAreaCode = phoneAreaCode;
498 public Country getPhoneCountry () {
499 return this.phoneCountry;
503 public void setPhoneCountry (final Country phoneCountry) {
504 this.phoneCountry = phoneCountry;
508 public Long getPhoneNumber () {
509 return this.phoneNumber;
513 public void setPhoneNumber (final Long phoneNumber) {
514 this.phoneNumber = phoneNumber;
518 public String getStreet () {
523 public void setStreet (final String street) {
524 this.street = street;
528 public String getUserName () {
529 return this.userName;
533 public void setUserName (final String userName) {
534 this.userName = userName;
538 public String getUserPassword () {
539 return this.userPassword;
543 public void setUserPassword (final String userPassword) {
544 this.userPassword = userPassword;
548 public String getUserPasswordRepeat () {
549 return this.userPasswordRepeat;
553 public void setUserPasswordRepeat (final String userPasswordRepeat) {
554 this.userPasswordRepeat = userPasswordRepeat;
558 public Integer getZipCode () {
563 public void setZipCode (final Integer zipCode) {
564 this.zipCode = zipCode;
568 public boolean hasUsers () {
569 return (!this.allUsers().isEmpty());
573 * Post-initialization of this class
576 public void init () {
577 // Initialize user list
578 this.userList = this.userBean.allUsers();
582 public User lookupUserById (final Long userId) throws UserNotFoundException {
586 // Try to lookup it in visible user list
587 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
589 User next = iterator.next();
591 // Is the user id found?
592 if (Objects.equals(next.getUserId(), userId)) {
593 // Copy to other variable
601 // Not visible for the current user
602 throw new UserNotFoundException(userId);
612 private void clear () {
614 this.setBirthday(null);
615 this.setCellphoneCarrier(null);
616 this.setCellphoneNumber(null);
618 this.setComment(null);
619 this.setCountry(null);
620 this.setEmailAddress(null);
621 this.setFamilyName(null);
622 this.setFaxAreaCode(null);
623 this.setFaxCountry(null);
624 this.setFaxNumber(null);
625 this.setFirstName(null);
626 this.setGender(null);
627 this.setHouseNumber(null);
628 this.setPhoneAreaCode(null);
629 this.setPhoneCountry(null);
630 this.setPhoneNumber(null);
631 this.setStreet(null);
632 this.setUserName(null);
633 this.setUserPassword(null);
634 this.setUserPasswordRepeat(null);
635 this.setZipCode(null);
639 * Checks if same password is entered and that they are not empty.
641 * @return Whether the same password was entered
643 private boolean isSamePasswordEntered () {
644 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));