]> git.mxchange.org Git - jfinancials-mailer-ejb.git/blobdiff - src/java/org/mxchange/jusercore/model/user/UserSessionBean.java
better throw EJBException here
[jfinancials-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / UserSessionBean.java
index 1b6e197b1c70d96a33f568013dfe6c5d505f049d..2f0cb500b9900248c0a12e8b709353996369ca7d 100644 (file)
@@ -19,6 +19,7 @@ package org.mxchange.jusercore.model.user;
 import java.text.MessageFormat;
 import java.util.GregorianCalendar;
 import java.util.List;
+import javax.ejb.EJBException;
 import javax.ejb.Stateless;
 import javax.persistence.NoResultException;
 import javax.persistence.PersistenceException;
@@ -159,6 +160,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("SearchUserId", LoginUser.class); //NOI18N
+
+               // Set parameter
+               query.setParameter("id", user.getUserId()); //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
@@ -288,58 +339,6 @@ public class UserSessionBean extends BaseDatabaseBean implements UserSessionBean
                return true;
        }
 
-       @Override
-       public void enqueueEmailAddressForChange (final User user) {
-               // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("enqueueEmailAddressForChange: user={0} - CALLED!", user));
-
-               // user should not be null
-               if (null == user) {
-                       // Abort here
-                       throw new NullPointerException("user is null"); //NOI18N
-               } else if (user.getUserId() == null) {
-                       // Throw NPE again
-                       throw new NullPointerException("user.userId is null"); //NOI18N
-               } else if (user.getUserId() < 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.ifUserIdExists(user.getUserId())) {
-                       // User does not exist
-                       throw new PersistenceException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
-               }
-
-               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
-       }
-
-       @Override
-       public void updateEmailAddress (final User user) {
-               // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateEmailAddress: user={0} - CALLED!", user));
-
-               // user should not be null
-               if (null == user) {
-                       // Abort here
-                       throw new NullPointerException("user is null"); //NOI18N
-               } else if (user.getUserId() == null) {
-                       // Throw NPE again
-                       throw new NullPointerException("user.userId is null"); //NOI18N
-               } else if (user.getUserId() < 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.ifUserIdExists(user.getUserId())) {
-                       // User does not exist
-                       throw new PersistenceException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
-               }
-
-               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
-       }
-
        @Override
        public void updateUserPersonalData (final User user) {
                // Trace message
@@ -358,9 +357,9 @@ 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
+                       throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
                }
 
                // Find the instance