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.exceptions.EmailAddressAlreadyRegisteredException;
47 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
48 import org.mxchange.jusercore.exceptions.UserNotFoundException;
49 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
50 import org.mxchange.jusercore.model.user.LoginUser;
51 import org.mxchange.jusercore.model.user.User;
52 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
53 import org.mxchange.jusercore.model.user.UserUtils;
54 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
55 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
58 * A user bean (controller)
60 * @author Roland Haeder<roland@mxchange.org>
62 @Named ("adminUserController")
64 public class JobsAdminUserWebSessionBean implements JobsAdminUserWebSessionController {
69 private static final long serialVersionUID = 542_145_347_916L;
72 * An event fired when the administrator has added a new user
76 private Event<AdminAddedUserEvent> addedUserEvent;
81 private Date birthday;
84 * Cellphone number's carrier
86 private MobileProvider cellphoneCarrier;
91 private Long cellphoneNumber;
101 private String comment;
106 private Country country;
111 private String emailAddress;
116 private String familyName;
119 * Fax number's area code
121 private Integer faxAreaCode;
124 * Country instance for fax number
126 private Country faxCountry;
131 private Long faxNumber;
136 private String firstName;
141 private Gender gender;
146 private Short houseNumber;
149 * Phone number area code
151 private Integer phoneAreaCode;
154 * Country instance for phone number
156 private Country phoneCountry;
161 private Long phoneNumber;
166 private String street;
171 private final UserSessionBeanRemote userBean;
174 * A list of all user profiles
176 private List<User> userList;
181 private String userName;
184 * User password (unencrypted from web form)
186 private String userPassword;
189 * User password repeated (unencrypted from web form)
191 private String userPasswordRepeat;
196 private Integer zipCode;
199 * Regular user controller
202 private JobsUserWebSessionController userController;
205 * Default constructor
207 public JobsAdminUserWebSessionBean () {
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/jjobs-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.setUserAccountStatus(UserAccountStatus.CONFIRMED);
230 user.setUserProfileMode(ProfileMode.INVISIBLE);
232 // Generate phone number
233 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
234 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
235 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
237 // Create new contact
238 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
239 contact.setContactStreet(this.getStreet());
240 contact.setContactHouseNumber(this.getHouseNumber());
241 contact.setContactZipCode(this.getZipCode());
242 contact.setContactCity(this.getCity());
243 contact.setContactCountry(this.getCountry());
244 contact.setContactEmailAddress(this.getEmailAddress());
246 // Don't set null or wrong references
247 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
248 // Now the number must be given
249 if (phone.getPhoneAreaCode() == null) {
251 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
252 } else if (phone.getPhoneAreaCode() < 1) {
254 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
255 } else if (phone.getPhoneNumber() == null) {
257 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
258 } else if (phone.getPhoneNumber() < 1) {
260 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
264 contact.setContactLandLineNumber(phone);
267 // Don't set null or wrong references
268 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
269 // Now the number must be given
270 if (fax.getPhoneAreaCode() == null) {
272 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
273 } else if (fax.getPhoneAreaCode() < 1) {
275 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
276 } else if (fax.getPhoneNumber() == null) {
278 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
279 } else if (fax.getPhoneNumber() < 1) {
281 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
285 contact.setContactFaxNumber(fax);
288 // Is the provider set?
289 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
290 // Is the number set?
291 if (cellphone.getPhoneNumber() == null) {
293 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
294 } else if (cellphone.getPhoneNumber() < 1) {
296 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
299 // Set cellphone number
300 contact.setContactCellphoneNumber(cellphone);
303 contact.setContactBirthday(this.getBirthday());
304 contact.setContactComment(this.getComment());
306 // Set contact in user
307 user.setUserContact(contact);
309 // Init variable for password
310 String password = null;
312 // Is the user name or email address used already?
313 // @TODO Add password length check
314 if (this.userController.isUserNameRegistered(user)) {
315 // User name is already used
316 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
317 } else if (this.userController.isEmailAddressRegistered(user)) {
318 // Email address is already used
319 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
320 } else if ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty())) {
321 // Empty password entered, then generate one
322 password = UserUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
323 } else if (!this.isSamePasswordEntered()) {
324 // Both passwords don't match
325 throw new FaceletException(new UserPasswordMismatchException(user));
327 // Both match, so get it from this bean
328 password = this.getUserPassword();
331 // The password should not be null and at least 5 characters long
332 assert (password != null) : "password is null";
333 assert (password.length() >= JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough.";
335 // Encrypt password and set it
336 user.setUserEncryptedPassword(UserUtils.encryptPassword(password));
338 // Init updated user instance
339 User updatedUser = null;
342 // Now, that all is set, call EJB
343 updatedUser = this.userBean.addUser(user);
344 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
346 throw new FaceletException(ex);
356 public List<User> allUsers () {
358 return Collections.unmodifiableList(this.userList);
362 public Date getBirthday () {
363 return this.birthday;
367 public void setBirthday (final Date birthday) {
368 this.birthday = birthday;
372 public MobileProvider getCellphoneCarrier () {
373 return this.cellphoneCarrier;
377 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
378 this.cellphoneCarrier = cellphoneCarrier;
382 public Long getCellphoneNumber () {
383 return this.cellphoneNumber;
387 public void setCellphoneNumber (Long cellphoneNumber) {
388 this.cellphoneNumber = cellphoneNumber;
392 public String getCity () {
397 public void setCity (final String city) {
402 public String getComment () {
407 public void setComment (final String comment) {
408 this.comment = comment;
412 public Country getCountry () {
417 public void setCountry (final Country country) {
418 this.country = country;
422 public String getEmailAddress () {
423 return this.emailAddress;
427 public void setEmailAddress (final String emailAddress) {
428 this.emailAddress = emailAddress;
432 public String getFamilyName () {
433 return this.familyName;
437 public void setFamilyName (final String familyName) {
438 this.familyName = familyName;
442 public Integer getFaxAreaCode () {
443 return this.faxAreaCode;
447 public void setFaxAreaCode (final Integer faxAreaCode) {
448 this.faxAreaCode = faxAreaCode;
452 public Country getFaxCountry () {
453 return this.faxCountry;
457 public void setFaxCountry (final Country faxCountry) {
458 this.faxCountry = faxCountry;
462 public Long getFaxNumber () {
463 return this.faxNumber;
467 public void setFaxNumber (final Long faxNumber) {
468 this.faxNumber = faxNumber;
472 public String getFirstName () {
473 return this.firstName;
477 public void setFirstName (final String firstName) {
478 this.firstName = firstName;
482 public Gender getGender () {
487 public void setGender (final Gender gender) {
488 this.gender = gender;
492 public Short getHouseNumber () {
493 return this.houseNumber;
497 public void setHouseNumber (final Short houseNumber) {
498 this.houseNumber = houseNumber;
502 public Integer getPhoneAreaCode () {
503 return this.phoneAreaCode;
507 public void setPhoneAreaCode (final Integer phoneAreaCode) {
508 this.phoneAreaCode = phoneAreaCode;
512 public Country getPhoneCountry () {
513 return this.phoneCountry;
517 public void setPhoneCountry (final Country phoneCountry) {
518 this.phoneCountry = phoneCountry;
522 public Long getPhoneNumber () {
523 return this.phoneNumber;
527 public void setPhoneNumber (final Long phoneNumber) {
528 this.phoneNumber = phoneNumber;
532 public String getStreet () {
537 public void setStreet (final String street) {
538 this.street = street;
542 public String getUserName () {
543 return this.userName;
547 public void setUserName (final String userName) {
548 this.userName = userName;
552 public String getUserPassword () {
553 return this.userPassword;
557 public void setUserPassword (final String userPassword) {
558 this.userPassword = userPassword;
562 public String getUserPasswordRepeat () {
563 return this.userPasswordRepeat;
567 public void setUserPasswordRepeat (final String userPasswordRepeat) {
568 this.userPasswordRepeat = userPasswordRepeat;
572 public Integer getZipCode () {
577 public void setZipCode (final Integer zipCode) {
578 this.zipCode = zipCode;
582 public boolean hasUsers () {
583 return (!this.allUsers().isEmpty());
587 * Post-initialization of this class
590 public void init () {
591 // Initialize user list
592 this.userList = this.userBean.allUsers();
596 public User lookupUserById (final Long userId) throws UserNotFoundException {
600 // Try to lookup it in visible user list
601 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
603 User next = iterator.next();
605 // Is the user id found?
606 if (Objects.equals(next.getUserId(), userId)) {
607 // Copy to other variable
615 // Not visible for the current user
616 throw new UserNotFoundException(userId);
626 private void clear () {
628 this.setBirthday(null);
629 this.setCellphoneCarrier(null);
630 this.setCellphoneNumber(null);
632 this.setComment(null);
633 this.setCountry(null);
634 this.setEmailAddress(null);
635 this.setFamilyName(null);
636 this.setFaxAreaCode(null);
637 this.setFaxCountry(null);
638 this.setFaxNumber(null);
639 this.setFirstName(null);
640 this.setGender(null);
641 this.setHouseNumber(null);
642 this.setPhoneAreaCode(null);
643 this.setPhoneCountry(null);
644 this.setPhoneNumber(null);
645 this.setStreet(null);
646 this.setUserName(null);
647 this.setUserPassword(null);
648 this.setUserPasswordRepeat(null);
649 this.setZipCode(null);
653 * Checks if same password is entered and that they are not empty.
655 * @return Whether the same password was entered
657 private boolean isSamePasswordEntered () {
658 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));