]> git.mxchange.org Git - jfinancials-lib.git/blob - src/org/mxchange/addressbook/validator/user/UserIdValidator.java
Updated copyright year
[jfinancials-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.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          * Cached user status
45          */
46         private static final Set<Long> cachedStatus = new TreeSet<>();
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 12_869_569_314_764_690L;
52
53         /**
54          * Remote bean
55          */
56         @EJB (mappedName = "ejb/stateless-user")
57         private UserSessionBeanRemote userBean;
58
59         /**
60          * Event fired when the user registration is complete
61          * <p>
62          * @param event User registration event
63          */
64         public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
65                 // Trace message
66                 System.out.println(MessageFormat.format("UserIdValidator:afterRegistrationEvent: event={0} - CALLED!", event)); //NOI18N
67
68                 // event should not be null
69                 if (null == event) {
70                         // Throw NPE
71                         throw new NullPointerException("event is null"); //NOI18N
72                 } else if (event.getUser() == null) {
73                         // Throw NPE again
74                         throw new NullPointerException("event.user is null"); //NOI18N
75                 } else if (event.getUser().getUserId() == null) {
76                         // userId is null
77                         throw new NullPointerException("event.user.userId is null"); //NOI18N
78                 } else if (event.getUser().getUserId() < 1) {
79                         // Not avalid id
80                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUser(), event.getUser().getUserId())); //NOI18N
81                 }
82
83                 // Get user instance
84                 User registeredUser = event.getUser();
85
86                 // Debug message
87                 System.out.println(MessageFormat.format("UserIdValidator:afterRegistrationEvent: registeredUser={0}", registeredUser)); //NOI18N
88
89                 // Update cache
90                 UserIdValidator.cachedStatus.add(registeredUser.getUserId());
91
92                 // Trace message
93                 System.out.println("UserIdValidator:afterRegistrationEvent: EXIT!"); //NOI18N
94         }
95
96         @Override
97         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
98                 // Trace message
99                 //this.getLogger().logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
100
101                 // All accepted, required fields
102                 String[] requiredFileds = {"userId"}; //NOI18N
103
104                 // Pre-validation (example: not null, not a string, empty string ...)
105                 super.preValidate(context, component, value, requiredFileds, false);
106
107                 // Cast value
108                 Long userId = (Long) value;
109
110                 // Define variable
111                 Boolean ifUserExists;
112
113                 // Is a map entry there?
114                 if (UserIdValidator.cachedStatus.contains(userId)) {
115                         // Get from cache
116                         ifUserExists = Boolean.TRUE;
117                 } else {
118                         // Get status
119                         ifUserExists = this.userBean.ifUserIdExists(userId);
120                 }
121
122                 // Is the user id valid?
123                 if (!ifUserExists) {
124                         // Is not valid
125                         throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N
126                 }
127
128                 // Add to cache if valid
129                 UserIdValidator.cachedStatus.add(userId);
130
131                 // Trace message
132                 //this.getLogger().logTrace("validate: EXIT!"); //NOI18N
133         }
134 }