]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java
More cleanup
[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 java.util.Arrays;
21 import javax.faces.application.FacesMessage;
22 import javax.faces.component.UIComponent;
23 import javax.faces.component.UIInput;
24 import javax.faces.component.ValueHolder;
25 import javax.faces.context.FacesContext;
26 import javax.faces.validator.Validator;
27 import javax.faces.validator.ValidatorException;
28 import org.mxchange.jcoree.BaseEeSystem;
29 import org.mxchange.jcoreee.validator.BaseObjectValidator;
30
31 /**
32  * A general boolean value validator.
33  *
34  * @author BalusC
35  * @author Roland Haeder
36  */
37 public abstract class BaseBooleanValidator extends BaseObjectValidator implements Validator {
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("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
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("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 Boolean)) {
60                                         // Value is not right type
61                                         facesMessage = new FacesMessage(BaseEeSystem.getMessageStringFromKey(String.format("error.%s.is_not_boolean", field))); //NOI18N
62                                         break;
63                                 }
64
65                                 // Cast to string
66                                 Boolean bool = (Boolean) value;
67
68                                 // Is it false?
69                                 if (bool.equals(Boolean.FALSE)) {
70                                         // Default message
71                                         String requiredMessage = ((UIInput) component).getRequiredMessage();
72
73                                         if (null == requiredMessage) {
74                                                 Object label = component.getAttributes().get("label"); //NOI18N
75
76                                                 // Check if label is null, or zero length
77                                                 if ((null == label) || (label instanceof CharSequence && ((CharSequence) label).length() == 0)) {
78                                                         label = component.getValueExpression("label"); //NOI18N
79                                                 }
80
81                                                 // Label is still null?
82                                                 if (null == label) {
83                                                         label = component.getClientId(context);
84                                                 }
85
86                                                 // Set message
87                                                 requiredMessage = MessageFormat.format(UIInput.REQUIRED_MESSAGE_ID, label);
88
89                                                 // Set value to false in UI component
90                                                 ((ValueHolder) component).setValue(Boolean.FALSE);
91                                         }
92
93                                         // Abort processing here
94                                         facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
95                                         break;
96                                 }
97                         }
98                 }
99
100                 // Is facesMessage set?
101                 if (facesMessage != null) {
102                         // Abort here
103                         throw new ValidatorException(facesMessage);
104                 }
105         }
106 }