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.Visual;
23  import org.apache.myfaces.tobago.internal.layout.LayoutUtils;
24  import org.apache.myfaces.tobago.util.ComponentUtils;
25  import org.apache.myfaces.tobago.util.DebugUtils;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import javax.faces.component.UIComponent;
30  import javax.faces.component.UIViewRoot;
31  import javax.faces.context.FacesContext;
32  import java.io.IOException;
33  import java.util.Iterator;
34  
35  public abstract class AbstractUIPage extends AbstractUIForm implements Visual {
36  
37    private static final Logger LOG = LoggerFactory.getLogger(AbstractUIPage.class);
38  
39    public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.Page";
40  
41    public static final String FORM_ACCEPT_CHARSET = "utf-8";
42  
43    private String formId;
44  
45    @Override
46    public boolean getRendersChildren() {
47      return true;
48    }
49  
50    public String getFormId(final FacesContext facesContext) {
51      if (formId == null) {
52        formId = getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "form";
53      }
54      return formId;
55    }
56  
57    @Override
58    public void encodeBegin(final FacesContext facesContext) throws IOException {
59      super.encodeBegin(facesContext);
60      final UIComponent layoutManager = LayoutUtils.getLayoutManager(this);
61      if (layoutManager != null) {
62        layoutManager.encodeBegin(facesContext);
63      }
64    }
65  
66    @Override
67    public void encodeChildren(final FacesContext facesContext) throws IOException {
68      final UIComponent layoutManager = LayoutUtils.getLayoutManager(this);
69      if (layoutManager != null) {
70        layoutManager.encodeChildren(facesContext);
71      } else {
72        super.encodeChildren(facesContext);
73      }
74    }
75  
76    @Override
77    public void encodeEnd(final FacesContext facesContext) throws IOException {
78      final UIComponent layoutManager = LayoutUtils.getLayoutManager(this);
79      if (layoutManager != null) {
80        layoutManager.encodeEnd(facesContext);
81      }
82      super.encodeEnd(facesContext);
83      if (LOG.isTraceEnabled()) {
84        LOG.trace(DebugUtils.toString(this.getParent(), 0));
85      }
86    }
87  
88    @Override
89    public void processDecodes(final FacesContext context) {
90  
91      decode(context);
92  
93      markSubmittedForm(context);
94  
95      // invoke processDecodes() on children
96      for (final Iterator kids = getFacetsAndChildren(); kids.hasNext();) {
97        final UIComponent kid = (UIComponent) kids.next();
98        kid.processDecodes(context);
99      }
100   }
101 
102   public void markSubmittedForm(final FacesContext facesContext) {
103     // find the form of the action command and set submitted to it and all
104     // children
105 
106     final UIViewRoot viewRoot = facesContext.getViewRoot();
107 
108     // reset old submitted state
109     setSubmitted(false);
110 
111     String sourceId = facesContext.getExternalContext().getRequestParameterMap().get("javax.faces.source");
112     UIComponent command = null;
113     if (sourceId != null) {
114       if (LOG.isDebugEnabled()) {
115         LOG.debug("sourceId = '" + sourceId + "'");
116       }
117       command = viewRoot.findComponent(sourceId);
118     } else {
119       LOG.warn("No sourceId found!");
120     }
121 
122     // TODO: remove this if block if proven this never happens anymore
123     if (command == null
124         && sourceId != null && sourceId.matches(".*:\\d+:.*")) {
125       // If currentActionId component was inside a sheet the id contains the
126       // rowIndex and is therefore not found here.
127       // We do not need the row here because we want just to find the
128       // related form, so removing the rowIndex will help here.
129       sourceId = sourceId.replaceAll(":\\d+:", ":");
130       try {
131         command = viewRoot.findComponent(sourceId);
132         //LOG.info("command = \"" + command + "\"", new Exception());
133       } catch (final Exception e) {
134         // ignore
135       }
136     }
137 
138     if (LOG.isTraceEnabled()) {
139       LOG.trace(sourceId);
140       LOG.trace("command:{}", command);
141       LOG.trace(DebugUtils.toString(viewRoot, 0));
142     }
143 
144     if (command != null) {
145       final AbstractUIForm form = ComponentUtils.findForm(command);
146       form.setSubmitted(true);
147 
148       if (LOG.isTraceEnabled()) {
149         LOG.trace("form:{}", form);
150         LOG.trace(form.getClientId(facesContext));
151       }
152     } else {
153       if (LOG.isDebugEnabled()) {
154         LOG.debug("Illegal actionId! Render response...");
155       }
156       facesContext.renderResponse();
157     }
158   }
159 }