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.jcontacts.events.contact.add.AdminAddedContactEvent;
37 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
38 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
39 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
40 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
41 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
42 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
43 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
44 import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
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.profilemodes.ProfileMode;
53 * A user bean (controller)
55 * @author Roland Haeder<roland@mxchange.org>
57 @Named ("userController")
59 public class JobsUserWebSessionBean implements JobsUserWebSessionController {
64 private static final long serialVersionUID = 542_145_347_916L;
67 * General contact controller
70 private JobsContactWebSessionController contactController;
73 * A list of all selectable contacts
75 private List<Contact> selectableContacts;
78 * Event being fired when user updated personal data
82 private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
87 private final UserSessionBeanRemote userBean;
95 * A list of all user profiles
97 private List<User> userList;
100 * Login bean (controller)
103 private JobsUserLoginWebSessionController userLoginController;
108 private String userName;
113 private List<String> userNameList;
116 * User password (unencrypted from web form)
118 private String userPassword;
121 * User password repeated (unencrypted from web form)
123 private String userPasswordRepeat;
126 * Whether the user wants a public profile
128 private ProfileMode userProfileMode;
131 * A list of all public user profiles
133 private List<User> visibleUserList;
136 * Default constructor
138 public JobsUserWebSessionBean () {
141 // Get initial context
142 Context context = new InitialContext();
145 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
146 } catch (final NamingException e) {
148 throw new FaceletException(e);
153 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
154 // The event must be valid
157 throw new NullPointerException("event is null"); //NOI18N
158 } else if (event.getAddedContact()== null) {
160 throw new NullPointerException("event.addedContact is null"); //NOI18N
161 } else if (event.getAddedContact().getContactId() == null) {
163 throw new NullPointerException("event.addedContact.customerId is null"); //NOI18N
164 } else if (event.getAddedContact().getContactId() < 1) {
166 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
170 this.selectableContacts.add(event.getAddedContact());
174 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
176 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
178 // event should not be null
181 throw new NullPointerException("event is null"); //NOI18N
182 } else if (event.getAddedUser() == null) {
184 throw new NullPointerException("event.addedUser is null"); //NOI18N
185 } else if (event.getAddedUser().getUserId() == null) {
187 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
188 } else if (event.getAddedUser().getUserId() < 1) {
190 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
193 // Add user to local list
194 this.userList.add(event.getAddedUser());
200 this.setUserId(event.getAddedUser().getUserId());
203 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
207 public void afterAdminUpdatedUserDataEvent (@Observes final AdminUpdatedUserDataEvent event) {
209 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminUpdatedUserEvent: event={0} - CALLED!", event)); //NOI18N
211 // event should not be null
214 throw new NullPointerException("event is null"); //NOI18N
215 } else if (event.getUpdatedUser() == null) {
217 throw new NullPointerException("event.updatedUser is null"); //NOI18N
218 } else if (event.getUpdatedUser().getUserId() == null) {
220 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
221 } else if (event.getUpdatedUser().getUserId() < 1) {
223 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
227 this.updateList(event.getUpdatedUser());
233 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminUpdatedUserEvent: EXIT!"); //NOI18N
237 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
239 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
241 // event should not be null
244 throw new NullPointerException("event is null"); //NOI18N
245 } else if (event.getRegisteredUser() == null) {
247 throw new NullPointerException("event.registeredUser is null"); //NOI18N
248 } else if (event.getRegisteredUser().getUserId() == null) {
250 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
251 } else if (event.getRegisteredUser().getUserId() < 1) {
253 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
257 User registeredUser = event.getRegisteredUser();
260 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
262 // Copy all data from registered->user
263 this.copyUser(registeredUser);
268 // Add user to local list
269 this.userList.add(registeredUser);
272 this.addUserName(registeredUser);
274 // Is the account public?
275 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
276 // Also add it to this list
277 this.visibleUserList.add(registeredUser);
281 this.setUserId(registeredUser.getUserId());
284 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
288 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
290 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
292 // event should not be null
295 throw new NullPointerException("event is null"); //NOI18N
296 } else if (event.getLoggedInUser() == null) {
298 throw new NullPointerException("event.registeredUser is null"); //NOI18N
299 } else if (event.getLoggedInUser().getUserId() == null) {
301 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
302 } else if (event.getLoggedInUser().getUserId() < 1) {
304 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
307 // Copy all data to this bean
308 this.copyUser(event.getLoggedInUser());
310 // Re-initialize list
311 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
314 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
318 public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
322 throw new NullPointerException("event is null"); //NOI18N
323 } else if (event.getUpdatedUser() == null) {
325 throw new NullPointerException("event.updatedUser is null"); //NOI18N
326 } else if (event.getUpdatedUser().getUserId() == null) {
328 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
329 } else if (event.getUpdatedUser().getUserId() < 1) {
331 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
334 // All fine, so update list
335 this.updateList(event.getUpdatedUser());
339 public List<User> allUsers () {
341 return Collections.unmodifiableList(this.userList);
345 public List<User> allVisibleUsers () {
347 return Collections.unmodifiableList(this.visibleUserList);
351 public User createUserInstance () {
353 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
355 // Required personal data must be set
356 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
358 // Create new user instance
359 User localUser = new LoginUser();
361 // Update all data ...
362 localUser.setUserName(this.getUserName());
363 localUser.setUserProfileMode(this.getUserProfileMode());
365 // Create contact instance
366 Contact contact = this.contactController.createContactInstance();
369 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
371 // Set contact in user
372 localUser.setUserContact(contact);
375 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
382 public User createUserLogin () {
384 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
387 if (this.getUserName() == null) {
389 throw new NullPointerException("recruiterName is null"); //NOI18N
390 } else if (this.getUserName().isEmpty()) {
392 throw new IllegalStateException("recruiterName is empty."); //NOI18N
395 // Create new recruiter instance
396 User recruiter = new LoginUser();
398 // Update all data ...
399 recruiter.setUserName(this.getUserName());
402 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: recruiter={1} - EXIT!", this.getClass().getSimpleName(), recruiter));
404 // Return the new instance
409 public String doChangePersonalData () {
410 // This method shall only be called if the user is logged-in
411 if (!this.userLoginController.isUserLoggedIn()) {
413 throw new IllegalStateException("User is not logged-in"); //NOI18N
414 } else if (!this.isRequiredChangePersonalDataSet()) {
415 // Not all required fields are set
416 throw new FaceletException("Not all required fields are set."); //NOI18N
417 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
418 // Password not matching
419 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
423 User user = this.userLoginController.getLoggedInUser();
425 // Copy contact data to contact instance
426 this.contactController.updateContactDataFromController(user.getUserContact());
428 // It should be there, so run some tests on it
429 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
430 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
431 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
432 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
433 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
434 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
437 user.setUserProfileMode(this.getUserProfileMode());
439 // Send it to the EJB
440 User updatedUser = this.userBean.updateUserPersonalData(user);
443 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
446 return "user_data_saved"; //NOI18N
450 public Long getUserId () {
455 public void setUserId (final Long userId) {
456 this.userId = userId;
460 public String getUserName () {
461 return this.userName;
465 public void setUserName (final String userName) {
466 this.userName = userName;
470 public String getUserPassword () {
471 return this.userPassword;
475 public void setUserPassword (final String userPassword) {
476 this.userPassword = userPassword;
480 public String getUserPasswordRepeat () {
481 return this.userPasswordRepeat;
485 public void setUserPasswordRepeat (final String userPasswordRepeat) {
486 this.userPasswordRepeat = userPasswordRepeat;
490 public ProfileMode getUserProfileMode () {
491 return this.userProfileMode;
495 public void setUserProfileMode (final ProfileMode userProfileMode) {
496 this.userProfileMode = userProfileMode;
500 public boolean hasUsers () {
501 return (!this.allUsers().isEmpty());
505 * Post-initialization of this class
508 public void init () {
509 // Initialize user list
510 this.userList = this.userBean.allUsers();
512 // Get full user name list for reducing EJB calls
513 this.userNameList = this.userBean.getUserNameList();
515 // Is the user logged-in?
516 if (this.userLoginController.isUserLoggedIn()) {
517 // Is logged-in, so load also users visible to memebers
518 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
520 // Initialize user list
521 this.visibleUserList = this.userBean.allPublicUsers();
525 List<User> allUsers = this.allUsers();
528 List<Contact> allContacts = this.contactBean.getAllContacts();
531 Iterator<Contact> iterator = allContacts.iterator();
534 while (iterator.hasNext()) {
536 Contact next = iterator.next();
539 Iterator<User> userIterator = allUsers.iterator();
541 // Loop through all users
542 while (userIterator.hasNext()) {
544 User nextUser = userIterator.next();
547 if (Objects.equals(next, nextUser.getUserContact())) {
556 this.selectableContacts = allContacts;
560 public boolean isContactFound (final Contact contact) {
561 // The contact must be valid
562 if (null == contact) {
564 throw new NullPointerException("contact is null"); //NOI18N
565 } else if (contact.getContactId() == null) {
567 throw new NullPointerException("contact.contactId is null"); //NOI18N
568 } else if (contact.getContactId() < 1) {
570 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
573 // Default is not found
574 boolean isFound = false;
577 Iterator<User> iterator = this.allUsers().iterator();
579 // Loop through all entries
580 while (iterator.hasNext()) {
582 User next = iterator.next();
584 // Compare both objects
585 if (Objects.equals(contact, next.getUserContact())) {
597 public boolean isRequiredChangePersonalDataSet () {
598 return ((this.getUserProfileMode() != null) &&
599 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
600 (this.contactController.isRequiredChangePersonalDataSet()));
604 public boolean isRequiredPersonalDataSet () {
605 return ((this.getUserName() != null) &&
606 (this.getUserProfileMode() != null) &&
607 (this.contactController.isRequiredPersonalDataSet()) &&
608 (this.getUserPassword() != null) &&
609 (this.getUserPasswordRepeat() != null));
613 public boolean isSamePasswordEntered () {
614 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
618 public boolean isUserIdEmpty () {
619 return ((this.getUserId() == null) || (this.getUserId() == 0));
623 public boolean isUserNameRegistered (final User user) {
624 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
628 public boolean isVisibleUserFound () {
629 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
633 public User lookupUserById (final Long userId) throws UserNotFoundException {
634 // Parameter must be valid
635 if (null == userId) {
637 throw new NullPointerException("userId is null"); //NOI18N
638 } else if (userId < 1) {
640 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
646 // Try to lookup it in visible user list
647 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
649 User next = iterator.next();
651 // Is the user id found?
652 if (Objects.equals(next.getUserId(), userId)) {
653 // Copy to other variable
661 // Not visible for the current user
662 throw new UserNotFoundException(userId);
670 public List<Contact> selectableContacts () {
671 return Collections.unmodifiableList(this.selectableContacts);
675 * Adds user's name to bean's internal list. It also updates the public user
676 * list if the user has decided to have a public account,
678 * @param user User instance
680 private void addUserName (final User user) {
681 // Make sure the entry is not added yet
682 if (this.userNameList.contains(user.getUserName())) {
684 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
685 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
687 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
691 this.userNameList.add(user.getUserName());
697 private void clear () {
700 this.setUserId(null);
701 this.setUserProfileMode(null);
704 this.setUserName(null);
705 this.setUserPassword(null);
706 this.setUserPasswordRepeat(null);
710 * Copies given user into the controller
712 * @param user User instance
714 private void copyUser (final User user) {
715 // Make sure the instance is valid
718 throw new NullPointerException("user is null"); //NOI18N
719 } else if (user.getUserContact() == null) {
721 throw new NullPointerException("user.userContact is null"); //NOI18N
726 this.setUserId(user.getUserId());
727 this.setUserProfileMode(user.getUserProfileMode());
731 * Updates list with given user instance
733 * @param user User instance
735 private void updateList (final User user) {
736 // The user should be valid
739 throw new NullPointerException("user is null"); //NOI18N
740 } else if (user.getUserId() == null) {
742 throw new NullPointerException("user.userId is null"); //NOI18N
743 } else if (user.getUserId() < 1) {
745 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
749 Iterator<User> iterator = this.userList.iterator();
752 while (iterator.hasNext()) {
754 User next = iterator.next();
756 // Is the same user id?
757 if (Objects.equals(user.getUserId(), next.getUserId())) {
758 // Found it, so remove it
759 this.userList.remove(next);
765 this.userList.add(user);