]> git.mxchange.org Git - addressbook-ejb.git/blob
563c0156c86b80ead34bcf95db5de5304931a43b
[addressbook-ejb.git] /
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
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.enterprise.BaseAddressbookEnterpriseBean;
26 import org.mxchange.jcontacts.model.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 BaseAddressbookEnterpriseBean 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 (lookup = "java:global/addressbook-ejb/adminUser!org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote")
52         private AdminUserSessionBeanRemote adminUserBean;
53
54         /**
55          * Regular user EJB
56          */
57         @EJB (lookup = "java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
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 User registerUser (final User user, final String baseUrl, final String randomPassword) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
116                 // Trace message
117                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2},randomPassword[]={3} - CALLED!", this.getClass().getSimpleName(), user, baseUrl, Objects.toString(randomPassword))); //NOI18N
118
119                 // user should not be null
120                 if (null == user) {
121                         // Abort here
122                         throw new NullPointerException("user is null"); //NOI18N
123                 } else if (user.getUserContact() == null) {
124                         // Throw NPE again
125                         throw new NullPointerException("user.userContact is null"); //NOI18N
126                 } else if (user.getUserContact().getContactEmailAddress() == null) {
127                         // Throw NPE again
128                         throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
129                 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
130                         // Is empty
131                         throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
132                 }
133
134                 // Check if user is registered
135                 if (this.userBean.isUserNameRegistered(user)) {
136                         // Abort here
137                         throw new UserNameAlreadyRegisteredException(user);
138                 } else if (this.userBean.isEmailAddressRegistered(user)) {
139                         // Abort here
140                         throw new EmailAddressAlreadyRegisteredException(user);
141                 }
142
143                 // Call other EJB
144                 final User addedUser = this.adminUserBean.addUser(user);
145
146                 // Default template is with no random password
147                 String templateName = "user_registration"; //NOI18N
148
149                 // Is password set?
150                 if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
151                         // Switch to other template
152                         templateName = "user_registration_random"; //NOI18N
153                 }
154
155                 // Send email
156                 // @TODO: Internationlize the subject line somehow
157                 this.sendEmail("Registration", templateName, addedUser, baseUrl, randomPassword); //NOI18N
158
159                 // Trace message
160                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N
161
162                 // Return it
163                 return addedUser;
164         }
165
166 }