]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/validator/number/BaseNumberValidator.java
Please cherry-pick:
[jcore-utils.git] / src / org / mxchange / jcoreee / validator / number / BaseNumberValidator.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcoreee.validator.number;
18
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.ValidatorException;
24 import org.mxchange.jcoreee.validator.BaseObjectValidator;
25
26 /**
27  * A general number value validator.
28  * <p>
29  * @author Roland Häder<roland@mxchange.org>
30  */
31 public abstract class BaseNumberValidator extends BaseObjectValidator<Object> {
32
33         /**
34          * Serial number
35          */
36         private static final long serialVersionUID = 25_481_878_590_589_321L;
37
38         @Override
39         public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException {
40                 // Trace message
41                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3},allowNull={4} - CALLED!", context, component, value, Arrays.toString(requiredFields, allowNull))); //NOI18N
42
43                 // Pre-validate
44                 super.preValidate(context, component, value, requiredFields, allowNull);
45
46                 // Get client id and init message + key
47                 String clientId = component.getClientId();
48                 String requiredMessage = null;
49
50                 // So far all fine, no check if the field is fine
51                 for (final String field : requiredFields) {
52                         // Debug message
53                         //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
54
55                         // Is it the same?
56                         if (clientId.endsWith(field)) {
57                                 // Init variables
58                                 Number number = null;
59
60                                 // Compare value's type, supported: String, Long, Integer
61                                 if (value instanceof String) {
62                                         // Is a string, then try to parse it as Long, largest range
63                                         try {
64                                                 number = Long.valueOf((String) value);
65                                         } catch (final NumberFormatException ex) {
66                                                 // Cannot parse string to long
67                                                 requiredMessage = MessageFormat.format("Field {0} cannot be parsed to long: {1}, exception:{2}", field, value, ex);
68
69                                                 // Abort processing here
70                                                 break;
71                                         }
72                                 } else if ((value instanceof Integer) || (value instanceof Long) || (value instanceof Short)) {
73                                         // Is any used number
74                                         number = (Number) value;
75                                 }
76
77                                 // Is the number below zero? Casting again largest range will not result in unexpected comparison
78                                 if ((!allowNull) && ((number instanceof Number) && ((Long) number < 0))) {
79                                         // Generate message
80                                         requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", number, field); //NOI18N
81
82                                         // Abort processing here
83                                         break;
84                                 }
85                         }
86                 }
87
88                 // Is facesMessage set?
89                 if (null != requiredMessage) {
90                         // Abort here
91                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage));
92                 }
93         }
94
95 }