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.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 AddressbookUserWebSessionBean implements AddressbookUserWebSessionController {
67 private static final long serialVersionUID = 542_145_347_916L;
70 * General contact controller
73 private AddressbookContactWebSessionController contactController;
76 * Login bean (controller)
79 private AddressbookUserLoginWebSessionController loginController;
82 * A list of all selectable contacts
84 private List<Contact> selectableContacts;
87 * Event being fired when user updated personal data
91 private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
96 private final UserSessionBeanRemote userBean;
104 * A list of all user profiles
106 private List<User> userList;
109 * Login bean (controller)
112 private AddressbookUserLoginWebSessionController userLoginController;
117 private String userName;
122 private List<String> userNameList;
125 * User password (unencrypted from web form)
127 private String userPassword;
130 * User password repeated (unencrypted from web form)
132 private String userPasswordRepeat;
135 * Whether the user wants a public profile
137 private ProfileMode userProfileMode;
140 * A list of all public user profiles
142 private List<User> visibleUserList;
145 * Default constructor
147 public AddressbookUserWebSessionBean () {
150 // Get initial context
151 Context context = new InitialContext();
154 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
155 } catch (final NamingException e) {
157 throw new FaceletException(e);
162 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
164 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
166 // event should not be null
169 throw new NullPointerException("event is null"); //NOI18N
170 } else if (event.getAddedUser() == null) {
172 throw new NullPointerException("event.addedUser is null"); //NOI18N
173 } else if (event.getAddedUser().getUserId() == null) {
175 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
176 } else if (event.getAddedUser().getUserId() < 1) {
178 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
181 // Add user to local list
182 this.userList.add(event.getAddedUser());
188 this.setUserId(event.getAddedUser().getUserId());
191 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
195 public void afterAdminUpdatedUserDataEvent (@Observes final AdminUpdatedUserDataEvent event) {
197 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminUpdatedUserEvent: event={0} - CALLED!", event)); //NOI18N
199 // event should not be null
202 throw new NullPointerException("event is null"); //NOI18N
203 } else if (event.getUpdatedUser() == null) {
205 throw new NullPointerException("event.updatedUser is null"); //NOI18N
206 } else if (event.getUpdatedUser().getUserId() == null) {
208 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
209 } else if (event.getUpdatedUser().getUserId() < 1) {
211 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
215 this.updateList(event.getUpdatedUser());
221 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminUpdatedUserEvent: EXIT!"); //NOI18N
225 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
227 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
229 // event should not be null
232 throw new NullPointerException("event is null"); //NOI18N
233 } else if (event.getRegisteredUser() == null) {
235 throw new NullPointerException("event.registeredUser is null"); //NOI18N
236 } else if (event.getRegisteredUser().getUserId() == null) {
238 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
239 } else if (event.getRegisteredUser().getUserId() < 1) {
241 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
245 User registeredUser = event.getRegisteredUser();
248 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
250 // Copy all data from registered->user
251 this.copyUser(registeredUser);
256 // Add user to local list
257 this.userList.add(registeredUser);
260 this.addUserName(registeredUser);
262 // Is the account public?
263 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
264 // Also add it to this list
265 this.visibleUserList.add(registeredUser);
269 this.setUserId(registeredUser.getUserId());
272 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
276 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
278 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
280 // event should not be null
283 throw new NullPointerException("event is null"); //NOI18N
284 } else if (event.getLoggedInUser() == null) {
286 throw new NullPointerException("event.registeredUser is null"); //NOI18N
287 } else if (event.getLoggedInUser().getUserId() == null) {
289 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
290 } else if (event.getLoggedInUser().getUserId() < 1) {
292 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
295 // Copy all data to this bean
296 this.copyUser(event.getLoggedInUser());
298 // Re-initialize list
299 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
305 public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
309 throw new NullPointerException("event is null"); //NOI18N
310 } else if (event.getUpdatedUser() == null) {
312 throw new NullPointerException("event.updatedUser is null"); //NOI18N
313 } else if (event.getUpdatedUser().getUserId() == null) {
315 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
316 } else if (event.getUpdatedUser().getUserId() < 1) {
318 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
321 // All fine, so update list
322 this.updateList(event.getUpdatedUser());
326 public List<User> allUsers () {
328 return Collections.unmodifiableList(this.userList);
332 public List<User> allVisibleUsers () {
334 return Collections.unmodifiableList(this.visibleUserList);
338 public User createUserInstance () {
340 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
342 // Required personal data must be set
343 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
345 // Create new user instance
346 User localUser = new LoginUser();
348 // Update all data ...
349 localUser.setUserName(this.getUserName());
350 localUser.setUserProfileMode(this.getUserProfileMode());
352 // Create contact instance
353 Contact contact = this.contactController.createContactInstance();
356 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
358 // Set contact in user
359 localUser.setUserContact(contact);
362 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
369 public User createUserLogin () {
371 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
374 if (this.getUserName() == null) {
376 throw new NullPointerException("recruiterName is null"); //NOI18N
377 } else if (this.getUserName().isEmpty()) {
379 throw new IllegalStateException("recruiterName is empty."); //NOI18N
382 // Create new recruiter instance
383 User recruiter = new LoginUser();
385 // Update all data ...
386 recruiter.setUserName(this.getUserName());
389 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: recruiter={1} - EXIT!", this.getClass().getSimpleName(), recruiter));
391 // Return the new instance
396 public String doChangePersonalData () {
397 // This method shall only be called if the user is logged-in
398 if (!this.loginController.isUserLoggedIn()) {
400 throw new IllegalStateException("User is not logged-in"); //NOI18N
401 } else if (!this.isRequiredChangePersonalDataSet()) {
402 // Not all required fields are set
403 throw new FaceletException("Not all required fields are set."); //NOI18N
404 } else if (!this.loginController.ifCurrentPasswordMatches()) {
405 // Password not matching
406 throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
410 User user = this.loginController.getLoggedInUser();
412 // Copy contact data to contact instance
413 this.contactController.updateContactDataFromController(user.getUserContact());
415 // It should be there, so run some tests on it
416 assert (user instanceof User) : "Instance loginController.loggedInUser is null";
417 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null";
418 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId());
419 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null";
420 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null";
421 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId());
424 user.setUserProfileMode(this.getUserProfileMode());
426 // Send it to the EJB
427 User updatedUser = this.userBean.updateUserPersonalData(user);
430 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
433 return "user_data_saved"; //NOI18N
437 public Long getUserId () {
442 public void setUserId (final Long userId) {
443 this.userId = userId;
447 public String getUserName () {
448 return this.userName;
452 public void setUserName (final String userName) {
453 this.userName = userName;
457 public String getUserPassword () {
458 return this.userPassword;
462 public void setUserPassword (final String userPassword) {
463 this.userPassword = userPassword;
467 public String getUserPasswordRepeat () {
468 return this.userPasswordRepeat;
472 public void setUserPasswordRepeat (final String userPasswordRepeat) {
473 this.userPasswordRepeat = userPasswordRepeat;
477 public ProfileMode getUserProfileMode () {
478 return this.userProfileMode;
482 public void setUserProfileMode (final ProfileMode userProfileMode) {
483 this.userProfileMode = userProfileMode;
487 public boolean hasUsers () {
488 return (!this.allUsers().isEmpty());
492 * Post-initialization of this class
495 public void init () {
496 // Initialize user list
497 this.userList = this.userBean.allUsers();
499 // Get full user name list for reducing EJB calls
500 this.userNameList = this.userBean.getUserNameList();
502 // Is the user logged-in?
503 if (this.userLoginController.isUserLoggedIn()) {
504 // Is logged-in, so load also users visible to memebers
505 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
507 // Initialize user list
508 this.visibleUserList = this.userBean.allPublicUsers();
512 List<User> allUsers = this.allUsers();
515 List<Contact> allContacts = this.contactBean.getAllContacts();
518 Iterator<Contact> iterator = allContacts.iterator();
521 while (iterator.hasNext()) {
523 Contact next = iterator.next();
526 Iterator<User> userIterator = allUsers.iterator();
528 // Loop through all users
529 while (userIterator.hasNext()) {
531 User nextUser = userIterator.next();
534 if (Objects.equals(next, nextUser.getUserContact())) {
543 this.selectableContacts = allContacts;
547 public boolean isContactFound (final Contact contact) {
548 // The contact must be valid
549 if (null == contact) {
551 throw new NullPointerException("contact is null"); //NOI18N
552 } else if (contact.getContactId() == null) {
554 throw new NullPointerException("contact.contactId is null"); //NOI18N
555 } else if (contact.getContactId() < 1) {
557 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
560 // Default is not found
561 boolean isFound = false;
564 Iterator<User> iterator = this.allUsers().iterator();
566 // Loop through all entries
567 while (iterator.hasNext()) {
569 User next = iterator.next();
571 // Compare both objects
572 if (Objects.equals(contact, next.getUserContact())) {
584 public boolean isRequiredChangePersonalDataSet () {
585 return ((this.getUserProfileMode() != null) &&
586 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
587 (this.contactController.isRequiredChangePersonalDataSet()));
591 public boolean isRequiredPersonalDataSet () {
592 return ((this.getUserName() != null) &&
593 (this.getUserProfileMode() != null) &&
594 (this.contactController.isRequiredPersonalDataSet()) &&
595 (this.getUserPassword() != null) &&
596 (this.getUserPasswordRepeat() != null));
600 public boolean isSamePasswordEntered () {
601 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
605 public boolean isUserIdEmpty () {
606 return ((this.getUserId() == null) || (this.getUserId() == 0));
610 public boolean isUserNameRegistered (final User user) {
611 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
615 public boolean isVisibleUserFound () {
616 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
620 public User lookupUserById (final Long userId) throws UserNotFoundException {
621 // Parameter must be valid
622 if (null == userId) {
624 throw new NullPointerException("userId is null"); //NOI18N
625 } else if (userId < 1) {
627 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
633 // Try to lookup it in visible user list
634 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
636 User next = iterator.next();
638 // Is the user id found?
639 if (Objects.equals(next.getUserId(), userId)) {
640 // Copy to other variable
648 // Not visible for the current user
649 throw new UserNotFoundException(userId);
657 public List<Contact> selectableContacts () {
658 return Collections.unmodifiableList(this.selectableContacts);
662 * Adds user's name to bean's internal list. It also updates the public user
663 * list if the user has decided to have a public account,
665 * @param user User instance
667 private void addUserName (final User user) {
668 // Make sure the entry is not added yet
669 if (this.userNameList.contains(user.getUserName())) {
671 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
672 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
674 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
678 this.userNameList.add(user.getUserName());
684 private void clear () {
687 this.setUserId(null);
688 this.setUserProfileMode(null);
691 this.setUserName(null);
692 this.setUserPassword(null);
693 this.setUserPasswordRepeat(null);
697 * Copies given user into the controller
699 * @param user User instance
701 private void copyUser (final User user) {
702 // Make sure the instance is valid
705 throw new NullPointerException("user is null"); //NOI18N
706 } else if (user.getUserContact() == null) {
708 throw new NullPointerException("user.userContact is null"); //NOI18N
713 this.setUserId(user.getUserId());
714 this.setUserProfileMode(user.getUserProfileMode());
718 * Updates list with given user instance
720 * @param user User instance
722 private void updateList (final User user) {
723 // The user should be valid
726 throw new NullPointerException("user is null"); //NOI18N
727 } else if (user.getUserId() == null) {
729 throw new NullPointerException("user.userId is null"); //NOI18N
730 } else if (user.getUserId() < 1) {
732 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
736 Iterator<User> iterator = this.userList.iterator();
739 while (iterator.hasNext()) {
741 User next = iterator.next();
743 // Is the same user id?
744 if (Objects.equals(user.getUserId(), next.getUserId())) {
745 // Found it, so remove it
746 this.userList.remove(next);
752 this.userList.add(user);