2 * Copyright (C) 2016 quix0r
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jusercore.model.email_address;
19 import java.text.MessageFormat;
20 import java.util.List;
22 import javax.ejb.EJBException;
23 import javax.ejb.Stateless;
24 import javax.persistence.NoResultException;
25 import javax.persistence.Query;
26 import org.mxchange.jcoreee.database.BaseDatabaseBean;
27 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
30 * A session bean for changing email addresses
32 * @author Roland Haeder<roland@mxchange.org>
34 @Stateless (name = "email-change", mappedName = "ejb/stateless-jjobs-email-change", description = "A bean handling email changes")
35 public class EmailChangeSessionBean extends BaseDatabaseBean implements EmailChangeSessionBeanRemote {
40 private static final long serialVersionUID = 182_698_165_971_548L;
46 private UserSessionBeanRemote userBean;
51 public EmailChangeSessionBean () {
55 @SuppressWarnings ("unchecked")
56 public List<String> allQueuedAddressesAsList () {
58 this.getLoggerBeanLocal().logTrace("allQueuedAddressesAsList: CALLED!"); //NOI18N
61 Query query = this.getEntityManager().createNamedQuery("AllEmailAddressChanges", List.class);
64 List<String> emailAddresses = query.getResultList();
67 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allQueuedAddressesAsList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
70 return emailAddresses;
74 public void enqueueEmailAddressForChange (final ChangeableEmailAddress emailAddress) {
76 this.getLoggerBeanLocal().logTrace(MessageFormat.format("enqueueEmailAddressForChange: emailAddress={0} - CALLED!", emailAddress)); //NOI18N
78 // user should not be null
79 if (null == emailAddress) {
81 throw new NullPointerException("emailAddress is null"); //NOI18N
82 } else if (!this.userBean.ifUserExists(emailAddress.getEmailChangeUser())) {
83 // User does not exist
84 throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailAddress.getEmailChangeId())); //NOI18N
85 } else if (this.isEmailAddressEnqueued(emailAddress.getEmailAddress())) {
86 // Email address is already enqueued
87 throw new EJBException(MessageFormat.format("Email address {0} is already enqueued.", emailAddress.getEmailAddress()));
90 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
94 public boolean isEmailAddressEnqueued (final String emailAddress) {
96 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressEnqueued: emailAddress={0} - CALLED!", emailAddress)); //NOI18N
98 // Create query instance
99 Query query = this.getEntityManager().createNamedQuery("SearchEmailChangeByEmail"); //NOI18N
101 // Add email address as parameter
102 query.setParameter("email", emailAddress); //NOI18N
104 // Initialize variable
105 boolean isFound = false;
109 // Try to get single result
110 ChangeableEmailAddress dummy = (ChangeableEmailAddress) query.getSingleResult();
114 } catch (final NoResultException ex) {
116 this.getLoggerBeanLocal().logException(ex);
120 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressEnqueued: isFound={0} - EXIT!", isFound)); //NOI18N
127 public void updateEmailAddress (final ChangeableEmailAddress emailAddress) {
129 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateEmailAddress: emailAddress={0} - CALLED!", emailAddress)); //NOI18N
131 // user should not be null
132 if (null == emailAddress) {
134 throw new NullPointerException("emailAddress is null"); //NOI18N
135 } else if (emailAddress.getEmailChangeId() == null) {
137 throw new NullPointerException("emailAddress.emailChangeId is null"); //NOI18N
138 } else if (emailAddress.getEmailChangeId() < 1) {
140 throw new IllegalArgumentException(MessageFormat.format("emailAddress.emailChangeId={0} is not valid.", emailAddress.getEmailChangeId())); //NOI18N
141 } else if (!this.userBean.ifUserExists(emailAddress.getEmailChangeUser())) {
142 // User does not exist
143 throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailAddress.getEmailChangeId())); //NOI18N
144 } else if (!this.isEmailAddressEnqueued(emailAddress.getEmailAddress())) {
145 // Email address is not enqueued
146 throw new EJBException(MessageFormat.format("Email address {0} is not enqueued.", emailAddress.getEmailAddress()));
149 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.