]> git.mxchange.org Git - jjobs-war.git/blob
3d0651e205f983a8dd21ebd06c28f3f3d2799d54
[jjobs-war.git] /
1 /*
2  * Copyright (C) 2016 - 2018 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jjobs.validator.business.branchoffice;
18
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.jjobs.beans.business.branchoffice.list.JobsBranchOfficeListWebViewBean;
29 import org.mxchange.jjobs.beans.business.branchoffice.list.JobsBranchOfficeListWebViewController;
30
31 /**
32  * A validator for branch office email address validation
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 @FacesValidator (value = "BranchOfficeEmailAddressValidator")
37 public class JobsBranchOfficeEmailAddressValidator extends BaseStringValidator {
38
39         /**
40          * Branch office backing bean
41          */
42         private static JobsBranchOfficeListWebViewController BRANCH_OFFICE_LIST_CONTROLLER;
43
44         /**
45          * Email pattern
46          */
47         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
48
49         /**
50          * Pattern matcher
51          */
52         private static final Pattern PATTERN_MATCHER = Pattern.compile(JobsBranchOfficeEmailAddressValidator.EMAIL_REGEX);
53
54         /**
55          * Serial number
56          */
57         private static final long serialVersionUID = 187_536_745_607_196L;
58
59         /**
60          * Whether empty data is allowed
61          */
62         private Boolean allowEmptyRequiredData;
63
64         /**
65          * Default constructor
66          */
67         public JobsBranchOfficeEmailAddressValidator () {
68                 this.allowEmptyRequiredData = Boolean.FALSE;
69         }
70
71         /**
72          * Setter for allowEmptyRequiredData flag
73          * <p>
74          * @param allowEmptyRequiredData Whether empty values are allowed
75          */
76         public void setAllowEmptyRequiredData (final Boolean allowEmptyRequiredData) {
77                 this.allowEmptyRequiredData = allowEmptyRequiredData;
78         }
79
80         @Override
81         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
82                 // The required field
83                 final String[] requiredFields = {"emailAddress", "emailAddressRepeat", "resendEmailAddress"}; //NOI18N
84
85                 // Pre-validation (example: not null, not a string, empty string ...)
86                 super.preValidate(context, component, value, requiredFields, this.allowEmptyRequiredData);
87
88                 // Is the email address empty and allowed?
89                 if (null == value && this.allowEmptyRequiredData) {
90                         // Then accept this here
91                         return;
92                 } else if (null == value) {
93                         // Abort here
94                         throw new ValidatorException(new FacesMessage("No empty email address allowed.")); //NOI18N
95                 }
96
97                 // Get string from object ... ;-)
98                 // @TODO Add IDN support (GNU lib?) Search for emailAddressRepeat
99                 final String emailAddress = String.valueOf(value).trim();
100
101                 // Checks if the email address matches a regex ("low-level" check)
102                 // @TODO Should also be done by <f:validatorRegex />)
103                 final boolean matches = PATTERN_MATCHER.matcher(emailAddress).matches(); //NOI18N
104
105                 // Is the email address valid?
106                 if (!matches) {
107                         // Generate message
108                         String message = MessageFormat.format("Email address {0} does not match regular expression.", emailAddress); //NOI18N
109
110                         // Not matching
111                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message));
112                 }
113
114                 // Get client id (aka form id)
115                 final String clientId = component.getClientId();
116
117                 // Is the instance there?
118                 if (null == BRANCH_OFFICE_LIST_CONTROLLER) {
119                         // Get bean from CDI directly
120                         BRANCH_OFFICE_LIST_CONTROLLER = CDI.current().select(JobsBranchOfficeListWebViewBean.class).get();
121                 }
122
123                 // Is it registered?
124                 final Boolean isRegistered = BRANCH_OFFICE_LIST_CONTROLLER.isEmailAddressRegistered(emailAddress);
125
126                 // Is the email address already registered?
127                 if ((!clientId.endsWith("resendEmailAddress")) && (isRegistered)) { //NOI18N
128                         // Generate message
129                         final String message = MessageFormat.format("Email address {0} is already registered.", emailAddress); //NOI18N
130
131                         // No, then abort here
132                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
133                 } else if ((clientId.endsWith("resendEmailAddress")) && (!isRegistered)) { //NOI18N
134                         // Generate message
135                         final String message = MessageFormat.format("Email address {0} is not registered.", emailAddress); //NOI18N
136
137                         // No, then abort here
138                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
139                 }
140         }
141
142 }