2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jcoreee.validator;
19 import java.text.MessageFormat;
20 import java.util.Arrays;
21 import javax.faces.application.FacesMessage;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.validator.Validator;
25 import javax.faces.validator.ValidatorException;
26 import org.mxchange.jcoree.BaseEeSystem;
29 * A general object validation class. Please implement javax.faces.validator.Validator
30 * (with import line!) and call preValidate(). You also may want to try out some
31 * other BaseFooValidator classes before directly inheriting from this class.
33 * @author Roland Haeder
35 public abstract class BaseObjectValidator extends BaseEeSystem implements Validator {
38 * Needs to be implemented as the Validator interface needs to be implemented.
43 * @throws ValidatorException
46 abstract public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException;
49 * The method pre-validates the given value. It makes sure that the component's id is found in
50 * requiredFields and is not null. Once the component's id has been found, it stops iteration on
51 * requiredFields (which saves execution time).
53 * @param context FacesContext instance
54 * @param component UIComponent instance
55 * @param value Value to check
56 * @param requiredFields Array of required field names (ending with)
57 * @throws ValidatorException If something more horrible went wrong
59 protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
61 this.getLogger().logTrace(MessageFormat.format("context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
63 // Init message and key
64 FacesMessage facesMessage = null;
65 String errKey = "error.unknown_id"; //NOI18N
68 final String clientId = component.getClientId();
70 // Default is no field is valid
71 boolean isValidField = false;
73 // Check component's id against required fields and find a match
74 for (final String field : requiredFields) {
76 this.getLogger().logDebug(MessageFormat.format("field={0},clientId={1}", field, clientId)); //NOI18N
79 if (clientId.endsWith(field)) {
85 errKey = String.format("error.%s.is_null", field); //NOI18N
88 facesMessage = new FacesMessage(BaseEeSystem.getMessageStringFromKey(errKey));
97 this.getLogger().logDebug(MessageFormat.format("isValidField={0}", isValidField)); //NOI18N
102 facesMessage = new FacesMessage(MessageFormat.format(errKey, clientId));
106 this.getLogger().logDebug(MessageFormat.format("facesMessage={0}", facesMessage)); //NOI18N
109 if (null != facesMessage) {
110 throw new ValidatorException(facesMessage);
114 this.getLogger().logTrace("EXIT!"); //NOI18N