2 * Copyright (C) 2016 - 2020 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.jfinancials.validator.emailaddress.headquarter;
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.jfinancials.beans.business.headquarter.list.FinancialsHeadquarterListWebViewBean;
29 import org.mxchange.jfinancials.beans.business.headquarter.list.FinancialsHeadquarterListWebViewController;
32 * A validator for headquarter's email address validation
34 * @author Roland Häder<roland@mxchange.org>
36 @FacesValidator (value = "HeadquarterEmailAddressValidator")
37 public class FinancialsHeadquarterEmailAddressValidator extends BaseStringValidator {
42 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
45 * Headquarter backing bean
47 private static FinancialsHeadquarterListWebViewController HEADQUARTER_LIST_CONTROLLER;
52 private static final Pattern PATTERN_MATCHER = Pattern.compile(FinancialsHeadquarterEmailAddressValidator.EMAIL_REGEX);
57 private static final long serialVersionUID = 187_536_745_607_194L;
60 * Whether empty data is allowed
62 private Boolean allowEmptyRequiredData;
67 public FinancialsHeadquarterEmailAddressValidator () {
68 // Default is not allowed
69 this.allowEmptyRequiredData = Boolean.FALSE;
73 * Setter for allowEmptyRequiredData flag
75 * @param allowEmptyRequiredData Whether empty values are allowed
77 public void setAllowEmptyRequiredData (final Boolean allowEmptyRequiredData) {
78 this.allowEmptyRequiredData = allowEmptyRequiredData;
82 public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
84 final String[] requiredFields = {"emailAddress", "emailAddressRepeat", "resendEmailAddress"}; //NOI18N
86 // Pre-validation (example: not null, not a string, empty string ...)
87 super.preValidate(context, component, value, requiredFields, this.allowEmptyRequiredData);
89 // Is the email address empty and allowed?
90 if (null == value && this.allowEmptyRequiredData) {
91 // Then accept this here
93 } else if (null == value) {
95 throw new ValidatorException(new FacesMessage("No empty email address allowed.")); //NOI18N
98 // Get string from object ... ;-)
99 // @TODO Add IDN support (GNU lib?) Search for emailAddressRepeat
100 final String emailAddress = String.valueOf(value).trim();
102 // Checks if the email address matches a regex ("low-level" check)
103 // @TODO Should also be done by <f:validatorRegex />)
104 final boolean matches = PATTERN_MATCHER.matcher(emailAddress).matches(); //NOI18N
106 // Is the email address valid?
109 String message = MessageFormat.format("Email address {0} does not match regular expression.", emailAddress); //NOI18N
112 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message));
115 // Get client id (aka form id)
116 final String clientId = component.getClientId();
118 // Is the instance there?
119 if (null == HEADQUARTER_LIST_CONTROLLER) {
120 // Get bean from CDI directly
121 HEADQUARTER_LIST_CONTROLLER = CDI.current().select(FinancialsHeadquarterListWebViewBean.class).get();
125 final Boolean isRegistered = HEADQUARTER_LIST_CONTROLLER.isEmailAddressRegistered(emailAddress);
127 // Is the email address already registered?
128 if ((!clientId.endsWith("resendEmailAddress")) && (isRegistered)) { //NOI18N
130 final String message = MessageFormat.format("Email address {0} is already registered.", emailAddress); //NOI18N
132 // No, then abort here
133 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
134 } else if ((clientId.endsWith("resendEmailAddress")) && (!isRegistered)) { //NOI18N
136 final String message = MessageFormat.format("Email address {0} is not registered.", emailAddress); //NOI18N
138 // No, then abort here
139 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));