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.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 JobsAdminUserWebSessionBean implements JobsAdminUserWebSessionController {
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 * A list of all user profiles
177 private List<User> userList;
182 private String userName;
185 * User password (unencrypted from web form)
187 private String userPassword;
190 * User password repeated (unencrypted from web form)
192 private String userPasswordRepeat;
197 private Integer zipCode;
200 * Regular user controller
203 private JobsUserWebSessionController userController;
206 * Default constructor
208 public JobsAdminUserWebSessionBean () {
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/jjobs-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 user = new LoginUser();
229 user.setUserName(this.getUserName());
230 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
231 user.setUserProfileMode(ProfileMode.INVISIBLE);
233 // Generate phone number
234 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
235 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
236 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
238 // Create new contact
239 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
240 contact.setContactStreet(this.getStreet());
241 contact.setContactHouseNumber(this.getHouseNumber());
242 contact.setContactZipCode(this.getZipCode());
243 contact.setContactCity(this.getCity());
244 contact.setContactCountry(this.getCountry());
245 contact.setContactEmailAddress(this.getEmailAddress());
247 // Don't set null or wrong references
248 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
249 // Now the number must be given
250 if (phone.getPhoneAreaCode() == null) {
252 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
253 } else if (phone.getPhoneAreaCode() < 1) {
255 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
256 } else if (phone.getPhoneNumber() == null) {
258 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
259 } else if (phone.getPhoneNumber() < 1) {
261 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
265 contact.setContactLandLineNumber(phone);
268 // Don't set null or wrong references
269 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
270 // Now the number must be given
271 if (fax.getPhoneAreaCode() == null) {
273 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
274 } else if (fax.getPhoneAreaCode() < 1) {
276 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
277 } else if (fax.getPhoneNumber() == null) {
279 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
280 } else if (fax.getPhoneNumber() < 1) {
282 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
286 contact.setContactFaxNumber(fax);
289 // Is the provider set?
290 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
291 // Is the number set?
292 if (cellphone.getPhoneNumber() == null) {
294 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
295 } else if (cellphone.getPhoneNumber() < 1) {
297 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
300 // Set cellphone number
301 contact.setContactCellphoneNumber(cellphone);
304 contact.setContactBirthday(this.getBirthday());
305 contact.setContactComment(this.getComment());
307 // Set contact in user
308 user.setUserContact(contact);
310 // Init variable for password
311 String password = null;
313 // Is the user name or email address used already?
314 // @TODO Add password length check
315 if (this.userController.isUserNameRegistered(user)) {
316 // User name is already used
317 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
318 } else if (this.userController.isEmailAddressRegistered(user)) {
319 // Email address is already used
320 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
321 } else if ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty())) {
322 // Empty password entered, then generate one
323 password = UserUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
324 } else if (!this.isSamePasswordEntered()) {
325 // Both passwords don't match
326 throw new FaceletException(new UserPasswordMismatchException(user));
328 // Both match, so get it from this bean
329 password = this.getUserPassword();
332 // The password should not be null and at least 5 characters long
333 assert (password != null) : "password is null"; //NOI18N
334 assert (password.length() >= JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
336 // Encrypt password and set it
337 user.setUserEncryptedPassword(UserUtils.encryptPassword(password));
339 // Init updated user instance
340 User updatedUser = null;
343 // Now, that all is set, call EJB
344 updatedUser = this.userBean.addUser(user);
345 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
347 throw new FaceletException(ex);
351 this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
353 // Add user to local list
354 this.userList.add(updatedUser);
361 public List<User> allUsers () {
363 return Collections.unmodifiableList(this.userList);
367 public Date getBirthday () {
368 return this.birthday;
372 public void setBirthday (final Date birthday) {
373 this.birthday = birthday;
377 public MobileProvider getCellphoneCarrier () {
378 return this.cellphoneCarrier;
382 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
383 this.cellphoneCarrier = cellphoneCarrier;
387 public Long getCellphoneNumber () {
388 return this.cellphoneNumber;
392 public void setCellphoneNumber (Long cellphoneNumber) {
393 this.cellphoneNumber = cellphoneNumber;
397 public String getCity () {
402 public void setCity (final String city) {
407 public String getComment () {
412 public void setComment (final String comment) {
413 this.comment = comment;
417 public Country getCountry () {
422 public void setCountry (final Country country) {
423 this.country = country;
427 public String getEmailAddress () {
428 return this.emailAddress;
432 public void setEmailAddress (final String emailAddress) {
433 this.emailAddress = emailAddress;
437 public String getFamilyName () {
438 return this.familyName;
442 public void setFamilyName (final String familyName) {
443 this.familyName = familyName;
447 public Integer getFaxAreaCode () {
448 return this.faxAreaCode;
452 public void setFaxAreaCode (final Integer faxAreaCode) {
453 this.faxAreaCode = faxAreaCode;
457 public Country getFaxCountry () {
458 return this.faxCountry;
462 public void setFaxCountry (final Country faxCountry) {
463 this.faxCountry = faxCountry;
467 public Long getFaxNumber () {
468 return this.faxNumber;
472 public void setFaxNumber (final Long faxNumber) {
473 this.faxNumber = faxNumber;
477 public String getFirstName () {
478 return this.firstName;
482 public void setFirstName (final String firstName) {
483 this.firstName = firstName;
487 public Gender getGender () {
492 public void setGender (final Gender gender) {
493 this.gender = gender;
497 public Short getHouseNumber () {
498 return this.houseNumber;
502 public void setHouseNumber (final Short houseNumber) {
503 this.houseNumber = houseNumber;
507 public Integer getPhoneAreaCode () {
508 return this.phoneAreaCode;
512 public void setPhoneAreaCode (final Integer phoneAreaCode) {
513 this.phoneAreaCode = phoneAreaCode;
517 public Country getPhoneCountry () {
518 return this.phoneCountry;
522 public void setPhoneCountry (final Country phoneCountry) {
523 this.phoneCountry = phoneCountry;
527 public Long getPhoneNumber () {
528 return this.phoneNumber;
532 public void setPhoneNumber (final Long phoneNumber) {
533 this.phoneNumber = phoneNumber;
537 public String getStreet () {
542 public void setStreet (final String street) {
543 this.street = street;
547 public String getUserName () {
548 return this.userName;
552 public void setUserName (final String userName) {
553 this.userName = userName;
557 public String getUserPassword () {
558 return this.userPassword;
562 public void setUserPassword (final String userPassword) {
563 this.userPassword = userPassword;
567 public String getUserPasswordRepeat () {
568 return this.userPasswordRepeat;
572 public void setUserPasswordRepeat (final String userPasswordRepeat) {
573 this.userPasswordRepeat = userPasswordRepeat;
577 public Integer getZipCode () {
582 public void setZipCode (final Integer zipCode) {
583 this.zipCode = zipCode;
587 public boolean hasUsers () {
588 return (!this.allUsers().isEmpty());
592 * Post-initialization of this class
595 public void init () {
596 // Initialize user list
597 this.userList = this.userBean.allUsers();
601 public User lookupUserById (final Long userId) throws UserNotFoundException {
605 // Try to lookup it in visible user list
606 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
608 User next = iterator.next();
610 // Is the user id found?
611 if (Objects.equals(next.getUserId(), userId)) {
612 // Copy to other variable
620 // Not visible for the current user
621 throw new UserNotFoundException(userId);
631 private void clear () {
633 this.setBirthday(null);
634 this.setCellphoneCarrier(null);
635 this.setCellphoneNumber(null);
637 this.setComment(null);
638 this.setCountry(null);
639 this.setEmailAddress(null);
640 this.setFamilyName(null);
641 this.setFaxAreaCode(null);
642 this.setFaxCountry(null);
643 this.setFaxNumber(null);
644 this.setFirstName(null);
645 this.setGender(null);
646 this.setHouseNumber(null);
647 this.setPhoneAreaCode(null);
648 this.setPhoneCountry(null);
649 this.setPhoneNumber(null);
650 this.setStreet(null);
651 this.setUserName(null);
652 this.setUserPassword(null);
653 this.setUserPasswordRepeat(null);
654 this.setZipCode(null);
658 * Checks if same password is entered and that they are not empty.
660 * @return Whether the same password was entered
662 private boolean isSamePasswordEntered () {
663 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));