KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > dialog > DialogSuper


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.dialog;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.ContentHandler;
17 import info.magnolia.cms.core.ItemType;
18 import info.magnolia.cms.core.NodeData;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Writer JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.jcr.RepositoryException;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.servlet.http.HttpSession JavaDoc;
33
34 import org.apache.commons.lang.BooleanUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.log4j.Logger;
37
38
39 /**
40  * @author Vinzenz Wyser
41  * @version 2.0
42  */

43 public abstract class DialogSuper implements DialogInterface {
44
45     public static final String JavaDoc SESSION_ATTRIBUTENAME_DIALOGOBJECT = "mgnlSessionAttribute"; //$NON-NLS-1$
46

47     public static final String JavaDoc SESSION_ATTRIBUTENAME_DIALOGOBJECT_REMOVE = "mgnlSessionAttributeRemove"; //$NON-NLS-1$
48

49     /**
50      * Logger.
51      */

52     private static Logger log = Logger.getLogger(DialogSuper.class);
53
54     /**
55      * Current request.
56      */

57     private HttpServletRequest JavaDoc request;
58
59     /**
60      * Current response.
61      */

62     private HttpServletResponse JavaDoc response;
63
64     /**
65      * content data.
66      */

67     private Content websiteNode;
68
69     /**
70      * config data.
71      */

72     private Map JavaDoc config = new Hashtable JavaDoc();
73
74     /**
75      * Sub controls.
76      */

77     private List JavaDoc subs = new ArrayList JavaDoc();
78
79     /**
80      * options (radio, checkbox...).
81      */

82     private List JavaDoc options = new ArrayList JavaDoc();
83
84     private String JavaDoc id = "mgnlControl"; //$NON-NLS-1$
85

86     private String JavaDoc value;
87
88     /**
89      * multiple values, e.g. checkbox.
90      */

91     private List JavaDoc values = new ArrayList JavaDoc();
92
93     private DialogSuper parent;
94
95     private DialogSuper topParent;
96
97     /**
98      */

99     public void init(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Content websiteNode, Content configNode)
100         throws RepositoryException {
101
102         if (log.isDebugEnabled()) {
103             log.debug("Init " + getClass().getName()); //$NON-NLS-1$
104
}
105
106         this.websiteNode = websiteNode;
107         this.request = request;
108         this.response = response;
109
110         this.initializeConfig(configNode);
111     }
112
113     /**
114      * @see info.magnolia.cms.gui.dialog.DialogInterface#drawHtml(Writer)
115      */

116     public void drawHtml(Writer JavaDoc out) throws IOException JavaDoc {
117         this.drawHtmlPreSubs(out);
118         this.drawSubs(out);
119         this.drawHtmlPostSubs(out);
120     }
121
122     public void addSub(Object JavaDoc o) {
123         this.getSubs().add(o);
124     }
125
126     public void setConfig(String JavaDoc key, String JavaDoc value) {
127         if (value != null) {
128             this.config.put(key, value);
129         }
130     }
131
132     public void setConfig(String JavaDoc key, boolean value) {
133         this.config.put(key, BooleanUtils.toBooleanObject(value).toString());
134     }
135
136     public void setConfig(String JavaDoc key, int value) {
137         this.config.put(key, Integer.toString(value));
138     }
139
140     public String JavaDoc getConfigValue(String JavaDoc key, String JavaDoc nullValue) {
141         if (this.config.containsKey(key)) {
142             return (String JavaDoc) this.config.get(key);
143         }
144
145         return nullValue;
146     }
147
148     public String JavaDoc getConfigValue(String JavaDoc key) {
149         return this.getConfigValue(key, StringUtils.EMPTY);
150     }
151
152     public void setValue(String JavaDoc s) {
153         this.value = s;
154     }
155
156     public String JavaDoc getValue() {
157         if (this.value != null) {
158             return this.value;
159         }
160         else if (this.getWebsiteNode() != null) {
161             return this.getWebsiteNode().getNodeData(this.getName()).getString();
162         }
163         else {
164             return StringUtils.EMPTY;
165         }
166     }
167
168     public void setSaveInfo(boolean b) {
169         this.setConfig("saveInfo", b); //$NON-NLS-1$
170
}
171
172     public void setName(String JavaDoc s) {
173         this.setConfig("name", s); //$NON-NLS-1$
174
}
175
176     public String JavaDoc getName() {
177         return this.getConfigValue("name"); //$NON-NLS-1$
178
}
179
180     public void addOption(Object JavaDoc o) {
181         this.getOptions().add(o);
182     }
183
184     public Content getWebsiteNode() {
185         return this.websiteNode;
186     }
187
188     public void setLabel(String JavaDoc s) {
189         this.config.put("label", s); //$NON-NLS-1$
190
}
191
192     public void setDescription(String JavaDoc s) {
193         this.config.put("description", s); //$NON-NLS-1$
194
}
195
196     public void removeSessionAttribute() {
197         String JavaDoc name = this.getConfigValue(SESSION_ATTRIBUTENAME_DIALOGOBJECT);
198         HttpServletRequest JavaDoc request = this.getRequest();
199         if (request == null) {
200             request = this.getTopParent().getRequest();
201         }
202         try {
203             HttpSession JavaDoc session = request.getSession();
204             session.removeAttribute(name);
205         }
206         catch (Exception JavaDoc e) {
207             log.debug("removeSessionAttribute() for " + name + " failed because this.request is null"); //$NON-NLS-1$ //$NON-NLS-2$
208
}
209     }
210
211     public HttpServletRequest JavaDoc getRequest() {
212         return this.request;
213     }
214
215     public void setOptions(List JavaDoc options) {
216         this.options = options;
217     }
218
219     protected void drawHtmlPreSubs(Writer JavaDoc out) throws IOException JavaDoc {
220         // do nothing
221
}
222
223     protected void drawSubs(Writer JavaDoc out) throws IOException JavaDoc {
224         Iterator JavaDoc it = this.getSubs().iterator();
225         int i = 0;
226         while (it.hasNext()) {
227             // use underscore (not divis)! could be used as js variable names
228
String JavaDoc dsId = this.getId() + "_" + i; //$NON-NLS-1$
229

230             DialogSuper ds = (DialogSuper) it.next();
231             ds.setId(dsId);
232             ds.setParent(this);
233             if (this.getParent() == null) {
234                 this.setTopParent(this);
235             }
236             ds.setTopParent(this.getTopParent());
237             ds.drawHtml(out);
238             i++;
239         }
240     }
241
242     protected void drawHtmlPostSubs(Writer JavaDoc out) throws IOException JavaDoc {
243         // do nothing
244
}
245
246     protected DialogSuper getParent() {
247         return this.parent;
248     }
249
250     protected void setTopParent(DialogSuper top) {
251         this.topParent = top;
252     }
253
254     protected DialogSuper getTopParent() {
255         return this.topParent;
256     }
257
258     protected List JavaDoc getSubs() {
259         return this.subs;
260     }
261
262     protected HttpServletResponse JavaDoc getResponse() {
263         return this.response;
264     }
265
266     /**
267      * @deprecated websitenode should only be set in init(), this is a workaround used in DialogDate
268      */

269     protected void clearWebsiteNode() {
270         this.websiteNode = null;
271     }
272
273     protected String JavaDoc getId() {
274         return this.id;
275     }
276
277     protected String JavaDoc getLabel() {
278         return this.getConfigValue("label", StringUtils.EMPTY); //$NON-NLS-1$
279
}
280
281     protected String JavaDoc getDescription() {
282         return this.getConfigValue("description", StringUtils.EMPTY); //$NON-NLS-1$
283
}
284
285     protected List JavaDoc getOptions() {
286         return this.options;
287     }
288
289     protected List JavaDoc getValues() {
290         if (this.getWebsiteNode() == null) {
291             return this.values;
292         }
293
294         try {
295             Iterator JavaDoc it = this.getWebsiteNode().getContent(this.getName()).getNodeDataCollection().iterator();
296             List JavaDoc l = new ArrayList JavaDoc();
297             while (it.hasNext()) {
298                 NodeData data = (NodeData) it.next();
299                 l.add(data.getString());
300             }
301             return l;
302         }
303         catch (RepositoryException re) {
304             return this.values;
305         }
306
307     }
308
309     protected void setSessionAttribute() {
310         String JavaDoc name = SESSION_ATTRIBUTENAME_DIALOGOBJECT + "_" + this.getName() + "_" + new Date JavaDoc().getTime(); //$NON-NLS-1$ //$NON-NLS-2$
311
this.setConfig(SESSION_ATTRIBUTENAME_DIALOGOBJECT, name);
312         HttpServletRequest JavaDoc request = this.getRequest();
313         if (request == null) {
314             request = this.getTopParent().getRequest();
315         }
316         try {
317             HttpSession JavaDoc session = request.getSession();
318             session.setAttribute(name, this);
319         }
320         catch (Exception JavaDoc e) {
321             log.error("setSessionAttribute() for " + name + " failed because this.request is null"); //$NON-NLS-1$ //$NON-NLS-2$
322
}
323     }
324
325     private void setId(String JavaDoc id) {
326         this.id = id;
327     }
328
329     private void initializeConfig(Content configNodeParent) throws RepositoryException {
330         // create config and subs out of dialog structure
331
Map JavaDoc config = new Hashtable JavaDoc();
332
333         if (configNodeParent == null) {
334             // can happen only if Dialog is instantiated directly
335
return;
336         }
337
338         // get properties -> to this.config
339
Iterator JavaDoc itProps = configNodeParent.getNodeDataCollection().iterator();
340         while (itProps.hasNext()) {
341             NodeData data = (NodeData) itProps.next();
342             String JavaDoc name = data.getName();
343             String JavaDoc value = data.getString();
344             config.put(name, value);
345         }
346         this.config = config;
347
348         Iterator JavaDoc it = configNodeParent.getChildren(ItemType.CONTENTNODE, ContentHandler.SORT_BY_SEQUENCE).iterator();
349         while (it.hasNext()) {
350             Content configNode = (Content) it.next();
351             String JavaDoc controlType = configNode.getNodeData("controlType").getString(); //$NON-NLS-1$
352

353             if (StringUtils.isEmpty(controlType)) {
354                 String JavaDoc name = configNode.getName();
355                 if (!name.startsWith("options")) { //$NON-NLS-1$
356
log.warn("Missing control type for configNode " + name); //$NON-NLS-1$
357
}
358                 return;
359             }
360
361             if (log.isDebugEnabled()) {
362                 log.debug("Loading control \"" + controlType + "\" for " + configNode.getHandle()); //$NON-NLS-1$ //$NON-NLS-2$
363
}
364             DialogInterface dialogControl = DialogFactory.loadDialog(
365                 request,
366                 response,
367                 this.getWebsiteNode(),
368                 configNode);
369             this.addSub(dialogControl);
370         }
371     }
372
373     private void setParent(DialogSuper parent) {
374         this.parent = parent;
375     }
376
377 }
378
Popular Tags