2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.juserlogincore.model.user.register;
19 import java.text.MessageFormat;
20 import java.util.Objects;
22 import javax.ejb.Stateless;
23 import javax.persistence.NoResultException;
24 import javax.persistence.Query;
25 import org.mxchange.addressbook.enterprise.BaseAddressbookEnterpriseBean;
26 import org.mxchange.jcontacts.model.contact.Contact;
27 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
28 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
29 import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
30 import org.mxchange.jusercore.model.user.LoginUser;
31 import org.mxchange.jusercore.model.user.User;
32 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
33 import org.mxchange.juserlogincore.login.UserLoginUtils;
36 * A session-scoped bean for user registration
38 * @author Roland Häder<roland@mxchange.org>
40 @Stateless (name = "userRegistration", description = "A bean handling the user registration")
41 public class AddressbookUserRegistrationSessionBean extends BaseAddressbookEnterpriseBean implements UserRegistrationSessionBeanRemote {
46 private static final long serialVersionUID = 12_348_958_986_818_627L;
49 * Administrative user bean
52 private AdminUserSessionBeanRemote adminUserBean;
58 private UserSessionBeanRemote userBean;
63 public AddressbookUserRegistrationSessionBean () {
64 // Call super constructor
65 super("jms/addressbook-queue-factory", "jms/addressbook-email-queue"); //NOI18N
69 public String generateConfirmationKey (final User user) {
71 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
73 // user should not be null
76 throw new NullPointerException("user is null"); //NOI18N
79 // Create named instance
80 final Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
82 // Init confirmation key
83 String confirmationKey = null;
86 while (confirmationKey == null) {
88 final String key = UserLoginUtils.generatedConfirmationKey(user);
90 // Set key as parameter
91 query.setParameter("confirmKey", key); //NOI18N
95 // Get contact instance
96 final Contact contact = (Contact) query.getSingleResult();
99 this.getLoggerBeanLocal().logWarning(MessageFormat.format("{0}.generateConfirmationKey: key {1} already found: contact.contactId={2}", this.getClass().getSimpleName(), key, contact.getContactId())); //NOI18N
100 } catch (final NoResultException ex) {
101 // Not found, normal case
102 confirmationKey = key;
108 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), confirmationKey)); //NOI18N
111 return confirmationKey;
115 public boolean isEmailAddressRegistered (final User user) {
117 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
120 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
122 // user should not be null
125 throw new NullPointerException("user is null"); //NOI18N
129 return this.userBean.isEmailAddressRegistered(user);
133 public boolean isUserNameRegistered (final User user) {
135 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
138 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
140 // user should not be null
143 throw new NullPointerException("user is null"); //NOI18N
147 return this.userBean.isUserNameRegistered(user);
151 public User registerUser (final User user, final String baseUrl, final String randomPassword) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
153 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2},randomPassword[]={3} - CALLED!", this.getClass().getSimpleName(), user, baseUrl, Objects.toString(randomPassword))); //NOI18N
155 // user should not be null
158 throw new NullPointerException("user is null"); //NOI18N
159 } else if (user.getUserContact() == null) {
161 throw new NullPointerException("user.userContact is null"); //NOI18N
162 } else if (user.getUserContact().getContactEmailAddress() == null) {
164 throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
165 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
167 throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
170 // Check if user is registered
171 if (this.isUserNameRegistered(user)) {
173 throw new UserNameAlreadyRegisteredException(user);
174 } else if (this.isEmailAddressRegistered(user)) {
176 throw new EmailAddressAlreadyRegisteredException(user);
180 final User addedUser = this.adminUserBean.addUser(user);
182 // Default template is with no random password
183 String templateName = "user_registration"; //NOI18N
186 if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
187 // Switch to other template
188 templateName = "user_registration_random"; //NOI18N
192 // @TODO: Internationlize the subject line somehow
193 this.sendEmail("Registration", templateName, addedUser, baseUrl, randomPassword); //NOI18N
196 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N