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.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.smsprovider.SmsProvider;
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;
54 * A user bean (controller)
56 * @author Roland Haeder<roland@mxchange.org>
58 @Named ("adminUserController")
60 public class AddressbookAdminUserWebSessionBean implements AddressbookAdminUserWebSessionController {
65 private static final long serialVersionUID = 542_145_347_916L;
67 /////////////////////// Properties /////////////////////
71 private Date birthday;
74 * Cellphone number's carrier
76 private SmsProvider cellphoneCarrier;
81 private Long cellphoneNumber;
91 private String comment;
96 private Country country;
101 private String emailAddress;
106 private String familyName;
109 * Fax number's area code
111 private Integer faxAreaCode;
114 * Country instance for fax number
116 private Country faxCountry;
121 private Long faxNumber;
126 private String firstName;
131 private Gender gender;
136 private Short houseNumber;
139 * Phone number area code
141 private Integer phoneAreaCode;
144 * Country instance for phone number
146 private Country phoneCountry;
151 private Long phoneNumber;
156 private String street;
161 private final UserSessionBeanRemote userBean;
169 * A list of all user profiles
171 private List<User> userList;
176 private String userName;
179 * User password (unencrypted from web form)
181 private String userPassword;
184 * User password repeated (unencrypted from web form)
186 private String userPasswordRepeat;
189 * Whether the user wants a public profile
191 private ProfileMode userProfileMode;
196 private Integer zipCode;
199 * Regular user controller
202 private AddressbookUserWebSessionController userController;
205 * Default constructor
207 public AddressbookAdminUserWebSessionBean () {
208 // Set gender to UNKNOWN
209 this.gender = Gender.UNKNOWN;
213 // Get initial context
214 Context context = new InitialContext();
217 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
218 } catch (final NamingException e) {
220 throw new FaceletException(e);
225 public void addUser () {
226 // Create new user instance
227 User user = new LoginUser();
228 user.setUserName(this.getUserName());
229 user.setUserProfileMode(this.getUserProfileMode());
231 // Generate phone number
232 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
233 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
234 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
236 // Create new contact
237 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
238 contact.setContactStreet(this.getStreet());
239 contact.setContactHouseNumber(this.getHouseNumber());
240 contact.setContactZipCode(this.getZipCode());
241 contact.setContactCity(this.getCity());
242 contact.setContactCountry(this.getCountry());
243 contact.setContactEmailAddress(this.getEmailAddress());
245 // Don't set null or wrong references
246 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
247 // Now the number must be given
248 if (phone.getPhoneAreaCode() == null) {
250 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
251 } else if (phone.getPhoneAreaCode() < 1) {
253 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
254 } else if (phone.getPhoneNumber() == null) {
256 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
257 } else if (phone.getPhoneNumber() < 1) {
259 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
263 contact.setContactLandLineNumber(phone);
266 // Don't set null or wrong references
267 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
268 // Now the number must be given
269 if (fax.getPhoneAreaCode() == null) {
271 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
272 } else if (fax.getPhoneAreaCode() < 1) {
274 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
275 } else if (fax.getPhoneNumber() == null) {
277 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
278 } else if (fax.getPhoneNumber() < 1) {
280 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
284 contact.setContactFaxNumber(fax);
287 // Is the provider set?
288 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof SmsProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
289 // Is the number set?
290 if (cellphone.getPhoneNumber() == null) {
292 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
293 } else if (cellphone.getPhoneNumber() < 1) {
295 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
298 // Set cellphone number
299 contact.setContactCellphoneNumber(cellphone);
302 contact.setContactBirthday(this.getBirthday());
303 contact.setContactComment(this.getComment());
305 // Set contact in user
306 user.setUserContact(contact);
308 // Init variable for password
309 String password = null;
311 // Is the user name or email address used already?
312 // @TODO Add password length check
313 if (this.userController.isUserNameRegistered(user)) {
314 // User name is already used
315 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
316 } else if (this.userController.isEmailAddressRegistered(user)) {
317 // Email address is already used
318 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
319 } else if ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty())) {
320 // Empty password entered, then generate one
321 password = UserUtils.createRandomPassword(AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
322 } else if (!this.isSamePasswordEntered()) {
323 // Both passwords don't match
324 throw new FaceletException(new UserPasswordMismatchException(user));
326 // Both match, so get it from this bean
327 password = this.getUserPassword();
330 // The password should not be null and at least 5 characters long
331 assert (password != null) : "password is null";
332 assert (password.length() >= AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough.";
334 // Encrypt password and set it
335 user.setUserEncryptedPassword(UserUtils.encryptPassword(password));
338 // Now, that all is set, call EJB
339 this.userBean.addUser(user);
340 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
342 throw new FaceletException(ex);
347 public List<User> allUsers () {
349 return Collections.unmodifiableList(this.userList);
353 public Date getBirthday () {
354 return this.birthday;
358 public void setBirthday (final Date birthday) {
359 this.birthday = birthday;
363 public SmsProvider getCellphoneCarrier () {
364 return this.cellphoneCarrier;
368 public void setCellphoneCarrier (final SmsProvider cellphoneCarrier) {
369 this.cellphoneCarrier = cellphoneCarrier;
373 public Long getCellphoneNumber () {
374 return this.cellphoneNumber;
378 public void setCellphoneNumber (Long cellphoneNumber) {
379 this.cellphoneNumber = cellphoneNumber;
383 public String getCity () {
388 public void setCity (final String city) {
393 public String getComment () {
398 public void setComment (final String comment) {
399 this.comment = comment;
403 public Country getCountry () {
408 public void setCountry (final Country country) {
409 this.country = country;
413 public String getEmailAddress () {
414 return this.emailAddress;
418 public void setEmailAddress (final String emailAddress) {
419 this.emailAddress = emailAddress;
423 public String getFamilyName () {
424 return this.familyName;
428 public void setFamilyName (final String familyName) {
429 this.familyName = familyName;
433 public Integer getFaxAreaCode () {
434 return this.faxAreaCode;
438 public void setFaxAreaCode (final Integer faxAreaCode) {
439 this.faxAreaCode = faxAreaCode;
443 public Country getFaxCountry () {
444 return this.faxCountry;
448 public void setFaxCountry (final Country faxCountry) {
449 this.faxCountry = faxCountry;
453 public Long getFaxNumber () {
454 return this.faxNumber;
458 public void setFaxNumber (final Long faxNumber) {
459 this.faxNumber = faxNumber;
463 public String getFirstName () {
464 return this.firstName;
468 public void setFirstName (final String firstName) {
469 this.firstName = firstName;
473 public Gender getGender () {
478 public void setGender (final Gender gender) {
479 this.gender = gender;
483 public Short getHouseNumber () {
484 return this.houseNumber;
488 public void setHouseNumber (final Short houseNumber) {
489 this.houseNumber = houseNumber;
493 public Integer getPhoneAreaCode () {
494 return this.phoneAreaCode;
498 public void setPhoneAreaCode (final Integer phoneAreaCode) {
499 this.phoneAreaCode = phoneAreaCode;
503 public Country getPhoneCountry () {
504 return this.phoneCountry;
508 public void setPhoneCountry (final Country phoneCountry) {
509 this.phoneCountry = phoneCountry;
513 public Long getPhoneNumber () {
514 return this.phoneNumber;
518 public void setPhoneNumber (final Long phoneNumber) {
519 this.phoneNumber = phoneNumber;
523 public String getStreet () {
528 public void setStreet (final String street) {
529 this.street = street;
533 public Long getUserId () {
538 public void setUserId (final Long userId) {
539 this.userId = userId;
543 public String getUserName () {
544 return this.userName;
548 public void setUserName (final String userName) {
549 this.userName = userName;
553 public String getUserPassword () {
554 return this.userPassword;
558 public void setUserPassword (final String userPassword) {
559 this.userPassword = userPassword;
563 public String getUserPasswordRepeat () {
564 return this.userPasswordRepeat;
568 public void setUserPasswordRepeat (final String userPasswordRepeat) {
569 this.userPasswordRepeat = userPasswordRepeat;
573 public ProfileMode getUserProfileMode () {
574 return this.userProfileMode;
578 public void setUserProfileMode (final ProfileMode userProfileMode) {
579 this.userProfileMode = userProfileMode;
583 public Integer getZipCode () {
588 public void setZipCode (final Integer zipCode) {
589 this.zipCode = zipCode;
593 public boolean hasUsers () {
594 return (!this.allUsers().isEmpty());
598 * Post-initialization of this class
601 public void init () {
602 // Initialize user list
603 this.userList = this.userBean.allUsers();
607 * Checks if same password is entered and that they are not empty.
609 * @return Whether the same password was entered
611 private boolean isSamePasswordEntered () {
612 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
616 public User lookupUserById (final Long userId) throws UserNotFoundException {
620 // Try to lookup it in visible user list
621 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
623 User next = iterator.next();
625 // Is the user id found?
626 if (Objects.equals(next.getUserId(), userId)) {
627 // Copy to other variable
635 // Not visible for the current user
636 throw new UserNotFoundException(userId);