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.addressbook.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.addressbook.beans.contact.AddressbookContactWebSessionController;
36 import org.mxchange.addressbook.beans.login.AddressbookUserLoginWebSessionController;
37 import org.mxchange.jcontacts.contact.Contact;
38 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
39 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
40 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
41 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
42 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
43 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
44 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
45 import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
46 import org.mxchange.jusercore.exceptions.UserNotFoundException;
47 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
48 import org.mxchange.jusercore.model.user.LoginUser;
49 import org.mxchange.jusercore.model.user.User;
50 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
51 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
54 * A user bean (controller)
56 * @author Roland Haeder<roland@mxchange.org>
58 @Named ("userController")
60 public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionController {
65 private static final long serialVersionUID = 542_145_347_916L;
70 private ContactSessionBeanRemote contactBean;
73 * General contact controller
76 private AddressbookContactWebSessionController contactController;
79 * Login bean (controller)
82 private AddressbookUserLoginWebSessionController loginController;
85 * A list of all selectable contacts
87 private List<Contact> selectableContacts;
90 * Event being fired when user updated personal data
94 private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
99 private final UserSessionBeanRemote userBean;
107 * A list of all user profiles
109 private List<User> userList;
112 * Login bean (controller)
115 private AddressbookUserLoginWebSessionController userLoginController;
120 private String userName;
125 private List<String> userNameList;
128 * User password (unencrypted from web form)
130 private String userPassword;
133 * User password repeated (unencrypted from web form)
135 private String userPasswordRepeat;
138 * Whether the user wants a public profile
140 private ProfileMode userProfileMode;
143 * A list of all public user profiles
145 private List<User> visibleUserList;
148 * Default constructor
150 public AddressbookUserWebSessionBean () {
153 // Get initial context
154 Context context = new InitialContext();
157 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
160 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
161 } catch (final NamingException e) {
163 throw new FaceletException(e);
168 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
169 // The event must be valid
172 throw new NullPointerException("event is null"); //NOI18N
173 } else if (event.getAddedContact()== null) {
175 throw new NullPointerException("event.addedContact is null"); //NOI18N
176 } else if (event.getAddedContact().getContactId() == null) {
178 throw new NullPointerException("event.addedContact.customerId is null"); //NOI18N
179 } else if (event.getAddedContact().getContactId() < 1) {
181 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
185 this.selectableContacts.add(event.getAddedContact());
189 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
191 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
193 // event should not be null
196 throw new NullPointerException("event is null"); //NOI18N
197 } else if (event.getAddedUser() == null) {
199 throw new NullPointerException("event.addedUser is null"); //NOI18N
200 } else if (event.getAddedUser().getUserId() == null) {
202 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
203 } else if (event.getAddedUser().getUserId() < 1) {
205 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
208 // Add user to local list
209 this.userList.add(event.getAddedUser());
215 this.setUserId(event.getAddedUser().getUserId());
218 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
222 public void afterAdminUpdatedUserDataEvent (@Observes final AdminUpdatedUserDataEvent event) {
224 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminUpdatedUserEvent: event={0} - CALLED!", event)); //NOI18N
226 // event should not be null
229 throw new NullPointerException("event is null"); //NOI18N
230 } else if (event.getUpdatedUser() == null) {
232 throw new NullPointerException("event.updatedUser is null"); //NOI18N
233 } else if (event.getUpdatedUser().getUserId() == null) {
235 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
236 } else if (event.getUpdatedUser().getUserId() < 1) {
238 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
242 this.updateList(event.getUpdatedUser());
248 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminUpdatedUserEvent: EXIT!"); //NOI18N
252 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
254 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
256 // event should not be null
259 throw new NullPointerException("event is null"); //NOI18N
260 } else if (event.getRegisteredUser() == null) {
262 throw new NullPointerException("event.registeredUser is null"); //NOI18N
263 } else if (event.getRegisteredUser().getUserId() == null) {
265 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
266 } else if (event.getRegisteredUser().getUserId() < 1) {
268 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
272 User registeredUser = event.getRegisteredUser();
275 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
277 // Copy all data from registered->user
278 this.copyUser(registeredUser);
283 // Add user to local list
284 this.userList.add(registeredUser);
287 this.addUserName(registeredUser);
289 // Is the account public?
290 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
291 // Also add it to this list
292 this.visibleUserList.add(registeredUser);
296 this.setUserId(registeredUser.getUserId());
299 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
303 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
305 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
307 // event should not be null
310 throw new NullPointerException("event is null"); //NOI18N
311 } else if (event.getLoggedInUser() == null) {
313 throw new NullPointerException("event.registeredUser is null"); //NOI18N
314 } else if (event.getLoggedInUser().getUserId() == null) {
316 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
317 } else if (event.getLoggedInUser().getUserId() < 1) {
319 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
322 // Copy all data to this bean
323 this.copyUser(event.getLoggedInUser());
325 // Re-initialize list
326 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
332 public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
336 throw new NullPointerException("event is null"); //NOI18N
337 } else if (event.getUpdatedUser() == null) {
339 throw new NullPointerException("event.updatedUser is null"); //NOI18N
340 } else if (event.getUpdatedUser().getUserId() == null) {
342 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
343 } else if (event.getUpdatedUser().getUserId() < 1) {
345 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
348 // All fine, so update list
349 this.updateList(event.getUpdatedUser());
353 public List<User> allUsers () {
355 return Collections.unmodifiableList(this.userList);
359 public List<User> allVisibleUsers () {
361 return Collections.unmodifiableList(this.visibleUserList);
365 public User createUserInstance () {
367 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
369 // Required personal data must be set
370 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
372 // Create new user instance
373 User localUser = new LoginUser();
375 // Update all data ...
376 localUser.setUserName(this.getUserName());
377 localUser.setUserProfileMode(this.getUserProfileMode());
379 // Create contact instance
380 Contact contact = this.contactController.createContactInstance();
383 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
385 // Set contact in user
386 localUser.setUserContact(contact);
389 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
396 public User createUserLogin () {
398 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
401 if (this.getUserName() == null) {
403 throw new NullPointerException("recruiterName is null"); //NOI18N
404 } else if (this.getUserName().isEmpty()) {
406 throw new IllegalStateException("recruiterName is empty."); //NOI18N
409 // Create new recruiter instance
410 User recruiter = new LoginUser();
412 // Update all data ...
413 recruiter.setUserName(this.getUserName());
416 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: recruiter={1} - EXIT!", this.getClass().getSimpleName(), recruiter));
418 // Return the new instance
423 public String doChangePersonalData () {
424 // This method shall only be called if the user is logged-in
425 if (!this.loginController.isUserLoggedIn()) {
427 throw new IllegalStateException("User is not logged-in"); //NOI18N
428 } else if (!this.isRequiredChangePersonalDataSet()) {
429 // Not all required fields are set
430 throw new FaceletException("Not all required fields are set."); //NOI18N
431 } else if (!this.loginController.ifCurrentPasswordMatches()) {
432 // Password not matching
433 throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
437 User user = this.loginController.getLoggedInUser();
439 // Copy contact data to contact instance
440 this.contactController.updateContactDataFromController(user.getUserContact());
442 // It should be there, so run some tests on it
443 assert (user instanceof User) : "Instance loginController.loggedInUser is null";
444 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null";
445 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId());
446 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null";
447 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null";
448 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId());
451 user.setUserProfileMode(this.getUserProfileMode());
453 // Send it to the EJB
454 User updatedUser = this.userBean.updateUserPersonalData(user);
457 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
460 return "user_data_saved"; //NOI18N
464 public Long getUserId () {
469 public void setUserId (final Long userId) {
470 this.userId = userId;
474 public String getUserName () {
475 return this.userName;
479 public void setUserName (final String userName) {
480 this.userName = userName;
484 public String getUserPassword () {
485 return this.userPassword;
489 public void setUserPassword (final String userPassword) {
490 this.userPassword = userPassword;
494 public String getUserPasswordRepeat () {
495 return this.userPasswordRepeat;
499 public void setUserPasswordRepeat (final String userPasswordRepeat) {
500 this.userPasswordRepeat = userPasswordRepeat;
504 public ProfileMode getUserProfileMode () {
505 return this.userProfileMode;
509 public void setUserProfileMode (final ProfileMode userProfileMode) {
510 this.userProfileMode = userProfileMode;
514 public boolean hasUsers () {
515 return (!this.allUsers().isEmpty());
519 * Post-initialization of this class
522 public void init () {
523 // Initialize user list
524 this.userList = this.userBean.allUsers();
526 // Get full user name list for reducing EJB calls
527 this.userNameList = this.userBean.getUserNameList();
529 // Is the user logged-in?
530 if (this.userLoginController.isUserLoggedIn()) {
531 // Is logged-in, so load also users visible to memebers
532 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
534 // Initialize user list
535 this.visibleUserList = this.userBean.allPublicUsers();
539 List<User> allUsers = this.allUsers();
542 List<Contact> allContacts = this.contactBean.getAllContacts();
545 Iterator<Contact> iterator = allContacts.iterator();
548 while (iterator.hasNext()) {
550 Contact next = iterator.next();
553 Iterator<User> userIterator = allUsers.iterator();
555 // Loop through all users
556 while (userIterator.hasNext()) {
558 User nextUser = userIterator.next();
561 if (Objects.equals(next, nextUser.getUserContact())) {
570 this.selectableContacts = allContacts;
574 public boolean isContactFound (final Contact contact) {
575 // The contact must be valid
576 if (null == contact) {
578 throw new NullPointerException("contact is null"); //NOI18N
579 } else if (contact.getContactId() == null) {
581 throw new NullPointerException("contact.contactId is null"); //NOI18N
582 } else if (contact.getContactId() < 1) {
584 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
587 // Default is not found
588 boolean isFound = false;
591 Iterator<User> iterator = this.allUsers().iterator();
593 // Loop through all entries
594 while (iterator.hasNext()) {
596 User next = iterator.next();
598 // Compare both objects
599 if (Objects.equals(contact, next.getUserContact())) {
611 public boolean isRequiredChangePersonalDataSet () {
612 return ((this.getUserProfileMode() != null) &&
613 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
614 (this.contactController.isRequiredChangePersonalDataSet()));
618 public boolean isRequiredPersonalDataSet () {
619 return ((this.getUserName() != null) &&
620 (this.getUserProfileMode() != null) &&
621 (this.contactController.isRequiredPersonalDataSet()) &&
622 (this.getUserPassword() != null) &&
623 (this.getUserPasswordRepeat() != null));
627 public boolean isSamePasswordEntered () {
628 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
632 public boolean isUserIdEmpty () {
633 return ((this.getUserId() == null) || (this.getUserId() == 0));
637 public boolean isUserNameRegistered (final User user) {
638 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
642 public boolean isVisibleUserFound () {
643 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
647 public User lookupUserById (final Long userId) throws UserNotFoundException {
648 // Parameter must be valid
649 if (null == userId) {
651 throw new NullPointerException("userId is null"); //NOI18N
652 } else if (userId < 1) {
654 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
660 // Try to lookup it in visible user list
661 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
663 User next = iterator.next();
665 // Is the user id found?
666 if (Objects.equals(next.getUserId(), userId)) {
667 // Copy to other variable
675 // Not visible for the current user
676 throw new UserNotFoundException(userId);
684 public List<Contact> selectableContacts () {
685 return Collections.unmodifiableList(this.selectableContacts);
689 * Adds user's name to bean's internal list. It also updates the public user
690 * list if the user has decided to have a public account,
692 * @param user User instance
694 private void addUserName (final User user) {
695 // Make sure the entry is not added yet
696 if (this.userNameList.contains(user.getUserName())) {
698 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
699 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
701 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
705 this.userNameList.add(user.getUserName());
711 private void clear () {
714 this.setUserId(null);
715 this.setUserProfileMode(null);
718 this.setUserName(null);
719 this.setUserPassword(null);
720 this.setUserPasswordRepeat(null);
724 * Copies given user into the controller
726 * @param user User instance
728 private void copyUser (final User user) {
729 // Make sure the instance is valid
732 throw new NullPointerException("user is null"); //NOI18N
733 } else if (user.getUserContact() == null) {
735 throw new NullPointerException("user.userContact is null"); //NOI18N
740 this.setUserId(user.getUserId());
741 this.setUserProfileMode(user.getUserProfileMode());
745 * Updates list with given user instance
747 * @param user User instance
749 private void updateList (final User user) {
750 // The user should be valid
753 throw new NullPointerException("user is null"); //NOI18N
754 } else if (user.getUserId() == null) {
756 throw new NullPointerException("user.userId is null"); //NOI18N
757 } else if (user.getUserId() < 1) {
759 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
763 Iterator<User> iterator = this.userList.iterator();
766 while (iterator.hasNext()) {
768 User next = iterator.next();
770 // Is the same user id?
771 if (Objects.equals(user.getUserId(), next.getUserId())) {
772 // Found it, so remove it
773 this.userList.remove(next);
779 this.userList.add(user);