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.events.contact.add.AdminAddedContactEvent;
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 AddressbookUserWebSessionBean implements AddressbookUserWebSessionController {
64 private static final long serialVersionUID = 542_145_347_916L;
67 * General contact controller
70 private AddressbookContactWebSessionController contactController;
73 * Login bean (controller)
76 private AddressbookUserLoginWebSessionController loginController;
79 * A list of all selectable contacts
81 private List<Contact> selectableContacts;
84 * Event being fired when user updated personal data
88 private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
93 private final UserSessionBeanRemote userBean;
101 * A list of all user profiles
103 private List<User> userList;
106 * Login bean (controller)
109 private AddressbookUserLoginWebSessionController userLoginController;
114 private String userName;
119 private List<String> userNameList;
122 * User password (unencrypted from web form)
124 private String userPassword;
127 * User password repeated (unencrypted from web form)
129 private String userPasswordRepeat;
132 * Whether the user wants a public profile
134 private ProfileMode userProfileMode;
137 * A list of all public user profiles
139 private List<User> visibleUserList;
142 * Default constructor
144 public AddressbookUserWebSessionBean () {
147 // Get initial context
148 Context context = new InitialContext();
151 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
152 } catch (final NamingException e) {
154 throw new FaceletException(e);
159 public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
160 // The event must be valid
163 throw new NullPointerException("event is null"); //NOI18N
164 } else if (event.getAddedContact()== null) {
166 throw new NullPointerException("event.addedContact is null"); //NOI18N
167 } else if (event.getAddedContact().getContactId() == null) {
169 throw new NullPointerException("event.addedContact.customerId is null"); //NOI18N
170 } else if (event.getAddedContact().getContactId() < 1) {
172 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
176 this.selectableContacts.add(event.getAddedContact());
180 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
182 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
184 // event should not be null
187 throw new NullPointerException("event is null"); //NOI18N
188 } else if (event.getAddedUser() == null) {
190 throw new NullPointerException("event.addedUser is null"); //NOI18N
191 } else if (event.getAddedUser().getUserId() == null) {
193 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
194 } else if (event.getAddedUser().getUserId() < 1) {
196 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
199 // Add user to local list
200 this.userList.add(event.getAddedUser());
206 this.setUserId(event.getAddedUser().getUserId());
209 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
213 public void afterAdminUpdatedUserDataEvent (@Observes final AdminUpdatedUserDataEvent event) {
215 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminUpdatedUserEvent: event={0} - CALLED!", event)); //NOI18N
217 // event should not be null
220 throw new NullPointerException("event is null"); //NOI18N
221 } else if (event.getUpdatedUser() == null) {
223 throw new NullPointerException("event.updatedUser is null"); //NOI18N
224 } else if (event.getUpdatedUser().getUserId() == null) {
226 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
227 } else if (event.getUpdatedUser().getUserId() < 1) {
229 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
233 this.updateList(event.getUpdatedUser());
239 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminUpdatedUserEvent: EXIT!"); //NOI18N
243 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
245 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
247 // event should not be null
250 throw new NullPointerException("event is null"); //NOI18N
251 } else if (event.getRegisteredUser() == null) {
253 throw new NullPointerException("event.registeredUser is null"); //NOI18N
254 } else if (event.getRegisteredUser().getUserId() == null) {
256 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
257 } else if (event.getRegisteredUser().getUserId() < 1) {
259 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
263 User registeredUser = event.getRegisteredUser();
266 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
268 // Copy all data from registered->user
269 this.copyUser(registeredUser);
274 // Add user to local list
275 this.userList.add(registeredUser);
278 this.addUserName(registeredUser);
280 // Is the account public?
281 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
282 // Also add it to this list
283 this.visibleUserList.add(registeredUser);
287 this.setUserId(registeredUser.getUserId());
290 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
294 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
296 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
298 // event should not be null
301 throw new NullPointerException("event is null"); //NOI18N
302 } else if (event.getLoggedInUser() == null) {
304 throw new NullPointerException("event.registeredUser is null"); //NOI18N
305 } else if (event.getLoggedInUser().getUserId() == null) {
307 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
308 } else if (event.getLoggedInUser().getUserId() < 1) {
310 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
313 // Copy all data to this bean
314 this.copyUser(event.getLoggedInUser());
316 // Re-initialize list
317 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
323 public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
327 throw new NullPointerException("event is null"); //NOI18N
328 } else if (event.getUpdatedUser() == null) {
330 throw new NullPointerException("event.updatedUser is null"); //NOI18N
331 } else if (event.getUpdatedUser().getUserId() == null) {
333 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
334 } else if (event.getUpdatedUser().getUserId() < 1) {
336 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
339 // All fine, so update list
340 this.updateList(event.getUpdatedUser());
344 public List<User> allUsers () {
346 return Collections.unmodifiableList(this.userList);
350 public List<User> allVisibleUsers () {
352 return Collections.unmodifiableList(this.visibleUserList);
356 public User createUserInstance () {
358 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
360 // Required personal data must be set
361 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
363 // Create new user instance
364 User localUser = new LoginUser();
366 // Update all data ...
367 localUser.setUserName(this.getUserName());
368 localUser.setUserProfileMode(this.getUserProfileMode());
370 // Create contact instance
371 Contact contact = this.contactController.createContactInstance();
374 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
376 // Set contact in user
377 localUser.setUserContact(contact);
380 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
387 public User createUserLogin () {
389 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
392 if (this.getUserName() == null) {
394 throw new NullPointerException("recruiterName is null"); //NOI18N
395 } else if (this.getUserName().isEmpty()) {
397 throw new IllegalStateException("recruiterName is empty."); //NOI18N
400 // Create new recruiter instance
401 User recruiter = new LoginUser();
403 // Update all data ...
404 recruiter.setUserName(this.getUserName());
407 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: recruiter={1} - EXIT!", this.getClass().getSimpleName(), recruiter));
409 // Return the new instance
414 public String doChangePersonalData () {
415 // This method shall only be called if the user is logged-in
416 if (!this.loginController.isUserLoggedIn()) {
418 throw new IllegalStateException("User is not logged-in"); //NOI18N
419 } else if (!this.isRequiredChangePersonalDataSet()) {
420 // Not all required fields are set
421 throw new FaceletException("Not all required fields are set."); //NOI18N
422 } else if (!this.loginController.ifCurrentPasswordMatches()) {
423 // Password not matching
424 throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
428 User user = this.loginController.getLoggedInUser();
430 // Copy contact data to contact instance
431 this.contactController.updateContactDataFromController(user.getUserContact());
433 // It should be there, so run some tests on it
434 assert (user instanceof User) : "Instance loginController.loggedInUser is null";
435 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null";
436 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId());
437 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null";
438 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null";
439 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId());
442 user.setUserProfileMode(this.getUserProfileMode());
444 // Send it to the EJB
445 User updatedUser = this.userBean.updateUserPersonalData(user);
448 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
451 return "user_data_saved"; //NOI18N
455 public Long getUserId () {
460 public void setUserId (final Long userId) {
461 this.userId = userId;
465 public String getUserName () {
466 return this.userName;
470 public void setUserName (final String userName) {
471 this.userName = userName;
475 public String getUserPassword () {
476 return this.userPassword;
480 public void setUserPassword (final String userPassword) {
481 this.userPassword = userPassword;
485 public String getUserPasswordRepeat () {
486 return this.userPasswordRepeat;
490 public void setUserPasswordRepeat (final String userPasswordRepeat) {
491 this.userPasswordRepeat = userPasswordRepeat;
495 public ProfileMode getUserProfileMode () {
496 return this.userProfileMode;
500 public void setUserProfileMode (final ProfileMode userProfileMode) {
501 this.userProfileMode = userProfileMode;
505 public boolean hasUsers () {
506 return (!this.allUsers().isEmpty());
510 * Post-initialization of this class
513 public void init () {
514 // Initialize user list
515 this.userList = this.userBean.allUsers();
517 // Get full user name list for reducing EJB calls
518 this.userNameList = this.userBean.getUserNameList();
520 // Is the user logged-in?
521 if (this.userLoginController.isUserLoggedIn()) {
522 // Is logged-in, so load also users visible to memebers
523 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
525 // Initialize user list
526 this.visibleUserList = this.userBean.allPublicUsers();
530 List<User> allUsers = this.allUsers();
533 List<Contact> allContacts = this.contactBean.getAllContacts();
536 Iterator<Contact> iterator = allContacts.iterator();
539 while (iterator.hasNext()) {
541 Contact next = iterator.next();
544 Iterator<User> userIterator = allUsers.iterator();
546 // Loop through all users
547 while (userIterator.hasNext()) {
549 User nextUser = userIterator.next();
552 if (Objects.equals(next, nextUser.getUserContact())) {
561 this.selectableContacts = allContacts;
565 public boolean isContactFound (final Contact contact) {
566 // The contact must be valid
567 if (null == contact) {
569 throw new NullPointerException("contact is null"); //NOI18N
570 } else if (contact.getContactId() == null) {
572 throw new NullPointerException("contact.contactId is null"); //NOI18N
573 } else if (contact.getContactId() < 1) {
575 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
578 // Default is not found
579 boolean isFound = false;
582 Iterator<User> iterator = this.allUsers().iterator();
584 // Loop through all entries
585 while (iterator.hasNext()) {
587 User next = iterator.next();
589 // Compare both objects
590 if (Objects.equals(contact, next.getUserContact())) {
602 public boolean isRequiredChangePersonalDataSet () {
603 return ((this.getUserProfileMode() != null) &&
604 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
605 (this.contactController.isRequiredChangePersonalDataSet()));
609 public boolean isRequiredPersonalDataSet () {
610 return ((this.getUserName() != null) &&
611 (this.getUserProfileMode() != null) &&
612 (this.contactController.isRequiredPersonalDataSet()) &&
613 (this.getUserPassword() != null) &&
614 (this.getUserPasswordRepeat() != null));
618 public boolean isSamePasswordEntered () {
619 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
623 public boolean isUserIdEmpty () {
624 return ((this.getUserId() == null) || (this.getUserId() == 0));
628 public boolean isUserNameRegistered (final User user) {
629 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
633 public boolean isVisibleUserFound () {
634 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
638 public User lookupUserById (final Long userId) throws UserNotFoundException {
639 // Parameter must be valid
640 if (null == userId) {
642 throw new NullPointerException("userId is null"); //NOI18N
643 } else if (userId < 1) {
645 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
651 // Try to lookup it in visible user list
652 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
654 User next = iterator.next();
656 // Is the user id found?
657 if (Objects.equals(next.getUserId(), userId)) {
658 // Copy to other variable
666 // Not visible for the current user
667 throw new UserNotFoundException(userId);
675 public List<Contact> selectableContacts () {
676 return Collections.unmodifiableList(this.selectableContacts);
680 * Adds user's name to bean's internal list. It also updates the public user
681 * list if the user has decided to have a public account,
683 * @param user User instance
685 private void addUserName (final User user) {
686 // Make sure the entry is not added yet
687 if (this.userNameList.contains(user.getUserName())) {
689 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
690 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
692 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
696 this.userNameList.add(user.getUserName());
702 private void clear () {
705 this.setUserId(null);
706 this.setUserProfileMode(null);
709 this.setUserName(null);
710 this.setUserPassword(null);
711 this.setUserPasswordRepeat(null);
715 * Copies given user into the controller
717 * @param user User instance
719 private void copyUser (final User user) {
720 // Make sure the instance is valid
723 throw new NullPointerException("user is null"); //NOI18N
724 } else if (user.getUserContact() == null) {
726 throw new NullPointerException("user.userContact is null"); //NOI18N
731 this.setUserId(user.getUserId());
732 this.setUserProfileMode(user.getUserProfileMode());
736 * Updates list with given user instance
738 * @param user User instance
740 private void updateList (final User user) {
741 // The user should be valid
744 throw new NullPointerException("user is null"); //NOI18N
745 } else if (user.getUserId() == null) {
747 throw new NullPointerException("user.userId is null"); //NOI18N
748 } else if (user.getUserId() < 1) {
750 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
754 Iterator<User> iterator = this.userList.iterator();
757 while (iterator.hasNext()) {
759 User next = iterator.next();
761 // Is the same user id?
762 if (Objects.equals(user.getUserId(), next.getUserId())) {
763 // Found it, so remove it
764 this.userList.remove(next);
770 this.userList.add(user);