2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.jfinancials.beans.user.action;
19 import java.text.MessageFormat;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.faces.FacesException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.jcontacts.model.contact.Contact;
28 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
29 import org.mxchange.jfinancials.beans.contact.FinancialsContactWebRequestController;
30 import org.mxchange.jfinancials.beans.features.FinancialsFeaturesWebApplicationController;
31 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
32 import org.mxchange.jusercore.events.user.update.post.ObservablePostUserPersonalDataUpdatedEvent;
33 import org.mxchange.jusercore.events.user.update.post.PostUserPersonalDataUpdatedEvent;
34 import org.mxchange.jusercore.events.user.update.pre.ObservablePreUserPersonalDataUpdatedEvent;
35 import org.mxchange.jusercore.events.user.update.pre.PreUserPersonalDataUpdatedEvent;
36 import org.mxchange.jusercore.model.user.User;
37 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
38 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
41 * A user action bean (controller)
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("userActionController")
47 public class FinancialsUserActionWebRequestBean extends BaseFinancialsBean implements FinancialsUserActionWebRequestController {
52 private static final long serialVersionUID = 542_145_347_920L;
55 * General contact controller
58 private FinancialsContactWebRequestController contactController;
64 private FinancialsFeaturesWebApplicationController featureController;
67 * Event being fired when user updated personal data
71 private Event<ObservablePostUserPersonalDataUpdatedEvent> postUpdatedPersonalDataEvent;
74 * Event being fired when user updated personal data
78 private Event<ObservablePreUserPersonalDataUpdatedEvent> preUpdatedPersonalDataEvent;
83 @EJB (lookup = "java:global/jfinancials-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
84 private UserSessionBeanRemote userBean;
87 * Login controller (bean)
90 private FinancialsUserLoginWebSessionController userLoginController;
95 public FinancialsUserActionWebRequestBean () {
96 // Call super constructor
101 public String doChangePersonalData () {
102 // This method shall only be called if the user is logged-in
103 if (!this.userLoginController.isUserLoggedIn()) {
105 throw new IllegalStateException("User is not logged-in"); //NOI18N
106 } else if (!this.contactController.isRequiredChangePersonalDataSet()) {
107 // Not all required fields are set
108 throw new FacesException("Not all required fields are set."); //NOI18N
109 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
110 // Password not matching
111 throw new FacesException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
112 } else if (!this.featureController.isFeatureEnabled("change_user_personal_data")) { //NOI18N
113 // Editing is not allowed
114 throw new IllegalStateException("User tried to edit personal data."); //NOI18N
118 final User user = this.userLoginController.getLoggedInUser();
120 // Fire pre-update event
121 this.preUpdatedPersonalDataEvent.fire(new PreUserPersonalDataUpdatedEvent(user));
123 // It should be there, so run some tests on it
124 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
125 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
126 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
127 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
128 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
129 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
131 // Send it to the EJB
132 final User updatedUser = this.userBean.updateUserPersonalData(user);
134 // Fire post-update event
135 this.postUpdatedPersonalDataEvent.fire(new PostUserPersonalDataUpdatedEvent(updatedUser));
138 return "user_contact_data_saved"; //NOI18N