2 * Copyright (C) 2016 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.number;
19 import java.text.MessageFormat;
20 import javax.faces.application.FacesMessage;
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.validator.Validator;
24 import javax.faces.validator.ValidatorException;
25 import org.mxchange.jcoreee.validator.BaseObjectValidator;
28 * A general number value validator.
30 * @author Roland Haeder<roland@mxchange.org>
32 public abstract class BaseLongValidator extends BaseObjectValidator implements Validator {
37 private static final long serialVersionUID = 25_481_878_590_589_321L;
40 public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, boolean allowNull) throws ValidatorException {
42 //this.getLogger().logTrace(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3},allowNull={4} - CALLED!", context, component, value, Arrays.toString(requiredFields, allowNull))); //NOI18N
45 super.preValidate(context, component, value, requiredFields, allowNull);
47 // Get client id and init message + key
48 String clientId = component.getClientId();
49 FacesMessage facesMessage = null;
50 String requiredMessage = null;
52 // So far all fine, no check if the field is fine
53 for (final String field : requiredFields) {
55 //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
58 if (clientId.endsWith(field)) {
63 // Compare value's type
64 if (value instanceof String) {
65 // Is a string, try long
67 l = Long.valueOf((String) value);
68 } catch (final NumberFormatException ex) {
69 // Cannot parse string to long
70 requiredMessage = MessageFormat.format("Field {0} cannot be parsed to long: {1}, exception:{2}", field, value, ex);
72 // Abort processing here
75 } else if (value instanceof Integer) {
78 } else if (value instanceof Long) {
83 // Is the number below zero?
84 if ((!allowNull) && ((i instanceof Integer) && (i < 0))) {
86 requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", i, field); //NOI18N
88 // Abort processing here
90 } else if ((!allowNull) && ((l instanceof Long) && (l < 0))) {
92 requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", l, field); //NOI18N
94 // Abort processing here
100 // Is requiredMessage set?
101 if (null != requiredMessage) {
102 // Embed into FacesMessage
103 facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
106 // Is facesMessage set?
107 if (null != facesMessage) {
109 throw new ValidatorException(facesMessage);