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.validator;
21  
22  import org.apache.myfaces.tobago.util.ComponentUtils;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.event.AbortProcessingException;
29  import javax.faces.event.ActionEvent;
30  import javax.faces.event.ActionListener;
31  import javax.faces.event.PhaseId;
32  import java.lang.invoke.MethodHandles;
33  import java.util.StringTokenizer;
34  
35  public class ClearValidatorsActionListener implements ActionListener {
36  
37    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
38  
39    public PhaseId getPhaseId() {
40      return PhaseId.APPLY_REQUEST_VALUES;
41    }
42  
43    @Override
44    public void processAction(final ActionEvent actionEvent) throws AbortProcessingException {
45      if (LOG.isDebugEnabled()) {
46        LOG.debug("actionEvent = '" + actionEvent + "'");
47      }
48      final UIComponent source = actionEvent.getComponent();
49      final String clearValidatorsFieldIds
50          = (String) ComponentUtils.findParameter(source, "clearValidatorsFieldIds");
51  
52      if (LOG.isDebugEnabled()) {
53        LOG.debug("clearValidatorsFieldIds = '" + clearValidatorsFieldIds + "'");
54      }
55  
56      // FIXME: finding mechanism??? JSF ???
57  
58      final StringTokenizer tokenizer = new StringTokenizer(clearValidatorsFieldIds, ",");
59      while (tokenizer.hasMoreTokens()) {
60        final String clearValidatorsFieldId = tokenizer.nextToken();
61  
62        UIComponent component = source.findComponent(clearValidatorsFieldId);
63        if (LOG.isDebugEnabled()) {
64          LOG.debug("component = '" + component + "'");
65        }
66  
67        if (component == null) { // not found locally
68          if (LOG.isDebugEnabled()) {
69            LOG.debug("Component not found locally, asking the tree.");
70          }
71          final FacesContext facesContext = FacesContext.getCurrentInstance();
72          component = facesContext.getViewRoot().findComponent(clearValidatorsFieldId);
73        }
74  
75        if (component == null) { // not found
76          LOG.warn("Component not found.");
77        } else {
78  //        component.clearValidators();
79          LOG.error("NO LONGER AVAILABLE: component.clearValidators();");
80        }
81      }
82    }
83  
84  }