import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
+import java.util.Objects;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
-import javax.persistence.NoResultException;
-import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.mxchange.jcontacts.model.contact.Contact;
import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
super("jms/jjobs-queue-factory", "jms/jjobs-email-queue"); //NOI18N
}
- @Override
- @SuppressWarnings ("unchecked")
- @Deprecated
- public List<String> allUserNames () {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: CALLED!", this.getClass().getSimpleName())); //NOI18N
-
- // Get query
- final Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
-
- // Get result list
- final List<String> userNameList = query.getResultList();
-
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: userNameList.size()={1} - EXIT!", this.getClass().getSimpleName(), userNameList.size())); //NOI18N
-
- // Return it
- return userNameList;
- }
-
- @Override
- @SuppressWarnings ("unchecked")
- public List<User> allUsers () {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
-
- // Get named query
- final Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
-
- // Get result
- final List<User> users = query.getResultList();
-
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
-
- // Return full list
- return users;
- }
-
@Override
public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException {
// Trace message
}
@Override
- @Deprecated
- public User fillUserData (final User user) throws UserNotFoundException {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
-
- // user should not be null
- if (null == user) {
- // Abort here
- throw new NullPointerException("user is null"); //NOI18N
- } else if (user.getUserName() == null) {
- // Throw NPE
- throw new NullPointerException("user.userName is null");
- } else if (user.getUserName().isEmpty()) {
- // Throw IAE
- throw new IllegalArgumentException("user.userName is empty");
- } else if (!this.ifUserExists(user)) {
- // User does not exist
- throw new UserNotFoundException(user);
- }
-
- // Try to locate it
- final Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
-
- // Set parameter
- query.setParameter("param", user.getUserName()); //NOI18N
-
- // Initialize variable
- final User foundUser;
-
- // Try it
- try {
- // Try to get single result
- foundUser = (User) query.getSingleResult();
- } catch (final NoResultException ex) {
- // Log it
- this.getLoggerBeanLocal().logException(ex);
- return null;
- }
-
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: foundUser={1} - EXIT!", this.getClass().getSimpleName(), foundUser)); //NOI18N
-
- // Return prepared instance
- return foundUser;
- }
-
- @Override
- @Deprecated
- public String generateRandomUserName () {
+ @SuppressWarnings ("unchecked")
+ public List<User> fetchAllUsers () {
// Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName - CALLED!", this.getClass().getSimpleName())); //NOI18N
-
- // Get full list
- final List<String> userList = this.allUserNames();
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
- // Init variable
- String userName = null;
+ // Get named query
+ final Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
- // Loop until a user name is found
- while ((userName == null) || (userList.contains(userName))) {
- // Generate random name
- userName = Users.generateRandomUserName();
- }
+ // Get result
+ final List<User> users = query.getResultList();
// Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName: userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
- // Found one, so return it
- return userName;
+ // Return full list
+ return users;
}
@Override
- @Deprecated
public boolean ifUserExists (final User user) {
// Trace message
this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
}
- // Generate query
- final Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
-
- // Set parameter
- query.setParameter("id", user.getUserId()); //NOI18N
-
- // Try this
- try {
- // Try to get single result
- final 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;
+ // Default is not found
+ boolean isFound = false;
+
+ // Fetch whole list
+ for (final User currentUser : this.fetchAllUsers()) {
+ // Is found?
+ if (Objects.equals(user, currentUser)) {
+ // Yes, then set flag and exit iteration
+ isFound = true;
+ break;
+ }
}
// Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: Found user {1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
- // Found it
- return true;
+ // Return flag
+ return isFound;
}
@Override
throw new NullPointerException("userName is empty"); //NOI18N
}
- // Generate query
- final Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
-
- // Set parameter
- query.setParameter("param", userName); //NOI18N
-
- // Try this
- try {
- // Try to get single result
- final User dummy = (User) query.getSingleResult();
-
- // Debug message
- this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
- } catch (final NoResultException ex) {
- // Log it
- this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
-
- // User name does not exist
- return false;
+ // Default is not registered
+ boolean isRegistered = false;
+
+ // Iterate over all records
+ for (final User currentUser : this.fetchAllUsers()) {
+ // Does the username match?
+ if (userName.equals(currentUser.getUserName())) {
+ // Yes, then set flag and exit iteration
+ isRegistered = true;
+ break;
+ }
}
// Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: Found userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
// Found it
return true;
throw new NullPointerException("user is null"); //NOI18N
}
- // Generate query
- final Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N
-
- // Set parameter
- query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
-
- // Search for it
- try {
- // Try to get single result
- final User dummy = (User) query.getSingleResult();
-
- // Debug message
- this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
- } catch (final NoResultException ex) {
- // Log it
- this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
-
- // Email address does not exist
- return false;
- } catch (final PersistenceException ex) {
- // Something bad happened
- this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
-
- // Throw again
- throw ex;
+ // Default is not registered
+ boolean isRegistered = false;
+
+ // Iterate over all records
+ for (final User currentUser : this.fetchAllUsers()) {
+ // Does the email address match?
+ if (user.getUserContact().getContactEmailAddress().equals(currentUser.getUserContact().getContactEmailAddress())) {
+ // Yes, then set flag and exit iteration
+ isRegistered = true;
+ break;
+ }
}
// Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
// Found it
- return true;
+ return isRegistered;
}
@Override
throw new NullPointerException("user is null"); //NOI18N
}
- // Generate query
- final Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
-
- // Set parameter
- query.setParameter("param", user.getUserName()); //NOI18N
-
- // Try this
- try {
- // Try to get single result
- final User dummy = (User) query.getSingleResult();
-
- // Debug message
- this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
- } catch (final NoResultException ex) {
- // Log it
- this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
-
- // User name does not exist
- return false;
- } catch (final PersistenceException ex) {
- // Something bad happened
- this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
-
- // Throw again
- throw ex;
- }
+ // Ask other method
+ final boolean isRegistered = this.ifUserNameExists(user.getUserName());
// Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
- // Found it
- return true;
+ // Return flag
+ return isRegistered;
}
@Override