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 ("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) {
84 // event should not be null
87 throw new NullPointerException("event is null"); //NOI18N
88 } else if (event.getRegisteredUser() == null) {
90 throw new NullPointerException("event.user is null"); //NOI18N
91 } else if (event.getRegisteredUser().getUserId() == null) {
93 throw new NullPointerException("event.user.userId is null"); //NOI18N
94 } else if (event.getRegisteredUser().getUserId() < 1) {
96 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
100 User registeredUser = event.getRegisteredUser();
103 JobsUserIdValidator.cachedStatus.add(registeredUser.getUserId());
107 public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
108 // All accepted, required fields
109 String[] requiredFields = {"userId"}; //NOI18N
111 // Pre-validation (example: not null, not a string, empty string ...)
112 super.preValidate(context, component, value, requiredFields, false);
115 Long userId = (Long) value;
118 Boolean ifUserExists;
120 // Is a map entry there?
121 if (JobsUserIdValidator.cachedStatus.contains(userId)) {
123 ifUserExists = Boolean.TRUE;
126 ifUserExists = this.userBean.ifUserIdExists(userId);
129 // Is the user id valid?
132 throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N
135 // Add to cache if valid
136 JobsUserIdValidator.cachedStatus.add(userId);