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.pizzaapplication.beans.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.jusercore.events.registration.ObservableUserRegisteredEvent;
34 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
35 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
36 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
37 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
38 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
39 import org.mxchange.jusercore.model.user.User;
40 import org.mxchange.jusercore.model.user.UserUtils;
41 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
42 import org.mxchange.pizzaapplication.beans.BasePizzaController;
43 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
44 import org.mxchange.pizzaapplication.beans.features.PizzaFeaturesWebApplicationController;
45 import org.mxchange.pizzaapplication.beans.user.PizzaAdminUserWebRequestController;
46 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
49 * A web bean for user registration
51 * @author Roland Häder<roland@mxchange.org>
53 @Named ("registerController")
55 public class PizzaUserRegisterWebSessionBean extends BasePizzaController implements PizzaUserRegisterWebSessionController {
60 private static final long serialVersionUID = 47_828_986_719_691_592L;
66 private PizzaAdminUserWebRequestController adminUserController;
72 private PizzaContactWebSessionController contactController;
78 private PizzaFeaturesWebApplicationController featureController;
81 * Remote register session-scoped bean
83 private UserRegistrationSessionBeanRemote registerBean;
89 private PizzaUserWebSessionController userController;
92 * An en event fireable when a new user has registered
96 private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
101 public PizzaUserRegisterWebSessionBean () {
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 // User name is already used
126 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
127 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
128 // Email address has already been taken
129 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
130 } else if (!this.contactController.isSameEmailAddressEntered()) {
131 // Not same email address entered
132 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
133 } else if (!this.userController.isSamePasswordEntered()) {
134 // Not same password entered
135 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
139 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
142 user.setUserEncryptedPassword(encryptedPassword);
144 // Is developer mode?
145 if (this.isDebugModeEnabled("register")) { //NOI18N
146 // For debugging/programming only:
147 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
149 // No debugging of this part
150 user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
152 // Ask EJB for generating a not-existing confirmation key
153 String confirmKey = this.registerBean.generateConfirmationKey(user);
156 user.setUserConfirmKey(confirmKey);
161 String baseUrl = FacesUtils.generateBaseUrl();
164 User registeredUser = this.registerBean.registerUser(user, baseUrl);
166 // The id number should be set
167 assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
170 this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
172 // All fine, redirect to proper page
173 return "register_done"; //NOI18N
174 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
176 throw new FaceletException(ex);
181 public String doRegisterMultiPage1 () {
182 // Is registration enabled?
183 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
185 throw new FaceletException("Registration is disabled."); //NOI18N
189 User user = this.userController.createUserInstance(false);
191 // First check if user is not null and user name is not used + if same email address is entered
194 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
195 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
196 // User name is already used
197 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
198 } else if (!this.contactController.isSameEmailAddressEntered()) {
199 // Not same email address entered
200 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
203 // Create half contact instance with email address
204 Contact contact = new UserContact();
205 contact.setContactEmailAddress(this.contactController.getEmailAddress());
207 // Set contact in user
208 user.setUserContact(contact);
210 // Check if email address is registered
211 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
212 // Email address has already been taken
213 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
216 // Now only redirect to next page as the JSF does it
217 return "user_register_page2"; //NOI18N
221 * Post-construction method
224 public void init () {
226 // Get initial context
227 Context context = new InitialContext();
230 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
231 } catch (final NamingException ex) {
233 throw new FaceletException(ex);