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.jjobs.beans.user;
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.enterprise.context.SessionScoped;
27 import javax.enterprise.event.Event;
28 import javax.enterprise.inject.Any;
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.jcontacts.contact.Contact;
36 import org.mxchange.jcontacts.contact.UserContact;
37 import org.mxchange.jcontacts.contact.gender.Gender;
38 import org.mxchange.jcountry.data.Country;
39 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
40 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
41 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
42 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
43 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
44 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
45 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
46 import org.mxchange.jusercore.events.user.AdminAddedUserEvent;
47 import org.mxchange.jusercore.events.user.AdminUserAddedEvent;
48 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
49 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
50 import org.mxchange.jusercore.exceptions.UserNotFoundException;
51 import org.mxchange.jusercore.exceptions.UserPasswordRepeatMismatchException;
52 import org.mxchange.jusercore.model.user.LoginUser;
53 import org.mxchange.jusercore.model.user.User;
54 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
55 import org.mxchange.jusercore.model.user.UserUtils;
56 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
57 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
60 * A user bean (controller)
62 * @author Roland Haeder<roland@mxchange.org>
64 @Named ("adminUserController")
66 public class JobsAdminUserWebSessionBean implements JobsAdminUserWebSessionController {
71 private static final long serialVersionUID = 542_145_347_916L;
74 * An event fired when the administrator has added a new user
78 private Event<AdminAddedUserEvent> addedUserEvent;
83 private Date birthday;
86 * Cellphone number's carrier
88 private MobileProvider cellphoneCarrier;
93 private Long cellphoneNumber;
103 private String comment;
108 private Country country;
113 private String emailAddress;
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 * Phone number area code
153 private Integer phoneAreaCode;
156 * Country instance for phone number
158 private Country phoneCountry;
163 private Long phoneNumber;
168 private String street;
173 private final UserSessionBeanRemote userBean;
176 * A list of all user profiles
178 private List<User> userList;
183 private String userName;
186 * User password (unencrypted from web form)
188 private String userPassword;
191 * User password repeated (unencrypted from web form)
193 private String userPasswordRepeat;
198 private Integer zipCode;
201 * Regular user controller
204 private JobsUserWebSessionController userController;
207 * Default constructor
209 public JobsAdminUserWebSessionBean () {
210 // Set gender to UNKNOWN
211 this.gender = Gender.UNKNOWN;
215 // Get initial context
216 Context context = new InitialContext();
219 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
220 } catch (final NamingException e) {
222 throw new FaceletException(e);
227 public void addUser () {
228 // Create new user instance
229 User localUser = new LoginUser();
231 // Set user name, CONFIRMED and INVISIBLE
232 localUser.setUserName(this.getUserName());
233 localUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
234 localUser.setUserProfileMode(ProfileMode.INVISIBLE);
236 // Generate phone number
237 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
238 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
239 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
241 // Create new contact
242 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
243 contact.setContactStreet(this.getStreet());
244 contact.setContactHouseNumber(this.getHouseNumber());
245 contact.setContactZipCode(this.getZipCode());
246 contact.setContactCity(this.getCity());
247 contact.setContactCountry(this.getCountry());
248 contact.setContactEmailAddress(this.getEmailAddress());
250 // Don't set null or wrong references
251 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
252 // Now the number must be given
253 if (phone.getPhoneAreaCode() == null) {
255 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
256 } else if (phone.getPhoneAreaCode() < 1) {
258 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
259 } else if (phone.getPhoneNumber() == null) {
261 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
262 } else if (phone.getPhoneNumber() < 1) {
264 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
268 contact.setContactLandLineNumber(phone);
271 // Don't set null or wrong references
272 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
273 // Now the number must be given
274 if (fax.getPhoneAreaCode() == null) {
276 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
277 } else if (fax.getPhoneAreaCode() < 1) {
279 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
280 } else if (fax.getPhoneNumber() == null) {
282 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
283 } else if (fax.getPhoneNumber() < 1) {
285 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
289 contact.setContactFaxNumber(fax);
292 // Is the provider set?
293 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
294 // Is the number set?
295 if (cellphone.getPhoneNumber() == null) {
297 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
298 } else if (cellphone.getPhoneNumber() < 1) {
300 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
303 // Set cellphone number
304 contact.setContactCellphoneNumber(cellphone);
307 contact.setContactBirthday(this.getBirthday());
308 contact.setContactComment(this.getComment());
310 // Set contact in user
311 localUser.setUserContact(contact);
313 // Init variable for password
314 String password = null;
316 // Is the user name or email address used already?
317 // @TODO Add password length check
318 if (this.userController.isUserNameRegistered(localUser)) {
319 // User name is already used
320 throw new FaceletException(new UserNameAlreadyRegisteredException(localUser));
321 } else if (this.userController.isEmailAddressRegistered(localUser)) {
322 // Email address is already used
323 throw new FaceletException(new EmailAddressAlreadyRegisteredException(localUser));
324 } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
325 // Empty password entered, then generate one
326 password = UserUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
327 } else if (!this.isSamePasswordEntered()) {
328 // Both passwords don't match
329 throw new FaceletException(new UserPasswordRepeatMismatchException(localUser));
331 // Both match, so get it from this bean
332 password = this.getUserPassword();
335 // The password should not be null and at least 5 characters long
336 assert (password != null) : "password is null"; //NOI18N
337 assert (password.length() >= JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
339 // Encrypt password and set it
340 localUser.setUserEncryptedPassword(UserUtils.encryptPassword(password));
342 // Init updated user instance
343 User updatedUser = null;
346 // Now, that all is set, call EJB
347 updatedUser = this.userBean.addUser(localUser);
348 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
350 throw new FaceletException(ex);
354 this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
356 // Add user to local list
357 this.userList.add(updatedUser);
364 public List<User> allUsers () {
366 return Collections.unmodifiableList(this.userList);
370 public Date getBirthday () {
371 return this.birthday;
375 public void setBirthday (final Date birthday) {
376 this.birthday = birthday;
380 public MobileProvider getCellphoneCarrier () {
381 return this.cellphoneCarrier;
385 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
386 this.cellphoneCarrier = cellphoneCarrier;
390 public Long getCellphoneNumber () {
391 return this.cellphoneNumber;
395 public void setCellphoneNumber (Long cellphoneNumber) {
396 this.cellphoneNumber = cellphoneNumber;
400 public String getCity () {
405 public void setCity (final String city) {
410 public String getComment () {
415 public void setComment (final String comment) {
416 this.comment = comment;
420 public Country getCountry () {
425 public void setCountry (final Country country) {
426 this.country = country;
430 public String getEmailAddress () {
431 return this.emailAddress;
435 public void setEmailAddress (final String emailAddress) {
436 this.emailAddress = emailAddress;
440 public String getFamilyName () {
441 return this.familyName;
445 public void setFamilyName (final String familyName) {
446 this.familyName = familyName;
450 public Integer getFaxAreaCode () {
451 return this.faxAreaCode;
455 public void setFaxAreaCode (final Integer faxAreaCode) {
456 this.faxAreaCode = faxAreaCode;
460 public Country getFaxCountry () {
461 return this.faxCountry;
465 public void setFaxCountry (final Country faxCountry) {
466 this.faxCountry = faxCountry;
470 public Long getFaxNumber () {
471 return this.faxNumber;
475 public void setFaxNumber (final Long faxNumber) {
476 this.faxNumber = faxNumber;
480 public String getFirstName () {
481 return this.firstName;
485 public void setFirstName (final String firstName) {
486 this.firstName = firstName;
490 public Gender getGender () {
495 public void setGender (final Gender gender) {
496 this.gender = gender;
500 public Short getHouseNumber () {
501 return this.houseNumber;
505 public void setHouseNumber (final Short houseNumber) {
506 this.houseNumber = houseNumber;
510 public Integer getPhoneAreaCode () {
511 return this.phoneAreaCode;
515 public void setPhoneAreaCode (final Integer phoneAreaCode) {
516 this.phoneAreaCode = phoneAreaCode;
520 public Country getPhoneCountry () {
521 return this.phoneCountry;
525 public void setPhoneCountry (final Country phoneCountry) {
526 this.phoneCountry = phoneCountry;
530 public Long getPhoneNumber () {
531 return this.phoneNumber;
535 public void setPhoneNumber (final Long phoneNumber) {
536 this.phoneNumber = phoneNumber;
540 public String getStreet () {
545 public void setStreet (final String street) {
546 this.street = street;
550 public String getUserName () {
551 return this.userName;
555 public void setUserName (final String userName) {
556 this.userName = userName;
560 public String getUserPassword () {
561 return this.userPassword;
565 public void setUserPassword (final String userPassword) {
566 this.userPassword = userPassword;
570 public String getUserPasswordRepeat () {
571 return this.userPasswordRepeat;
575 public void setUserPasswordRepeat (final String userPasswordRepeat) {
576 this.userPasswordRepeat = userPasswordRepeat;
580 public Integer getZipCode () {
585 public void setZipCode (final Integer zipCode) {
586 this.zipCode = zipCode;
590 public boolean hasUsers () {
591 return (!this.allUsers().isEmpty());
595 * Post-initialization of this class
598 public void init () {
599 // Initialize user list
600 this.userList = this.userBean.allUsers();
604 public User lookupUserById (final Long userId) throws UserNotFoundException {
605 if (null == userId) {
607 throw new NullPointerException("userId is null"); //NOI18N
608 } else if (userId < 1) {
610 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
616 // Try to lookup it in visible user list
617 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
619 User next = iterator.next();
621 // Is the user id found?
622 if (Objects.equals(next.getUserId(), userId)) {
623 // Copy to other variable
631 // Not visible for the current user
632 throw new UserNotFoundException(userId);
642 private void clear () {
644 this.setBirthday(null);
645 this.setCellphoneCarrier(null);
646 this.setCellphoneNumber(null);
648 this.setComment(null);
649 this.setCountry(null);
650 this.setEmailAddress(null);
651 this.setFamilyName(null);
652 this.setFaxAreaCode(null);
653 this.setFaxCountry(null);
654 this.setFaxNumber(null);
655 this.setFirstName(null);
656 this.setGender(null);
657 this.setHouseNumber(null);
658 this.setPhoneAreaCode(null);
659 this.setPhoneCountry(null);
660 this.setPhoneNumber(null);
661 this.setStreet(null);
662 this.setUserName(null);
663 this.setUserPassword(null);
664 this.setUserPasswordRepeat(null);
665 this.setZipCode(null);
669 * Checks if same password is entered and that they are not empty.
671 * @return Whether the same password was entered
673 private boolean isSamePasswordEntered () {
674 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));