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.jjobs.beans.contact.JobsContactWebSessionController;
37 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
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.jusercore.events.login.UserLoggedInEvent;
42 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
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 * Login bean (controller)
76 private JobsUserLoginWebSessionController loginController;
79 * Event being fired when user updated personal data
83 private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
88 private final UserSessionBeanRemote userBean;
98 private String userName;
103 private List<String> userNameList;
106 * User password (unencrypted from web form)
108 private String userPassword;
111 * User password repeated (unencrypted from web form)
113 private String userPasswordRepeat;
116 * Whether the user wants a public profile
118 private ProfileMode userProfileMode;
121 * A list of all public user profiles
123 private List<User> visibleUserList;
126 * Default constructor
128 public JobsUserWebSessionBean () {
131 // Get initial context
132 Context context = new InitialContext();
135 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
136 } catch (final NamingException e) {
138 throw new FaceletException(e);
143 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
145 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
147 // event should not be null
150 throw new NullPointerException("event is null"); //NOI18N
151 } else if (event.getRegisteredUser() == null) {
153 throw new NullPointerException("event.user is null"); //NOI18N
154 } else if (event.getRegisteredUser().getUserId() == null) {
156 throw new NullPointerException("event.user.userId is null"); //NOI18N
157 } else if (event.getRegisteredUser().getUserId() < 1) {
159 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
163 User registeredUser = event.getRegisteredUser();
166 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
168 // Copy all data from registered->user
169 this.copyUser(registeredUser);
171 // Add user name and email address
172 this.addUserNameEmailAddress(registeredUser);
178 this.setUserId(registeredUser.getUserId());
180 // Is the account public?
181 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
182 // Also add it to this list
183 this.visibleUserList.add(registeredUser);
187 System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
191 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
193 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
195 // event should not be null
198 throw new NullPointerException("event is null"); //NOI18N
199 } else if (event.getLoggedInUser() == null) {
201 throw new NullPointerException("event.user is null"); //NOI18N
202 } else if (event.getLoggedInUser().getUserId() == null) {
204 throw new NullPointerException("event.user.userId is null"); //NOI18N
205 } else if (event.getLoggedInUser().getUserId() < 1) {
207 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
210 // Re-initialize list
211 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
213 // Copy all data to this bean
214 this.copyUser(event.getLoggedInUser());
217 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
221 public List<User> allVisibleUsers () {
223 return Collections.unmodifiableList(this.visibleUserList);
227 public User createUserInstance () {
229 //this.getLogger().logTrace("createUserInstance: CALLED!");
231 // Required personal data must be set
232 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
234 // Create new user instance
235 User localUser = new LoginUser();
237 // Update all data ...
238 localUser.setUserName(this.getUserName());
239 localUser.setUserProfileMode(this.getUserProfileMode());
241 // Create contact instance
242 Contact contact = this.contactController.createContactInstance();
244 // Set contact in user
245 localUser.setUserContact(contact);
248 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
254 public String doChangePersonalData () {
255 // This method shall only be called if the user is logged-in
256 if (!this.loginController.isUserLoggedIn()) {
258 throw new IllegalStateException("User is not logged-in"); //NOI18N
259 } else if (!this.isRequiredChangePersonalDataSet()) {
260 // Not all required fields are set
261 throw new FaceletException("Not all required fields are set."); //NOI18N
262 } else if (!this.loginController.ifCurrentPasswordMatches()) {
263 // Password not matching
264 throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
268 User user = this.loginController.getLoggedInUser();
270 // Copy contact data to contact instance
271 this.contactController.updateContactDataFromController(user.getUserContact());
273 // It should be there, so run some tests on it
274 assert (user instanceof User) : "Instance loginController.loggedInUser is null";
275 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null";
276 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId());
277 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null";
278 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null";
279 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId());
282 user.setUserProfileMode(this.getUserProfileMode());
284 // Send it to the EJB
285 User updatedUser = this.userBean.updateUserPersonalData(user);
288 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
291 return "user_data_saved"; //NOI18N
295 public Long getUserId () {
300 public void setUserId (final Long userId) {
301 this.userId = userId;
305 public String getUserName () {
306 return this.userName;
310 public void setUserName (final String userName) {
311 this.userName = userName;
315 public String getUserPassword () {
316 return this.userPassword;
320 public void setUserPassword (final String userPassword) {
321 this.userPassword = userPassword;
325 public String getUserPasswordRepeat () {
326 return this.userPasswordRepeat;
330 public void setUserPasswordRepeat (final String userPasswordRepeat) {
331 this.userPasswordRepeat = userPasswordRepeat;
335 public ProfileMode getUserProfileMode () {
336 return this.userProfileMode;
340 public void setUserProfileMode (final ProfileMode userProfileMode) {
341 this.userProfileMode = userProfileMode;
345 * Post-initialization of this class
348 public void init () {
349 // Get full user name list for reducing EJB calls
350 this.userNameList = this.userBean.getUserNameList();
352 // Is the user logged-in?
353 if (this.loginController.isUserLoggedIn()) {
354 // Is logged-in, so load also users visible to memebers
355 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
357 // Initialize user list
358 this.visibleUserList = this.userBean.allPublicUsers();
363 public boolean isRequiredChangePersonalDataSet () {
364 return ((this.getUserProfileMode() != null) &&
365 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
366 (this.contactController.isRequiredChangePersonalDataSet()));
370 public boolean isRequiredPersonalDataSet () {
371 return ((this.getUserName() != null) &&
372 (this.getUserProfileMode() != null) &&
373 (this.contactController.isRequiredPersonalDataSet()) &&
374 (this.getUserPassword() != null) &&
375 (this.getUserPasswordRepeat() != null));
379 public boolean isSamePasswordEntered () {
380 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
384 public boolean isUserIdEmpty () {
385 return ((this.getUserId() == null) || (this.getUserId() == 0));
389 public boolean isUserNameRegistered (final User user) {
390 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
394 public boolean isVisibleUserFound () {
395 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
399 public User lookupUserById (final Long userId) throws UserNotFoundException {
401 User localUser = null;
406 // Try to lookup it in visible user list
407 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
409 User next = iterator.next();
411 // Is the user id found?
412 if (Objects.equals(next.getUserId(), userId)) {
413 // Copy to other variable
420 if (null == localUser) {
421 // Not visible for the current user
422 throw new UserNotFoundException(userId);
425 // Copy all data to this bean
426 this.copyUser(localUser);
433 * Adds user's name and email address to bean's internal list. It also
434 * updates the public user list if the user has decided to ha }
436 * @param user User instance
438 private void addUserNameEmailAddress (final User user) {
439 // Make sure the entry is not added yet
440 if (this.userNameList.contains(user.getUserName())) {
442 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
443 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
445 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
449 this.userNameList.add(user.getUserName());
452 this.contactController.addEmailAddress(user.getUserContact().getContactEmailAddress());
458 private void clear () {
461 this.setUserId(null);
462 this.setUserProfileMode(null);
465 this.setUserName(null);
466 this.setUserPassword(null);
467 this.setUserPasswordRepeat(null);
471 * Copies given user into the controller
473 * @param user User instance
475 private void copyUser (final User user) {
478 this.setUserId(user.getUserId());
479 this.setUserProfileMode(user.getUserProfileMode());
481 // Get cellphone, phone and fax instance
482 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
483 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
484 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();