]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java
Let's get rid of this class. It was maybe a good idea, but it required a lot work...
[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 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;
26
27 /**
28  * A general number value validator.
29  * <p>
30  * @author Roland Haeder<roland@mxchange.org>
31  */
32 public abstract class BaseLongValidator extends BaseObjectValidator implements Validator {
33
34         /**
35          * Serial number
36          */
37         private static final long serialVersionUID = 25_481_878_590_589_321L;
38
39         @Override
40         public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
41                 // Trace message
42                 //this.getLogger().logTrace(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
43
44                 // Pre-validate
45                 super.preValidate(context, component, value, requiredFields);
46
47                 // Get client id and init message + key
48                 String clientId = component.getClientId();
49                 FacesMessage facesMessage = null;
50                 String requiredMessage;
51
52                 // So far all fine, no check if the field is fine
53                 for (final String field : requiredFields) {
54                         // Debug message
55                         //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
56
57                         // Is it the same?
58                         if (clientId.endsWith(field)) {
59                                 // Compare value's type
60                                 if (!(value instanceof Long)) {
61                                         // Generate message
62                                         requiredMessage = MessageFormat.format("Field {0} is not Long.", field); //NOI18N
63
64                                         // Value is not right type
65                                         facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N
66                                         break;
67                                 }
68
69                                 // Cast to string
70                                 Long num = (Long) value;
71
72                                 // Is the number below zero?
73                                 if (num < 0) {
74                                         // Generate message
75                                         requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", num, field); //NOI18N
76
77                                         // Abort processing here
78                                         facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
79                                         break;
80                                 }
81                         }
82                 }
83
84                 // Is facesMessage set?
85                 if (facesMessage != null) {
86                         // Abort here
87                         throw new ValidatorException(facesMessage);
88                 }
89         }
90 }