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