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.Objects;
21 import javax.annotation.PostConstruct;
22 import javax.enterprise.context.SessionScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.event.Observes;
25 import javax.enterprise.inject.Any;
26 import javax.faces.view.facelets.FaceletException;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.addressbook.beans.contact.AddressbookContactWebSessionController;
33 import org.mxchange.addressbook.beans.login.AddressbookUserLoginWebSessionController;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
36 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
37 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
38 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
39 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
40 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
41 import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
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 AddressbookUserWebSessionBean implements AddressbookUserWebSessionController {
60 private static final long serialVersionUID = 542_145_347_916L;
63 * General contact controller
66 private AddressbookContactWebSessionController contactController;
69 * Login bean (controller)
72 private AddressbookUserLoginWebSessionController loginController;
75 * Event being fired when user updated personal data
79 private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
84 private final UserSessionBeanRemote userBean;
94 private String userName;
97 * User password (unencrypted from web form)
99 private String userPassword;
102 * User password repeated (unencrypted from web form)
104 private String userPasswordRepeat;
107 * Whether the user wants a public profile
109 private ProfileMode userProfileMode;
112 * Default constructor
114 public AddressbookUserWebSessionBean () {
117 // Get initial context
118 Context context = new InitialContext();
121 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
122 } catch (final NamingException e) {
124 throw new FaceletException(e);
129 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
131 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
133 // event should not be null
136 throw new NullPointerException("event is null"); //NOI18N
137 } else if (event.getRegisteredUser() == null) {
139 throw new NullPointerException("event.user is null"); //NOI18N
140 } else if (event.getRegisteredUser().getUserId() == null) {
142 throw new NullPointerException("event.user.userId is null"); //NOI18N
143 } else if (event.getRegisteredUser().getUserId() < 1) {
145 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
149 User registeredUser = event.getRegisteredUser();
152 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
154 // Copy all data from registered->user
155 this.copyUser(registeredUser);
161 this.setUserId(registeredUser.getUserId());
164 System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
168 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
170 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
172 // event should not be null
175 throw new NullPointerException("event is null"); //NOI18N
176 } else if (event.getLoggedInUser() == null) {
178 throw new NullPointerException("event.user is null"); //NOI18N
179 } else if (event.getLoggedInUser().getUserId() == null) {
181 throw new NullPointerException("event.user.userId is null"); //NOI18N
182 } else if (event.getLoggedInUser().getUserId() < 1) {
184 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
187 // Copy all data to this bean
188 this.copyUser(event.getLoggedInUser());
194 public User createUserInstance () {
196 //this.getLogger().logTrace("createUserInstance: CALLED!");
198 // Required personal data must be set
199 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
201 // Create new user instance
202 User localUser = new LoginUser();
204 // Update all data ...
205 localUser.setUserName(this.getUserName());
206 localUser.setUserProfileMode(this.getUserProfileMode());
208 // Create contact instance
209 Contact contact = this.contactController.createContactInstance();
211 // Set contact in user
212 localUser.setUserContact(contact);
215 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
221 public String doChangePersonalData () {
222 // This method shall only be called if the user is logged-in
223 if (!this.loginController.isUserLoggedIn()) {
225 throw new IllegalStateException("User is not logged-in"); //NOI18N
226 } else if (!this.isRequiredChangePersonalDataSet()) {
227 // Not all required fields are set
228 throw new FaceletException("Not all required fields are set."); //NOI18N
229 } else if (!this.loginController.ifCurrentPasswordMatches()) {
230 // Password not matching
231 throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
235 User user = this.loginController.getLoggedInUser();
237 // Copy contact data to contact instance
238 this.contactController.updateContactDataFromController(user.getUserContact());
240 // It should be there, so run some tests on it
241 assert (user instanceof User) : "Instance loginController.loggedInUser is null";
242 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null";
243 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId());
244 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null";
245 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null";
246 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId());
249 user.setUserProfileMode(this.getUserProfileMode());
251 // Send it to the EJB
252 User updatedUser = this.userBean.updateUserPersonalData(user);
255 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
258 return "user_data_saved"; //NOI18N
262 public Long getUserId () {
267 public void setUserId (final Long userId) {
268 this.userId = userId;
272 public String getUserName () {
273 return this.userName;
277 public void setUserName (final String userName) {
278 this.userName = userName;
282 public String getUserPassword () {
283 return this.userPassword;
287 public void setUserPassword (final String userPassword) {
288 this.userPassword = userPassword;
292 public String getUserPasswordRepeat () {
293 return this.userPasswordRepeat;
297 public void setUserPasswordRepeat (final String userPasswordRepeat) {
298 this.userPasswordRepeat = userPasswordRepeat;
302 public ProfileMode getUserProfileMode () {
303 return this.userProfileMode;
307 public void setUserProfileMode (final ProfileMode userProfileMode) {
308 this.userProfileMode = userProfileMode;
312 * Post-initialization of this class
315 public void init () {
319 public boolean isRequiredChangePersonalDataSet () {
320 return ((this.getUserProfileMode() != null) &&
321 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
322 (this.contactController.isRequiredChangePersonalDataSet()));
326 public boolean isRequiredPersonalDataSet () {
327 return ((this.getUserName() != null) &&
328 (this.getUserProfileMode() != null) &&
329 (this.contactController.isRequiredPersonalDataSet()) &&
330 (this.getUserPassword() != null) &&
331 (this.getUserPasswordRepeat() != null));
335 public boolean isSamePasswordEntered () {
336 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
340 public boolean isUserIdEmpty () {
341 return ((this.getUserId() == null) || (this.getUserId() == 0));
347 private void clear () {
350 this.setUserId(null);
351 this.setUserProfileMode(null);
354 this.setUserName(null);
355 this.setUserPassword(null);
356 this.setUserPasswordRepeat(null);
360 * Copies given user into the controller
362 * @param user User instance
364 private void copyUser (final User user) {
367 this.setUserId(user.getUserId());
368 this.setUserProfileMode(user.getUserProfileMode());
370 // Get cellphone, phone and fax instance
371 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
372 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
373 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();