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