2 * Copyright (C) 2016 - 2022 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
51 @EJB (lookup = "java:global/addressbook-ejb/adminUser!org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote")
52 private AdminUserSessionBeanRemote adminUserBean;
57 @EJB (lookup = "java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
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 User registerUser (final User user, final String baseUrl, final String randomPassword) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
117 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2},randomPassword[]={3} - CALLED!", this.getClass().getSimpleName(), user, baseUrl, Objects.toString(randomPassword))); //NOI18N
119 // user should not be null
122 throw new NullPointerException("user is null"); //NOI18N
123 } else if (user.getUserContact() == null) {
125 throw new NullPointerException("user.userContact is null"); //NOI18N
126 } else if (user.getUserContact().getContactEmailAddress() == null) {
128 throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
129 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
131 throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
132 } else if (null == baseUrl) {
134 throw new NullPointerException("baseUrl is null"); //NOI18N
135 } else if (baseUrl.isEmpty()) {
137 throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
140 // Check if user is registered
141 if (this.userBean.isUserNameRegistered(user)) {
143 throw new UserNameAlreadyRegisteredException(user);
144 } else if (this.userBean.isEmailAddressRegistered(user)) {
146 throw new EmailAddressAlreadyRegisteredException(user);
150 final User addedUser = this.adminUserBean.addUser(user);
152 // Default template is with no random password
153 String templateName = "user_registration"; //NOI18N
156 if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
157 // Switch to other template
158 templateName = "user_registration_random"; //NOI18N
162 // @TODO: Internationlize the subject line somehow
163 this.sendEmail("Registration", templateName, addedUser, baseUrl, randomPassword); //NOI18N
166 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N