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.Iterator;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.SessionScoped;
25 import javax.enterprise.event.Event;
26 import javax.enterprise.event.Observes;
27 import javax.enterprise.inject.Any;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
36 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
37 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
38 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
39 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
40 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
41 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
42 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
43 import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
44 import org.mxchange.jusercore.exceptions.UserNotFoundException;
45 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
46 import org.mxchange.jusercore.model.user.LoginUser;
47 import org.mxchange.jusercore.model.user.User;
48 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
49 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
52 * A user bean (controller)
54 * @author Roland Haeder<roland@mxchange.org>
56 @Named ("userController")
58 public class JobsUserWebSessionBean implements JobsUserWebSessionController {
63 private static final long serialVersionUID = 542_145_347_916L;
66 * General contact controller
69 private JobsContactWebSessionController contactController;
72 * Event being fired when user updated personal data
76 private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
81 private final UserSessionBeanRemote userBean;
89 * Login bean (controller)
92 private JobsUserLoginWebSessionController userLoginController;
97 private String userName;
100 * User password (unencrypted from web form)
102 private String userPassword;
105 * User password repeated (unencrypted from web form)
107 private String userPasswordRepeat;
110 * Whether the user wants a public profile
112 private ProfileMode userProfileMode;
115 * A list of all public user profiles
117 private List<User> visibleUserList;
120 * Default constructor
122 public JobsUserWebSessionBean () {
125 // Get initial context
126 Context context = new InitialContext();
129 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
130 } catch (final NamingException e) {
132 throw new FaceletException(e);
137 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
139 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
141 // event should not be null
144 throw new NullPointerException("event is null"); //NOI18N
145 } else if (event.getRegisteredUser() == null) {
147 throw new NullPointerException("event.user is null"); //NOI18N
148 } else if (event.getRegisteredUser().getUserId() == null) {
150 throw new NullPointerException("event.user.userId is null"); //NOI18N
151 } else if (event.getRegisteredUser().getUserId() < 1) {
153 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
157 User registeredUser = event.getRegisteredUser();
160 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
162 // Copy all data from registered->user
163 this.copyUser(registeredUser);
169 this.setUserId(registeredUser.getUserId());
172 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
176 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
178 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
180 // event should not be null
183 throw new NullPointerException("event is null"); //NOI18N
184 } else if (event.getLoggedInUser() == null) {
186 throw new NullPointerException("event.user is null"); //NOI18N
187 } else if (event.getLoggedInUser().getUserId() == null) {
189 throw new NullPointerException("event.user.userId is null"); //NOI18N
190 } else if (event.getLoggedInUser().getUserId() < 1) {
192 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
195 // Copy all data to this bean
196 this.copyUser(event.getLoggedInUser());
199 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
203 public User createUserInstance () {
205 //this.getLogger().logTrace("createUserInstance: CALLED!");
207 // Required personal data must be set
208 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
210 // Create new user instance
211 User localUser = new LoginUser();
213 // Update all data ...
214 localUser.setUserName(this.getUserName());
215 localUser.setUserProfileMode(this.getUserProfileMode());
217 // Create contact instance
218 Contact contact = this.contactController.createContactInstance();
220 // Set contact in user
221 localUser.setUserContact(contact);
224 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
230 public String doChangePersonalData () {
231 // This method shall only be called if the user is logged-in
232 if (!this.userLoginController.isUserLoggedIn()) {
234 throw new IllegalStateException("User is not logged-in"); //NOI18N
235 } else if (!this.isRequiredChangePersonalDataSet()) {
236 // Not all required fields are set
237 throw new FaceletException("Not all required fields are set."); //NOI18N
238 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
239 // Password not matching
240 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
244 User user = this.userLoginController.getLoggedInUser();
246 // Copy contact data to contact instance
247 this.contactController.updateContactDataFromController(user.getUserContact());
249 // It should be there, so run some tests on it
250 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
251 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
252 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
253 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
254 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
255 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
258 user.setUserProfileMode(this.getUserProfileMode());
260 // Send it to the EJB
261 User updatedUser = this.userBean.updateUserPersonalData(user);
264 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
267 return "user_data_saved"; //NOI18N
271 public Long getUserId () {
276 public void setUserId (final Long userId) {
277 this.userId = userId;
281 public String getUserName () {
282 return this.userName;
286 public void setUserName (final String userName) {
287 this.userName = userName;
291 public String getUserPassword () {
292 return this.userPassword;
296 public void setUserPassword (final String userPassword) {
297 this.userPassword = userPassword;
301 public String getUserPasswordRepeat () {
302 return this.userPasswordRepeat;
306 public void setUserPasswordRepeat (final String userPasswordRepeat) {
307 this.userPasswordRepeat = userPasswordRepeat;
311 public ProfileMode getUserProfileMode () {
312 return this.userProfileMode;
316 public void setUserProfileMode (final ProfileMode userProfileMode) {
317 this.userProfileMode = userProfileMode;
321 * Post-initialization of this class
324 public void init () {
328 public boolean isRequiredChangePersonalDataSet () {
329 return ((this.getUserProfileMode() != null) &&
330 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
331 (this.contactController.isRequiredChangePersonalDataSet()));
335 public boolean isRequiredPersonalDataSet () {
336 return ((this.getUserName() != null) &&
337 (this.getUserProfileMode() != null) &&
338 (this.contactController.isRequiredPersonalDataSet()) &&
339 (this.getUserPassword() != null) &&
340 (this.getUserPasswordRepeat() != null));
344 public boolean isSamePasswordEntered () {
345 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
349 public boolean isUserIdEmpty () {
350 return ((this.getUserId() == null) || (this.getUserId() == 0));
354 public User lookupUserById (final Long userId) throws UserNotFoundException {
356 User localUser = null;
361 // Try to lookup it in visible user list
362 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
364 User next = iterator.next();
366 // Is the user id found?
367 if (Objects.equals(next.getUserId(), userId)) {
368 // Copy to other variable
375 if (null == localUser) {
376 // Not visible for the current user
377 throw new UserNotFoundException(userId);
380 // Copy all data to this bean
381 this.copyUser(localUser);
390 private void clear () {
393 this.setUserId(null);
394 this.setUserProfileMode(null);
397 this.setUserName(null);
398 this.setUserPassword(null);
399 this.setUserPasswordRepeat(null);
403 * Copies given user into the controller
405 * @param user User instance
407 private void copyUser (final User user) {
410 this.setUserId(user.getUserId());
411 this.setUserProfileMode(user.getUserProfileMode());
413 // Get cellphone, phone and fax instance
414 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
415 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
416 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();