2 * Copyright (C) 2016 - 2018 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jjobs.validator.emailaddress.basicdata;
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 <<<<<<< HEAD:src/java/org/mxchange/jjobs/validator/emailaddress/basicdata/JobsBasicDataEmailAddressValidator.java
29 import org.mxchange.jjobs.beans.business.basicdata.JobsBasicDataWebRequestBean;
30 import org.mxchange.jjobs.beans.business.basicdata.JobsBasicDataWebRequestController;
32 import org.mxchange.jjobs.beans.business.basicdata.list.JobsBasicDataListWebViewBean;
33 import org.mxchange.jjobs.beans.business.basicdata.list.JobsBasicDataListWebViewController;
34 >>>>>>> 11ccdf2a4... Please cherry-pick::src/java/org/mxchange/jjobs/validator/emailaddress/basicdata/JobsBasicDataEmailAddressValidator.java
37 * A validator for basic company data email address validation
39 * @author Roland Häder<roland@mxchange.org>
41 @FacesValidator (value = "BasicDataEmailAddressValidator")
42 public class JobsBasicDataEmailAddressValidator extends BaseStringValidator {
45 * Branch office backing bean
47 <<<<<<< HEAD:src/java/org/mxchange/jjobs/validator/emailaddress/basicdata/JobsBasicDataEmailAddressValidator.java
48 private static JobsBasicDataWebRequestController BASIC_DATA_CONTROLLER;
50 private static JobsBasicDataListWebViewController BASIC_DATA_LIST_CONTROLLER;
51 >>>>>>> 11ccdf2a4... Please cherry-pick::src/java/org/mxchange/jjobs/validator/emailaddress/basicdata/JobsBasicDataEmailAddressValidator.java
56 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
61 private static final Pattern PATTERN_MATCHER = Pattern.compile(JobsBasicDataEmailAddressValidator.EMAIL_REGEX);
66 private static final long serialVersionUID = 187_536_745_607_195L;
69 * Whether empty data is allowed
71 private Boolean allowEmptyRequiredData;
76 public JobsBasicDataEmailAddressValidator () {
77 this.allowEmptyRequiredData = Boolean.FALSE;
81 * Setter for allowEmptyRequiredData flag
83 * @param allowEmptyRequiredData Whether empty values are allowed
85 public void setAllowEmptyRequiredData (final Boolean allowEmptyRequiredData) {
86 this.allowEmptyRequiredData = allowEmptyRequiredData;
90 public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
92 final String[] requiredFields = {"emailAddress", "emailAddressRepeat", "resendEmailAddress"}; //NOI18N
94 // Pre-validation (example: not null, not a string, empty string ...)
95 super.preValidate(context, component, value, requiredFields, this.allowEmptyRequiredData);
97 // Is the email address empty and allowed?
98 if (null == value && this.allowEmptyRequiredData) {
99 // Then accept this here
101 } else if (null == value) {
103 throw new ValidatorException(new FacesMessage("No empty email address allowed.")); //NOI18N
106 // Get string from object ... ;-)
107 // @TODO Add IDN support (GNU lib?) Search for emailAddressRepeat
108 final String emailAddress = String.valueOf(value).trim();
110 // Checks if the email address matches a regex ("low-level" check)
111 // @TODO Should also be done by <f:validatorRegex />)
112 final boolean matches = PATTERN_MATCHER.matcher(emailAddress).matches(); //NOI18N
114 // Is the email address valid?
117 String message = MessageFormat.format("Email address {0} does not match regular expression.", emailAddress); //NOI18N
120 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message));
123 // Get client id (aka form id)
124 final String clientId = component.getClientId();
126 // Is the instance there?
127 if (null == BASIC_DATA_LIST_CONTROLLER) {
128 // Get bean from CDI directly
129 <<<<<<< HEAD:src/java/org/mxchange/jjobs/validator/emailaddress/basicdata/JobsBasicDataEmailAddressValidator.java
130 BASIC_DATA_CONTROLLER = CDI.current().select(JobsBasicDataWebRequestBean.class).get();
132 BASIC_DATA_LIST_CONTROLLER = CDI.current().select(JobsBasicDataListWebViewBean.class).get();
133 >>>>>>> 11ccdf2a4... Please cherry-pick::src/java/org/mxchange/jjobs/validator/emailaddress/basicdata/JobsBasicDataEmailAddressValidator.java
137 final Boolean isRegistered = BASIC_DATA_LIST_CONTROLLER.isEmailAddressRegistered(emailAddress);
139 // Is the email address already registered?
140 if ((!clientId.endsWith("resendEmailAddress")) && (isRegistered)) { //NOI18N
142 final String message = MessageFormat.format("Email address {0} is already registered.", emailAddress); //NOI18N
144 // No, then abort here
145 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
146 } else if ((clientId.endsWith("resendEmailAddress")) && (!isRegistered)) { //NOI18N
148 final String message = MessageFormat.format("Email address {0} is not registered.", emailAddress); //NOI18N
150 // No, then abort here
151 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));