]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java
More cleanup
[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 java.text.MessageFormat;
20 import java.util.Arrays;
21 import javax.faces.application.FacesMessage;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.validator.ValidatorException;
25 import org.mxchange.jcoree.BaseEeSystem;
26 import org.mxchange.jcoreee.validator.BaseObjectValidator;
27
28 /**
29  * A general string validation class. You normally want to inherit from this
30  * class for many form fields, e.g. surname, street name, city name and such.
31  *
32  * @author Roland Haeder
33  */
34 public abstract class BaseStringValidator extends BaseObjectValidator {
35
36         @Override
37         protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
38                 // Trace message
39                 this.getLogger().logTrace(MessageFormat.format("context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
40
41                 // Pre-validate (e.g. on null)
42                 super.preValidate(context, component, value, requiredFields);
43
44                 // Get client id and init message + key
45                 String clientId = component.getClientId();
46                 FacesMessage facesMessage = null;
47                 String errKey;
48
49                 // So far all fine, no check if the field is fine
50                 for (final String field : requiredFields) {
51                         // Debug message
52                         this.getLogger().logDebug(MessageFormat.format("field={0},clientId={1}", field, clientId)); //NOI18N
53
54                         // Is it the same?
55                         if (clientId.endsWith(field)) {
56                                 // Compare value's type
57                                 if (!(value instanceof String)) {
58                                         // Value is empty
59                                         errKey = String.format("error.%s.is_not_string", field); //NOI18N
60
61                                         facesMessage = new FacesMessage(BaseEeSystem.getMessageStringFromKey(errKey));
62                                 }
63
64                                 // Cast to string
65                                 String str = (String) value;
66
67                                 // Is it empty?
68                                 if (str.isEmpty()) {
69                                         // Value is empty
70                                         errKey = String.format("error.%s.is_empty", field); //NOI18N
71
72                                         facesMessage = new FacesMessage(BaseEeSystem.getMessageStringFromKey(errKey));
73                                 }
74                         }
75                 }
76
77                 // Debug message
78                 this.getLogger().logDebug(MessageFormat.format("facesMessage={0}", facesMessage)); //NOI18N
79
80                 // Is it not null?
81                 if (null != facesMessage) {
82                         throw new ValidatorException(facesMessage);
83                 }
84
85                 // Trace message
86                 this.getLogger().logTrace("EXIT!"); //NOI18N
87         }
88 }