From: Roland Haeder <roland@mxchange.org>
Date: Sat, 12 Mar 2016 17:08:52 +0000 (+0100)
Subject: Continued a bit:
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=4ddd0b27ca287571cfca51b38fc4d56656a2f6a6;p=jjobs-mailer-ejb.git

Continued a bit:
- added ifUserExists()
- rewrote enqueueEmailAddressForChange() which now takes ChangeableEmailAddress as instance
- also rewrote updateEmailAddress() with same parameter
- added isEmailAddressEnqueued()
- updated jar(s)
---

diff --git a/lib/juser-core.jar b/lib/juser-core.jar
index 8488d71..1924b0b 100644
Binary files a/lib/juser-core.jar and b/lib/juser-core.jar differ
diff --git a/lib/juser-lib.jar b/lib/juser-lib.jar
index dd11c24..d70697a 100644
Binary files a/lib/juser-lib.jar and b/lib/juser-lib.jar differ
diff --git a/src/java/org/mxchange/jusercore/model/email_address/EmailChangeSessionBean.java b/src/java/org/mxchange/jusercore/model/email_address/EmailChangeSessionBean.java
index 2eddde8..a049514 100644
--- a/src/java/org/mxchange/jusercore/model/email_address/EmailChangeSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/email_address/EmailChangeSessionBean.java
@@ -17,11 +17,13 @@
 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;
 
 /**
@@ -32,6 +34,7 @@ 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
 	 */
@@ -50,52 +53,84 @@ public class EmailChangeSessionBean extends BaseDatabaseBean implements EmailCha
 	}
 
 	@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.
diff --git a/src/java/org/mxchange/jusercore/model/user/UserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/UserSessionBean.java
index 4795d84..59fe23a 100644
--- a/src/java/org/mxchange/jusercore/model/user/UserSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/UserSessionBean.java
@@ -159,6 +159,56 @@ public class UserSessionBean extends BaseDatabaseBean implements UserSessionBean
 		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
@@ -306,7 +356,7 @@ public class UserSessionBean extends BaseDatabaseBean implements UserSessionBean
 		} 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
 		}