2 * Copyright (C) 2015 Roland Haeder
\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
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
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
17 package org.mxchange.jsfcore.validator;
\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
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
35 * @author Roland Haeder
\r
37 public abstract class BaseObjectValidator extends BaseFrameworkSystem implements FrameworkInterface, Validator {
\r
40 * Needs to be implemented as the Validator interface needs to be implemented.
\r
45 * @throws ValidatorException
\r
48 abstract public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException;
\r
51 * Initializes resource bundle
\r
53 * @param FacesContext instance
\r
55 private void initResourceBundle (final FacesContext context) {
\r
57 if (null == this.getBundle()) {
\r
59 setBundle(ResourceBundle.getBundle("org.mxchange.localization.messages", context.getViewRoot().getLocale()));
\r
64 * Pre-validation of value, e.g. not null
\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
72 protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
\r
74 this.getLogger().trace(MessageFormat.format("context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields)));
\r
76 // Set resource bundle
\r
77 this.initResourceBundle(context);
\r
79 // Init message and key
\r
80 FacesMessage facesMessage = null;
\r
81 String errKey = "error.unknown_id";
\r
84 String clientId = component.getClientId();
\r
86 // Default is no field is valid
\r
87 boolean isValidField = false;
\r
89 for (final String field : requiredFields) {
\r
91 this.getLogger().debug(MessageFormat.format("field={0},clientId={1}", field, clientId));
\r
94 if (clientId.endsWith(field)) {
\r
96 isValidField = true;
\r
99 if (null == value) {
\r
100 errKey = String.format("error.%s.is_null", field);
\r
103 facesMessage = new FacesMessage(this.getMessageStringFromKey(errKey));
\r
109 this.getLogger().debug("isValidField=" + isValidField);
\r
112 if (!isValidField) {
\r
114 facesMessage = new FacesMessage(MessageFormat.format(errKey, clientId));
\r
118 this.getLogger().debug(MessageFormat.format("facesMessage={0}", facesMessage));
\r
121 if (null != facesMessage) {
\r
122 throw new ValidatorException(facesMessage);
\r
126 this.getLogger().trace("EXIT!");
\r