]> git.mxchange.org Git - jbonuscard-lib.git/blob - src/org/mxchange/addressbook/validator/user/UserIdValidator.java
no redirect target needed here and makes no sense, too
[jbonuscard-lib.git] / src / org / mxchange / addressbook / validator / user / UserIdValidator.java
1 /*
2  * Copyright (C) 2016 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.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.validator.FacesValidator;
27 import javax.faces.validator.Validator;
28 import javax.faces.validator.ValidatorException;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.jcoreee.validator.number.BaseLongValidator;
33 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
34 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
35 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
36 import org.mxchange.jusercore.model.user.User;
37 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
38
39 /**
40  * A validator for user ids
41  * <p>
42  * @author Roland Haeder<roland@mxchange.org>
43  */
44 @FacesValidator (value = "UserIdValidator")
45 public class UserIdValidator extends BaseLongValidator implements Validator {
46
47         /**
48          * Cached user status
49          */
50         private static final Set<Long> cachedStatus = new TreeSet<>();
51
52         /**
53          * Serial number
54          */
55         private static final long serialVersionUID = 12_869_569_314_764_690L;
56
57         /**
58          * Logger instance
59          */
60         @Log
61         private LoggerBeanLocal loggerBeanLocal;
62
63         /**
64          * Remote bean
65          */
66         private UserSessionBeanRemote userBean;
67
68         /**
69          * Initialization of this converter
70          */
71         public UserIdValidator () {
72                 // Try to get it
73                 try {
74                         // Get initial context
75                         Context context = new InitialContext();
76
77                         // Lookup logger
78                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
79
80                         // ... and user controller
81                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
82                 } catch (final NamingException ex) {
83                         // Continue to throw it
84                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
85                 }
86         }
87
88         /**
89          * Event fired when the user registration is complete
90          * <p>
91          * @param event User registration event
92          */
93         public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
94                 // Trace message
95                 this.loggerBeanLocal.logTrace(MessageFormat.format("UserIdValidator:afterRegistrationEvent: event={0} - CALLED!", event)); //NOI18N
96
97                 // event should not be null
98                 if (null == event) {
99                         // Throw NPE
100                         throw new NullPointerException("event is null"); //NOI18N
101                 } else if (event.getRegisteredUser() == null) {
102                         // Throw NPE again
103                         throw new NullPointerException("event.user is null"); //NOI18N
104                 } else if (event.getRegisteredUser().getUserId() == null) {
105                         // userId is null
106                         throw new NullPointerException("event.user.userId is null"); //NOI18N
107                 } else if (event.getRegisteredUser().getUserId() < 1) {
108                         // Not avalid id
109                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
110                 }
111
112                 // Get user instance
113                 User registeredUser = event.getRegisteredUser();
114
115                 // Debug message
116                 this.loggerBeanLocal.logDebug(MessageFormat.format("UserIdValidator:afterRegistrationEvent: registeredUser={0}", registeredUser)); //NOI18N
117
118                 // Update cache
119                 UserIdValidator.cachedStatus.add(registeredUser.getUserId());
120
121                 // Trace message
122                 this.loggerBeanLocal.logTrace("UserIdValidator:afterRegistrationEvent: EXIT!"); //NOI18N
123         }
124
125         @Override
126         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
127                 // Trace message
128                 this.loggerBeanLocal.logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
129
130                 // All accepted, required fields
131                 String[] requiredFields = {"userId"}; //NOI18N
132
133                 // Pre-validation (example: not null, not a string, empty string ...)
134                 super.preValidate(context, component, value, requiredFields, false);
135
136                 // Cast value
137                 Long userId = (Long) value;
138
139                 // Define variable
140                 Boolean ifUserExists;
141
142                 // Is a map entry there?
143                 if (UserIdValidator.cachedStatus.contains(userId)) {
144                         // Get from cache
145                         ifUserExists = Boolean.TRUE;
146                 } else {
147                         // Get status
148                         ifUserExists = this.userBean.ifUserIdExists(userId);
149                 }
150
151                 // Is the user id valid?
152                 if (!ifUserExists) {
153                         // Is not valid
154                         throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N
155                 }
156
157                 // Add to cache if valid
158                 UserIdValidator.cachedStatus.add(userId);
159
160                 // Trace message
161                 this.loggerBeanLocal.logTrace("validate: EXIT!"); //NOI18N
162         }
163
164 }