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