2 * Copyright (C) 2015 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;
23 import javax.enterprise.event.Observes;
24 import javax.faces.application.FacesMessage;
25 import javax.faces.component.UIComponent;
26 import javax.faces.context.FacesContext;
27 import javax.faces.validator.FacesValidator;
28 import javax.faces.validator.Validator;
29 import javax.faces.validator.ValidatorException;
30 import org.mxchange.jcoreee.validator.number.BaseLongValidator;
31 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
32 import org.mxchange.jusercore.model.user.User;
33 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
36 * A validator for user ids
38 * @author Roland Haeder
40 @FacesValidator (value = "UserIdValidator")
41 public class UserIdValidator extends BaseLongValidator implements Validator {
46 private static final Set<Long> cachedStatus = new TreeSet<>();
51 private static final long serialVersionUID = 12_869_569_314_764_690L;
56 @EJB (mappedName = "ejb/stateless-user")
57 private UserSessionBeanRemote userBean;
60 * Event fired when the user registration is complete
62 * @param event User registration event
64 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
66 System.out.println(MessageFormat.format("UserIdValidator:afterRegistrationEvent: event={0} - CALLED!", event)); //NOI18N
68 // event should not be null
71 throw new NullPointerException("event is null"); //NOI18N
72 } else if (event.getUser() == null) {
74 throw new NullPointerException("event.user is null"); //NOI18N
75 } else if (event.getUser().getUserId() == null) {
77 throw new NullPointerException("event.user.userId is null"); //NOI18N
78 } else if (event.getUser().getUserId() < 1) {
80 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
84 User registeredUser = event.getUser();
87 System.out.println(MessageFormat.format("UserIdValidator:afterRegistrationEvent: registeredUser={0}", registeredUser)); //NOI18N
90 UserIdValidator.cachedStatus.add(registeredUser.getUserId());
93 System.out.println("UserIdValidator:afterRegistrationEvent: EXIT!"); //NOI18N
97 public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
99 //this.getLogger().logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
101 // All accepted, required fields
102 String[] requiredFileds = {"userId"}; //NOI18N
104 // Pre-validation (example: not null, not a string, empty string ...)
105 super.preValidate(context, component, value, requiredFileds, false);
108 Long userId = (Long) value;
111 Boolean ifUserExists;
113 // Is a map entry there?
114 if (UserIdValidator.cachedStatus.contains(userId)) {
116 ifUserExists = Boolean.TRUE;
119 ifUserExists = this.userBean.ifUserIdExists(userId);
122 // Is the user id valid?
125 throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N
128 // Add to cache if valid
129 UserIdValidator.cachedStatus.add(userId);
132 //this.getLogger().logTrace("validate: EXIT!"); //NOI18N