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
29
30
31
32
33
34 package info.magnolia.module.templating;
35
36 import info.magnolia.cms.core.AggregationState;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.i18n.I18nContentWrapper;
39 import info.magnolia.context.MgnlContext;
40
41 import java.io.Writer;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.Map;
45
46 import org.apache.commons.lang.exception.ExceptionUtils;
47
48
49
50
51
52
53
54
55
56
57
58
59 @Deprecated
60 public abstract class AbstractRenderer implements RenderingModelBasedRenderer {
61
62 private static final String MODEL_ATTRIBUTE = RenderingModel.class.getName();
63
64 public AbstractRenderer() {
65 }
66
67 protected void render(Content content, RenderableDefinition definition, Writer out) throws RenderException {
68
69 final RenderingModel parentModel = (RenderingModel) MgnlContext.getAttribute(MODEL_ATTRIBUTE);
70
71 RenderingModel model;
72 String actionResult;
73
74 model = (RenderingModel) MgnlContext.getAttribute(ModelExecutionFilter.MODEL_ATTRIBUTE_PREFIX + content.getUUID());
75
76 if (model == null) {
77
78 model = newModel(content, definition, parentModel);
79
80 actionResult = model.execute();
81
82 if (RenderingModel.SKIP_RENDERING.equals(actionResult)) {
83 return;
84 }
85 } else {
86 actionResult = (String) MgnlContext.getAttribute(ModelExecutionFilter.ACTION_RESULT_ATTRIBUTE_PREFIX + content.getUUID());
87 if (model instanceof EarlyExecutionAware) {
88 ((EarlyExecutionAware)model).setParent(parentModel);
89 }
90 }
91
92 String templatePath = determineTemplatePath(content, definition, model, actionResult);
93
94 final Map ctx = newContext();
95 final Map savedContextState = saveContextState(ctx);
96 setupContext(ctx, content, definition, model, actionResult);
97 MgnlContext.setAttribute(MODEL_ATTRIBUTE, model);
98 onRender(content, definition, out, ctx, templatePath);
99 MgnlContext.setAttribute(MODEL_ATTRIBUTE, parentModel);
100
101 restoreContext(ctx, savedContextState);
102 }
103
104 protected String determineTemplatePath(Content content, RenderableDefinition definition, RenderingModel model, final String actionResult) {
105 String templatePath = definition.determineTemplatePath(actionResult, model);
106
107 if (templatePath == null) {
108 throw new IllegalStateException("Unable to render " + definition.getClass().getName() + " " + definition.getName() + " in page " + content.getHandle() + ": templatePath not set.");
109 }
110 return templatePath;
111 }
112
113
114
115
116 @Override
117 public RenderingModel newModel(Content content, RenderableDefinition definition, RenderingModel parentModel) throws RenderException {
118 try {
119 final Content wrappedContent = wrapNodeForModel(content, getMainContentSafely(content));
120 return definition.newModel(wrappedContent, definition, parentModel);
121 } catch (Exception e) {
122 throw new RenderException("Can't create rendering model: " + ExceptionUtils.getRootCauseMessage(e), e);
123 }
124 }
125
126 protected Map saveContextState(final Map ctx) {
127 Map state = new HashMap();
128
129 saveAttribute(ctx, state, "content");
130 saveAttribute(ctx, state, "def");
131 saveAttribute(ctx, state, "state");
132 saveAttribute(ctx, state, "mgnl");
133 saveAttribute(ctx, state, "model");
134 saveAttribute(ctx, state, "actionResult");
135
136 saveAttribute(ctx, state, getPageAttributeName());
137 return state;
138 }
139
140 protected void saveAttribute(final Map ctx, Map state, String name) {
141 state.put(name, ctx.get(name));
142 }
143
144 protected void restoreContext(final Map ctx, Map state) {
145 for (Iterator iterator = state.keySet().iterator(); iterator.hasNext();) {
146 String name = (String) iterator.next();
147 setContextAttribute(ctx, name, state.get(name));
148 }
149 }
150
151 protected void setupContext(final Map ctx, Content content, RenderableDefinition definition, RenderingModel model, Object actionResult){
152 final Content mainContent = getMainContentSafely(content);
153
154 setContextAttribute(ctx, getPageAttributeName(), wrapNodeForTemplate(mainContent, mainContent));
155 setContextAttribute(ctx, "content", wrapNodeForTemplate(content, mainContent));
156 setContextAttribute(ctx, "def", definition);
157 setContextAttribute(ctx, "state", getAggregationStateSafely());
158 setContextAttribute(ctx, "mgnl", getMagnoliaTemplatingUtilities());
159 setContextAttribute(ctx, "model", model);
160 setContextAttribute(ctx, "actionResult", actionResult);
161 }
162
163
164
165
166 protected Content getMainContentSafely(Content current) {
167 AggregationState state = getAggregationStateSafely();
168 if(state != null){
169 return state.getMainContent();
170 }
171 return current;
172 }
173
174
175
176
177 protected AggregationState getAggregationStateSafely() {
178 if(MgnlContext.isWebContext()){
179 return MgnlContext.getAggregationState();
180 }
181 return null;
182 }
183
184 protected MagnoliaTemplatingUtilities getMagnoliaTemplatingUtilities() {
185 return MagnoliaTemplatingUtilities.getInstance();
186 }
187
188
189
190
191
192
193 protected Content wrapNodeForModel(Content currentContent, Content mainContent) {
194 return new I18nContentWrapper(currentContent);
195 }
196
197
198
199
200
201
202
203
204 protected Content wrapNodeForTemplate(Content currentContent, Content mainContent) {
205 return new I18nContentWrapper(currentContent);
206 }
207
208 protected Object setContextAttribute(final Map ctx, final String name, Object value) {
209 return ctx.put(name, value);
210 }
211
212
213
214
215 protected String getPageAttributeName() {
216 return "page";
217 }
218
219
220
221
222 protected abstract Map newContext();
223
224
225
226
227
228 protected abstract void onRender(Content content, RenderableDefinition definition, Writer out, Map ctx, String templatePath) throws RenderException;
229
230 }