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 General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.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.addressbook.beans.user.UserWebSessionController;
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;
41 * A web bean for user registration
43 * @author Roland Haeder<roland@mxchange.org>
45 @Named ("registerController")
47 public class UserRegisterWebSessionBean implements UserRegisterWebSessionController {
52 private static final long serialVersionUID = 47_828_986_719_691_592L;
55 * Reemote register session bean
57 private UserRegistrationSessionBeanRemote registerBean;
60 * An en event fireable when a new user has registered
64 private Event<UserRegisteredEvent> registeredEvent;
70 private UserWebSessionController userController;
75 public UserRegisterWebSessionBean () {
77 // Get initial context
78 Context context = new InitialContext();
81 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("ejb/stateless-addressbook-register"); //NOI18N
82 } catch (final NamingException ex) {
84 throw new FaceletException(ex);
89 public String doRegister () {
91 User user = this.userController.createUserInstance();
93 // Is the user already used?
96 throw new NullPointerException("user is null"); //NOI18N
97 } else if (!this.userController.isRequiredPersonalDataSet()) {
98 // Not all required fields are set
99 throw new FaceletException("Not all required fields are set."); //NOI18N
100 } else if (this.userController.isUserNameRegistered(user)) {
101 // User name is already used
102 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
103 } else if (this.userController.isEmailAddressRegistered(user)) {
104 // Email address has already been taken
105 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
106 } else if (!this.userController.isSameEmailAddressEntered()) {
107 // Not same email address entered
108 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.userController.getEmailAddress(), this.userController.getEmailAddressRepeat()))); //NOI18N
109 } else if (!this.userController.isSamePasswordEntered()) {
110 // Not same password entered
111 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
115 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
118 user.setUserEncryptedPassword(encryptedPassword);
120 // For debugging/programming only:
121 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
125 User registeredUser = this.registerBean.registerUser(user);
128 this.registeredEvent.fire(new RegisteredUserEvent(registeredUser));
130 // All fine, redirect to proper page
131 return "register_done"; //NOI18N
132 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
134 throw new FaceletException(ex);