2 * Copyright (C) 2016 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.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.view.facelets.FaceletException;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jcontacts.contact.Contact;
30 import org.mxchange.jcontacts.contact.UserContact;
31 import org.mxchange.jcoreee.utils.FacesUtils;
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.features.PizzaFeaturesWebApplicationController;
44 import org.mxchange.pizzaapplication.beans.user.PizzaAdminUserWebRequestController;
45 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
48 * A web bean for user registration
50 * @author Roland Häder<roland@mxchange.org>
52 @Named ("registerController")
54 public class PizzaUserRegisterWebSessionBean extends BasePizzaController implements PizzaUserRegisterWebSessionController {
59 private static final long serialVersionUID = 47_828_986_719_691_592L;
65 private PizzaAdminUserWebRequestController adminUserController;
71 private PizzaContactWebSessionController contactController;
77 private PizzaFeaturesWebApplicationController featureController;
80 * Reemote register session bean
82 private UserRegistrationSessionBeanRemote registerBean;
85 * An en event fireable when a new user has registered
89 private Event<UserRegisteredEvent> registeredEvent;
95 private PizzaUserWebSessionController userController;
100 public PizzaUserRegisterWebSessionBean () {
102 // Get initial context
103 Context context = new InitialContext();
106 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
107 } catch (final NamingException ex) {
109 throw new FaceletException(ex);
114 public String doFinishRegistration () {
115 // Is registration enabled?
116 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
118 throw new FaceletException("Registration is disabled."); //NOI18N
122 User user = this.userController.createUserInstance(true);
124 // Is the user already used?
127 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
128 } else if (!this.userController.isRequiredPersonalDataSet()) {
129 // Not all required fields are set
130 throw new FaceletException("Not all required fields are set."); //NOI18N
131 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
132 // User name is already used
133 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
134 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
135 // Email address has already been taken
136 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
137 } else if (!this.contactController.isSameEmailAddressEntered()) {
138 // Not same email address entered
139 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
140 } else if (!this.userController.isSamePasswordEntered()) {
141 // Not same password entered
142 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
146 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
149 user.setUserEncryptedPassword(encryptedPassword);
151 // Is developer mode?
152 if (this.isDebugModeEnabled("register")) { //NOI18N
153 // For debugging/programming only:
154 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
156 // No debugging of this part
157 user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
159 // Ask EJB for generating a not-existing confirmation key
160 String confirmKey = this.registerBean.generateConfirmationKey(user);
163 user.setUserConfirmKey(confirmKey);
168 String baseUrl = FacesUtils.generateBaseUrl();
171 User registeredUser = this.registerBean.registerUser(user, baseUrl);
173 // The id number should be set
174 assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
177 this.registeredEvent.fire(new RegisteredUserEvent(registeredUser));
179 // All fine, redirect to proper page
180 return "register_done"; //NOI18N
181 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
183 throw new FaceletException(ex);
188 public String doRegisterMultiPage1 () {
189 // Is registration enabled?
190 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
192 throw new FaceletException("Registration is disabled."); //NOI18N
196 User user = this.userController.createUserInstance(false);
198 // First check if user is not null and user name is not used + if same email address is entered
201 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
202 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
203 // User name is already used
204 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
205 } else if (!this.contactController.isSameEmailAddressEntered()) {
206 // Not same email address entered
207 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
210 // Create half contact instance with email address
211 Contact contact = new UserContact();
212 contact.setContactEmailAddress(this.contactController.getEmailAddress());
214 // Set contact in user
215 user.setUserContact(contact);
217 // Check if email address is registered
218 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
219 // Email address has already been taken
220 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
223 // Now only redirect to next page as the JSF does it
224 return "register_page2"; //NOI18N