2 * Copyright (C) 2016, 2017 Roland Häder
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.jusercore.model.user.register;
19 import java.text.MessageFormat;
20 import java.util.Objects;
22 import javax.ejb.EJBException;
23 import javax.ejb.Stateless;
24 import javax.mail.Address;
25 import javax.mail.internet.AddressException;
26 import javax.mail.internet.InternetAddress;
27 import javax.persistence.NoResultException;
28 import javax.persistence.Query;
29 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
30 import org.mxchange.jcontacts.contact.Contact;
31 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
32 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
33 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
34 import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
35 import org.mxchange.jusercore.model.user.LoginUser;
36 import org.mxchange.jusercore.model.user.User;
37 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
38 import org.mxchange.jusercore.model.user.UserUtils;
41 * A session-scoped bean for user registration
43 * @author Roland Häder<roland@mxchange.org>
45 @Stateless (name = "register", description = "A bean handling the user registration")
46 public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatabaseBean implements UserRegistrationSessionBeanRemote {
51 private static final long serialVersionUID = 12_348_958_986_818_627L;
54 * Administrative user bean
57 private AdminUserSessionBeanRemote adminUserBean;
63 private UserSessionBeanRemote userBean;
68 public AddressbookUserRegistrationSessionBean () {
69 // Call super constructor
74 public String generateConfirmationKey (final User user) {
76 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
78 // user should not be null
81 throw new NullPointerException("user is null"); //NOI18N
84 // Create named instance
85 Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
87 // Init confirmation key
88 String confirmationKey = null;
91 while (confirmationKey == null) {
93 String key = UserUtils.generatedConfirmationKey(user);
95 // Set key as parameter
96 query.setParameter("confirmKey", key); //NOI18N
100 // Get contact instance
101 Contact contact = (Contact) query.getSingleResult();
104 this.getLoggerBeanLocal().logWarning(MessageFormat.format("{0}.generateConfirmationKey: key {1} already found: contact.contactId={2}", this.getClass().getSimpleName(), key, contact.getContactId())); //NOI18N
105 } catch (final NoResultException ex) {
106 // Not found, normal case
107 confirmationKey = key;
113 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), confirmationKey)); //NOI18N
116 return confirmationKey;
120 public boolean isEmailAddressRegistered (final User user) {
122 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
125 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
127 // user should not be null
130 throw new NullPointerException("user is null"); //NOI18N
134 return this.userBean.isEmailAddressRegistered(user);
138 public boolean isUserNameRegistered (final User user) {
140 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
143 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
145 // user should not be null
148 throw new NullPointerException("user is null"); //NOI18N
152 return this.userBean.isUserNameRegistered(user);
156 public User registerUser (final User user, final String baseUrl, final String randomPassword) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
158 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2},randomPassword[]={3} - CALLED!", this.getClass().getSimpleName(), user, baseUrl, Objects.toString(randomPassword))); //NOI18N
160 // user should not be null
163 throw new NullPointerException("user is null"); //NOI18N
164 } else if (user.getUserContact() == null) {
166 throw new NullPointerException("user.userContact is null"); //NOI18N
167 } else if (user.getUserContact().getContactEmailAddress() == null) {
169 throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
170 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
172 throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
175 // Check if user is registered
176 if (this.isUserNameRegistered(user)) {
178 throw new UserNameAlreadyRegisteredException(user);
179 } else if (this.isEmailAddressRegistered(user)) {
181 throw new EmailAddressAlreadyRegisteredException(user);
185 User addedUser = this.adminUserBean.addUser(user);
188 Address emailAddress;
191 // Create email address and set
192 emailAddress = new InternetAddress(addedUser.getUserContact().getContactEmailAddress());
193 } catch (final AddressException ex) {
195 throw new EJBException(ex);
198 // Default template is with no random password
199 String templateName = "user_registration"; //NOI18N
202 if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
203 // Switch to other template
204 templateName = "user_registration_random"; //NOI18N
208 // @TODO: Internationlize the subject line somehow
209 this.sendEmail("Registration", templateName, emailAddress, addedUser, baseUrl, randomPassword); //NOI18N
212 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N