|
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 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| public class ComponentTool |
|
53 |
| { |
|
54 |
| |
|
55 |
| protected Context context; |
|
56 |
| |
|
57 |
| |
|
58 |
| protected MVCClassFinder classFinder; |
|
59 |
| |
|
60 |
| |
|
61 |
| protected MVCTemplateFinder templateFinder; |
|
62 |
| |
|
63 |
| |
|
64 |
| protected SecurityHelper securityHelper; |
|
65 |
| |
|
66 |
| |
|
67 |
| protected Component defaultComponent; |
|
68 |
| |
|
69 |
| |
|
70 |
| protected Template defaultTemplate; |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
92
| public ComponentTool(Context context, MVCClassFinder classFinder,
|
|
81 |
| MVCTemplateFinder templateFinder, SecurityHelper securityHelper) |
|
82 |
| { |
|
83 |
92
| this.context = context;
|
|
84 |
92
| this.classFinder = classFinder;
|
|
85 |
92
| this.templateFinder = templateFinder;
|
|
86 |
92
| this.securityHelper = securityHelper;
|
|
87 |
92
| this.defaultComponent = new DefaultComponent(context);
|
|
88 |
92
| this.defaultTemplate = new DefaultTemplate();
|
|
89 |
| } |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
230
| public String embed(String componentName)
|
|
100 |
| throws BuildException, ProcessingException |
|
101 |
| { |
|
102 |
230
| return embed(componentName, (Map)null);
|
|
103 |
| } |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
0
| public String embed(String componentName, List config)
|
|
115 |
| throws BuildException, ProcessingException |
|
116 |
| { |
|
117 |
0
| return embed(componentName, CollectionUtils.listToMap(config));
|
|
118 |
| } |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
230
| public String embed(String componentName, Map config)
|
|
130 |
| throws BuildException, ProcessingException |
|
131 |
| { |
|
132 |
230
| Component component = classFinder.getComponent(componentName);
|
|
133 |
230
| Template template = templateFinder.getComponentTemplate(componentName);
|
|
134 |
230
| template = resolveTemplate(template);
|
|
135 |
230
| if(component != null)
|
|
136 |
| { |
|
137 |
92
| Template newTemplate = component.getTemplate();
|
|
138 |
92
| if(newTemplate != null)
|
|
139 |
| { |
|
140 |
46
| template = newTemplate;
|
|
141 |
| } |
|
142 |
92
| if(template == null)
|
|
143 |
| { |
|
144 |
46
| template = defaultTemplate;
|
|
145 |
| } |
|
146 |
| } |
|
147 |
| else |
|
148 |
| { |
|
149 |
138
| if(template == null)
|
|
150 |
| { |
|
151 |
46
| throw new IllegalArgumentException("No component nor template with name "
|
|
152 |
| + componentName); |
|
153 |
| } |
|
154 |
92
| component = defaultComponent;
|
|
155 |
| } |
|
156 |
184
| securityHelper.checkSecurity(component, context);
|
|
157 |
184
| String result = null;
|
|
158 |
184
| try
|
|
159 |
| { |
|
160 |
184
| if(config != null)
|
|
161 |
| { |
|
162 |
0
| Map<String, Object> store = new HashMap<String, Object>();
|
|
163 |
0
| TemplatingContext tContext =
|
|
164 |
| TemplatingContext.getTemplatingContext(context); |
|
165 |
0
| Iterator i = config.keySet().iterator();
|
|
166 |
0
| while(i.hasNext())
|
|
167 |
| { |
|
168 |
0
| String key = (String)i.next();
|
|
169 |
0
| store.put(key, tContext.get(key));
|
|
170 |
0
| tContext.put(key, config.get(key));
|
|
171 |
| } |
|
172 |
0
| result = component.build(template);
|
|
173 |
0
| i = config.keySet().iterator();
|
|
174 |
0
| while(i.hasNext())
|
|
175 |
| { |
|
176 |
0
| String key = (String)i.next();
|
|
177 |
0
| tContext.put(key, store.get(key));
|
|
178 |
| } |
|
179 |
| } |
|
180 |
| else |
|
181 |
| { |
|
182 |
184
| result = component.build(template);
|
|
183 |
| } |
|
184 |
138
| return result;
|
|
185 |
| } |
|
186 |
| catch(Exception e) |
|
187 |
| { |
|
188 |
46
| throw new ProcessingException("Failed to build component with template: "+template.getName(), e);
|
|
189 |
| } |
|
190 |
| } |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
230
| protected Template resolveTemplate(Template template)
|
|
197 |
| { |
|
198 |
230
| return template;
|
|
199 |
| } |
|
200 |
| } |