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.jjobs.beans.register;
19 import java.text.MessageFormat;
20 import javax.enterprise.context.SessionScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.context.FacesContext;
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.user.JobsAdminUserWebRequestController;
36 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
37 import org.mxchange.jusercore.events.registration.RegisteredUserEvent;
38 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
39 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
40 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
41 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
42 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
43 import org.mxchange.jusercore.model.user.User;
44 import org.mxchange.jusercore.model.user.UserUtils;
45 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
48 * A web bean for user registration
50 * @author Roland Haeder<roland@mxchange.org>
52 @Named ("registerController")
54 public class JobsUserRegisterWebSessionBean extends BaseJobsController implements JobsUserRegisterWebSessionController {
59 private static final long serialVersionUID = 47_828_986_719_691_592L;
65 private JobsAdminUserWebRequestController adminUserController;
71 private JobsContactWebSessionController contactController;
74 * Reemote register session bean
76 private UserRegistrationSessionBeanRemote registerBean;
79 * An en event fireable when a new user has registered
83 private Event<UserRegisteredEvent> registeredEvent;
89 private JobsUserWebSessionController userController;
94 public JobsUserRegisterWebSessionBean () {
96 // Get initial context
97 Context context = new InitialContext();
100 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/jjobs-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
101 } catch (final NamingException ex) {
103 throw new FaceletException(ex);
108 public String doFinishRegistration () {
110 User user = this.userController.createUserInstance();
112 // Is the user already used?
115 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
116 } else if (!this.userController.isRequiredPersonalDataSet()) {
117 // Not all required fields are set
118 throw new FaceletException("Not all required fields are set."); //NOI18N
119 } else if ((this.userController.isUserNameRequired()) && (this.userController.isUserNameRegistered(user))) {
120 // User name is already used
121 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
122 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
123 // Email address has already been taken
124 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
125 } else if (!this.contactController.isSameEmailAddressEntered()) {
126 // Not same email address entered
127 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
128 } else if (!this.userController.isSamePasswordEntered()) {
129 // Not same password entered
130 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
134 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
137 user.setUserEncryptedPassword(encryptedPassword);
139 // Is developer mode?
140 if (this.isDebugModeEnabled("register")) { //NOI18N
141 // For debugging/programming only:
142 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
144 // No debugging of this part
145 user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
147 // Ask EJB for generating a not-existing confirmation key
148 String confirmKey = this.registerBean.generateConfirmationKey(user);
151 user.setUserConfirmKey(confirmKey);
156 String baseUrl = FacesUtils.generateBaseUrl();
159 User registeredUser = this.registerBean.registerUser(user, baseUrl);
161 // The id number should be set
162 assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
165 this.registeredEvent.fire(new RegisteredUserEvent(registeredUser));
167 // All fine, redirect to proper page
168 return "register_done"; //NOI18N
169 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
171 throw new FaceletException(ex);
176 public String doRegisterMultiPage1 () {
178 User user = this.userController.createUserInstance();
180 // First check if user is not null and user name is not used + if same email address is entered
183 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
184 } else if ((this.userController.isUserNameRequired()) && (this.userController.isUserNameRegistered(user))) {
185 // User name is already used
186 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
187 } else if (!this.contactController.isSameEmailAddressEntered()) {
188 // Not same email address entered
189 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
192 // Create half contact instance with email address
193 Contact contact = new UserContact();
194 contact.setContactEmailAddress(this.contactController.getEmailAddress());
196 // Set contact in user
197 user.setUserContact(contact);
199 // Check if email address is registered
200 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
201 // Email address has already been taken
202 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
205 // Now only redirect to next page as the JSF does it
206 return "register_page2"; //NOI18N
210 public boolean isMultiplePageEnabled () {
211 // Get context parameter
212 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_multi_register_page"); //NOI18N
215 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
222 public boolean isResendConfirmationLinkEnabled () {
223 // Get context parameter
224 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_resend_confirm_link_enabled"); //NOI18N
227 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N