View Javadoc

1   /*
2    * Copyright 2011 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/ecl1.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.io.IOException;
19  import java.util.Collections;
20  
21  import org.kuali.rice.krad.uif.component.Component;
22  import org.kuali.rice.krad.uif.lifecycle.AbstractViewLifecycleTask;
23  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
24  import org.kuali.rice.krad.uif.lifecycle.ViewLifecyclePhase;
25  
26  import freemarker.core.Environment;
27  import freemarker.core.Macro;
28  import freemarker.template.TemplateException;
29  import freemarker.template.TemplateModel;
30  
31  /**
32   * Perform actual rendering on a component during the lifecycle.
33   * 
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class RenderComponentTask extends AbstractViewLifecycleTask {
37  
38      /**
39       * Constructor.
40       * 
41       * @param phase The render phase for the component.
42       */
43      public RenderComponentTask(ViewLifecyclePhase phase) {
44          super(phase);
45      }
46  
47      /**
48       * @see org.kuali.rice.krad.uif.lifecycle.AbstractViewLifecycleTask#performLifecycleTask()
49       */
50      @Override
51      protected void performLifecycleTask() {
52          Component component = getPhase().getComponent();
53          LifecycleRenderingContext renderingContext = ViewLifecycle.getRenderingContext();
54          renderingContext.clearRenderingBuffer();
55  
56          try {
57              Environment env = renderingContext.getEnvironment();
58  
59              // Check for a single-arg macro, with the parameter name "component"
60              // defer for parent rendering if not found
61              Macro fmMacro = (Macro) env.getMainNamespace().get(component.getTemplateName());
62  
63              if (fmMacro == null) {
64                  return;
65              }
66  
67              String[] args = fmMacro.getArgumentNames();
68              if (args == null || args.length != 1 || !component.getComponentTypeName().equals(args[0])) {
69                  return;
70              }
71  
72              FreeMarkerInlineRenderUtils.renderTemplate(env, component,
73                      null, false, false, Collections.<String, TemplateModel> emptyMap());
74          } catch (TemplateException e) {
75              throw new IllegalStateException("Error rendering component " + component.getId(), e);
76          } catch (IOException e) {
77              throw new IllegalStateException("Error rendering component " + component.getId(), e);
78          }
79  
80          component.setSelfRendered(true);
81          component.setRenderedHtmlOutput(renderingContext.getRenderedOutput());
82      }
83  
84  }