]> git.mxchange.org Git - jcoreee.git/blob - src/org/mxchange/jsfcore/validator/BaseObjectValidator.java
Renamed variable to make it more clear
[jcoreee.git] / src / org / mxchange / jsfcore / validator / BaseObjectValidator.java
1 /*\r
2  * Copyright (C) 2015 Roland Haeder\r
3  *\r
4  * This program is free software: you can redistribute it and/or modify\r
5  * it under the terms of the GNU General Public License as published by\r
6  * the Free Software Foundation, either version 3 of the License, or\r
7  * (at your option) any later version.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
16  */\r
17 package org.mxchange.jsfcore.validator;\r
18 \r
19 import java.text.MessageFormat;\r
20 import java.util.Arrays;\r
21 import java.util.ResourceBundle;\r
22 import javax.faces.application.FacesMessage;\r
23 import javax.faces.component.UIComponent;\r
24 import javax.faces.context.FacesContext;\r
25 import javax.faces.validator.Validator;\r
26 import javax.faces.validator.ValidatorException;\r
27 import org.mxchange.jcore.BaseFrameworkSystem;\r
28 import org.mxchange.jcore.FrameworkInterface;\r
29 \r
30 /**\r
31  * A general object validation class. Please implement javax.faces.validator.Validator\r
32  * (with import line!) and call preValidate(). You also may want to try out some\r
33  * other BaseFooValidator classes before directly inheriting from this class.\r
34  *\r
35  * @author Roland Haeder\r
36  */\r
37 public abstract class BaseObjectValidator extends BaseFrameworkSystem implements FrameworkInterface, Validator {\r
38 \r
39         /**\r
40          * Needs to be implemented as the Validator interface needs to be implemented.\r
41          *\r
42          * @param context\r
43          * @param component\r
44          * @param value\r
45          * @throws ValidatorException \r
46          */\r
47         @Override\r
48         abstract public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException;\r
49 \r
50         /**\r
51          * Initializes resource bundle\r
52          *\r
53          * @param FacesContext instance\r
54          */\r
55         private void initResourceBundle (final FacesContext context) {\r
56                 // Is it set?\r
57                 if (null == this.getBundle()) {\r
58                         // Set it now\r
59                         setBundle(ResourceBundle.getBundle("org.mxchange.localization.messages", context.getViewRoot().getLocale()));\r
60                 }\r
61         }\r
62 \r
63         /**\r
64          * Pre-validation of value, e.g. not null\r
65          *\r
66          * @param context FacesContext instance\r
67          * @param component UIComponent instance\r
68          * @param value Value to check\r
69          * @param requiredFields Array of required field names (ending with)\r
70          * @throws ValidatorException If something more horrible went wrong\r
71          */\r
72         protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {\r
73                 // Trace message\r
74                 this.getLogger().trace(MessageFormat.format("context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields)));\r
75 \r
76                 // Set resource bundle\r
77                 this.initResourceBundle(context);\r
78 \r
79                 // Init message and key\r
80                 FacesMessage facesMessage = null;\r
81                 String errKey = "error.unknown_id";\r
82 \r
83                 // Get client id\r
84                 String clientId = component.getClientId();\r
85 \r
86                 // Default is no field is valid\r
87                 boolean isValidField = false;\r
88 \r
89                 for (final String field : requiredFields) {\r
90                         // Get logger\r
91                         this.getLogger().debug(MessageFormat.format("field={0},clientId={1}", field, clientId));\r
92 \r
93                         // Is it the same?\r
94                         if (clientId.endsWith(field)) {\r
95                                 // Is valid field\r
96                                 isValidField = true;\r
97 \r
98                                 // Is it null?\r
99                                 if (null == value) {\r
100                                         errKey = String.format("error.%s.is_null", field);\r
101 \r
102                                         // Value it null\r
103                                         facesMessage = new FacesMessage(this.getMessageStringFromKey(errKey));\r
104                                 }\r
105                         }\r
106                 }\r
107 \r
108                 // Debug message\r
109                 this.getLogger().debug("isValidField=" + isValidField);\r
110 \r
111                 // Valid field?\r
112                 if (!isValidField) {\r
113                         // Invalid field\r
114                         facesMessage = new FacesMessage(MessageFormat.format(errKey, clientId));\r
115                 }\r
116 \r
117                 // Debug message\r
118                 this.getLogger().debug(MessageFormat.format("facesMessage={0}", facesMessage));\r
119 \r
120                 // Is it not null?\r
121                 if (null != facesMessage) {\r
122                         throw new ValidatorException(facesMessage);\r
123                 }\r
124 \r
125                 // Trace message\r
126                 this.getLogger().trace("EXIT!");\r
127         }\r
128 }\r