]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/validator/BaseObjectValidator.java
90a98df1186eba2aa36187f2033415da343d1067
[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 import org.mxchange.jcoreee.BaseEeSystem;
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  *
33  * @author Roland Haeder<roland@mxchange.org>
34  */
35 public abstract class BaseObjectValidator extends BaseEeSystem implements Validator {
36
37         /**
38          * Needs to be implemented as the Validator interface needs to be
39          * implemented.
40          *
41          * @param context
42          * @param component
43          * @param value
44          * @throws ValidatorException
45          */
46         @Override
47         abstract public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException;
48
49         /**
50          * The method pre-validates the given value. It makes sure that the
51          * component's id is found in requiredFields and is not null. Once the
52          * component's id has been found, it stops iteration on requiredFields
53          * (which saves execution time).
54          *
55          * @param context FacesContext instance
56          * @param component UIComponent instance
57          * @param value Value to check
58          * @param requiredFields Array of required field names (ending with)
59          * @throws ValidatorException If something more horrible went wrong
60          */
61         protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
62                 // Trace message
63                 //this.getLogger().logTrace(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
64
65                 // Init message and key
66                 FacesMessage facesMessage = null;
67                 String errKey = "error.unknown_id"; //NOI18N
68
69                 // Get client id
70                 final String clientId = component.getClientId();
71
72                 // Default is no field is valid
73                 boolean isValidField = false;
74
75                 // Check component's id against required fields and find a match
76                 for (final String field : requiredFields) {
77                         // Get logger
78                         //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
79
80                         // Is it the same?
81                         if (clientId.endsWith(field)) {
82                                 // Is valid field
83                                 isValidField = true;
84
85                                 // Is it null?
86                                 if (null == value) {
87                                         // Generate message
88                                         errKey = String.format("ERROR_%s_IS_NULL", field.toUpperCase()); //NOI18N
89
90                                         // Value it null
91                                         facesMessage = new FacesMessage(getMessageStringFromKey(errKey));
92                                 }
93
94                                 // Abort here
95                                 break;
96                         }
97                 }
98
99                 // Debug message
100                 //this.getLogger().logDebug(MessageFormat.format("preValidate: isValidField={0}", isValidField)); //NOI18N
101                 // Valid field?
102                 if (!isValidField) {
103                         // Invalid field
104                         facesMessage = new FacesMessage(MessageFormat.format(errKey, clientId));
105                 }
106
107                 // Debug message
108                 //this.getLogger().logDebug(MessageFormat.format("preValidate: facesMessage={0}", facesMessage)); //NOI18N
109                 // Is it not null?
110                 if (null != facesMessage) {
111                         throw new ValidatorException(facesMessage);
112                 }
113
114                 // Trace message
115                 //this.getLogger().logTrace("preValidate: EXIT!"); //NOI18N
116         }
117 }