From 1ce5ac9419f86cc0e71ca35b1ee7ffb319f49edc Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
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
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Signed-off-by: Roland Häder <roland@mxchange.org>
---
 ...AdminBranchOfficeActionWebRequestBean.java |  11 +-
 ...JobsBranchOfficeEmailAddressValidator.java | 142 ------------------
 .../JobsBasicDataEmailAddressValidator.java   |   2 +-
 ...JobsBranchOfficeEmailAddressValidator.java |  43 ++++--
 .../JobsHeadquarterEmailAddressValidator.java |   2 +-
 5 files changed, 39 insertions(+), 161 deletions(-)
 delete mode 100644 src/java/org/mxchange/jjobs/validator/business/branchoffice/JobsBranchOfficeEmailAddressValidator.java

diff --git a/src/java/org/mxchange/jjobs/beans/business/department/action/JobsAdminBranchOfficeActionWebRequestBean.java b/src/java/org/mxchange/jjobs/beans/business/department/action/JobsAdminBranchOfficeActionWebRequestBean.java
index 2556ad6b..59c37566 100644
--- a/src/java/org/mxchange/jjobs/beans/business/department/action/JobsAdminBranchOfficeActionWebRequestBean.java
+++ b/src/java/org/mxchange/jjobs/beans/business/department/action/JobsAdminBranchOfficeActionWebRequestBean.java
@@ -257,7 +257,7 @@ public class JobsAdminBranchOfficeActionWebRequestBean extends BaseJobsBean impl
 		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 JobsAdminBranchOfficeActionWebRequestBean extends BaseJobsBean impl
 	 */
 	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 JobsAdminBranchOfficeActionWebRequestBean extends BaseJobsBean impl
 	 * <p>
 	 * @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<BranchOffice> branchOffices = this.branchOfficeListController.getAllBranchOffices();
 
diff --git a/src/java/org/mxchange/jjobs/validator/business/branchoffice/JobsBranchOfficeEmailAddressValidator.java b/src/java/org/mxchange/jjobs/validator/business/branchoffice/JobsBranchOfficeEmailAddressValidator.java
deleted file mode 100644
index a6409a33..00000000
--- a/src/java/org/mxchange/jjobs/validator/business/branchoffice/JobsBranchOfficeEmailAddressValidator.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 <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.jjobs.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.jjobs.beans.business.branchoffice.list.JobsBranchOfficeListWebViewBean;
-import org.mxchange.jjobs.beans.business.branchoffice.list.JobsBranchOfficeListWebViewController;
-
-/**
- * A validator for branch office email address validation
- * <p>
- * @author Roland Häder<roland@mxchange.org>
- */
-@FacesValidator (value = "BranchOfficeEmailAddressValidator")
-public class JobsBranchOfficeEmailAddressValidator extends BaseStringValidator {
-
-	/**
-	 * Branch office backing bean
-	 */
-	private static JobsBranchOfficeListWebViewController 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(JobsBranchOfficeEmailAddressValidator.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 JobsBranchOfficeEmailAddressValidator () {
-		this.allowEmptyRequiredData = Boolean.FALSE;
-	}
-
-	/**
-	 * Setter for allowEmptyRequiredData flag
-	 * <p>
-	 * @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 <f:validatorRegex />)
-		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(JobsBranchOfficeListWebViewBean.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/jjobs/validator/emailaddress/basicdata/JobsBasicDataEmailAddressValidator.java b/src/java/org/mxchange/jjobs/validator/emailaddress/basicdata/JobsBasicDataEmailAddressValidator.java
index 8edef4d2..aec9e121 100644
--- a/src/java/org/mxchange/jjobs/validator/emailaddress/basicdata/JobsBasicDataEmailAddressValidator.java
+++ b/src/java/org/mxchange/jjobs/validator/emailaddress/basicdata/JobsBasicDataEmailAddressValidator.java
@@ -68,7 +68,7 @@ public class JobsBasicDataEmailAddressValidator extends BaseStringValidator {
 		}
 
 		// 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/jjobs/validator/emailaddress/branchoffice/JobsBranchOfficeEmailAddressValidator.java b/src/java/org/mxchange/jjobs/validator/emailaddress/branchoffice/JobsBranchOfficeEmailAddressValidator.java
index 4ea639ae..2baba45e 100644
--- a/src/java/org/mxchange/jjobs/validator/emailaddress/branchoffice/JobsBranchOfficeEmailAddressValidator.java
+++ b/src/java/org/mxchange/jjobs/validator/emailaddress/branchoffice/JobsBranchOfficeEmailAddressValidator.java
@@ -54,29 +54,44 @@ public class JobsBranchOfficeEmailAddressValidator extends BaseStringValidator {
 	/**
 	 * 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
+	 * <p>
+	 * @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/jjobs/validator/emailaddress/headquarter/JobsHeadquarterEmailAddressValidator.java b/src/java/org/mxchange/jjobs/validator/emailaddress/headquarter/JobsHeadquarterEmailAddressValidator.java
index c573e176..cda7086a 100644
--- a/src/java/org/mxchange/jjobs/validator/emailaddress/headquarter/JobsHeadquarterEmailAddressValidator.java
+++ b/src/java/org/mxchange/jjobs/validator/emailaddress/headquarter/JobsHeadquarterEmailAddressValidator.java
@@ -68,7 +68,7 @@ public class JobsHeadquarterEmailAddressValidator extends BaseStringValidator {
 		}
 
 		// 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