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.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.contact.AddressbookContactWebSessionController;
30 import org.mxchange.addressbook.beans.user.AddressbookAdminUserWebRequestController;
31 import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController;
32 import org.mxchange.jusercore.events.registration.RegisteredUserEvent;
33 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
34 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
35 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
36 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
37 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
38 import org.mxchange.jusercore.model.user.User;
39 import org.mxchange.jusercore.model.user.UserUtils;
40 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
43 * A web bean for user registration
45 * @author Roland Haeder<roland@mxchange.org>
47 @Named ("registerController")
49 public class AddressbookUserRegisterWebSessionBean implements AddressbookUserRegisterWebSessionController {
54 private static final long serialVersionUID = 47_828_986_719_691_592L;
57 * Administrative user controller
60 private AddressbookAdminUserWebRequestController adminUserController;
66 private AddressbookContactWebSessionController 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 AddressbookUserWebSessionController userController;
89 public AddressbookUserRegisterWebSessionBean () {
91 // Get initial context
92 Context context = new InitialContext();
95 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/addressbook-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);