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