]> git.mxchange.org Git - jfinancials-lib.git/blob - src/org/mxchange/addressbook/validator/user/UserIdValidator.java
renamed number -> userId to make it more clear + updated jar(s)
[jfinancials-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 final ConcurrentMap<Long, Boolean> cachedStatus;
58
59         /**
60          * Default constructor
61          */
62         public UserIdValidator () {
63                 // Init map
64                 this.cachedStatus = new ConcurrentHashMap<>(0);
65         }
66
67         @Override
68         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
69                 // Trace message
70                 //this.getLogger().logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
71
72                 // All accepted, required fields
73                 String[] requiredFileds = {"userId"}; //NOI18N
74
75                 // Pre-validation (example: not null, not a string, empty string ...)
76                 super.preValidate(context, component, value, requiredFileds, false);
77
78                 // Cast value
79                 Long userId = (Long) value;
80
81                 // Define variable
82                 Boolean ifUserExists = false;
83
84                 // Is a map entry there?
85                 if (this.cachedStatus.containsKey(userId)) {
86                         // Get from cache
87                         ifUserExists = this.cachedStatus.get(userId);
88                 } else {
89                         // Get status
90                         ifUserExists = this.userBean.ifUserIdExists(userId);
91
92                         // Add to cache
93                         this.cachedStatus.put(userId, ifUserExists);
94                 }
95
96                 // Is the address book id valid?
97                 if (!ifUserExists) {
98                         // Is not valid
99                         throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N
100                 }
101
102                 // Trace message
103                 //this.getLogger().logTrace("validate: EXIT!"); //NOI18N
104         }
105
106         /**
107          * Event fired when the user registration is complete
108          * <p>
109          * @param event User registration event
110          */
111         public void afterRegistration (final @Observes UserRegisteredEvent event) {
112                 // event should not be null
113                 if (null == event) {
114                         // Throw NPE
115                         throw new NullPointerException("event is null"); //NOI18N
116                 } else if (event.getUser() == null) {
117                         // Throw NPE again
118                         throw new NullPointerException("event.user is null"); //NOI18N
119                 } else if (event.getUser().getUserId() == null) {
120                         // userId is null
121                         throw new NullPointerException("event.user.userId is null"); //NOI18N
122                 } else if (event.getUser().getUserId() < 1) {
123                         // Not avalid id
124                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
125                 }
126
127                 // Get user instance
128                 User registeredUser = event.getUser();
129
130                 // Update cache
131                 this.cachedStatus.put(registeredUser.getUserId(), Boolean.TRUE);
132         }
133 }