View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.freemarker;
17  
18  import java.util.Collections;
19  
20  import org.apache.log4j.Logger;
21  import org.kuali.rice.krad.uif.component.Component;
22  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
23  import org.kuali.rice.krad.uif.lifecycle.ViewLifecyclePhase;
24  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
25  
26  import freemarker.core.Environment;
27  import freemarker.core.Macro;
28  import freemarker.template.TemplateModel;
29  
30  /**
31   * Perform actual rendering on a component during the lifecycle.
32   * 
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class RenderComponentTask extends ViewLifecycleTaskBase<Component> {
36  
37      private static final Logger LOG = Logger.getLogger(RenderComponentTask.class);
38  
39      /**
40       * Constructor.
41       * 
42       * @param phase The render phase for the component.
43       */
44      public RenderComponentTask() {
45          super(Component.class);
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      @Override
52      protected void performLifecycleTask() {
53          Component component = (Component) getElementState().getElement();
54          if (!component.isRender() || component.getTemplate() == null) {
55              return;
56          }
57          
58          LifecycleRenderingContext renderingContext = ViewLifecycle.getRenderingContext();
59          renderingContext.clearRenderingBuffer();
60  
61          renderingContext.importTemplate(component.getTemplate());
62  
63          for (String additionalTemplate : component.getAdditionalTemplates()) {
64              renderingContext.importTemplate(additionalTemplate);
65          }
66  
67          try {
68              Environment env = renderingContext.getEnvironment();
69  
70              // Check for a single-arg macro, with the parameter name "component"
71              // defer for parent rendering if not found
72              Macro fmMacro = (Macro) env.getMainNamespace().get(component.getTemplateName());
73  
74              if (fmMacro == null) {
75                  return;
76              }
77  
78              String[] args = fmMacro.getArgumentNames();
79              if (args == null || args.length != 1 || !component.getComponentTypeName().equals(args[0])) {
80                  return;
81              }
82  
83              FreeMarkerInlineRenderUtils.renderTemplate(env, component,
84                      null, false, false, Collections.<String, TemplateModel> emptyMap());
85  
86              component.setRenderedHtmlOutput(renderingContext.getRenderedOutput());
87              component.setSelfRendered(true);
88          } catch (Throwable e) {
89              if (ViewLifecycle.isStrict()) {
90                  LOG.warn("Error rendering component during lifecycle phase " + getElementState()
91                          + " falling back to higher level rendering", e);
92              } else if (ViewLifecycle.isTrace() && LOG.isDebugEnabled()) {
93                  LOG.debug("component rendering failed during lifecycle phase " + getElementState()
94                          + " falling back to higher level rendering", e);
95              }
96          }
97  
98      }
99  }