]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java
auto-formatted project + updated jars
[jcore-utils.git] / src / org / mxchange / jcoreee / validator / number / BaseLongValidator.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
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 javax.faces.application.FacesMessage;
20 import javax.faces.component.UIComponent;
21 import javax.faces.context.FacesContext;
22 import javax.faces.validator.Validator;
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 Haeder<roland@mxchange.org>
30  */
31 public abstract class BaseLongValidator extends BaseObjectValidator implements Validator {
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) throws ValidatorException {
40                 // Trace message
41                 //this.getLogger().logTrace(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
42
43                 // Pre-validate
44                 super.preValidate(context, component, value, requiredFields);
45
46                 // Get client id and init message + key
47                 String clientId = component.getClientId();
48                 FacesMessage facesMessage = null;
49                 String requiredMessage;
50
51                 // So far all fine, no check if the field is fine
52                 for (final String field : requiredFields) {
53                         // Debug message
54                         //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
55
56                         // Is it the same?
57                         if (clientId.endsWith(field)) {
58                                 // Compare value's type
59                                 if (!(value instanceof Long)) {
60                                         // Generate message
61                                         requiredMessage = this.getMessageStringFromKey(String.format("ERROR_%s_IS_NOT_LONG", field.toUpperCase()));
62
63                                         // Value is not right type
64                                         facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N
65                                         break;
66                                 }
67
68                                 // Cast to string
69                                 Long num = (Long) value;
70
71                                 // Is the number below zero?
72                                 if (num < 0) {
73                                         // Generate message
74                                         requiredMessage = this.getMessageStringFromKey(String.format("ERROR_%s_IS_BELOW_ZERO", field.toUpperCase()));
75
76                                         // Abort processing here
77                                         facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
78                                         break;
79                                 }
80                         }
81                 }
82
83                 // Is facesMessage set?
84                 if (facesMessage != null) {
85                         // Abort here
86                         throw new ValidatorException(facesMessage);
87                 }
88         }
89 }