]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/user/action/FinancialsUserActionWebRequestBean.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / user / action / FinancialsUserActionWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jfinancials.beans.user.action;
18
19 import java.text.MessageFormat;
20 import javax.ejb.EJB;
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;
39
40 /**
41  * A user action bean (controller)
42  * <p>
43  * @author Roland Häder<roland@mxchange.org>
44  */
45 @Named ("userActionController")
46 @RequestScoped
47 public class FinancialsUserActionWebRequestBean extends BaseFinancialsBean implements FinancialsUserActionWebRequestController {
48
49         /**
50          * Serial number
51          */
52         private static final long serialVersionUID = 542_145_347_920L;
53
54         /**
55          * General contact controller
56          */
57         @Inject
58         private FinancialsContactWebRequestController contactController;
59
60         /**
61          * Features controller
62          */
63         @Inject
64         private FinancialsFeaturesWebApplicationController featureController;
65
66         /**
67          * Event being fired when user updated personal data
68          */
69         @Inject
70         @Any
71         private Event<ObservablePostUserPersonalDataUpdatedEvent> postUpdatedPersonalDataEvent;
72
73         /**
74          * Event being fired when user updated personal data
75          */
76         @Inject
77         @Any
78         private Event<ObservablePreUserPersonalDataUpdatedEvent> preUpdatedPersonalDataEvent;
79
80         /**
81          * Remote user bean
82          */
83         @EJB (lookup = "java:global/jfinancials-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
84         private UserSessionBeanRemote userBean;
85
86         /**
87          * Login controller (bean)
88          */
89         @Inject
90         private FinancialsUserLoginWebSessionController userLoginController;
91
92         /**
93          * Default constructor
94          */
95         public FinancialsUserActionWebRequestBean () {
96                 // Call super constructor
97                 super();
98         }
99
100         @Override
101         public String doChangePersonalData () {
102                 // This method shall only be called if the user is logged-in
103                 if (!this.userLoginController.isUserLoggedIn()) {
104                         // Not logged-in
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
115                 }
116
117                 // Get user instance
118                 final User user = this.userLoginController.getLoggedInUser();
119
120                 // Fire pre-update event
121                 this.preUpdatedPersonalDataEvent.fire(new PreUserPersonalDataUpdatedEvent(user));
122
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
130
131                 // Send it to the EJB
132                 final User updatedUser = this.userBean.updateUserPersonalData(user);
133
134                 // Fire post-update event
135                 this.postUpdatedPersonalDataEvent.fire(new PostUserPersonalDataUpdatedEvent(updatedUser));
136
137                 // All fine
138                 return "user_contact_data_saved"; //NOI18N
139         }
140
141 }