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.internal.component;
21  
22  import org.apache.myfaces.tobago.component.Form;
23  import org.apache.myfaces.tobago.util.ComponentUtils;
24  import org.apache.myfaces.tobago.util.TobagoCallback;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import javax.faces.FacesException;
29  import javax.faces.component.ContextCallback;
30  import javax.faces.component.UIComponent;
31  import javax.faces.component.UIForm;
32  import javax.faces.context.FacesContext;
33  import javax.faces.event.PhaseId;
34  import java.util.Iterator;
35  
36  public abstract class AbstractUIForm extends UIForm implements Form {
37  
38    private static final Logger LOG = LoggerFactory.getLogger(AbstractUIForm.class);
39  
40    public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.Form";
41    public static final String SUBMITTED_MARKER = COMPONENT_TYPE + ".InSubmitted";
42  
43    @Override
44    public void processDecodes(final FacesContext facesContext) {
45  
46      // Process this component first
47      // to know the active actionId
48      // for the following childrend
49      decode(facesContext);
50  
51      final Iterator kids = getFacetsAndChildren();
52      while (kids.hasNext()) {
53        final UIComponent kid = (UIComponent) kids.next();
54        kid.processDecodes(facesContext);
55      }
56    }
57  
58    @Override
59    public void setSubmitted(final boolean b) {
60      super.setSubmitted(b);
61  
62      // set submitted for all subforms
63      for (final AbstractUIForm subForm : ComponentUtils.findSubForms(this)) {
64        subForm.setSubmitted(b);
65      }
66    }
67  
68    @Override
69    public void processValidators(final FacesContext facesContext) {
70      // if we're not the submitted form, only process subforms.
71      if (LOG.isDebugEnabled()) {
72        LOG.debug("processValidators for form: {}", getClientId(facesContext));
73      }
74      if (!isSubmitted()) {
75        for (final AbstractUIForm subForm : ComponentUtils.findSubForms(this)) {
76          subForm.processValidators(facesContext);
77        }
78      } else {
79        // Process all facets and children of this component
80        final Iterator kids = getFacetsAndChildren();
81        while (kids.hasNext()) {
82          final UIComponent kid = (UIComponent) kids.next();
83          kid.processValidators(facesContext);
84        }
85      }
86    }
87  
88    @Override
89    public void processUpdates(final FacesContext facesContext) {
90      // if we're not the submitted form, only process subforms.
91      if (LOG.isDebugEnabled()) {
92        LOG.debug("processUpdates for form: {}", getClientId(facesContext));
93      }
94      if (!isSubmitted()) {
95        for (final AbstractUIForm subForm : ComponentUtils.findSubForms(this)) {
96          subForm.processUpdates(facesContext);
97        }
98      } else {
99        // Process all facets and children of this component
100       final Iterator kids = getFacetsAndChildren();
101       while (kids.hasNext()) {
102         final UIComponent kid = (UIComponent) kids.next();
103         kid.processUpdates(facesContext);
104       }
105     }
106   }
107 
108   @Override
109   public boolean invokeOnComponent(final FacesContext context, final String clientId, final ContextCallback callback)
110       throws FacesException {
111     // TODO is this needed?
112     if (callback instanceof TobagoCallback) {
113       if (PhaseId.APPLY_REQUEST_VALUES.equals(((TobagoCallback) callback).getPhaseId())) {
114         decode(context);
115       }
116     }
117     context.getExternalContext().getRequestMap().put(AbstractUIForm.SUBMITTED_MARKER, isSubmitted());
118 
119     return super.invokeOnComponent(context, clientId, callback);
120   }
121 }