View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.component.validate;
20  
21  import javax.faces.component.UIInput;
22  import javax.faces.context.FacesContext;
23  import javax.faces.convert.Converter;
24  import javax.faces.validator.BeanValidator;
25  import javax.faces.validator.Validator;
26  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent;
27  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
28  import org.apache.myfaces.shared.util.WebConfigParamUtils;
29  
30  /**
31   *
32   */
33  @JSFComponent
34  public class ValidateWholeBeanComponent extends UIInput
35  {
36      static public final String COMPONENT_FAMILY =
37          "javax.faces.Input";
38      static public final String COMPONENT_TYPE =
39          "org.apache.myfaces.component.validate.ValidateWholeBean";
40  
41      public ValidateWholeBeanComponent()
42      {
43          setRendererType(null);
44      }
45  
46      @Override
47      public Object getSubmittedValue()
48      {
49          return "WholeBeanValidator";
50      }
51  
52      @Override
53      public void addValidator(Validator validator)
54      {
55          // No-op. It does not make sense to allow additional validators to be installed.
56      }
57  
58      @Override
59      public void validate(FacesContext context)
60      {
61          
62          Boolean enabled = WebConfigParamUtils.getBooleanInitParameter(context.getExternalContext(), 
63                  BeanValidator.ENABLE_VALIDATE_WHOLE_BEAN_PARAM_NAME, Boolean.FALSE);
64          
65          if (Boolean.TRUE.equals(enabled) && !isDisabled())
66          {
67              //Install WholeBeanValidator
68              Validator[] validators = this.getValidators();
69              if (validators != null && validators.length > 0)
70              {
71                  //No op
72              }
73              else
74              {
75                  super.addValidator(new WholeBeanValidator());
76              }
77              super.validate(context);
78          }
79      }
80  
81      @Override
82      public void setConverter(Converter converter)
83      {
84          // No-op. It does not make sense to allow a converter to be installed.
85      }
86      
87      @JSFProperty
88      public String getValidationGroups()
89      {
90          return (String) getStateHelper().eval(PropertyKeys.validationGroups);
91      }
92      
93      public void setValidationGroups(String validationGroups)
94      {
95          getStateHelper().put(PropertyKeys.validationGroups, validationGroups );
96      }
97      
98      @JSFProperty(defaultValue="false")
99      public boolean isDisabled()
100     {
101         return (Boolean) getStateHelper().eval(PropertyKeys.disabled, false);
102     }
103     
104     public void setDisabled(boolean disabled)
105     {
106         getStateHelper().put(PropertyKeys.disabled, disabled);
107     }
108     
109     enum PropertyKeys
110     {
111         validationGroups,
112         disabled
113     }
114 }