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.pizzaapplication.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.jusercore.events.registration.RegisteredUserEvent;
33 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
34 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
35 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
36 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
37 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
38 import org.mxchange.jusercore.model.user.User;
39 import org.mxchange.jusercore.model.user.UserUtils;
40 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
41 import org.mxchange.pizzaapplication.beans.BasePizzaController;
42 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
43 import org.mxchange.pizzaapplication.beans.user.PizzaAdminUserWebRequestController;
44 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
47 * A web bean for user registration
49 * @author Roland Haeder<roland@mxchange.org>
51 @Named ("registerController")
53 public class PizzaUserRegisterWebSessionBean extends BasePizzaController implements PizzaUserRegisterWebSessionController {
58 private static final long serialVersionUID = 47_828_986_719_691_592L;
64 private PizzaAdminUserWebRequestController adminUserController;
70 private PizzaContactWebSessionController contactController;
73 * Reemote register session bean
75 private UserRegistrationSessionBeanRemote registerBean;
78 * An en event fireable when a new user has registered
82 private Event<UserRegisteredEvent> registeredEvent;
88 private PizzaUserWebSessionController userController;
93 public PizzaUserRegisterWebSessionBean () {
95 // Get initial context
96 Context context = new InitialContext();
99 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
100 } catch (final NamingException ex) {
102 throw new FaceletException(ex);
107 public String doFinishRegistration () {
109 User user = this.userController.createUserInstance();
111 // Is the user already used?
114 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
115 } else if (!this.userController.isRequiredPersonalDataSet()) {
116 // Not all required fields are set
117 throw new FaceletException("Not all required fields are set."); //NOI18N
118 } else if ((this.userController.isUserNameRequired()) && (this.userController.isUserNameRegistered(user))) {
119 // User name is already used
120 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
121 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
122 // Email address has already been taken
123 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
124 } else if (!this.contactController.isSameEmailAddressEntered()) {
125 // Not same email address entered
126 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
127 } else if (!this.userController.isSamePasswordEntered()) {
128 // Not same password entered
129 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
133 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
136 user.setUserEncryptedPassword(encryptedPassword);
138 // Is developer mode?
139 if (this.isDebugModeEnabled("register")) { //NOI18N
140 // For debugging/programming only:
141 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
143 // No debugging of this part
144 user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
146 // Ask EJB for generating a not-existing confirmation key
147 String confirmKey = this.registerBean.generateConfirmationKey(user);
150 user.setUserConfirmKey(confirmKey);
155 User registeredUser = this.registerBean.registerUser(user);
157 // The id number should be set
158 assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
161 this.registeredEvent.fire(new RegisteredUserEvent(registeredUser));
163 // All fine, redirect to proper page
164 return "register_done"; //NOI18N
165 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
167 throw new FaceletException(ex);
172 public String doRegisterMultiPage1 () {
174 User user = this.userController.createUserInstance();
176 // First check if user is not null and user name is not used + if same email address is entered
179 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
180 } else if ((this.userController.isUserNameRequired()) && (this.userController.isUserNameRegistered(user))) {
181 // User name is already used
182 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
183 } else if (!this.contactController.isSameEmailAddressEntered()) {
184 // Not same email address entered
185 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
188 // Create half contact instance with email address
189 Contact contact = new UserContact();
190 contact.setContactEmailAddress(this.contactController.getEmailAddress());
192 // Set contact in user
193 user.setUserContact(contact);
195 // Check if email address is registered
196 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
197 // Email address has already been taken
198 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
201 // Now only redirect to next page as the JSF does it
202 return "register_page2"; //NOI18N
206 public boolean isMultiplePageEnabled () {
207 // Get context parameter
208 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_multi_register_page"); //NOI18N
211 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
218 public boolean isResendConfirmationLinkEnabled () {
219 // Get context parameter
220 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_resend_confirm_link_enabled"); //NOI18N
223 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N