]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/validator/BaseObjectValidator.java
2e9d26f9c5a33ed1e5886804bb74f15170dda494
[jcore-utils.git] / src / org / mxchange / jcoreee / validator / BaseObjectValidator.java
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 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;
18
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import javax.faces.application.FacesMessage;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.validator.Validator;
25 import javax.faces.validator.ValidatorException;
26
27 /**
28  * A general object validation class. Please implement
29  * javax.faces.validator.Validator (with import line!) and call preValidate().
30  * You also may want to try out some other BaseFooValidator classes before
31  * directly inheriting from this class.
32  * <p>
33  * @param <Object> Any object that needs validation
34  * <p>
35  * @author Roland Häder<roland@mxchange.org>
36  */
37 public abstract class BaseObjectValidator<Object> implements Validator, Serializable {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 48_574_878_176_939_512L;
43
44         /**
45          * The method pre-validates the given value. It makes sure that the
46          * component's id is found in requiredFields and is not null. Once the
47          * component's id has been found, it stops iteration on requiredFields
48          * (which saves execution time).
49          * <p>
50          * @param context        FacesContext instance
51          * @param component      UIComponent instance
52          * @param value          Value to check
53          * @param requiredFields Array of required field names (ending with)
54          * @param allowNull      Whether null or empty values are allowed
55          * <p>
56          * @throws ValidatorException If something more horrible went wrong
57          */
58         protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException {
59                 // Trace message
60                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
61
62                 // Init message and key
63                 String requiredMessage = null;
64
65                 // Get client id
66                 final String clientId = component.getClientId();
67
68                 // Check component's id against required fields and find a match
69                 for (final String field : requiredFields) {
70                         // Get logger
71                         //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
72
73                         // Is it the same?
74                         if (clientId.endsWith(field)) {
75                                 // Is it null?
76                                 if ((!allowNull) && (null == value)) {
77                                         // Value it null
78                                         requiredMessage = MessageFormat.format("Field {0} is null.", field); //NOI18N
79                                 }
80
81                                 // Abort here
82                                 break;
83                         }
84                 }
85
86                 // Debug message
87                 //* NOISY-DEBUG: */ this.getLogger().logDebug(MessageFormat.format("preValidate: requiredMessage={0}", requiredMessage)); //NOI18N
88                 // Is it not null?
89                 if (null != requiredMessage) {
90                         throw new ValidatorException(new FacesMessage(MessageFormat.format("Value {0} for clientId={1} is not valid/unexpected.", value, clientId)));
91                 }
92
93                 // Trace message
94                 //* NOISY-DEBUG: */ System.out.println("preValidate: EXIT!"); //NOI18N
95         }
96
97 }