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