]> git.mxchange.org Git - jaddressbook-share-lib.git/commitdiff
added cache (back!) for user status and let the status being updated on registration
authorRoland Haeder <roland@mxchange.org>
Thu, 15 Oct 2015 10:17:44 +0000 (12:17 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 15 Oct 2015 10:19:11 +0000 (12:19 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

src/org/mxchange/addressbook/validator/user/UserIdValidator.java

index e8836f6d1aae858dda257bf9e80b49ae102b6ea6..a3ff257869ad9c995204127790f0d4cbda2137cd 100644 (file)
 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 javax.enterprise.event.Observes;
 import javax.faces.application.FacesMessage;
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
@@ -25,6 +28,8 @@ import javax.faces.validator.FacesValidator;
 import javax.faces.validator.Validator;
 import javax.faces.validator.ValidatorException;
 import org.mxchange.jcoreee.validator.number.BaseLongValidator;
+import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
+import org.mxchange.jusercore.model.user.User;
 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
 
 /**
@@ -46,6 +51,19 @@ public class UserIdValidator extends BaseLongValidator implements Validator {
        @EJB (mappedName = "ejb/stateless-user")
        private UserSessionBeanRemote userBean;
 
+       /**
+        * Cached user status
+        */
+       private final ConcurrentMap<Long, Boolean> cachedStatus;
+
+       /**
+        * Default constructor
+        */
+       public UserIdValidator () {
+               // Init map
+               this.cachedStatus = new ConcurrentHashMap<>(0);
+       }
+
        @Override
        public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
                // Trace message
@@ -60,8 +78,20 @@ public class UserIdValidator extends BaseLongValidator implements Validator {
                // Cast value
                Long number = (Long) value;
 
-               // Get status
-               Boolean ifUserExists = this.userBean.ifUserIdExists(number);
+               // Define variable
+               Boolean ifUserExists = false;
+
+               // Is a map entry there?
+               if (this.cachedStatus.containsKey(number)) {
+                       // Get from cache
+                       ifUserExists = this.cachedStatus.get(number);
+               } else {
+                       // Get status
+                       ifUserExists = this.userBean.ifUserIdExists(number);
+
+                       // Add to cache
+                       this.cachedStatus.put(number, ifUserExists);
+               }
 
                // Is the address book id valid?
                if (!ifUserExists) {
@@ -72,4 +102,32 @@ public class UserIdValidator extends BaseLongValidator implements Validator {
                // Trace message
                //this.getLogger().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
+               this.cachedStatus.put(registeredUser.getUserId(), Boolean.TRUE);
+       }
 }