2 * Copyright (C) 2016 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.validator.user;
19 import java.text.MessageFormat;
21 import java.util.TreeSet;
22 import javax.enterprise.event.Observes;
23 import javax.faces.application.FacesMessage;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.validator.FacesValidator;
27 import javax.faces.validator.Validator;
28 import javax.faces.validator.ValidatorException;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.jcoreee.validator.number.BaseLongValidator;
33 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
34 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
35 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
36 import org.mxchange.jusercore.model.user.User;
37 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
40 * A validator for user ids
42 * @author Roland Haeder<roland@mxchange.org>
44 @FacesValidator (value = "UserIdValidator")
45 public class UserIdValidator extends BaseLongValidator implements Validator {
50 private static final Set<Long> cachedStatus = new TreeSet<>();
55 private static final long serialVersionUID = 12_869_569_314_764_690L;
61 private LoggerBeanLocal loggerBeanLocal;
66 private UserSessionBeanRemote userBean;
69 * Initialization of this converter
71 public UserIdValidator () {
74 // Get initial context
75 Context context = new InitialContext();
78 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
80 // ... and user controller
81 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
82 } catch (final NamingException ex) {
83 // Continue to throw it
84 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
89 * Event fired when the user registration is complete
91 * @param event User registration event
93 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
95 this.loggerBeanLocal.logTrace(MessageFormat.format("UserIdValidator:afterRegistrationEvent: event={0} - CALLED!", event)); //NOI18N
97 // event should not be null
100 throw new NullPointerException("event is null"); //NOI18N
101 } else if (event.getRegisteredUser() == null) {
103 throw new NullPointerException("event.user is null"); //NOI18N
104 } else if (event.getRegisteredUser().getUserId() == null) {
106 throw new NullPointerException("event.user.userId is null"); //NOI18N
107 } else if (event.getRegisteredUser().getUserId() < 1) {
109 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
113 User registeredUser = event.getRegisteredUser();
116 this.loggerBeanLocal.logDebug(MessageFormat.format("UserIdValidator:afterRegistrationEvent: registeredUser={0}", registeredUser)); //NOI18N
119 UserIdValidator.cachedStatus.add(registeredUser.getUserId());
122 this.loggerBeanLocal.logTrace("UserIdValidator:afterRegistrationEvent: EXIT!"); //NOI18N
126 public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
128 this.loggerBeanLocal.logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
130 // All accepted, required fields
131 String[] requiredFields = {"userId"}; //NOI18N
133 // Pre-validation (example: not null, not a string, empty string ...)
134 super.preValidate(context, component, value, requiredFields, false);
137 Long userId = (Long) value;
140 Boolean ifUserExists;
142 // Is a map entry there?
143 if (UserIdValidator.cachedStatus.contains(userId)) {
145 ifUserExists = Boolean.TRUE;
148 ifUserExists = this.userBean.ifUserIdExists(userId);
151 // Is the user id valid?
154 throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N
157 // Add to cache if valid
158 UserIdValidator.cachedStatus.add(userId);
161 this.loggerBeanLocal.logTrace("validate: EXIT!"); //NOI18N