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;
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
return true;
}
- @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
} 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