]> git.mxchange.org Git - jfinancials-lib.git/blobdiff - src/org/mxchange/addressbook/validator/user/UserIdValidator.java
Added email address for author + made default constructor public (maybe needed for...
[jfinancials-lib.git] / src / org / mxchange / addressbook / validator / user / UserIdValidator.java
index 17f76a7e809bfba5ee67f6984ce2bd594846b405..1b14542779c2232929b687ee62483fc2e93968c8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Roland Haeder
+ * Copyright (C) 2016 Roland Haeder
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,9 +17,8 @@
 package org.mxchange.addressbook.validator.user;
 
 import java.text.MessageFormat;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import javax.ejb.EJB;
+import java.util.Set;
+import java.util.TreeSet;
 import javax.enterprise.event.Observes;
 import javax.faces.application.FacesMessage;
 import javax.faces.component.UIComponent;
@@ -27,7 +26,12 @@ import javax.faces.context.FacesContext;
 import javax.faces.validator.FacesValidator;
 import javax.faces.validator.Validator;
 import javax.faces.validator.ValidatorException;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import org.mxchange.jcoreee.validator.number.BaseLongValidator;
+import org.mxchange.jcoreeelogger.beans.local.logger.Log;
+import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
 import org.mxchange.jusercore.model.user.User;
 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
@@ -35,31 +39,93 @@ import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
 /**
  * A validator for user ids
  * <p>
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
  */
 @FacesValidator (value = "UserIdValidator")
 public class UserIdValidator extends BaseLongValidator implements Validator {
 
+       /**
+        * Cached user status
+        */
+       private static final Set<Long> cachedStatus = new TreeSet<>();
+
        /**
         * Serial number
         */
        private static final long serialVersionUID = 12_869_569_314_764_690L;
 
+       /**
+        * Logger instance
+        */
+       @Log
+       private LoggerBeanLocal loggerBeanLocal;
+
        /**
         * Remote bean
         */
-       @EJB (mappedName = "ejb/stateless-user")
        private UserSessionBeanRemote userBean;
 
        /**
-        * Cached user status
+        * Initialization of this converter
         */
-       private static final ConcurrentMap<Long, Boolean> cachedStatus = new ConcurrentHashMap<>(0);
+       public UserIdValidator () {
+               // Try to get it
+               try {
+                       // Get initial context
+                       Context context = new InitialContext();
+
+                       // Lookup logger
+                       this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
+
+                       // ... and user controller
+                       this.userBean = (UserSessionBeanRemote) context.lookup("java:global/juser-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
+               } catch (final NamingException ex) {
+                       // Continue to throw it
+                       throw new RuntimeException("context.lookup() failed.", ex); //NOI18N
+               }
+       }
+
+       /**
+        * Event fired when the user registration is complete
+        * <p>
+        * @param event User registration event
+        */
+       public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
+               // Trace message
+               this.loggerBeanLocal.logTrace(MessageFormat.format("UserIdValidator:afterRegistrationEvent: event={0} - CALLED!", event)); //NOI18N
+
+               // event should not be null
+               if (null == event) {
+                       // Throw NPE
+                       throw new NullPointerException("event is null"); //NOI18N
+               } else if (event.getUser() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.user is null"); //NOI18N
+               } else if (event.getUser().getUserId() == null) {
+                       // userId is null
+                       throw new NullPointerException("event.user.userId is null"); //NOI18N
+               } else if (event.getUser().getUserId() < 1) {
+                       // Not avalid id
+                       throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
+               }
+
+               // Get user instance
+               User registeredUser = event.getUser();
+
+               // Debug message
+               this.loggerBeanLocal.logDebug(MessageFormat.format("UserIdValidator:afterRegistrationEvent: registeredUser={0}", registeredUser)); //NOI18N
+
+               // Update cache
+               UserIdValidator.cachedStatus.add(registeredUser.getUserId());
+
+               // Trace message
+               this.loggerBeanLocal.logTrace("UserIdValidator:afterRegistrationEvent: EXIT!"); //NOI18N
+       }
 
        @Override
        public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
                // Trace message
-               //this.getLogger().logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
+               this.loggerBeanLocal.logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
 
                // All accepted, required fields
                String[] requiredFileds = {"userId"}; //NOI18N
@@ -71,55 +137,28 @@ public class UserIdValidator extends BaseLongValidator implements Validator {
                Long userId = (Long) value;
 
                // Define variable
-               Boolean ifUserExists = false;
+               Boolean ifUserExists;
 
                // Is a map entry there?
-               if (UserIdValidator.cachedStatus.containsKey(userId)) {
+               if (UserIdValidator.cachedStatus.contains(userId)) {
                        // Get from cache
-                       ifUserExists = UserIdValidator.cachedStatus.get(userId);
+                       ifUserExists = Boolean.TRUE;
                } else {
                        // Get status
                        ifUserExists = this.userBean.ifUserIdExists(userId);
-
-                       // Add to cache
-                       UserIdValidator.cachedStatus.put(userId, ifUserExists);
                }
 
-               // Is the address book id valid?
+               // Is the user id valid?
                if (!ifUserExists) {
                        // Is not valid
                        throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N
                }
 
+               // Add to cache if valid
+               UserIdValidator.cachedStatus.add(userId);
+
                // Trace message
-               //this.getLogger().logTrace("validate: EXIT!"); //NOI18N
+               this.loggerBeanLocal.logTrace("validate: EXIT!"); //NOI18N
        }
 
-       /**
-        * Event fired when the user registration is complete
-        * <p>
-        * @param event User registration event
-        */
-       public void afterRegistration (final @Observes UserRegisteredEvent event) {
-               // event should not be null
-               if (null == event) {
-                       // Throw NPE
-                       throw new NullPointerException("event is null"); //NOI18N
-               } else if (event.getUser() == null) {
-                       // Throw NPE again
-                       throw new NullPointerException("event.user is null"); //NOI18N
-               } else if (event.getUser().getUserId() == null) {
-                       // userId is null
-                       throw new NullPointerException("event.user.userId is null"); //NOI18N
-               } else if (event.getUser().getUserId() < 1) {
-                       // Not avalid id
-                       throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
-               }
-
-               // Get user instance
-               User registeredUser = event.getUser();
-
-               // Update cache
-               UserIdValidator.cachedStatus.put(registeredUser.getUserId(), Boolean.TRUE);
-       }
 }