package org.mxchange.jusercore.model.email_address;
import java.text.MessageFormat;
+import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
+import javax.persistence.NoResultException;
import javax.persistence.PersistenceException;
+import javax.persistence.Query;
import org.mxchange.jcoreee.database.BaseDatabaseBean;
-import org.mxchange.jusercore.model.user.User;
import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
/**
@Stateless (name = "email-change", mappedName = "ejb/stateless-addressbook-email-change", description = "A bean handling email changes")
public class EmailChangeSessionBean extends BaseDatabaseBean implements EmailChangeSessionBeanRemote {
+
/**
* Serial number
*/
}
@Override
- public void enqueueEmailAddressForChange (final User user) {
+ public List<String> allQueuedAddressesAsList () {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void enqueueEmailAddressForChange (final ChangeableEmailAddress emailAddress) {
// Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("enqueueEmailAddressForChange: user={0} - CALLED!", user));
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("enqueueEmailAddressForChange: emailAddress={0} - CALLED!", emailAddress));
// user should not be null
- if (null == user) {
+ if (null == emailAddress) {
// Abort here
- throw new NullPointerException("user is null"); //NOI18N
- } else if (user.getUserId() == null) {
+ throw new NullPointerException("emailAddress is null"); //NOI18N
+ } else if (emailAddress.getEmailChangeId() == null) {
// Throw NPE again
- throw new NullPointerException("user.userId is null"); //NOI18N
- } else if (user.getUserId() < 1) {
+ throw new NullPointerException("emailAddress.emailChangeId is null"); //NOI18N
+ } else if (emailAddress.getEmailChangeId() < 1) {
// Not valid
- throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
- } else if (user.getUserAccountStatus() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
- } else if (!this.userBean.ifUserIdExists(user.getUserId())) {
+ throw new IllegalArgumentException(MessageFormat.format("emailAddress.emailChangeId={0} is not valid.", emailAddress.getEmailChangeId())); //NOI18N
+ } else if (!this.userBean.ifUserExists(emailAddress.getEmailChangeUser())) {
// User does not exist
- throw new PersistenceException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
+ throw new PersistenceException(MessageFormat.format("Email change with id {0} does not exist.", emailAddress.getEmailChangeId())); //NOI18N
}
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
- public void updateEmailAddress (final User user) {
+ public boolean isEmailAddressEnqueued (final String emailAddress) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressEnqueued: emailAddress={0} - CALLED!", emailAddress)); //NOI18N
+
+ // Create query instance
+ Query query = this.getEntityManager().createNamedQuery("SearchEmailChangeByEmail"); //NOI18N
+
+ // Add email address as parameter
+ query.setParameter("email", emailAddress); //NOI18N
+
+ // Initialize variable
+ boolean isFound = false;
+
+ // Try it
+ try {
+ // Try to get single result
+ ChangeableEmailAddress dummy = (ChangeableEmailAddress) query.getSingleResult();
+
+ // Found it
+ isFound = true;
+ } catch (final NoResultException ex) {
+ // Log it
+ this.getLoggerBeanLocal().logException(ex);
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressEnqueued: isFound={0} - EXIT!", isFound)); //NOI18N
+
+ // Return it
+ return isFound;
+ }
+
+ @Override
+ public void updateEmailAddress (final ChangeableEmailAddress emailAddress) {
// Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateEmailAddress: user={0} - CALLED!", user));
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateEmailAddress: emailAddress={0} - CALLED!", emailAddress));
// user should not be null
- if (null == user) {
+ if (null == emailAddress) {
// Abort here
- throw new NullPointerException("user is null"); //NOI18N
- } else if (user.getUserId() == null) {
+ throw new NullPointerException("emailAddress is null"); //NOI18N
+ } else if (emailAddress.getEmailChangeId() == null) {
// Throw NPE again
- throw new NullPointerException("user.userId is null"); //NOI18N
- } else if (user.getUserId() < 1) {
+ throw new NullPointerException("emailAddress.emailChangeId is null"); //NOI18N
+ } else if (emailAddress.getEmailChangeId() < 1) {
// Not valid
- throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
- } else if (user.getUserAccountStatus() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
- } else if (!this.userBean.ifUserIdExists(user.getUserId())) {
+ throw new IllegalArgumentException(MessageFormat.format("emailAddress.emailChangeId={0} is not valid.", emailAddress.getEmailChangeId())); //NOI18N
+ } else if (!this.userBean.ifUserExists(emailAddress.getEmailChangeUser())) {
// User does not exist
- throw new PersistenceException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
+ throw new PersistenceException(MessageFormat.format("Email change with id {0} does not exist.", emailAddress.getEmailChangeId())); //NOI18N
}
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return userNameList;
}
+ @Override
+ public boolean ifUserExists (final User user) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
+
+ // userId should not be null
+ if (null == user) {
+ // Abort here
+ throw new NullPointerException("user is null"); //NOI18N
+ } else if (user.getUserId() == null) {
+ // Abort here
+ throw new NullPointerException("user.userId is null"); //NOI18N
+ } else if (user.getUserId() < 1) {
+ // Invalid number
+ throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
+ }
+
+ // Generate query
+ Query query = this.getEntityManager().createNamedQuery("SearchUser", LoginUser.class); //NOI18N
+
+ // Set parameter
+ query.setParameter("user", user); //NOI18N
+
+ // Try this
+ try {
+ User dummy = (User) query.getSingleResult();
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
+ } catch (final NoResultException ex) {
+ // Log it
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
+
+ // User name does not exist
+ return false;
+ } catch (final PersistenceException ex) {
+ // Something bad happened
+ this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
+
+ // Throw again
+ throw ex;
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
+
+ // Found it
+ return true;
+ }
+
@Override
public boolean ifUserIdExists (final Long userId) {
// Trace message
} else if (user.getUserAccountStatus() == null) {
// Throw NPE again
throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
- } else if (!this.ifUserIdExists(user.getUserId())) {
+ } else if (!this.ifUserExists(user)) {
// User does not exist
throw new PersistenceException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
}