2 * Copyright (C) 2016, 2017 Roland Häder
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.register;
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;
49 * A web bean for user registration
51 * @author Roland Häder<roland@mxchange.org>
53 @Named ("registerController")
55 public class JobsUserRegisterWebSessionBean extends BaseJobsController implements JobsUserRegisterWebSessionController {
60 private static final long serialVersionUID = 47_828_986_719_691_592L;
66 private JobsAdminUserWebRequestController adminUserController;
72 private JobsContactWebSessionController contactController;
78 private JobsFeaturesWebApplicationController featureController;
81 * Remote register session-scoped bean
83 private UserRegistrationSessionBeanRemote registerBean;
89 private JobsUserWebSessionController userController;
92 * An en event fireable when a new user has registered
96 private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
101 public JobsUserRegisterWebSessionBean () {
102 // Call super constructor
107 public String doFinishRegistration () {
108 // Is registration enabled?
109 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
111 throw new FaceletException("Registration is disabled."); //NOI18N
115 User user = this.userController.createUserInstance(true);
117 // Is the user already used?
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));
130 // May happen here, reset field
131 this.userController.setUserName(null);
132 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
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));
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
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
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
159 } else if (!this.userController.isSamePasswordEntered()) {
160 // Not same password entered
161 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
165 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
168 user.setUserEncryptedPassword(encryptedPassword);
170 // Is developer mode?
171 if (this.isDebugModeEnabled("register")) { //NOI18N
172 // For debugging/programming only:
173 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
175 // No debugging of this part
176 user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
178 // Ask EJB for generating a not-existing confirmation key
179 String confirmKey = this.registerBean.generateConfirmationKey(user);
182 user.setUserConfirmKey(confirmKey);
187 String baseUrl = FacesUtils.generateBaseUrl();
190 User registeredUser = this.registerBean.registerUser(user, baseUrl);
192 // The id number should be set
193 assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
196 this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
198 // All fine, redirect to proper page
199 return "user_register_done"; //NOI18N
200 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
202 throw new FaceletException(ex);
207 public String doRegisterMultiPage1 () {
208 // Is registration enabled?
209 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
211 throw new FaceletException("Registration is disabled."); //NOI18N
215 User user = this.userController.createUserInstance(false);
217 // First check if user is not null and user name is not used + if same email address is entered
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
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
234 // Create half contact instance with email address
235 Contact contact = new UserContact();
236 contact.setContactEmailAddress(this.contactController.getEmailAddress());
238 // Set contact in user
239 user.setUserContact(contact);
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
250 // Now only redirect to next page as the JSF does it
251 return "user_register_page2"; //NOI18N
255 * Post-construction method
258 public void init () {
260 // Get initial context
261 Context context = new InitialContext();
264 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/jjobs-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
265 } catch (final NamingException ex) {
267 throw new FaceletException(ex);