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  
20  package org.apache.myfaces.tobago.facelets;
21  
22  import org.apache.myfaces.tobago.component.InputSuggest;
23  import org.apache.myfaces.tobago.component.Visual;
24  import org.apache.myfaces.tobago.config.TobagoConfig;
25  import org.apache.myfaces.tobago.event.SheetStateChangeSource;
26  import org.apache.myfaces.tobago.event.SortActionSource;
27  import org.apache.myfaces.tobago.event.TabChangeSource;
28  import org.apache.myfaces.tobago.internal.config.TobagoConfigImpl;
29  
30  import javax.enterprise.inject.spi.CDI;
31  import javax.faces.component.EditableValueHolder;
32  import javax.faces.component.UIComponent;
33  import javax.faces.context.FacesContext;
34  import javax.faces.validator.Validator;
35  import javax.faces.view.facelets.ComponentConfig;
36  import javax.faces.view.facelets.ComponentHandler;
37  import javax.faces.view.facelets.FaceletContext;
38  import javax.faces.view.facelets.MetaRuleset;
39  import java.util.HashSet;
40  import java.util.Map;
41  import java.util.Set;
42  
43  public class TobagoComponentHandler extends ComponentHandler {
44  
45    public TobagoComponentHandler(final ComponentConfig componentConfig) {
46      super(componentConfig);
47    }
48  
49    @Override
50    protected MetaRuleset createMetaRuleset(final Class aClass) {
51      final MetaRuleset metaRuleset = super.createMetaRuleset(aClass);
52      if (SortActionSource.class.isAssignableFrom(aClass)) {
53        metaRuleset.addRule(SortActionSourceRule.INSTANCE);
54      }
55      if (TabChangeSource.class.isAssignableFrom(aClass)) {
56        metaRuleset.addRule(TabChangeSourceRule.INSTANCE);
57      }
58      if (SheetStateChangeSource.class.isAssignableFrom(aClass)) {
59        metaRuleset.addRule(SheetStateChangeSourceRule.INSTANCE);
60      }
61      if (Visual.class.isAssignableFrom(aClass)) {
62        metaRuleset.addRule(SupportsMarkupRule.INSTANCE);
63      }
64      if (InputSuggest.class.isAssignableFrom(aClass)) {
65        metaRuleset.addRule(SuggestMethodRule.INSTANCE);
66      }
67  
68      return metaRuleset;
69    }
70  
71    @Override
72    public void onComponentPopulated(
73        final FaceletContext context, final UIComponent component, final UIComponent parent) {
74  
75      // TODO call only if component was created
76      if (component instanceof EditableValueHolder) {
77        addDefaultValidators(context.getFacesContext(), (EditableValueHolder) component);
78      }
79    }
80  
81    private void addDefaultValidators(final FacesContext context, final EditableValueHolder component) {
82      final TobagoConfigImpl tobagoConfig = (TobagoConfigImpl) CDI.current().select(TobagoConfig.class).get();
83      final Map<String, String> validatorInfoMap = tobagoConfig.getDefaultValidatorInfo();
84      if (validatorInfoMap.isEmpty()) {
85        return;
86      }
87      final Validator[] validators = component.getValidators();
88      if (validators.length > 0) {
89        final Set<String> classNames = new HashSet<>();
90        // collect classNames of validators
91        for (final Validator validator : validators) {
92          classNames.add(validator.getClass().getName());
93        }
94        validatorInfoMap.forEach((key, value) -> {
95          if (!classNames.contains(value)) {
96            component.addValidator(context.getApplication().createValidator(key));
97          }
98        });
99      } else {
100       for (final String next : validatorInfoMap.keySet()) {
101         component.addValidator(context.getApplication().createValidator(next));
102       }
103     }
104   }
105 }