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.register;
19 import java.text.MessageFormat;
21 import javax.ejb.EJBException;
22 import javax.ejb.Stateless;
23 import javax.mail.Address;
24 import javax.mail.internet.AddressException;
25 import javax.mail.internet.InternetAddress;
26 import javax.persistence.NoResultException;
27 import javax.persistence.Query;
28 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
29 import org.mxchange.jcontacts.contact.Contact;
30 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
31 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
32 import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
33 import org.mxchange.jusercore.model.user.LoginUser;
34 import org.mxchange.jusercore.model.user.User;
35 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
36 import org.mxchange.jusercore.model.user.UserUtils;
39 * A session-scoped bean for user registration
41 * @author Roland Häder<roland@mxchange.org>
43 @Stateless (name = "register", description = "A bean handling the user registration")
44 public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatabaseBean implements UserRegistrationSessionBeanRemote {
49 private static final long serialVersionUID = 12_348_958_986_818_627L;
52 * Administrative user bean
55 private AdminUserSessionBeanRemote adminUserBean;
61 private UserSessionBeanRemote userBean;
66 public AddressbookUserRegistrationSessionBean () {
67 // Call super constructor
72 public String generateConfirmationKey (final User user) {
74 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
76 // user should not be null
79 throw new NullPointerException("user is null"); //NOI18N
82 // Create named instance
83 Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
85 // Init confirmation key
86 String confirmationKey = null;
89 while (confirmationKey == null) {
91 String key = UserUtils.generatedConfirmationKey(user);
93 // Set key as parameter
94 query.setParameter("confirmKey", key); //NOI18N
98 // Get contact instance
99 Contact contact = (Contact) query.getSingleResult();
102 this.getLoggerBeanLocal().logWarning(MessageFormat.format("{0}.generateConfirmationKey: key {1} already found: contact.contactId={2}", this.getClass().getSimpleName(), key, contact.getContactId())); //NOI18N
103 } catch (final NoResultException ex) {
104 // Not found, normal case
105 confirmationKey = key;
111 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), confirmationKey)); //NOI18N
114 return confirmationKey;
118 public boolean isEmailAddressRegistered (final User user) {
120 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
123 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
125 // user should not be null
128 throw new NullPointerException("user is null"); //NOI18N
132 return this.userBean.isEmailAddressRegistered(user);
136 public boolean isUserNameRegistered (final User user) {
138 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
141 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
143 // user should not be null
146 throw new NullPointerException("user is null"); //NOI18N
150 return this.userBean.isUserNameRegistered(user);
154 public User registerUser (final User user, final String baseUrl) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
156 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
158 // user should not be null
161 throw new NullPointerException("user is null"); //NOI18N
162 } else if (user.getUserContact() == null) {
164 throw new NullPointerException("user.userContact is null"); //NOI18N
165 } else if (user.getUserContact().getContactEmailAddress() == null) {
167 throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
168 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
170 throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
173 // Check if user is registered
174 if (this.isUserNameRegistered(user)) {
176 throw new UserNameAlreadyRegisteredException(user);
177 } else if (this.isEmailAddressRegistered(user)) {
179 throw new EmailAddressAlreadyRegisteredException(user);
183 User addedUser = this.adminUserBean.addUser(user);
186 Address emailAddress;
189 // Create email address and set
190 emailAddress = new InternetAddress(addedUser.getUserContact().getContactEmailAddress());
191 } catch (final AddressException ex) {
193 throw new EJBException(ex);
197 // @TODO: Internationlize the subject line somehow
198 this.sendEmail("Registration", "registration", emailAddress, addedUser, baseUrl); //NOI18N
201 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N