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