]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/validator/phone/abroad/PizzaAbroadDialValidator.java
331d81bda8a4f6ba1cb89f03cad7bc8010f60227
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / validator / phone / abroad / PizzaAbroadDialValidator.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
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.pizzaapplication.validator.phone.abroad;
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.FacesValidator;
24 import javax.faces.validator.ValidatorException;
25 import org.mxchange.jcoreee.validator.number.BaseNumberValidator;
26
27 /**
28  * A validator for phone, fax and mobile numbers
29  * <p>
30  * @author Roland Häder<roland@mxchange.org>
31  */
32 @FacesValidator ("AbroadDialValidator")
33 public class PizzaAbroadDialValidator extends BaseNumberValidator {
34
35         /**
36          * Serial number
37          */
38         private static final long serialVersionUID = 186_897_817_692_786_900L;
39
40         @Override
41         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
42                 // The required field
43                 final String[] requiredFields = {"countryAbroadDialPrefix"}; //NOI18N
44
45                 // Pre-validation (example: not null, not a string, empty string ...)
46                 super.preValidate(context, component, value, requiredFields, true);
47
48                 // Parse value as string first
49                 final String dialNumber = String.valueOf(value);
50
51                 // Is it not +?
52                 if (!dialNumber.equals("+")) { //NOI18N
53                         // No, then try to ...
54                         try {
55                                 // ..parse as number
56                                 final Long number = Long.valueOf(dialNumber);
57
58                                 // Not valid range? (1 - 99, very rude)
59                                 if (number < 1 || number > 99) {
60                                         // Not allowed
61                                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, MessageFormat.format("Number {0} is not between 1 and 99 inclusive.", number), MessageFormat.format("The entered number {0} is not in the range of 1 and 99 inclusive. This cannot be used as abroad dial prefix. Alternatively enter international '+' sign.", number))); //NOI18N
62                                 }
63                         } catch (final NumberFormatException ex) {
64                                 // Didn't work
65                                 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, "No number or + entered", "No number or + sign has been entered."), ex); //NOI18N
66                         }
67                 }
68         }
69
70 }