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;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
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 long serialVersionUID = 12_869_569_314_764_690L;
51 @EJB (mappedName = "ejb/stateless-user")
52 private UserSessionBeanRemote userBean;
57 private final ConcurrentMap<Long, Boolean> cachedStatus;
62 public UserIdValidator () {
64 this.cachedStatus = new ConcurrentHashMap<>(0);
68 public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
70 //this.getLogger().logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
72 // All accepted, required fields
73 String[] requiredFileds = {"userId"}; //NOI18N
75 // Pre-validation (example: not null, not a string, empty string ...)
76 super.preValidate(context, component, value, requiredFileds, false);
79 Long userId = (Long) value;
82 Boolean ifUserExists = false;
84 // Is a map entry there?
85 if (this.cachedStatus.containsKey(userId)) {
87 ifUserExists = this.cachedStatus.get(userId);
90 ifUserExists = this.userBean.ifUserIdExists(userId);
93 this.cachedStatus.put(userId, ifUserExists);
96 // Is the address book id valid?
99 throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N
103 //this.getLogger().logTrace("validate: EXIT!"); //NOI18N
107 * Event fired when the user registration is complete
109 * @param event User registration event
111 public void afterRegistration (final @Observes UserRegisteredEvent event) {
112 // event should not be null
115 throw new NullPointerException("event is null"); //NOI18N
116 } else if (event.getUser() == null) {
118 throw new NullPointerException("event.user is null"); //NOI18N
119 } else if (event.getUser().getUserId() == null) {
121 throw new NullPointerException("event.user.userId is null"); //NOI18N
122 } else if (event.getUser().getUserId() < 1) {
124 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
128 User registeredUser = event.getUser();
131 this.cachedStatus.put(registeredUser.getUserId(), Boolean.TRUE);