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.jratecalc.beans.contact.PizzaContactWebSessionController;
30 import org.mxchange.jusercore.events.registration.RegisteredUserEvent;
31 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
32 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
33 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
34 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
35 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
36 import org.mxchange.jusercore.model.user.User;
37 import org.mxchange.jusercore.model.user.UserUtils;
38 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
39 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
42 * A web bean for user registration
44 * @author Roland Haeder<roland@mxchange.org>
46 @Named ("registerController")
48 public class PizzaUserRegisterWebSessionBean implements PizzaUserRegisterWebSessionController {
53 private static final long serialVersionUID = 47_828_986_719_691_592L;
59 private PizzaContactWebSessionController contactController;
62 * Reemote register session bean
64 private UserRegistrationSessionBeanRemote registerBean;
67 * An en event fireable when a new user has registered
71 private Event<UserRegisteredEvent> registeredEvent;
77 private PizzaUserWebSessionController userController;
82 public PizzaUserRegisterWebSessionBean () {
84 // Get initial context
85 Context context = new InitialContext();
88 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
89 } catch (final NamingException ex) {
91 throw new FaceletException(ex);
96 public String doRegister () {
98 User user = this.userController.createUserInstance();
100 // Is the user already used?
103 throw new NullPointerException("user is null"); //NOI18N
104 } else if (!this.userController.isRequiredPersonalDataSet()) {
105 // Not all required fields are set
106 throw new FaceletException("Not all required fields are set."); //NOI18N
107 } else if (this.userController.isUserNameRegistered(user)) {
108 // User name is already used
109 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
110 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
111 // Email address has already been taken
112 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
113 } else if (!this.contactController.isSameEmailAddressEntered()) {
114 // Not same email address entered
115 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
116 } else if (!this.userController.isSamePasswordEntered()) {
117 // Not same password entered
118 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
122 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
125 user.setUserEncryptedPassword(encryptedPassword);
127 // For debugging/programming only:
128 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
132 User registeredUser = this.registerBean.registerUser(user);
135 this.registeredEvent.fire(new RegisteredUserEvent(registeredUser));
137 // All fine, redirect to proper page
138 return "register_done"; //NOI18N
139 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
141 throw new FaceletException(ex);