From cdeda058f858b094977852327032e5ccd2de3b90 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 25 Sep 2022 22:21:32 +0200 Subject: [PATCH] Please cherry-pick: - removed duplicate validator for branch office's email address - validators should check specific fields, not generic - arranged code a bit for better readability - renamed private method --- ...AdminBranchOfficeActionWebRequestBean.java | 11 +- ...ialsBranchOfficeEmailAddressValidator.java | 142 ------------------ ...ancialsBasicDataEmailAddressValidator.java | 2 +- ...ialsBranchOfficeEmailAddressValidator.java | 43 ++++-- ...cialsHeadquarterEmailAddressValidator.java | 2 +- 5 files changed, 39 insertions(+), 161 deletions(-) delete mode 100644 src/java/org/mxchange/jfinancials/validator/business/branchoffice/FinancialsBranchOfficeEmailAddressValidator.java diff --git a/src/java/org/mxchange/jfinancials/beans/business/branchoffice/action/FinancialsAdminBranchOfficeActionWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/business/branchoffice/action/FinancialsAdminBranchOfficeActionWebRequestBean.java index 6fc0eee9..351f08a2 100644 --- a/src/java/org/mxchange/jfinancials/beans/business/branchoffice/action/FinancialsAdminBranchOfficeActionWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/business/branchoffice/action/FinancialsAdminBranchOfficeActionWebRequestBean.java @@ -257,7 +257,7 @@ public class FinancialsAdminBranchOfficeActionWebRequestBean extends BaseFinanci final BranchOffice branchOffice = this.createBranchOffice(); // Is the branch office not created yet? - if (this.isBranchOfficeCreatedByRequiredData(branchOffice)) { + if (this.isBranchOfficeCreatedBySameAddress(branchOffice)) { // Then show proper faces message this.showFacesMessage("form-admin-add-branch-office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED", FacesMessage.SEVERITY_WARN); //NOI18N return; @@ -1076,7 +1076,12 @@ public class FinancialsAdminBranchOfficeActionWebRequestBean extends BaseFinanci */ private OpeningTime createOpeningTimes () { // Create new openingTime instance - final OpeningTime openingTime = new BusinessOpeningTime(this.getOpeningEndDay(), this.getOpeningEndTime(), this.getOpeningStartDay(), this.getOpeningStartTime()); + final OpeningTime openingTime = new BusinessOpeningTime( + this.getOpeningEndDay(), + this.getOpeningEndTime(), + this.getOpeningStartDay(), + this.getOpeningStartTime() + ); // Return fully prepared instance return openingTime; @@ -1092,7 +1097,7 @@ public class FinancialsAdminBranchOfficeActionWebRequestBean extends BaseFinanci *

* @return Whether the address has been found */ - private boolean isBranchOfficeCreatedByRequiredData (final BranchOffice branchOffice) { + private boolean isBranchOfficeCreatedBySameAddress (final BranchOffice branchOffice) { // Get full list from other bean final List branchOffices = this.branchOfficeListController.getAllBranchOffices(); diff --git a/src/java/org/mxchange/jfinancials/validator/business/branchoffice/FinancialsBranchOfficeEmailAddressValidator.java b/src/java/org/mxchange/jfinancials/validator/business/branchoffice/FinancialsBranchOfficeEmailAddressValidator.java deleted file mode 100644 index fb3d5b6a..00000000 --- a/src/java/org/mxchange/jfinancials/validator/business/branchoffice/FinancialsBranchOfficeEmailAddressValidator.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright (C) 2016 - 2022 Free Software Foundation - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.jfinancials.validator.business.branchoffice; - -import java.text.MessageFormat; -import java.util.regex.Pattern; -import javax.enterprise.inject.spi.CDI; -import javax.faces.application.FacesMessage; -import javax.faces.component.UIComponent; -import javax.faces.context.FacesContext; -import javax.faces.validator.FacesValidator; -import javax.faces.validator.ValidatorException; -import org.mxchange.jcoreee.validator.string.BaseStringValidator; -import org.mxchange.jfinancials.beans.business.branchoffice.list.FinancialsBranchOfficeListWebViewBean; -import org.mxchange.jfinancials.beans.business.branchoffice.list.FinancialsBranchOfficeListWebViewController; - -/** - * A validator for branch office email address validation - *

- * @author Roland Häder - */ -@FacesValidator (value = "BranchOfficeEmailAddressValidator") -public class FinancialsBranchOfficeEmailAddressValidator extends BaseStringValidator { - - /** - * Branch office backing bean - */ - private static FinancialsBranchOfficeListWebViewController BRANCH_OFFICE_LIST_CONTROLLER; - - /** - * Email pattern - */ - 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 - - /** - * Pattern matcher - */ - private static final Pattern PATTERN_MATCHER = Pattern.compile(FinancialsBranchOfficeEmailAddressValidator.EMAIL_REGEX); - - /** - * Serial number - */ - private static final long serialVersionUID = 187_536_745_607_196L; - - /** - * Whether empty data is allowed - */ - private Boolean allowEmptyRequiredData; - - /** - * Default constructor - */ - public FinancialsBranchOfficeEmailAddressValidator () { - this.allowEmptyRequiredData = Boolean.FALSE; - } - - /** - * Setter for allowEmptyRequiredData flag - *

- * @param allowEmptyRequiredData Whether empty values are allowed - */ - public void setAllowEmptyRequiredData (final Boolean allowEmptyRequiredData) { - this.allowEmptyRequiredData = allowEmptyRequiredData; - } - - @Override - public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException { - // The required field - final String[] requiredFields = {"emailAddress", "emailAddressRepeat", "resendEmailAddress"}; //NOI18N - - // Pre-validation (example: not null, not a string, empty string ...) - super.preValidate(context, component, value, requiredFields, this.allowEmptyRequiredData); - - // Is the email address empty and allowed? - if (null == value && this.allowEmptyRequiredData) { - // Then accept this here - return; - } else if (null == value) { - // Abort here - throw new ValidatorException(new FacesMessage("No empty email address allowed.")); //NOI18N - } - - // Get string from object ... ;-) - // @TODO Add IDN support (GNU lib?) Search for emailAddressRepeat - final String emailAddress = String.valueOf(value).trim(); - - // Checks if the email address matches a regex ("low-level" check) - // @TODO Should also be done by ) - final boolean matches = PATTERN_MATCHER.matcher(emailAddress).matches(); //NOI18N - - // Is the email address valid? - if (!matches) { - // Generate message - String message = MessageFormat.format("Email address {0} does not match regular expression.", emailAddress); //NOI18N - - // Not matching - throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); - } - - // Get client id (aka form id) - final String clientId = component.getClientId(); - - // Is the instance there? - if (null == BRANCH_OFFICE_LIST_CONTROLLER) { - // Get bean from CDI directly - BRANCH_OFFICE_LIST_CONTROLLER = CDI.current().select(FinancialsBranchOfficeListWebViewBean.class).get(); - } - - // Is it registered? - final Boolean isRegistered = BRANCH_OFFICE_LIST_CONTROLLER.isEmailAddressRegistered(emailAddress); - - // Is the email address already registered? - if ((!clientId.endsWith("resendEmailAddress")) && (isRegistered)) { //NOI18N - // Generate message - final String message = MessageFormat.format("Email address {0} is already registered.", emailAddress); //NOI18N - - // No, then abort here - throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message)); - } else if ((clientId.endsWith("resendEmailAddress")) && (!isRegistered)) { //NOI18N - // Generate message - final String message = MessageFormat.format("Email address {0} is not registered.", emailAddress); //NOI18N - - // No, then abort here - throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message)); - } - } - -} diff --git a/src/java/org/mxchange/jfinancials/validator/emailaddress/basicdata/FinancialsBasicDataEmailAddressValidator.java b/src/java/org/mxchange/jfinancials/validator/emailaddress/basicdata/FinancialsBasicDataEmailAddressValidator.java index 0ceb3699..883ba286 100644 --- a/src/java/org/mxchange/jfinancials/validator/emailaddress/basicdata/FinancialsBasicDataEmailAddressValidator.java +++ b/src/java/org/mxchange/jfinancials/validator/emailaddress/basicdata/FinancialsBasicDataEmailAddressValidator.java @@ -68,7 +68,7 @@ public class FinancialsBasicDataEmailAddressValidator extends BaseStringValidato } // The required field - final String[] requiredFields = {"emailAddress", "emailAddressRepeat", "resendEmailAddress"}; //NOI18N + final String[] requiredFields = {"companyEmailAddress"}; //NOI18N // Pre-validation (example: not null, not a string, empty string ...) super.preValidate(context, component, value, requiredFields, Boolean.TRUE); diff --git a/src/java/org/mxchange/jfinancials/validator/emailaddress/branchoffice/FinancialsBranchOfficeEmailAddressValidator.java b/src/java/org/mxchange/jfinancials/validator/emailaddress/branchoffice/FinancialsBranchOfficeEmailAddressValidator.java index fbd91350..6a9aee4a 100644 --- a/src/java/org/mxchange/jfinancials/validator/emailaddress/branchoffice/FinancialsBranchOfficeEmailAddressValidator.java +++ b/src/java/org/mxchange/jfinancials/validator/emailaddress/branchoffice/FinancialsBranchOfficeEmailAddressValidator.java @@ -54,29 +54,44 @@ public class FinancialsBranchOfficeEmailAddressValidator extends BaseStringValid /** * Serial number */ - private static final long serialVersionUID = 187_536_745_607_195L; + private static final long serialVersionUID = 187_536_745_607_196L; + + /** + * Whether empty data is allowed + */ + private Boolean allowEmptyRequiredData; + + /** + * Default constructor + */ + public FinancialsBranchOfficeEmailAddressValidator () { + this.allowEmptyRequiredData = Boolean.FALSE; + } + + /** + * Setter for allowEmptyRequiredData flag + *

+ * @param allowEmptyRequiredData Whether empty values are allowed + */ + public void setAllowEmptyRequiredData (final Boolean allowEmptyRequiredData) { + this.allowEmptyRequiredData = allowEmptyRequiredData; + } @Override public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException { - // Validate parameter - if (null == context) { - // Throw NPE - throw new NullPointerException("Parameter context is null"); //NOI18N - } else if (null == component) { - // Throw NPE again - throw new NullPointerException("Parameter component is null"); //NOI18N - } - // The required field - final String[] requiredFields = {"emailAddress", "emailAddressRepeat", "resendEmailAddress"}; //NOI18N + final String[] requiredFields = {"branchEmailAddress"}; //NOI18N // Pre-validation (example: not null, not a string, empty string ...) - super.preValidate(context, component, value, requiredFields, Boolean.TRUE); + super.preValidate(context, component, value, requiredFields, this.allowEmptyRequiredData); - // Is the email address empty? - if (null == value) { + // Is the email address empty and allowed? + if (null == value && this.allowEmptyRequiredData) { // Then accept this here return; + } else if (null == value) { + // Abort here + throw new ValidatorException(new FacesMessage("No empty email address allowed.")); //NOI18N } // Get string from object ... ;-) diff --git a/src/java/org/mxchange/jfinancials/validator/emailaddress/headquarter/FinancialsHeadquarterEmailAddressValidator.java b/src/java/org/mxchange/jfinancials/validator/emailaddress/headquarter/FinancialsHeadquarterEmailAddressValidator.java index 75225cce..ccc98fc3 100644 --- a/src/java/org/mxchange/jfinancials/validator/emailaddress/headquarter/FinancialsHeadquarterEmailAddressValidator.java +++ b/src/java/org/mxchange/jfinancials/validator/emailaddress/headquarter/FinancialsHeadquarterEmailAddressValidator.java @@ -68,7 +68,7 @@ public class FinancialsHeadquarterEmailAddressValidator extends BaseStringValida } // The required field - final String[] requiredFields = {"emailAddress", "emailAddressRepeat", "resendEmailAddress"}; //NOI18N + final String[] requiredFields = {"headquarterEmailAddress"}; //NOI18N // Pre-validation (example: not null, not a string, empty string ...) super.preValidate(context, component, value, requiredFields, Boolean.TRUE); -- 2.39.5