]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java
59871c345fd772039a72513cd14a3023cf674f3a
[jcore-utils.git] / src / org / mxchange / jcoreee / validator / string / BaseStringValidator.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcoreee.validator.string;
18
19 import javax.faces.application.FacesMessage;
20 import javax.faces.component.UIComponent;
21 import javax.faces.context.FacesContext;
22 import javax.faces.validator.ValidatorException;
23 import org.mxchange.jcoreee.validator.BaseObjectValidator;
24
25 /**
26  * A general string validation class. You normally want to inherit from this
27  * class for many form fields, e.g. surname, street name, city name and such.
28  *
29  * @author Roland Haeder<roland@mxchange.org>
30  */
31 public abstract class BaseStringValidator extends BaseObjectValidator {
32
33         @Override
34         protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
35                 // Trace message
36                 //this.getLogger().logTrace(MessageFormat.format("preValidate: context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
37
38                 // Pre-validate (e.g. on null)
39                 super.preValidate(context, component, value, requiredFields);
40
41                 // Get client id and init message + key
42                 String clientId = component.getClientId();
43                 FacesMessage facesMessage = null;
44                 String errKey;
45
46                 // So far all fine, no check if the field is fine
47                 for (final String field : requiredFields) {
48                         // Debug message
49                         //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
50
51                         // Is it the same?
52                         if (clientId.endsWith(field)) {
53                                 // Compare value's type
54                                 if (!(value instanceof String)) {
55                                         // Value is empty
56                                         errKey = String.format("ERROR_%s_IS_NOT_STRING", field.toUpperCase()); //NOI18N
57
58                                         facesMessage = new FacesMessage(getMessageStringFromKey(errKey));
59                                 }
60
61                                 // Cast to string
62                                 String str = (String) value;
63
64                                 // Is it empty?
65                                 if (str.isEmpty()) {
66                                         // Value is empty
67                                         errKey = String.format("ERROR_%s_IS_EMPTY", field.toUpperCase()); //NOI18N
68
69                                         facesMessage = new FacesMessage(getMessageStringFromKey(errKey));
70                                 }
71                         }
72                 }
73
74                 // Debug message
75                 //this.getLogger().logDebug(MessageFormat.format("preValidate: facesMessage={0}", facesMessage)); //NOI18N
76                 // Is it not null?
77                 if (null != facesMessage) {
78                         throw new ValidatorException(facesMessage);
79                 }
80
81                 // Trace message
82                 //this.getLogger().logTrace("preValidate: EXIT!"); //NOI18N
83         }
84 }