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.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;
86 * An en event fireable when a new user has registered
90 private Event<ObservableUserRegisteredEvent> registeredEvent;
96 private PizzaUserWebSessionController userController;
101 public PizzaUserRegisterWebSessionBean () {
105 public String doFinishRegistration () {
106 // Is registration enabled?
107 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
109 throw new FaceletException("Registration is disabled."); //NOI18N
113 User user = this.userController.createUserInstance(true);
115 // Is the user already used?
118 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
119 } else if (!this.userController.isRequiredPersonalDataSet()) {
120 // Not all required fields are set
121 throw new FaceletException("Not all required fields are set."); //NOI18N
122 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
123 // User name is already used
124 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
125 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
126 // Email address has already been taken
127 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
128 } else if (!this.contactController.isSameEmailAddressEntered()) {
129 // Not same email address entered
130 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
131 } else if (!this.userController.isSamePasswordEntered()) {
132 // Not same password entered
133 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
137 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
140 user.setUserEncryptedPassword(encryptedPassword);
142 // Is developer mode?
143 if (this.isDebugModeEnabled("register")) { //NOI18N
144 // For debugging/programming only:
145 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
147 // No debugging of this part
148 user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
150 // Ask EJB for generating a not-existing confirmation key
151 String confirmKey = this.registerBean.generateConfirmationKey(user);
154 user.setUserConfirmKey(confirmKey);
159 String baseUrl = FacesUtils.generateBaseUrl();
162 User registeredUser = this.registerBean.registerUser(user, baseUrl);
164 // The id number should be set
165 assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
168 this.registeredEvent.fire(new UserRegisteredEvent(registeredUser));
170 // All fine, redirect to proper page
171 return "register_done"; //NOI18N
172 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
174 throw new FaceletException(ex);
179 public String doRegisterMultiPage1 () {
180 // Is registration enabled?
181 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
183 throw new FaceletException("Registration is disabled."); //NOI18N
187 User user = this.userController.createUserInstance(false);
189 // First check if user is not null and user name is not used + if same email address is entered
192 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
193 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
194 // User name is already used
195 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
196 } else if (!this.contactController.isSameEmailAddressEntered()) {
197 // Not same email address entered
198 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
201 // Create half contact instance with email address
202 Contact contact = new UserContact();
203 contact.setContactEmailAddress(this.contactController.getEmailAddress());
205 // Set contact in user
206 user.setUserContact(contact);
208 // Check if email address is registered
209 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
210 // Email address has already been taken
211 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
214 // Now only redirect to next page as the JSF does it
215 return "user_register_page2"; //NOI18N
219 * Post-construction method
222 public void init () {
224 // Get initial context
225 Context context = new InitialContext();
228 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
229 } catch (final NamingException ex) {
231 throw new FaceletException(ex);