2 * Copyright (C) 2016 Roland Häder
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.jjobs.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.convert.ConverterException;
27 import javax.faces.validator.FacesValidator;
28 import javax.faces.validator.Validator;
29 import javax.faces.validator.ValidatorException;
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32 import javax.naming.NamingException;
33 import org.mxchange.jcoreee.validator.number.BaseLongValidator;
34 import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent;
35 import org.mxchange.jusercore.model.user.User;
36 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
39 * A validator for user ids
41 * @author Roland Häder<roland@mxchange.org>
43 @FacesValidator (value = "UserIdValidator")
44 public class JobsUserIdValidator extends BaseLongValidator implements Validator {
49 private static final Set<Long> cachedStatus = new TreeSet<>();
54 private static final long serialVersionUID = 12_869_569_314_764_690L;
59 private UserSessionBeanRemote userBean;
62 * Initialization of this converter
64 public JobsUserIdValidator () {
67 // Get initial context
68 Context context = new InitialContext();
70 // ... and user controller
71 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
72 } catch (final NamingException ex) {
73 // Continue to throw it
74 throw new ConverterException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
79 * Event fired when the user registration is complete
81 * @param event User registration event
83 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
85 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("UserIdValidator:afterRegistrationEvent: event={0} - CALLED!", event)); //NOI18N
87 // event should not be null
90 throw new NullPointerException("event is null"); //NOI18N
91 } else if (event.getRegisteredUser() == null) {
93 throw new NullPointerException("event.user is null"); //NOI18N
94 } else if (event.getRegisteredUser().getUserId() == null) {
96 throw new NullPointerException("event.user.userId is null"); //NOI18N
97 } else if (event.getRegisteredUser().getUserId() < 1) {
99 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
103 User registeredUser = event.getRegisteredUser();
106 // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("UserIdValidator:afterRegistrationEvent: registeredUser={0}", registeredUser)); //NOI18N
109 JobsUserIdValidator.cachedStatus.add(registeredUser.getUserId());
112 // NOISY-DEBUG: this.loggerBeanLocal.logTrace("UserIdValidator:afterRegistrationEvent: EXIT!"); //NOI18N
116 public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
118 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
120 // All accepted, required fields
121 String[] requiredFields = {"userId"}; //NOI18N
123 // Pre-validation (example: not null, not a string, empty string ...)
124 super.preValidate(context, component, value, requiredFields, false);
127 Long userId = (Long) value;
130 Boolean ifUserExists;
132 // Is a map entry there?
133 if (JobsUserIdValidator.cachedStatus.contains(userId)) {
135 ifUserExists = Boolean.TRUE;
138 ifUserExists = this.userBean.ifUserIdExists(userId);
141 // Is the user id valid?
144 throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N
147 // Add to cache if valid
148 JobsUserIdValidator.cachedStatus.add(userId);
151 // NOISY-DEBUG: this.loggerBeanLocal.logTrace("validate: EXIT!"); //NOI18N