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.cms.gui.dialog;
35
36 import freemarker.template.TemplateException;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.core.ItemType;
39 import info.magnolia.cms.core.NodeData;
40 import info.magnolia.cms.gui.control.ControlImpl;
41 import info.magnolia.cms.gui.control.FreemarkerControl;
42 import info.magnolia.cms.security.AccessDeniedException;
43 import info.magnolia.cms.util.NodeDataUtil;
44
45 import java.io.IOException;
46 import java.io.Writer;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.HashMap;
50 import java.util.Iterator;
51 import java.util.LinkedHashMap;
52 import java.util.List;
53 import java.util.Map;
54
55 import javax.jcr.PathNotFoundException;
56 import javax.jcr.PropertyType;
57 import javax.jcr.RepositoryException;
58 import javax.servlet.http.HttpServletRequest;
59 import javax.servlet.http.HttpServletResponse;
60
61 import org.apache.commons.lang.StringUtils;
62 import org.apache.commons.lang.exception.NestableRuntimeException;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 public class DialogFreemarker extends DialogBox {
133
134
135
136
137 private static Logger log = LoggerFactory.getLogger(DialogFreemarker.class);
138
139 private Map configuration;
140
141 private String path;
142
143
144
145
146
147 public void setPath(String path) {
148 this.path = path;
149 }
150
151
152
153
154
155 public Map getConfiguration() {
156 return configuration;
157 }
158
159
160
161
162
163 public String getPath() {
164 return path;
165 }
166
167
168
169
170 @Override
171 protected List readValues() {
172
173 List values = new ArrayList();
174
175 if (this.getStorageNode() != null) {
176 try {
177
178 int size = this.getStorageNode().getContent(this.getName()).getNodeDataCollection().size();
179 for (int i = 0; i < size; i++) {
180 NodeData data = this.getStorageNode().getContent(this.getName()).getNodeData("" + i);
181 values.add(data.getString());
182 }
183 }
184 catch (PathNotFoundException e) {
185
186 }
187 catch (RepositoryException re) {
188 log.error("can't set values", re);
189 }
190 }
191 return values;
192 }
193
194
195
196
197
198
199
200
201
202 protected Map getSubNodes(Content node) throws RepositoryException, AccessDeniedException {
203
204 Map values = new LinkedHashMap();
205
206
207 Collection properties = node.getNodeDataCollection();
208
209 if (properties != null && properties.size() > 0) {
210 Iterator propertiesIt = properties.iterator();
211 while (propertiesIt.hasNext()) {
212 NodeData property = (NodeData) propertiesIt.next();
213 values.put(property.getName(), NodeDataUtil.getValueObject(property));
214 }
215 }
216
217
218 Collection children = node.getChildren(ItemType.CONTENTNODE);
219 if (children != null && children.size() > 0) {
220 Iterator childrenIt = children.iterator();
221 while (childrenIt.hasNext()) {
222 Content child = (Content) childrenIt.next();
223
224
225 Map subValues = getSubNodes(child);
226
227 values.put(child.getName(), subValues);
228 }
229 }
230
231 return values;
232 }
233
234
235
236
237
238 @Override
239 public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode)
240 throws RepositoryException {
241 super.init(request, response, websiteNode, configNode);
242 configuration = this.getSubNodes(configNode);
243 }
244
245
246
247
248 @Override
249 public void drawHtml(Writer out) throws IOException {
250 String ftlPath = StringUtils.defaultIfEmpty(this.getConfigValue("path"), path);
251 drawHtml(out, ftlPath);
252 }
253
254
255
256
257
258
259 protected void drawHtml(Writer out, String freemarkerTemplate) throws IOException {
260 Map parameters = new HashMap();
261 parameters.put("name", this.getName());
262 parameters.put("value", this.getValue());
263 parameters.put("values", this.getValues());
264 parameters.put("request", this.getRequest());
265 parameters.put("configuration", this.configuration);
266
267 this.drawHtmlPre(out);
268
269 try {
270 FreemarkerControl control = new FreemarkerControl("multiple".equals(this.getConfigValue("valueType"))
271 ? ControlImpl.VALUETYPE_MULTIPLE
272 : ControlImpl.VALUETYPE_SINGLE);
273 control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING));
274 control.setName(this.getName());
275 control.drawHtml(out, freemarkerTemplate, parameters);
276 }
277
278 catch (TemplateException ex) {
279 log.error("Error processing dialog template:", ex);
280 throw new NestableRuntimeException(ex);
281 }
282
283 this.drawHtmlPost(out);
284 }
285
286 }