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