2 * Copyright (C) 2016, 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.jusercore.model.user.email_address;
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.List;
23 import javax.ejb.EJBException;
24 import javax.ejb.Stateless;
25 import javax.mail.Address;
26 import javax.mail.internet.AddressException;
27 import javax.mail.internet.InternetAddress;
28 import javax.persistence.NoResultException;
29 import javax.persistence.Query;
30 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
31 import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress;
32 import org.mxchange.jusercore.model.email_address.EmailAddressChange;
33 import org.mxchange.jusercore.model.email_address.UserEmailChangeSessionBeanRemote;
34 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
35 import org.mxchange.jusercore.model.user.UserUtils;
38 * A session-scoped bean for changing email addresses
40 * @author Roland Häder<roland@mxchange.org>
42 @Stateless (name = "userEmailChange", description = "A bean handling user email changes")
43 public class AddressbookUserEmailChangeSessionBean extends BaseAddressbookDatabaseBean implements UserEmailChangeSessionBeanRemote {
48 private static final long serialVersionUID = 182_698_165_971_548L;
54 private UserSessionBeanRemote userBean;
59 public AddressbookUserEmailChangeSessionBean () {
60 // Call super constructor
65 @SuppressWarnings ("unchecked")
66 public List<String> allQueuedAddresses () {
68 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allQueuedAddresses: CALLED!", this.getClass().getSimpleName())); //NOI18N
71 Query query = this.getEntityManager().createNamedQuery("AllEmailAddressChanges", String.class); //NOI18N
74 List<String> emailAddresses = query.getResultList();
77 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allQueuedAddresses: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N
80 return emailAddresses;
84 public void enqueueEmailAddressForChange (final ChangeableEmailAddress emailChange, final String baseUrl) {
86 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.enqueueEmailAddressForChange: emailChange={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), emailChange, baseUrl)); //NOI18N
88 // Email address change should be valid
89 if (null == emailChange) {
91 throw new NullPointerException("emailChange is null"); //NOI18N
92 } else if (emailChange.getEmailChangeUser() == null) {
94 throw new NullPointerException("emailChange.emailChangeUser is null"); //NOI18N
95 } else if (emailChange.getEmailChangeUser().getUserId() == null) {
97 throw new NullPointerException("emailChange.emailChangeUser.userId is null"); //NOI18N
98 } else if (emailChange.getEmailChangeUser().getUserId() < 1) {
100 throw new IllegalArgumentException(MessageFormat.format("emailChange.emailChangeUser.userId={0} is invalid.", emailChange.getEmailChangeUser().getUserId())); //NOI18N
101 } else if (!this.userBean.ifUserExists(emailChange.getEmailChangeUser())) {
102 // User does not exist
103 throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailChange.getEmailChangeId())); //NOI18N
104 } else if (emailChange.getEmailAddress().trim().isEmpty()) {
105 // Email address is empty
106 throw new IllegalArgumentException("emailChange.emaiLAddress is empty."); //NOI18N
107 } else if (this.isEmailAddressEnqueued(emailChange.getEmailAddress())) {
108 // Email address is already enqueued
109 throw new EJBException(MessageFormat.format("Email address {0} is already enqueued.", emailChange.getEmailAddress())); //NOI18N
112 // The email change is not (yet) there, add secure hash and "created" timestamp
113 emailChange.setEmailChangeCreated(new GregorianCalendar());
114 this.generateSecureHash(emailChange);
117 //@TODO Fix email delivery then allow this: this.getEntityManager().persist(emailChange);
119 Address emailAddress;
122 // Create email address and set
123 emailAddress = new InternetAddress(emailChange.getEmailAddress());
124 } catch (final AddressException ex) {
126 throw new EJBException(ex);
130 this.sendEmail("User email change", "user_email_change", emailAddress, emailChange.getEmailChangeUser(), baseUrl, null); //NOI18N
133 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.enqueueEmailAddressForChange - EXIT!", this.getClass().getSimpleName())); //NOI18N
137 public boolean isEmailAddressEnqueued (final String emailAddress) {
139 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressEnqueued: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
141 // Create query instance
142 Query query = this.getEntityManager().createNamedQuery("SearchEmailChangeByEmail"); //NOI18N
144 // Add email address as parameter
145 query.setParameter("email", emailAddress); //NOI18N
147 // Initialize variable
148 boolean isFound = false;
152 // Try to get single result
153 ChangeableEmailAddress dummy = (ChangeableEmailAddress) query.getSingleResult();
157 } catch (final NoResultException ex) {
159 this.getLoggerBeanLocal().logException(ex);
163 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressEnqueued: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
170 public void updateEmailAddress (final ChangeableEmailAddress emailAddress) {
172 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
174 // Email address change should be valid
175 if (null == emailAddress) {
177 throw new NullPointerException("emailAddress is null"); //NOI18N
178 } else if (emailAddress.getEmailChangeId() == null) {
180 throw new NullPointerException("emailAddress.emailChangeId is null"); //NOI18N
181 } else if (emailAddress.getEmailChangeId() < 1) {
183 throw new IllegalArgumentException(MessageFormat.format("emailAddress.emailChangeId={0} is not valid.", emailAddress.getEmailChangeId())); //NOI18N
184 } else if (emailAddress.getEmailAddress().trim().isEmpty()) {
185 // Email address is empty
186 throw new IllegalArgumentException("emailAddress.emaiLAddress is empty."); //NOI18N
187 } else if (!this.userBean.ifUserExists(emailAddress.getEmailChangeUser())) {
188 // User does not exist
189 throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailAddress.getEmailChangeId())); //NOI18N
190 } else if (!this.isEmailAddressEnqueued(emailAddress.getEmailAddress())) {
191 // Email address is not enqueued
192 throw new EJBException(MessageFormat.format("Email address {0} is not enqueued.", emailAddress.getEmailAddress())); //NOI18N
195 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
199 * Generates a secure, unique hash for given email address change. This
200 * requires to check if the hash is really not there.
202 * @param emailAddress Email address change
204 private void generateSecureHash (final ChangeableEmailAddress emailAddress) {
205 // Email address change should be valid
206 if (null == emailAddress) {
208 throw new NullPointerException("emailAddress is null"); //NOI18N
209 } else if (emailAddress.getEmailAddress().trim().isEmpty()) {
210 // Email address is empty
211 throw new IllegalArgumentException("emailAddress.emaiLAddress is empty."); //NOI18N
214 // Initialize loop with null
217 // Default is not used
218 boolean isUsed = true;
220 // Search for free hash
222 // Generate hash, there is already in UserUtils a nice method that can be used for this purpose.
223 hash = UserUtils.encryptPassword(String.format("%s:%s", emailAddress.getEmailAddress(), emailAddress.toString())); //NOI18N
225 // The hash *may* be unique, better test it
226 Query query = this.getEntityManager().createNamedQuery("SearchEmailChangeByHash", EmailAddressChange.class); //NOI18N
228 // Set hash as parameter
229 query.setParameter("hash", hash); //NOI18N
231 // Try to get single result
234 ChangeableEmailAddress dummy = (ChangeableEmailAddress) query.getSingleResult();
235 } catch (final NoResultException ex) {
241 // hash should not be null and set
242 assert (hash != null) : "hash is null"; //NOI18N
243 assert (!hash.isEmpty()) : "hash is empty"; //NOI18N
245 // Set it in email change
246 emailAddress.setEmailChangeHash(hash);