]> git.mxchange.org Git - jjobs-war.git/blob
3a6b076fa239ceaeb0faa50159f09f83a3cf08ce
[jjobs-war.git] /
1 /*
2  * Copyright (C) 2016, 2017 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 Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jjobs.validator.emailaddress.basicdata;
18
19 import java.text.MessageFormat;
20 import java.util.regex.Pattern;
21 import javax.enterprise.inject.spi.CDI;
22 import javax.faces.application.FacesMessage;
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25 import javax.faces.validator.FacesValidator;
26 import javax.faces.validator.ValidatorException;
27 import org.mxchange.jcoreee.validator.string.BaseStringValidator;
28 import org.mxchange.jjobs.beans.business.basicdata.JobsBasicDataWebRequestBean;
29 import org.mxchange.jjobs.beans.business.basicdata.JobsBasicDataWebRequestController;
30
31 /**
32  * A validator for basic company data email address validation
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 @FacesValidator (value = "BasicDataEmailAddressValidator")
37 public class JobsBasicDataEmailAddressValidator extends BaseStringValidator {
38
39         /**
40          * Branch office backing bean
41          */
42         private static JobsBasicDataWebRequestController BASIC_DATA_CONTROLLER;
43
44         /**
45          * Email pattern
46          */
47         private static final String EMAIL_REGEX = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"; //NOI18N
48
49         /**
50          * Pattern matcher
51          */
52         private static final Pattern PATTERN_MATCHER = Pattern.compile(JobsBasicDataEmailAddressValidator.EMAIL_REGEX);
53
54         /**
55          * Serial number
56          */
57         private static final long serialVersionUID = 187_536_745_607_195L;
58
59         @Override
60         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
61                 // The required field
62                 final String[] requiredFields = {"emailAddress", "emailAddressRepeat", "resendEmailAddress"}; //NOI18N
63
64                 // Default is to reject empty email address fields
65                 Boolean allowEmptyValue = Boolean.FALSE;
66
67                 // Is attribute "allowEmptyValue" set?
68                 if (component.getAttributes().containsKey("allowEmptyValue")) { //NOI18N
69                         // Get attribute
70                         final Object attribute = component.getAttributes().get("allowEmptyValue"); //NOI18N
71
72                         // Make sure, it is Boolean as no String is accepted anymore
73                         if (!(attribute instanceof String)) {
74                                 // Not valid attribute, please use "true" or "false" (default)
75                                 throw new IllegalArgumentException("allowEmptyValue must be of type String. Please use \"true\" or \"false\" for f:attribute value."); //NOI18N
76                         }
77
78                         // Securely cast it
79                         allowEmptyValue = Boolean.parseBoolean((String) attribute);
80                 }
81
82                 // Pre-validation (example: not null, not a string, empty string ...)
83                 super.preValidate(context, component, value, requiredFields, allowEmptyValue);
84
85                 // Is the email address empty and allowed?
86                 if (null == value && allowEmptyValue) {
87                         // Then accept this here
88                         return;
89                 } else if (null == value) {
90                         // Abort here
91                         throw new ValidatorException(new FacesMessage("No empty email address allowed.")); //NOI18N
92                 }
93
94                 // Get string from object ... ;-)
95                 // @TODO Add IDN support (GNU lib?) Search for emailAddressRepeat
96                 final String emailAddress = String.valueOf(value).trim();
97
98                 // Checks if the email address matches a regex ("low-level" check)
99                 // @TODO Should also be done by <f:validatorRegex />)
100                 final boolean matches = PATTERN_MATCHER.matcher(emailAddress).matches(); //NOI18N
101
102                 // Is the email address valid?
103                 if (!matches) {
104                         // Generate message
105                         String message = MessageFormat.format("Email address {0} does not match regular expression.", emailAddress); //NOI18N
106
107                         // Not matching
108                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message));
109                 }
110
111                 // Get client id (aka form id)
112                 final String clientId = component.getClientId();
113
114                 // Is the instance there?
115                 if (null == BASIC_DATA_CONTROLLER) {
116                         // Get bean from CDI directly
117                         BASIC_DATA_CONTROLLER = CDI.current().select(JobsBasicDataWebRequestBean.class).get();
118                 }
119
120                 // Is it registered?
121                 final Boolean isRegistered = BASIC_DATA_CONTROLLER.isEmailAddressRegistered(emailAddress);
122
123                 // Is the email address already registered?
124                 if ((!clientId.endsWith("resendEmailAddress")) && (isRegistered)) { //NOI18N
125                         // Generate message
126                         final String message = MessageFormat.format("Email address {0} is already registered.", emailAddress); //NOI18N
127
128                         // No, then abort here
129                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
130                 } else if ((clientId.endsWith("resendEmailAddress")) && (!isRegistered)) { //NOI18N
131                         // Generate message
132                         final String message = MessageFormat.format("Email address {0} is not registered.", emailAddress); //NOI18N
133
134                         // No, then abort here
135                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
136                 }
137         }
138
139 }