]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java
ebc17f33588bd8ef7fd44cbb915e221b0f6114e2
[jcore-utils.git] / src / org / mxchange / jcoreee / validator / bool / BaseBooleanValidator.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.bool;
18
19 import java.text.MessageFormat;
20 import javax.faces.application.FacesMessage;
21 import javax.faces.component.UIComponent;
22 import javax.faces.component.UIInput;
23 import javax.faces.component.ValueHolder;
24 import javax.faces.context.FacesContext;
25 import javax.faces.validator.ValidatorException;
26 import org.mxchange.jcoreee.validator.BaseObjectValidator;
27
28 /**
29  * A general boolean value validator.
30  * <p>
31  * @author BalusC
32  * @author Roland Häder<roland@mxchange.org>
33  */
34 public abstract class BaseBooleanValidator extends BaseObjectValidator<Object> {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 42_378_178_715_910_689L;
40
41         @Override
42         public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException {
43                 // Trace message
44                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
45
46                 // Pre-validate
47                 super.preValidate(context, component, value, requiredFields, allowNull);
48
49                 // Get client id and init message + key
50                 String clientId = component.getClientId();
51                 String requiredMessage = null;
52
53                 // So far all fine, no check if the field is fine
54                 for (final String field : requiredFields) {
55                         // Debug message
56                         //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
57
58                         // Is it the same?
59                         if (clientId.endsWith(field)) {
60                                 // Compare value's type
61                                 if (!(value instanceof Boolean)) {
62                                         // Generate message
63                                         requiredMessage = MessageFormat.format("Field {0} is not Boolean: {1}", field, value); //NOI18N
64
65                                         // Value is not right type
66                                         break;
67                                 }
68
69                                 // Cast to string
70                                 Boolean bool = (Boolean) value;
71
72                                 // Is it false?
73                                 if (bool.equals(Boolean.FALSE)) {
74                                         // Default message
75                                         requiredMessage = ((UIInput) component).getRequiredMessage();
76
77                                         if (null == requiredMessage) {
78                                                 Object label = component.getAttributes().get("label"); //NOI18N
79
80                                                 // Check if label is null, or zero length
81                                                 if ((null == label) || (label instanceof CharSequence && ((CharSequence) label).length() == 0)) {
82                                                         label = component.getValueExpression("label"); //NOI18N
83                                                 }
84
85                                                 // Label is still null?
86                                                 if (null == label) {
87                                                         label = component.getClientId(context);
88                                                 }
89
90                                                 // Set message
91                                                 requiredMessage = MessageFormat.format(UIInput.REQUIRED_MESSAGE_ID, label);
92
93                                                 // Set value to false in UI component
94                                                 ((ValueHolder) component).setValue(Boolean.FALSE);
95                                         }
96
97                                         // Abort processing here
98                                         break;
99                                 }
100                         }
101                 }
102
103                 // Is facesMessage set?
104                 if (null != requiredMessage) {
105                         // Abort here
106                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage));
107                 }
108         }
109
110 }