1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
35 public class RenderComponentTask extends ViewLifecycleTaskBase<Component> {
36
37 private static final Logger LOG = Logger.getLogger(RenderComponentTask.class);
38
39
40
41
42
43
44 public RenderComponentTask() {
45 super(Component.class);
46 }
47
48
49
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
71
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 }