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.util.StringTokenizer;
33  
34  public class ClearValidatorsActionListener implements ActionListener {
35  
36    private static final Logger LOG = LoggerFactory.getLogger(ClearValidatorsActionListener.class);
37  
38    public PhaseId getPhaseId() {
39      return PhaseId.APPLY_REQUEST_VALUES;
40    }
41  
42    @Override
43    public void processAction(final ActionEvent actionEvent) throws AbortProcessingException {
44      if (LOG.isDebugEnabled()) {
45        LOG.debug("actionEvent = '" + actionEvent + "'");
46      }
47      final UIComponent source = actionEvent.getComponent();
48      final String clearValidatorsFieldIds
49          = (String) ComponentUtils.findParameter(source, "clearValidatorsFieldIds");
50  
51      if (LOG.isDebugEnabled()) {
52        LOG.debug("clearValidatorsFieldIds = '" + clearValidatorsFieldIds + "'");
53      }
54  
55      // FIXME: finding mechanism??? JSF ???
56  
57      for (final StringTokenizer tokenizer
58          = new StringTokenizer(clearValidatorsFieldIds, ",");
59           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  }