/*
- * Copyright (C) 2016 Roland Häder
+ * Copyright (C) 2016 - 2018 Free Software Foundation
*
* 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
package org.mxchange.jjobs.validator.user;
import java.text.MessageFormat;
-import java.util.Set;
-import java.util.TreeSet;
-import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.CDI;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
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.model.user.User;
-import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
-import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent;
+import org.mxchange.jcoreee.validator.number.BaseNumberValidator;
+import org.mxchange.jjobs.beans.user.JobsUserWebRequestBean;
+import org.mxchange.jjobs.beans.user.JobsUserWebRequestController;
/**
* A validator for user ids
* <p>
* @author Roland Häder<roland@mxchange.org>
*/
-@FacesValidator (value = "UserIdValidator")
-public class JobsUserIdValidator extends BaseLongValidator implements Validator {
+@FacesValidator ("UserIdValidator")
+public class JobsUserIdValidator extends BaseNumberValidator {
/**
- * Cached user status
+ * User backing bean
*/
- private static final Set<Long> cachedStatus = new TreeSet<>();
+ private static JobsUserWebRequestController USER_CONTROLLER;
/**
* Serial number
*/
private static final long serialVersionUID = 12_869_569_314_764_690L;
- /**
- * Logger instance
- */
- @Log
- private LoggerBeanLocal loggerBeanLocal;
-
- /**
- * Remote bean
- */
- private UserSessionBeanRemote userBean;
-
- /**
- * Initialization of this converter
- */
- public JobsUserIdValidator () {
- // 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/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
- } catch (final NamingException ex) {
- // Continue to throw it
- throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
- }
- }
-
- /**
- * Event fired when the user registration is complete
- * <p>
- * @param event User registration event
- */
- public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
- // Trace message
- this.loggerBeanLocal.logTrace(MessageFormat.format("UserIdValidator:afterUserRegistrationEvent: 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.getRegisteredUser() == null) {
- // Throw NPE again
- throw new NullPointerException("event.user is null"); //NOI18N
- } else if (event.getRegisteredUser().getUserId() == null) {
- // userId is null
- throw new NullPointerException("event.user.userId is null"); //NOI18N
- } else if (event.getRegisteredUser().getUserId() < 1) {
- // Not avalid id
- throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
- }
-
- // Get user instance
- User registeredUser = event.getRegisteredUser();
-
- // Debug message
- this.loggerBeanLocal.logDebug(MessageFormat.format("UserIdValidator:afterUserRegistrationEvent: registeredUser={0}", registeredUser)); //NOI18N
-
- // Update cache
- JobsUserIdValidator.cachedStatus.add(registeredUser.getUserId());
-
- // Trace message
- this.loggerBeanLocal.logTrace("UserIdValidator:afterUserRegistrationEvent: EXIT!"); //NOI18N
- }
-
@Override
public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
- // Trace message
- this.loggerBeanLocal.logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
-
// All accepted, required fields
- String[] requiredFields = {"userId"}; //NOI18N
+ final String[] requiredFields = {"userId"}; //NOI18N
// Pre-validation (example: not null, not a string, empty string ...)
super.preValidate(context, component, value, requiredFields, false);
+ // Is the instance there?
+ if (null == USER_CONTROLLER) {
+ // Get bean from CDI directly
+ USER_CONTROLLER = CDI.current().select(JobsUserWebRequestBean.class).get();
+ }
+
// Cast value
- Long userId = (Long) value;
+ final Long userId = (Long) value;
// Define variable
- Boolean ifUserExists;
-
- // Is a map entry there?
- if (JobsUserIdValidator.cachedStatus.contains(userId)) {
- // Get from cache
- ifUserExists = Boolean.TRUE;
- } else {
- // Get status
- ifUserExists = this.userBean.ifUserIdExists(userId);
- }
+ final Boolean ifUserExists = USER_CONTROLLER.ifUserIdExists(userId);
// 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
- JobsUserIdValidator.cachedStatus.add(userId);
-
- // Trace message
- this.loggerBeanLocal.logTrace("validate: EXIT!"); //NOI18N
}
}