]> git.mxchange.org Git - addressbook-ejb.git/blob
4a514f8f55145bc83973f1fa09142079436b325b
[addressbook-ejb.git] /
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.juserlogincore.model.user.register;
18
19 import java.text.MessageFormat;
20 import java.util.Objects;
21 import javax.ejb.EJB;
22 import javax.ejb.Stateless;
23 import javax.persistence.NoResultException;
24 import javax.persistence.Query;
25 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
26 import org.mxchange.jcontacts.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;
34
35 /**
36  * A session-scoped bean for user registration
37  * <p>
38  * @author Roland Häder<roland@mxchange.org>
39  */
40 @Stateless (name = "userRegistration", description = "A bean handling the user registration")
41 public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatabaseBean implements UserRegistrationSessionBeanRemote {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 12_348_958_986_818_627L;
47
48         /**
49          * Administrative user bean
50          */
51         @EJB
52         private AdminUserSessionBeanRemote adminUserBean;
53
54         /**
55          * Regular user EJB
56          */
57         @EJB
58         private UserSessionBeanRemote userBean;
59
60         /**
61          * Default constructor
62          */
63         public AddressbookUserRegistrationSessionBean () {
64                 // Call super constructor
65                 super("jms/addressbook-queue-factory", "jms/addressbook-email-queue"); //NOI18N
66         }
67
68         @Override
69         public String generateConfirmationKey (final User user) {
70                 // Trace message
71                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
72
73                 // user should not be null
74                 if (null == user) {
75                         // Abort here
76                         throw new NullPointerException("user is null"); //NOI18N
77                 }
78
79                 // Create named instance
80                 final Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
81
82                 // Init confirmation key
83                 String confirmationKey = null;
84
85                 // Find a free one
86                 while (confirmationKey == null) {
87                         // Create new one
88                         final String key = UserLoginUtils.generatedConfirmationKey(user);
89
90                         // Set key as parameter
91                         query.setParameter("confirmKey", key); //NOI18N
92
93                         // Try it
94                         try {
95                                 // Get contact instance
96                                 final Contact contact = (Contact) query.getSingleResult();
97
98                                 // Warning message
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;
103                                 break;
104                         }
105                 }
106
107                 // Log trace message
108                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), confirmationKey)); //NOI18N
109
110                 // Return it
111                 return confirmationKey;
112         }
113
114         @Override
115         public boolean isEmailAddressRegistered (final User user) {
116                 // Trace message
117                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
118
119                 // Check bean
120                 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
121
122                 // user should not be null
123                 if (null == user) {
124                         // Abort here
125                         throw new NullPointerException("user is null"); //NOI18N
126                 }
127
128                 // Call other bean
129                 return this.userBean.isEmailAddressRegistered(user);
130         }
131
132         @Override
133         public boolean isUserNameRegistered (final User user) {
134                 // Trace message
135                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
136
137                 // Check bean
138                 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
139
140                 // user should not be null
141                 if (null == user) {
142                         // Abort here
143                         throw new NullPointerException("user is null"); //NOI18N
144                 }
145
146                 // Call other bean
147                 return this.userBean.isUserNameRegistered(user);
148         }
149
150         @Override
151         public User registerUser (final User user, final String baseUrl, final String randomPassword) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
152                 // Trace message
153                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2},randomPassword[]={3} - CALLED!", this.getClass().getSimpleName(), user, baseUrl, Objects.toString(randomPassword))); //NOI18N
154
155                 // user should not be null
156                 if (null == user) {
157                         // Abort here
158                         throw new NullPointerException("user is null"); //NOI18N
159                 } else if (user.getUserContact() == null) {
160                         // Throw NPE again
161                         throw new NullPointerException("user.userContact is null"); //NOI18N
162                 } else if (user.getUserContact().getContactEmailAddress() == null) {
163                         // Throw NPE again
164                         throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
165                 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
166                         // Is empty
167                         throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
168                 }
169
170                 // Check if user is registered
171                 if (this.isUserNameRegistered(user)) {
172                         // Abort here
173                         throw new UserNameAlreadyRegisteredException(user);
174                 } else if (this.isEmailAddressRegistered(user)) {
175                         // Abort here
176                         throw new EmailAddressAlreadyRegisteredException(user);
177                 }
178
179                 // Call other EJB
180                 final User addedUser = this.adminUserBean.addUser(user);
181
182                 // Default template is with no random password
183                 String templateName = "user_registration"; //NOI18N
184
185                 // Is password set?
186                 if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
187                         // Switch to other template
188                         templateName = "user_registration_random"; //NOI18N
189                 }
190
191                 // Send email
192                 // @TODO: Internationlize the subject line somehow
193                 this.sendEmail("Registration", templateName, addedUser, baseUrl, randomPassword); //NOI18N
194
195                 // Trace message
196                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N
197
198                 // Return it
199                 return addedUser;
200         }
201
202 }