1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.objectledge.web.mvc.components;
29
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34
35 import org.objectledge.context.Context;
36 import org.objectledge.pipeline.ProcessingException;
37 import org.objectledge.templating.Template;
38 import org.objectledge.templating.TemplatingContext;
39 import org.objectledge.utils.CollectionUtils;
40 import org.objectledge.web.mvc.builders.BuildException;
41 import org.objectledge.web.mvc.builders.DefaultTemplate;
42 import org.objectledge.web.mvc.finders.MVCClassFinder;
43 import org.objectledge.web.mvc.finders.MVCTemplateFinder;
44 import org.objectledge.web.mvc.security.SecurityHelper;
45
46 /***
47 * A templating context tool for embedding components in application UI.
48 *
49 * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a>
50 * @version $Id: ComponentTool.java,v 1.16 2005/07/22 17:25:52 pablo Exp $
51 */
52 public class ComponentTool
53 {
54 /*** The context. */
55 protected Context context;
56
57 /*** The class finder for finding component objects. */
58 protected MVCClassFinder classFinder;
59
60 /*** The template finder for finding component templates. */
61 protected MVCTemplateFinder templateFinder;
62
63 /*** SecurityHelper for access checking. */
64 protected SecurityHelper securityHelper;
65
66 /*** the default component implementation. */
67 protected Component defaultComponent;
68
69 /*** the default template. */
70 protected Template defaultTemplate;
71
72 /***
73 * Construct a component tool.
74 *
75 * @param context thread's processing context.
76 * @param classFinder class finder for finding component objects.
77 * @param templateFinder template finder for finding component templates.
78 * @param securityHelper security helper for access checking
79 */
80 public ComponentTool(Context context, MVCClassFinder classFinder,
81 MVCTemplateFinder templateFinder, SecurityHelper securityHelper)
82 {
83 this.context = context;
84 this.classFinder = classFinder;
85 this.templateFinder = templateFinder;
86 this.securityHelper = securityHelper;
87 this.defaultComponent = new DefaultComponent(context);
88 this.defaultTemplate = new DefaultTemplate();
89 }
90
91 /***
92 * Embeds a component with a givemn name.
93 *
94 * @param componentName name of the component to be embedded.
95 * @return contents of a rendered component
96 * @throws BuildException on problems with component building
97 * @throws ProcessingException if processing fails.
98 */
99 public String embed(String componentName)
100 throws BuildException, ProcessingException
101 {
102 return embed(componentName, (Map)null);
103 }
104
105 /***
106 * Embeds a component with a givemn name.
107 *
108 * @param componentName name of the component to be embedded.
109 * @param config localy scoped configuration in list form.
110 * @return contents of a rendered component
111 * @throws BuildException on problems with component building
112 * @throws ProcessingException if processing fails.
113 */
114 public String embed(String componentName, List config)
115 throws BuildException, ProcessingException
116 {
117 return embed(componentName, CollectionUtils.listToMap(config));
118 }
119
120 /***
121 * Embeds a component with a givemn name.
122 *
123 * @param componentName name of the component to be embedded.
124 * @param config localy scoped configuration.
125 * @return contents of a rendered component
126 * @throws BuildException on problems with component building
127 * @throws ProcessingException if processing fails.
128 */
129 public String embed(String componentName, Map config)
130 throws BuildException, ProcessingException
131 {
132 Component component = classFinder.getComponent(componentName);
133 Template template = templateFinder.getComponentTemplate(componentName);
134 template = resolveTemplate(template);
135 if(component != null)
136 {
137 Template newTemplate = component.getTemplate();
138 if(newTemplate != null)
139 {
140 template = newTemplate;
141 }
142 if(template == null)
143 {
144 template = defaultTemplate;
145 }
146 }
147 else
148 {
149 if(template == null)
150 {
151 throw new IllegalArgumentException("No component nor template with name "
152 + componentName);
153 }
154 component = defaultComponent;
155 }
156 securityHelper.checkSecurity(component, context);
157 String result = null;
158 try
159 {
160 if(config != null)
161 {
162 Map<String, Object> store = new HashMap<String, Object>();
163 TemplatingContext tContext =
164 TemplatingContext.getTemplatingContext(context);
165 Iterator i = config.keySet().iterator();
166 while(i.hasNext())
167 {
168 String key = (String)i.next();
169 store.put(key, tContext.get(key));
170 tContext.put(key, config.get(key));
171 }
172 result = component.build(template);
173 i = config.keySet().iterator();
174 while(i.hasNext())
175 {
176 String key = (String)i.next();
177 tContext.put(key, store.get(key));
178 }
179 }
180 else
181 {
182 result = component.build(template);
183 }
184 return result;
185 }
186 catch(Exception e)
187 {
188 throw new ProcessingException("Failed to build component with template: "+template.getName(), e);
189 }
190 }
191
192 /***
193 * Allows overriding of the used template.
194 * @param template the template to be overriden.
195 */
196 protected Template resolveTemplate(Template template)
197 {
198 return template;
199 }
200 }