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