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.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.jusercore.events.registration.RegisteredUserEvent;
30 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
31 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
32 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
33 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
34 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
35 import org.mxchange.jusercore.model.user.User;
36 import org.mxchange.jusercore.model.user.UserUtils;
37 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
38 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
39 import org.mxchange.pizzaapplication.beans.user.PizzaAdminUserWebRequestController;
40 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
43 * A web bean for user registration
45 * @author Roland Haeder<roland@mxchange.org>
47 @Named ("registerController")
49 public class PizzaUserRegisterWebSessionBean implements PizzaUserRegisterWebSessionController {
54 private static final long serialVersionUID = 47_828_986_719_691_592L;
60 private PizzaAdminUserWebRequestController adminUserController;
66 private PizzaContactWebSessionController contactController;
69 * Reemote register session bean
71 private UserRegistrationSessionBeanRemote registerBean;
74 * An en event fireable when a new user has registered
78 private Event<UserRegisteredEvent> registeredEvent;
84 private PizzaUserWebSessionController userController;
89 public PizzaUserRegisterWebSessionBean () {
91 // Get initial context
92 Context context = new InitialContext();
95 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
96 } catch (final NamingException ex) {
98 throw new FaceletException(ex);
103 public String doRegister () {
105 User user = this.userController.createUserInstance();
107 // Is the user already used?
110 throw new NullPointerException("user is null"); //NOI18N
111 } else if (!this.userController.isRequiredPersonalDataSet()) {
112 // Not all required fields are set
113 throw new FaceletException("Not all required fields are set."); //NOI18N
114 } else if (this.userController.isUserNameRegistered(user)) {
115 // User name is already used
116 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
117 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
118 // Email address has already been taken
119 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
120 } else if (!this.contactController.isSameEmailAddressEntered()) {
121 // Not same email address entered
122 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
123 } else if (!this.userController.isSamePasswordEntered()) {
124 // Not same password entered
125 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
129 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
132 user.setUserEncryptedPassword(encryptedPassword);
134 // For debugging/programming only:
135 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
139 User registeredUser = this.registerBean.registerUser(user);
141 // The id number should be set
142 assert (registeredUser.getUserId() instanceof Long) : "registerUser.userId is null after registerUser() was called."; //NOI18N
145 this.registeredEvent.fire(new RegisteredUserEvent(registeredUser));
147 // All fine, redirect to proper page
148 return "register_done"; //NOI18N
149 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
151 throw new FaceletException(ex);