--- /dev/null
+/*
+ * Copyright (C) 2016 Roland Haeder
+ *
+ * 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.pizzaapplication.validator.emailaddress;
+
+import java.text.MessageFormat;
+import java.util.regex.Pattern;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.FacesValidator;
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
+import javax.faces.view.facelets.FaceletException;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
+import org.mxchange.jcoreee.validator.string.BaseStringValidator;
+
+/**
+ * A validator for email address validation
+ * <p>
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+@FacesValidator ("EmailAddressValidator")
+public class PizzaEmailAddressValidator extends BaseStringValidator implements Validator {
+
+ /**
+ * Serial number
+ */
+ private static final long serialVersionUID = 187_536_745_607_192L;
+
+ /**
+ * Contact session bean
+ */
+ private final ContactSessionBeanRemote contactBean;
+
+ /**
+ * Default constructor
+ */
+ public PizzaEmailAddressValidator () {
+ // Try it
+ try {
+ // Get initial context
+ Context context = new InitialContext();
+
+ // Try to lookup
+ this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
+ } catch (final NamingException e) {
+ // Throw again
+ throw new FaceletException(e);
+ }
+ }
+
+ @Override
+ public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
+ // Trace message
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
+
+ // The required field
+ String[] requiredFields = {"emailAddress", "emailAddressRepeat"}; //NOI18N
+
+ // Pre-validation (example: not null, not a string, empty string ...)
+ super.preValidate(context, component, value, requiredFields, false);
+
+ // Get string from object ... ;-)
+ String emailAddress = String.valueOf(value);
+
+ // Checks if the email address matches a regex ("low-level" check, should also be done by <f:validatorRegex />)
+ boolean matches = Pattern.matches("^([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})(\\]?)$", emailAddress); //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_ERROR, message, message));
+ }
+
+ // Is the email address already registered?
+ if (this.contactBean.isEmailAddressRegistered(emailAddress)) {
+ // Generate message
+ 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));
+ }
+
+ // Trace message
+ //* NOISY-DEBUG: */ System.out.println("validate: EXIT!"); //NOI18N
+ }
+
+}