]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/register/JobsUserRegisterWebSessionBean.java
Don't cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / register / JobsUserRegisterWebSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.jjobs.beans.user.register;
18
19 import java.text.MessageFormat;
20 import javax.annotation.PostConstruct;
21 import javax.enterprise.context.SessionScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jcontacts.contact.Contact;
31 import org.mxchange.jcontacts.contact.UserContact;
32 import org.mxchange.jcoreee.utils.FacesUtils;
33 import org.mxchange.jjobs.beans.BaseJobsController;
34 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
35 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
36 import org.mxchange.jjobs.beans.user.JobsAdminUserWebRequestController;
37 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
38 import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent;
39 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
40 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
41 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
42 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
43 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
44 import org.mxchange.jusercore.model.user.User;
45 import org.mxchange.jusercore.model.user.UserUtils;
46 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
47
48 /**
49  * A web bean for user registration
50  * <p>
51  * @author Roland Häder<roland@mxchange.org>
52  */
53 @Named ("registerController")
54 @SessionScoped
55 public class JobsUserRegisterWebSessionBean extends BaseJobsController implements JobsUserRegisterWebSessionController {
56
57         /**
58          * Serial number
59          */
60         private static final long serialVersionUID = 47_828_986_719_691_592L;
61
62         /**
63          * User controller
64          */
65         @Inject
66         private JobsAdminUserWebRequestController adminUserController;
67
68         /**
69          * Contact controller
70          */
71         @Inject
72         private JobsContactWebSessionController contactController;
73
74         /**
75          * Features controller
76          */
77         @Inject
78         private JobsFeaturesWebApplicationController featureController;
79
80         /**
81          * Remote register session-scoped bean
82          */
83         private UserRegistrationSessionBeanRemote registerBean;
84
85         /**
86          * User controller
87          */
88         @Inject
89         private JobsUserWebSessionController userController;
90
91         /**
92          * An en event fireable when a new user has registered
93          */
94         @Inject
95         @Any
96         private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
97
98         /**
99          * Default constructor
100          */
101         public JobsUserRegisterWebSessionBean () {
102                 // Call super constructor
103                 super();
104         }
105
106         @Override
107         public String doFinishRegistration () {
108                 // Is registration enabled?
109                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
110                         // Is not enabled
111                         throw new FaceletException("Registration is disabled."); //NOI18N
112                 }
113
114                 // Get user instance
115                 User user = this.userController.createUserInstance(true);
116
117                 // Is the user already used?
118                 if (null == user) {
119                         // user must be set
120                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
121                 } else if (!this.userController.isRequiredPersonalDataSet()) {
122                         // Not all required fields are set
123                         throw new FaceletException("Not all required fields are set."); //NOI18N
124                 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
125                         // Is multi-page enabled?
126                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
127                                 // User name is already used, should not happen here
128                                 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
129                         } else {
130                                 // May happen here, reset field
131                                 this.userController.setUserName(null);
132                                 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
133                                 return ""; //NOI18N
134                         }
135                 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
136                         // Is multi-page enabled?
137                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
138                                 // Email address has already been taken, should not happen here
139                                 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
140                         } else {
141                                 // May happen here, reset fields
142                                 this.contactController.setEmailAddress(null);
143                                 this.contactController.setEmailAddressRepeat(null);
144                                 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
145                                 return ""; //NOI18N
146                         }
147                 } else if (!this.contactController.isSameEmailAddressEntered()) {
148                         // Is multi-page enabled?
149                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
150                                 // Not same email address entered, should not happen here
151                                 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
152                         } else {
153                                 // May happen here, reset fields
154                                 this.contactController.setEmailAddress(null);
155                                 this.contactController.setEmailAddressRepeat(null);
156                                 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
157                                 return ""; //NOI18N
158                         }
159                 } else if (!this.userController.isSamePasswordEntered()) {
160                         // Not same password entered
161                         throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
162                 }
163
164                 // Encrypt password
165                 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
166
167                 // Set it here
168                 user.setUserEncryptedPassword(encryptedPassword);
169
170                 // Is developer mode?
171                 if (this.isDebugModeEnabled("register")) { //NOI18N
172                         // For debugging/programming only:
173                         user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
174                 } else {
175                         // No debugging of this part
176                         user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
177
178                         // Ask EJB for generating a not-existing confirmation key
179                         String confirmKey = this.registerBean.generateConfirmationKey(user);
180
181                         // Set it in user
182                         user.setUserConfirmKey(confirmKey);
183                 }
184
185                 try {
186                         // Get base URL
187                         String baseUrl = FacesUtils.generateBaseUrl();
188
189                         // Call bean
190                         User registeredUser = this.registerBean.registerUser(user, baseUrl);
191
192                         // The id number should be set
193                         assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
194
195                         // Fire event
196                         this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
197
198                         // All fine, redirect to proper page
199                         return "user_register_done"; //NOI18N
200                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
201                         // Continue to throw
202                         throw new FaceletException(ex);
203                 }
204         }
205
206         @Override
207         public String doRegisterMultiPage1 () {
208                 // Is registration enabled?
209                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
210                         // Is not enabled
211                         throw new FaceletException("Registration is disabled."); //NOI18N
212                 }
213
214                 // Get user instance
215                 User user = this.userController.createUserInstance(false);
216
217                 // First check if user is not null and user name is not used + if same email address is entered
218                 if (null == user) {
219                         // user must be set
220                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
221                 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
222                         // User name is already used, so clear it
223                         this.userController.setUserName(null);
224                         this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
225                         return ""; //NOI18N
226                 } else if (!this.contactController.isSameEmailAddressEntered()) {
227                         // Not same email address entered, clear both
228                         this.contactController.setEmailAddress(null);
229                         this.contactController.setEmailAddressRepeat(null);
230                         this.showFacesMessage("form_register_page1:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
231                         return ""; //NOI18N
232                 }
233
234                 // Create half contact instance with email address
235                 Contact contact = new UserContact();
236                 contact.setContactEmailAddress(this.contactController.getEmailAddress());
237
238                 // Set contact in user
239                 user.setUserContact(contact);
240
241                 // Check if email address is registered
242                 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
243                         // Email address has already been taken, clear both
244                         this.contactController.setEmailAddress(null);
245                         this.contactController.setEmailAddressRepeat(null);
246                         this.showFacesMessage("form_register_page1:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
247                         return ""; //NOI18N
248                 }
249
250                 // Now only redirect to next page as the JSF does it
251                 return "user_register_page2"; //NOI18N
252         }
253
254         /**
255          * Post-construction method
256          */
257         @PostConstruct
258         public void init () {
259                 try {
260                         // Get initial context
261                         Context context = new InitialContext();
262
263                         // Try to lookup
264                         this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/jjobs-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
265                 } catch (final NamingException ex) {
266                         // Continue to throw
267                         throw new FaceletException(ex);
268                 }
269         }
270
271 }