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.Observes;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32 import javax.naming.NamingException;
33 import org.mxchange.jcontacts.contact.Contact;
34 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
35 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
36 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
37 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
38 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
39 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
40 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
41 import org.mxchange.jusercore.exceptions.UserNotFoundException;
42 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
43 import org.mxchange.jusercore.model.user.LoginUser;
44 import org.mxchange.jusercore.model.user.User;
45 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
46 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
49 * A user bean (controller)
51 * @author Roland Haeder<roland@mxchange.org>
53 @Named ("userController")
55 public class JobsUserWebSessionBean implements JobsUserWebSessionController {
60 private static final long serialVersionUID = 542_145_347_916L;
63 * General contact controller
66 private JobsContactWebSessionController contactController;
69 * Login bean (controller)
72 private JobsUserLoginWebSessionController loginController;
77 private final UserSessionBeanRemote userBean;
87 private String userName;
92 private List<String> userNameList;
95 * User password (unencrypted from web form)
97 private String userPassword;
100 * User password repeated (unencrypted from web form)
102 private String userPasswordRepeat;
105 * Whether the user wants a public profile
107 private ProfileMode userProfileMode;
110 * A list of all public user profiles
112 private List<User> visibleUserList;
115 * Default constructor
117 public JobsUserWebSessionBean () {
120 // Get initial context
121 Context context = new InitialContext();
124 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
125 } catch (final NamingException e) {
127 throw new FaceletException(e);
132 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
134 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
136 // event should not be null
139 throw new NullPointerException("event is null"); //NOI18N
140 } else if (event.getRegisteredUser() == null) {
142 throw new NullPointerException("event.user is null"); //NOI18N
143 } else if (event.getRegisteredUser().getUserId() == null) {
145 throw new NullPointerException("event.user.userId is null"); //NOI18N
146 } else if (event.getRegisteredUser().getUserId() < 1) {
148 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
152 User registeredUser = event.getRegisteredUser();
155 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
157 // Copy all data from registered->user
158 this.copyUser(registeredUser);
160 // Add user name and email address
161 this.addUserNameEmailAddress(registeredUser);
167 this.setUserId(registeredUser.getUserId());
169 // Is the account public?
170 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
171 // Also add it to this list
172 this.visibleUserList.add(registeredUser);
176 System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
180 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
182 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
184 // event should not be null
187 throw new NullPointerException("event is null"); //NOI18N
188 } else if (event.getLoggedInUser() == null) {
190 throw new NullPointerException("event.user is null"); //NOI18N
191 } else if (event.getLoggedInUser().getUserId() == null) {
193 throw new NullPointerException("event.user.userId is null"); //NOI18N
194 } else if (event.getLoggedInUser().getUserId() < 1) {
196 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
199 // Re-initialize list
200 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
202 // Copy all data to this bean
203 this.copyUser(event.getLoggedInUser());
206 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
210 public List<User> allVisibleUsers () {
212 return Collections.unmodifiableList(this.visibleUserList);
216 public User createUserInstance () {
218 //this.getLogger().logTrace("createUserInstance: CALLED!");
220 // Required personal data must be set
221 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
223 // Create new user instance
224 User localUser = new LoginUser();
226 // Update all data ...
227 localUser.setUserName(this.getUserName());
228 localUser.setUserProfileMode(this.getUserProfileMode());
230 // Create contact instance
231 Contact contact = this.contactController.createContactInstance();
233 // Set contact in user
234 localUser.setUserContact(contact);
237 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
243 public String doChangePersonalData () {
244 // This method shall only be called if the user is logged-in
245 if (!this.loginController.isUserLoggedIn()) {
247 throw new IllegalStateException("User is not logged-in"); //NOI18N
248 } else if (!this.isRequiredChangePersonalDataSet()) {
249 // Not all required fields are set
250 throw new FaceletException("Not all required fields are set."); //NOI18N
251 } else if (!this.loginController.ifCurrentPasswordMatches()) {
252 // Password not matching
253 throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
257 User user = this.loginController.getLoggedInUser();
259 // Copy contact data to contact instance
260 this.contactController.updateContactDataFromController(user.getUserContact());
262 // It should be there, so run some tests on it
263 assert (user instanceof User) : "Instance loginController.loggedInUser is null";
264 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null";
265 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId());
266 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null";
267 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null";
268 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId());
271 user.setUserProfileMode(this.getUserProfileMode());
273 // Send it to the EJB
274 this.userBean.updateUserPersonalData(user);
277 return "user_data_saved"; //NOI18N
281 public Long getUserId () {
286 public void setUserId (final Long userId) {
287 this.userId = userId;
291 public String getUserName () {
292 return this.userName;
296 public void setUserName (final String userName) {
297 this.userName = userName;
301 public String getUserPassword () {
302 return this.userPassword;
306 public void setUserPassword (final String userPassword) {
307 this.userPassword = userPassword;
311 public String getUserPasswordRepeat () {
312 return this.userPasswordRepeat;
316 public void setUserPasswordRepeat (final String userPasswordRepeat) {
317 this.userPasswordRepeat = userPasswordRepeat;
321 public ProfileMode getUserProfileMode () {
322 return this.userProfileMode;
326 public void setUserProfileMode (final ProfileMode userProfileMode) {
327 this.userProfileMode = userProfileMode;
331 * Post-initialization of this class
334 public void init () {
335 // Get full user name list for reducing EJB calls
336 this.userNameList = this.userBean.getUserNameList();
338 // Is the user logged-in?
339 if (this.loginController.isUserLoggedIn()) {
340 // Is logged-in, so load also users visible to memebers
341 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
343 // Initialize user list
344 this.visibleUserList = this.userBean.allPublicUsers();
349 public boolean isRequiredChangePersonalDataSet () {
350 return ((this.getUserProfileMode() != null) &&
351 (this.contactController.isRequiredChangePersonalDataSet()));
355 public boolean isRequiredPersonalDataSet () {
356 return ((this.getUserName() != null) &&
357 (this.getUserProfileMode() != null) &&
358 (this.contactController.isRequiredPersonalDataSet()) &&
359 (this.getUserPassword() != null) &&
360 (this.getUserPasswordRepeat() != null));
364 public boolean isSamePasswordEntered () {
365 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
369 public boolean isUserIdEmpty () {
370 return ((this.getUserId() == null) || (this.getUserId() == 0));
374 public boolean isUserNameRegistered (final User user) {
375 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
379 public boolean isVisibleUserFound () {
380 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
384 public User lookupUserById (final Long userId) throws UserNotFoundException {
386 User localUser = null;
391 // Try to lookup it in visible user list
392 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
394 User next = iterator.next();
396 // Is the user id found?
397 if (Objects.equals(next.getUserId(), userId)) {
398 // Copy to other variable
405 if (null == localUser) {
406 // Not visible for the current user
407 throw new UserNotFoundException(userId);
410 // Copy all data to this bean
411 this.copyUser(localUser);
418 * Adds user's name and email address to bean's internal list. It also
419 * updates the public user list if the user has decided to ha }
421 * @param user User instance
423 private void addUserNameEmailAddress (final User user) {
424 // Make sure the entry is not added yet
425 if (this.userNameList.contains(user.getUserName())) {
427 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
428 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
430 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
434 this.userNameList.add(user.getUserName());
437 this.contactController.addEmailAddress(user.getUserContact().getContactEmailAddress());
443 private void clear () {
446 this.setUserId(null);
447 this.setUserProfileMode(null);
450 this.setUserName(null);
451 this.setUserPassword(null);
452 this.setUserPasswordRepeat(null);
456 * Copies given user into the controller
458 * @param user User instance
460 private void copyUser (final User user) {
463 this.setUserId(user.getUserId());
464 this.setUserProfileMode(user.getUserProfileMode());
466 // Get cellphone, phone and fax instance
467 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
468 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
469 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();