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.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.jcontacts.contact.Contact;
29 import org.mxchange.jcoreee.database.BaseDatabaseBean;
30 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
31 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
32 import org.mxchange.jusercore.model.user.LoginUser;
33 import org.mxchange.jusercore.model.user.User;
34 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
35 import org.mxchange.jusercore.model.user.UserUtils;
38 * A session bean for user registration
40 * @author Roland Haeder<roland@mxchange.org>
42 @Stateless (name = "register", description = "A bean handling the user registration")
43 public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean implements UserRegistrationSessionBeanRemote {
48 private static final long serialVersionUID = 12_348_958_986_818_627L;
54 private UserSessionBeanRemote userBean;
57 public String generateConfirmationKey (final User user) {
59 this.getLoggerBeanLocal().logTrace(MessageFormat.format("generateConfirmationKey: user={0} - CALLED!", user)); //NOI18N
61 // user should not be null
64 throw new NullPointerException("user is null"); //NOI18N
67 // Create named instance
68 Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
70 // Init confirmation key
71 String confirmationKey = null;
74 while (confirmationKey == null) {
76 String key = UserUtils.generatedConfirmationKey(user);
78 // Set key as parameter
79 query.setParameter("confirmKey", key); //NOI18N
83 // Get contact instance
84 Contact contact = (Contact) query.getSingleResult();
87 this.getLoggerBeanLocal().logWarning(MessageFormat.format("generateConfirmationKey: key {0} already found: contact.contactId={1}", key, contact.getContactId())); //NOI18N
88 } catch (final NoResultException ex) {
89 // Not found, normal case
90 confirmationKey = key;
96 this.getLoggerBeanLocal().logTrace(MessageFormat.format("generateConfirmationKey: confirmationKey={0} - EXIT!", confirmationKey)); //NOI18N
99 return confirmationKey;
103 public boolean isEmailAddressRegistered (final User user) {
105 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
108 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
110 // user should not be null
113 throw new NullPointerException("user is null"); //NOI18N
117 return this.userBean.isEmailAddressRegistered(user);
121 public boolean isUserNameRegistered (final User user) {
123 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
126 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
128 // user should not be null
131 throw new NullPointerException("user is null"); //NOI18N
135 return this.userBean.isUserNameRegistered(user);
139 public User registerUser (final User user, final String baseUrl) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
141 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: user={0},baseUrl={1} - CALLED!", user, baseUrl)); //NOI18N
143 // user should not be null
146 throw new NullPointerException("user is null"); //NOI18N
147 } else if (user.getUserContact() == null) {
149 throw new NullPointerException("user.userContact is null"); //NOI18N
150 } else if (user.getUserContact().getContactEmailAddress() == null) {
152 throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
153 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
155 throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
158 // Check if user is registered
159 if (this.isUserNameRegistered(user)) {
161 throw new UserNameAlreadyRegisteredException(user);
162 } else if (this.isEmailAddressRegistered(user)) {
164 throw new EmailAddressAlreadyRegisteredException(user);
168 User addedUser = this.userBean.addUser(user);
171 Address emailAddress;
174 // Create email address and set
175 emailAddress = new InternetAddress(addedUser.getUserContact().getContactEmailAddress());
176 } catch (final AddressException ex) {
178 throw new EJBException(ex);
182 // TODO: Internationlize the subject line somehow
183 this.sendEmail("Registration", "registration", emailAddress, addedUser, baseUrl); //NOI18N
186 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: addedUser={0},addedUser.userId={1} - EXIT!", addedUser, addedUser.getUserId())); //NOI18N