+++ /dev/null
-/*
- * Copyright (C) 2016 - 2018 Free Software Foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.jcoreee.validator.bool;
-
-import java.text.MessageFormat;
-import javax.faces.application.FacesMessage;
-import javax.faces.component.UIComponent;
-import javax.faces.component.UIInput;
-import javax.faces.component.ValueHolder;
-import javax.faces.context.FacesContext;
-import javax.faces.validator.ValidatorException;
-import org.mxchange.jcoreee.validator.BaseObjectValidator;
-
-/**
- * A general boolean value validator.
- * <p>
- * @author BalusC
- * @author Roland Häder<roland@mxchange.org>
- */
-public abstract class BaseBooleanValidator extends BaseObjectValidator<Object> {
-
- /**
- * Serial number
- */
- private static final long serialVersionUID = 42_378_178_715_910_689L;
-
- @Override
- public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException {
- // Trace message
- //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
-
- // Pre-validate
- super.preValidate(context, component, value, requiredFields, allowNull);
-
- // Get client id and init message + key
- String clientId = component.getClientId();
- String requiredMessage = null;
-
- // So far all fine, no check if the field is fine
- for (final String field : requiredFields) {
- // Debug message
- //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
-
- // Is it the same?
- if (clientId.endsWith(field)) {
- // Compare value's type
- if (!(value instanceof Boolean)) {
- // Generate message
- requiredMessage = MessageFormat.format("Field {0} is not Boolean: {1}", field, value); //NOI18N
-
- // Value is not right type
- break;
- }
-
- // Cast to string
- Boolean bool = (Boolean) value;
-
- // Is it false?
- if (bool.equals(Boolean.FALSE)) {
- // Default message
- requiredMessage = ((UIInput) component).getRequiredMessage();
-
- if (null == requiredMessage) {
- Object label = component.getAttributes().get("label"); //NOI18N
-
- // Check if label is null, or zero length
- if ((null == label) || (label instanceof CharSequence && ((CharSequence) label).length() == 0)) {
- label = component.getValueExpression("label"); //NOI18N
- }
-
- // Label is still null?
- if (null == label) {
- label = component.getClientId(context);
- }
-
- // Set message
- requiredMessage = MessageFormat.format(UIInput.REQUIRED_MESSAGE_ID, label);
-
- // Set value to false in UI component
- ((ValueHolder) component).setValue(Boolean.FALSE);
- }
-
- // Abort processing here
- break;
- }
- }
- }
-
- // Is facesMessage set?
- if (null != requiredMessage) {
- // Abort here
- throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage));
- }
- }
-
-}