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.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.event.Observes;
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.jjobs.beans.contact.JobsContactWebSessionController;
37 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
38 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
39 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
40 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
41 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
42 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
43 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
44 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
45 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
46 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
47 import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
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.profilemodes.ProfileMode;
56 * A user bean (controller)
58 * @author Roland Haeder<roland@mxchange.org>
60 @Named ("userController")
62 public class JobsUserWebSessionBean implements JobsUserWebSessionController {
67 private static final long serialVersionUID = 542_145_347_916L;
70 * General contact controller
73 private JobsContactWebSessionController contactController;
76 * A list of all selectable contacts
78 private List<Contact> selectableContacts;
81 * Event being fired when user updated personal data
85 private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
90 private final UserSessionBeanRemote userBean;
98 * A list of all user profiles
100 private List<User> userList;
103 * Login bean (controller)
106 private JobsUserLoginWebSessionController userLoginController;
111 private String userName;
116 private List<String> userNameList;
119 * User password (unencrypted from web form)
121 private String userPassword;
124 * User password repeated (unencrypted from web form)
126 private String userPasswordRepeat;
129 * Whether the user wants a public profile
131 private ProfileMode userProfileMode;
134 * A list of all public user profiles
136 private List<User> visibleUserList;
139 * Default constructor
141 public JobsUserWebSessionBean () {
144 // Get initial context
145 Context context = new InitialContext();
148 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
149 } catch (final NamingException e) {
151 throw new FaceletException(e);
156 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
158 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
160 // event should not be null
163 throw new NullPointerException("event is null"); //NOI18N
164 } else if (event.getAddedUser() == null) {
166 throw new NullPointerException("event.addedUser is null"); //NOI18N
167 } else if (event.getAddedUser().getUserId() == null) {
169 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
170 } else if (event.getAddedUser().getUserId() < 1) {
172 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
175 // Add user to local list
176 this.userList.add(event.getAddedUser());
182 this.setUserId(event.getAddedUser().getUserId());
185 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
189 public void afterAdminUpdatedUserDataEvent (@Observes final AdminUpdatedUserDataEvent event) {
191 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminUpdatedUserEvent: event={0} - CALLED!", event)); //NOI18N
193 // event should not be null
196 throw new NullPointerException("event is null"); //NOI18N
197 } else if (event.getUpdatedUser() == null) {
199 throw new NullPointerException("event.updatedUser is null"); //NOI18N
200 } else if (event.getUpdatedUser().getUserId() == null) {
202 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
203 } else if (event.getUpdatedUser().getUserId() < 1) {
205 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
209 this.updateList(event.getUpdatedUser());
215 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminUpdatedUserEvent: EXIT!"); //NOI18N
219 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
221 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
223 // event should not be null
226 throw new NullPointerException("event is null"); //NOI18N
227 } else if (event.getRegisteredUser() == null) {
229 throw new NullPointerException("event.registeredUser is null"); //NOI18N
230 } else if (event.getRegisteredUser().getUserId() == null) {
232 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
233 } else if (event.getRegisteredUser().getUserId() < 1) {
235 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
239 User registeredUser = event.getRegisteredUser();
242 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
244 // Copy all data from registered->user
245 this.copyUser(registeredUser);
250 // Add user to local list
251 this.userList.add(registeredUser);
254 this.addUserName(registeredUser);
256 // Is the account public?
257 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
258 // Also add it to this list
259 this.visibleUserList.add(registeredUser);
263 this.setUserId(registeredUser.getUserId());
266 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
270 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
272 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
274 // event should not be null
277 throw new NullPointerException("event is null"); //NOI18N
278 } else if (event.getLoggedInUser() == null) {
280 throw new NullPointerException("event.registeredUser is null"); //NOI18N
281 } else if (event.getLoggedInUser().getUserId() == null) {
283 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
284 } else if (event.getLoggedInUser().getUserId() < 1) {
286 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
289 // Copy all data to this bean
290 this.copyUser(event.getLoggedInUser());
292 // Re-initialize list
293 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
296 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
300 public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
304 throw new NullPointerException("event is null"); //NOI18N
305 } else if (event.getUpdatedUser() == null) {
307 throw new NullPointerException("event.updatedUser is null"); //NOI18N
308 } else if (event.getUpdatedUser().getUserId() == null) {
310 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
311 } else if (event.getUpdatedUser().getUserId() < 1) {
313 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
316 // All fine, so update list
317 this.updateList(event.getUpdatedUser());
321 public List<User> allUsers () {
323 return Collections.unmodifiableList(this.userList);
327 public List<User> allVisibleUsers () {
329 return Collections.unmodifiableList(this.visibleUserList);
333 public User createUserInstance () {
335 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
337 // Required personal data must be set
338 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
340 // Create new user instance
341 User localUser = new LoginUser();
343 // Update all data ...
344 localUser.setUserName(this.getUserName());
345 localUser.setUserProfileMode(this.getUserProfileMode());
347 // Create contact instance
348 Contact contact = this.contactController.createContactInstance();
351 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
353 // Set contact in user
354 localUser.setUserContact(contact);
357 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
364 public User createUserLogin () {
366 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
369 if (this.getUserName() == null) {
371 throw new NullPointerException("recruiterName is null"); //NOI18N
372 } else if (this.getUserName().isEmpty()) {
374 throw new IllegalStateException("recruiterName is empty."); //NOI18N
377 // Create new recruiter instance
378 User recruiter = new LoginUser();
380 // Update all data ...
381 recruiter.setUserName(this.getUserName());
384 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: recruiter={1} - EXIT!", this.getClass().getSimpleName(), recruiter));
386 // Return the new instance
391 public String doChangePersonalData () {
392 // This method shall only be called if the user is logged-in
393 if (!this.userLoginController.isUserLoggedIn()) {
395 throw new IllegalStateException("User is not logged-in"); //NOI18N
396 } else if (!this.isRequiredChangePersonalDataSet()) {
397 // Not all required fields are set
398 throw new FaceletException("Not all required fields are set."); //NOI18N
399 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
400 // Password not matching
401 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
405 User user = this.userLoginController.getLoggedInUser();
407 // Copy contact data to contact instance
408 this.contactController.updateContactDataFromController(user.getUserContact());
410 // It should be there, so run some tests on it
411 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
412 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
413 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
414 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
415 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
416 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
419 user.setUserProfileMode(this.getUserProfileMode());
421 // Send it to the EJB
422 User updatedUser = this.userBean.updateUserPersonalData(user);
425 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
428 return "user_data_saved"; //NOI18N
432 public Long getUserId () {
437 public void setUserId (final Long userId) {
438 this.userId = userId;
442 public String getUserName () {
443 return this.userName;
447 public void setUserName (final String userName) {
448 this.userName = userName;
452 public String getUserPassword () {
453 return this.userPassword;
457 public void setUserPassword (final String userPassword) {
458 this.userPassword = userPassword;
462 public String getUserPasswordRepeat () {
463 return this.userPasswordRepeat;
467 public void setUserPasswordRepeat (final String userPasswordRepeat) {
468 this.userPasswordRepeat = userPasswordRepeat;
472 public ProfileMode getUserProfileMode () {
473 return this.userProfileMode;
477 public void setUserProfileMode (final ProfileMode userProfileMode) {
478 this.userProfileMode = userProfileMode;
482 public boolean hasUsers () {
483 return (!this.allUsers().isEmpty());
487 * Post-initialization of this class
490 public void init () {
491 // Initialize user list
492 this.userList = this.userBean.allUsers();
494 // Get full user name list for reducing EJB calls
495 this.userNameList = this.userBean.getUserNameList();
497 // Is the user logged-in?
498 if (this.userLoginController.isUserLoggedIn()) {
499 // Is logged-in, so load also users visible to memebers
500 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
502 // Initialize user list
503 this.visibleUserList = this.userBean.allPublicUsers();
507 List<User> allUsers = this.allUsers();
510 List<Contact> allContacts = this.contactController.allContacts();
513 Iterator<Contact> iterator = allContacts.iterator();
516 while (iterator.hasNext()) {
518 Contact next = iterator.next();
521 Iterator<User> userIterator = allUsers.iterator();
523 // Loop through all users
524 while (userIterator.hasNext()) {
526 User nextUser = userIterator.next();
529 if (Objects.equals(next, nextUser.getUserContact())) {
538 this.selectableContacts = allContacts;
542 public boolean isContactFound (final Contact contact) {
543 // The contact must be valid
544 if (null == contact) {
546 throw new NullPointerException("contact is null"); //NOI18N
547 } else if (contact.getContactId() == null) {
549 throw new NullPointerException("contact.contactId is null"); //NOI18N
550 } else if (contact.getContactId() < 1) {
552 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
555 // Default is not found
556 boolean isFound = false;
559 Iterator<User> iterator = this.allUsers().iterator();
561 // Loop through all entries
562 while (iterator.hasNext()) {
564 User next = iterator.next();
566 // Compare both objects
567 if (Objects.equals(contact, next.getUserContact())) {
579 public boolean isRequiredChangePersonalDataSet () {
580 return ((this.getUserProfileMode() != null) &&
581 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
582 (this.contactController.isRequiredChangePersonalDataSet()));
586 public boolean isRequiredPersonalDataSet () {
587 return ((this.getUserName() != null) &&
588 (this.getUserProfileMode() != null) &&
589 (this.contactController.isRequiredPersonalDataSet()) &&
590 (this.getUserPassword() != null) &&
591 (this.getUserPasswordRepeat() != null));
595 public boolean isSamePasswordEntered () {
596 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
600 public boolean isUserIdEmpty () {
601 return ((this.getUserId() == null) || (this.getUserId() == 0));
605 public boolean isUserNameRegistered (final User user) {
606 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
610 public boolean isVisibleUserFound () {
611 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
615 public User lookupUserById (final Long userId) throws UserNotFoundException {
616 // Parameter must be valid
617 if (null == userId) {
619 throw new NullPointerException("userId is null"); //NOI18N
620 } else if (userId < 1) {
622 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
628 // Try to lookup it in visible user list
629 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
631 User next = iterator.next();
633 // Is the user id found?
634 if (Objects.equals(next.getUserId(), userId)) {
635 // Copy to other variable
643 // Not visible for the current user
644 throw new UserNotFoundException(userId);
652 public List<Contact> selectableContacts () {
653 return Collections.unmodifiableList(this.selectableContacts);
657 * Adds user's name to bean's internal list. It also updates the public user
658 * list if the user has decided to have a public account,
660 * @param user User instance
662 private void addUserName (final User user) {
663 // Make sure the entry is not added yet
664 if (this.userNameList.contains(user.getUserName())) {
666 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
667 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
669 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
673 this.userNameList.add(user.getUserName());
679 private void clear () {
682 this.setUserId(null);
683 this.setUserProfileMode(null);
686 this.setUserName(null);
687 this.setUserPassword(null);
688 this.setUserPasswordRepeat(null);
692 * Copies given user into the controller
694 * @param user User instance
696 private void copyUser (final User user) {
697 // Make sure the instance is valid
700 throw new NullPointerException("user is null"); //NOI18N
701 } else if (user.getUserContact() == null) {
703 throw new NullPointerException("user.userContact is null"); //NOI18N
708 this.setUserId(user.getUserId());
709 this.setUserProfileMode(user.getUserProfileMode());
713 * Updates list with given user instance
715 * @param user User instance
717 private void updateList (final User user) {
718 // The user should be valid
721 throw new NullPointerException("user is null"); //NOI18N
722 } else if (user.getUserId() == null) {
724 throw new NullPointerException("user.userId is null"); //NOI18N
725 } else if (user.getUserId() < 1) {
727 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
731 Iterator<User> iterator = this.userList.iterator();
734 while (iterator.hasNext()) {
736 User next = iterator.next();
738 // Is the same user id?
739 if (Objects.equals(user.getUserId(), next.getUserId())) {
740 // Found it, so remove it
741 this.userList.remove(next);
747 this.userList.add(user);