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