]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/validator/user/JobsUserIdValidator.java
no need for value='' here, only the pure value is okay
[jjobs-war.git] / src / java / org / mxchange / jjobs / validator / user / JobsUserIdValidator.java
1 /*
2  * Copyright (C) 2016 Roland Häder
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.jjobs.validator.user;
18
19 import java.text.MessageFormat;
20 import java.util.Set;
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;
37
38 /**
39  * A validator for user ids
40  * <p>
41  * @author Roland Häder<roland@mxchange.org>
42  */
43 @FacesValidator ("UserIdValidator")
44 public class JobsUserIdValidator extends BaseLongValidator implements Validator {
45
46         /**
47          * Cached user status
48          */
49         private static final Set<Long> cachedStatus = new TreeSet<>();
50
51         /**
52          * Serial number
53          */
54         private static final long serialVersionUID = 12_869_569_314_764_690L;
55
56         /**
57          * Remote bean
58          */
59         private UserSessionBeanRemote userBean;
60
61         /**
62          * Initialization of this converter
63          */
64         public JobsUserIdValidator () {
65                 // Try to get it
66                 try {
67                         // Get initial context
68                         Context context = new InitialContext();
69
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
75                 }
76         }
77
78         /**
79          * Event fired when the user registration is complete
80          * <p>
81          * @param event User registration event
82          */
83         public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
84                 // Trace message
85                 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("UserIdValidator:afterRegistrationEvent: event={0} - CALLED!", event)); //NOI18N
86
87                 // event should not be null
88                 if (null == event) {
89                         // Throw NPE
90                         throw new NullPointerException("event is null"); //NOI18N
91                 } else if (event.getRegisteredUser() == null) {
92                         // Throw NPE again
93                         throw new NullPointerException("event.user is null"); //NOI18N
94                 } else if (event.getRegisteredUser().getUserId() == null) {
95                         // userId is null
96                         throw new NullPointerException("event.user.userId is null"); //NOI18N
97                 } else if (event.getRegisteredUser().getUserId() < 1) {
98                         // Not avalid id
99                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
100                 }
101
102                 // Get user instance
103                 User registeredUser = event.getRegisteredUser();
104
105                 // Debug message
106                 // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("UserIdValidator:afterRegistrationEvent: registeredUser={0}", registeredUser)); //NOI18N
107
108                 // Update cache
109                 JobsUserIdValidator.cachedStatus.add(registeredUser.getUserId());
110
111                 // Trace message
112                 // NOISY-DEBUG: this.loggerBeanLocal.logTrace("UserIdValidator:afterRegistrationEvent: EXIT!"); //NOI18N
113         }
114
115         @Override
116         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
117                 // Trace message
118                 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
119
120                 // All accepted, required fields
121                 String[] requiredFields = {"userId"}; //NOI18N
122
123                 // Pre-validation (example: not null, not a string, empty string ...)
124                 super.preValidate(context, component, value, requiredFields, false);
125
126                 // Cast value
127                 Long userId = (Long) value;
128
129                 // Define variable
130                 Boolean ifUserExists;
131
132                 // Is a map entry there?
133                 if (JobsUserIdValidator.cachedStatus.contains(userId)) {
134                         // Get from cache
135                         ifUserExists = Boolean.TRUE;
136                 } else {
137                         // Get status
138                         ifUserExists = this.userBean.ifUserIdExists(userId);
139                 }
140
141                 // Is the user id valid?
142                 if (!ifUserExists) {
143                         // Is not valid
144                         throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N
145                 }
146
147                 // Add to cache if valid
148                 JobsUserIdValidator.cachedStatus.add(userId);
149
150                 // Trace message
151                 // NOISY-DEBUG: this.loggerBeanLocal.logTrace("validate: EXIT!"); //NOI18N
152         }
153
154 }