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 protected List readValues() {
171
172 List values = new ArrayList();
173
174 if (this.getStorageNode() != null) {
175 try {
176
177 int size = this.getStorageNode().getContent(this.getName()).getNodeDataCollection().size();
178 for (int i = 0; i < size; i++) {
179 NodeData data = this.getStorageNode().getContent(this.getName()).getNodeData("" + i);
180 values.add(data.getString());
181 }
182 }
183 catch (PathNotFoundException e) {
184
185 }
186 catch (RepositoryException re) {
187 log.error("can't set values", re);
188 }
189 }
190 return values;
191 }
192
193
194
195
196
197
198
199
200
201 protected Map getSubNodes(Content node) throws RepositoryException, AccessDeniedException {
202
203 Map values = new LinkedHashMap();
204
205
206 Collection properties = node.getNodeDataCollection();
207
208 if (properties != null && properties.size() > 0) {
209 Iterator propertiesIt = properties.iterator();
210 while (propertiesIt.hasNext()) {
211 NodeData property = (NodeData) propertiesIt.next();
212 values.put(property.getName(), NodeDataUtil.getValueObject(property));
213 }
214 }
215
216
217 Collection children = node.getChildren(ItemType.CONTENTNODE);
218 if (children != null && children.size() > 0) {
219 Iterator childrenIt = children.iterator();
220 while (childrenIt.hasNext()) {
221 Content child = (Content) childrenIt.next();
222
223
224 Map subValues = getSubNodes(child);
225
226 values.put(child.getName(), subValues);
227 }
228 }
229
230 return values;
231 }
232
233
234
235
236
237 public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode)
238 throws RepositoryException {
239 super.init(request, response, websiteNode, configNode);
240 configuration = this.getSubNodes(configNode);
241 }
242
243
244
245
246 public void drawHtml(Writer out) throws IOException {
247 String ftlPath = StringUtils.defaultIfEmpty(this.getConfigValue("path"), path);
248 drawHtml(out, ftlPath);
249 }
250
251
252
253
254
255
256 protected void drawHtml(Writer out, String freemarkerTemplate) throws IOException {
257 Map parameters = new HashMap();
258 parameters.put("name", this.getName());
259 parameters.put("value", this.getValue());
260 parameters.put("values", this.getValues());
261 parameters.put("request", this.getRequest());
262 parameters.put("configuration", this.configuration);
263
264 this.drawHtmlPre(out);
265
266 try {
267 FreemarkerControl control = new FreemarkerControl("multiple".equals(this.getConfigValue("valueType"))
268 ? ControlImpl.VALUETYPE_MULTIPLE
269 : ControlImpl.VALUETYPE_SINGLE);
270 control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING));
271 control.setName(this.getName());
272 control.drawHtml(out, freemarkerTemplate, parameters);
273 }
274
275 catch (TemplateException ex) {
276 log.error("Error processing dialog template:", ex);
277 throw new NestableRuntimeException(ex);
278 }
279
280 this.drawHtmlPost(out);
281 }
282
283 }