]> git.mxchange.org Git - addressbook-lib.git/blob - src/org/mxchange/addressbook/validator/user/UserIdValidator.java
17f76a7e809bfba5ee67f6984ce2bd594846b405
[addressbook-lib.git] / src / org / mxchange / addressbook / validator / user / UserIdValidator.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.validator.user;
18
19 import java.text.MessageFormat;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
22 import javax.ejb.EJB;
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;
34
35 /**
36  * A validator for user ids
37  * <p>
38  * @author Roland Haeder
39  */
40 @FacesValidator (value = "UserIdValidator")
41 public class UserIdValidator extends BaseLongValidator implements Validator {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 12_869_569_314_764_690L;
47
48         /**
49          * Remote bean
50          */
51         @EJB (mappedName = "ejb/stateless-user")
52         private UserSessionBeanRemote userBean;
53
54         /**
55          * Cached user status
56          */
57         private static final ConcurrentMap<Long, Boolean> cachedStatus = new ConcurrentHashMap<>(0);
58
59         @Override
60         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
61                 // Trace message
62                 //this.getLogger().logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
63
64                 // All accepted, required fields
65                 String[] requiredFileds = {"userId"}; //NOI18N
66
67                 // Pre-validation (example: not null, not a string, empty string ...)
68                 super.preValidate(context, component, value, requiredFileds, false);
69
70                 // Cast value
71                 Long userId = (Long) value;
72
73                 // Define variable
74                 Boolean ifUserExists = false;
75
76                 // Is a map entry there?
77                 if (UserIdValidator.cachedStatus.containsKey(userId)) {
78                         // Get from cache
79                         ifUserExists = UserIdValidator.cachedStatus.get(userId);
80                 } else {
81                         // Get status
82                         ifUserExists = this.userBean.ifUserIdExists(userId);
83
84                         // Add to cache
85                         UserIdValidator.cachedStatus.put(userId, ifUserExists);
86                 }
87
88                 // Is the address book id valid?
89                 if (!ifUserExists) {
90                         // Is not valid
91                         throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N
92                 }
93
94                 // Trace message
95                 //this.getLogger().logTrace("validate: EXIT!"); //NOI18N
96         }
97
98         /**
99          * Event fired when the user registration is complete
100          * <p>
101          * @param event User registration event
102          */
103         public void afterRegistration (final @Observes UserRegisteredEvent event) {
104                 // event should not be null
105                 if (null == event) {
106                         // Throw NPE
107                         throw new NullPointerException("event is null"); //NOI18N
108                 } else if (event.getUser() == null) {
109                         // Throw NPE again
110                         throw new NullPointerException("event.user is null"); //NOI18N
111                 } else if (event.getUser().getUserId() == null) {
112                         // userId is null
113                         throw new NullPointerException("event.user.userId is null"); //NOI18N
114                 } else if (event.getUser().getUserId() < 1) {
115                         // Not avalid id
116                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
117                 }
118
119                 // Get user instance
120                 User registeredUser = event.getUser();
121
122                 // Update cache
123                 UserIdValidator.cachedStatus.put(registeredUser.getUserId(), Boolean.TRUE);
124         }
125 }