KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > DialogMVCHandler


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.module.admininterface;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.beans.runtime.MultipartForm;
17 import info.magnolia.cms.core.Content;
18 import info.magnolia.cms.core.HierarchyManager;
19 import info.magnolia.cms.gui.control.Save;
20 import info.magnolia.cms.gui.dialog.DialogDialog;
21 import info.magnolia.cms.gui.dialog.DialogFactory;
22 import info.magnolia.cms.gui.misc.Sources;
23 import info.magnolia.cms.i18n.MessagesManager;
24 import info.magnolia.cms.security.SessionAccessControl;
25 import info.magnolia.cms.servlets.MVCServletHandlerImpl;
26 import info.magnolia.cms.util.RequestFormUtil;
27 import info.magnolia.cms.util.Resource;
28
29 import java.io.IOException JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31
32 import javax.jcr.RepositoryException;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.log4j.Logger;
38
39
40 /**
41  * This is the MVCHandler for dialogs. You can make a subclass to take influence on creation or saving.
42  * @author Philipp Bracher
43  * @version $Id$
44  */

45
46 public class DialogMVCHandler extends MVCServletHandlerImpl {
47
48     /**
49      * Logger.
50      */

51     private static Logger log = Logger.getLogger(DialogMVCHandler.class);
52
53     /*
54      * Commands
55      */

56     protected static final String JavaDoc COMMAND_SAVE = "save"; //$NON-NLS-1$
57

58     protected static final String JavaDoc COMMAND_SELECT_PARAGRAPH = "selectParagraph"; //$NON-NLS-1$
59

60     protected static final String JavaDoc COMMAND_SHOW_DIALOG = "showDialog"; //$NON-NLS-1$
61

62     /*
63      * Views
64      */

65     protected static final String JavaDoc VIEW_CLOSE_WINDOW = "close"; //$NON-NLS-1$
66

67     protected static final String JavaDoc VIEW_SHOW_DIALOG = "show"; //$NON-NLS-1$
68

69     /**
70      * The posted multipart form. Use params for easy access.
71      */

72     protected MultipartForm form;
73
74     /**
75      * Path to the node containing the data
76      */

77     protected String JavaDoc path = StringUtils.EMPTY;
78
79     /**
80      * If the dialog serves a collection (multiple instances of the same dialog)
81      */

82     private String JavaDoc nodeCollectionName = StringUtils.EMPTY;
83
84     /**
85      * the node containing the date for this dialog
86      */

87     protected String JavaDoc nodeName = StringUtils.EMPTY;
88
89     protected String JavaDoc richE = StringUtils.EMPTY;
90
91     protected String JavaDoc richEPaste = StringUtils.EMPTY;
92
93     protected String JavaDoc repository = StringUtils.EMPTY;
94
95     protected HierarchyManager hm;
96
97     protected DialogDialog dialog;
98
99     protected info.magnolia.cms.i18n.Messages msgs;
100
101     protected RequestFormUtil params;
102
103     private Content storageNode;
104
105     /**
106      * Initialize the used parameters: path, nodeCollectionName, nodeName, ..
107      * @param request
108      * @param response
109      */

110     public DialogMVCHandler(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
111         super(name, request, response);
112
113         this.request = request;
114         this.response = response;
115
116         form = Resource.getPostedForm(request);
117
118         params = new RequestFormUtil(request, form);
119
120         path = params.getParameter("mgnlPath"); //$NON-NLS-1$
121
nodeCollectionName = params.getParameter("mgnlNodeCollection"); //$NON-NLS-1$
122
nodeName = params.getParameter("mgnlNode"); //$NON-NLS-1$
123
richE = params.getParameter("mgnlRichE"); //$NON-NLS-1$
124
richEPaste = params.getParameter("mgnlRichEPaste"); //$NON-NLS-1$
125
repository = params.getParameter("mgnlRepository", getRepository()); //$NON-NLS-1$
126

127         hm = SessionAccessControl.getHierarchyManager(request, repository);
128         msgs = MessagesManager.getMessages(request);
129     }
130
131     /*
132      * @see info.magnolia.cms.servlets.MVCServletHandler#getCommand()
133      */

134     public String JavaDoc getCommand() {
135         if (form != null) {
136             return COMMAND_SAVE;
137         }
138         return COMMAND_SHOW_DIALOG;
139     }
140
141     /**
142      * Calls createDialog and sets the common parameters on the dialog
143      * @return
144      */

145     public String JavaDoc showDialog() {
146         Content configNode = getConfigNode();
147         Content storageNode = getStorageNode();
148
149         try {
150             dialog = createDialog(configNode, storageNode);
151         }
152         catch (RepositoryException e) {
153             log.error("can't instantiate dialog", e); //$NON-NLS-1$
154
}
155
156         dialog.setConfig("dialog", getName()); //$NON-NLS-1$
157
dialog.setConfig("path", path); //$NON-NLS-1$
158
dialog.setConfig("nodeCollection", nodeCollectionName); //$NON-NLS-1$
159
dialog.setConfig("node", nodeName); //$NON-NLS-1$
160
dialog.setConfig("richE", richE); //$NON-NLS-1$
161
dialog.setConfig("richEPaste", richEPaste); //$NON-NLS-1$
162
dialog.setConfig("repository", repository); //$NON-NLS-1$
163

164         return VIEW_SHOW_DIALOG;
165     }
166
167     /**
168      * Is called during showDialog(). Here can you create/ add controls for the dialog.
169      * @param configNode
170      * @param storageNode
171      * @throws RepositoryException
172      */

173     protected DialogDialog createDialog(Content configNode, Content storageNode) throws RepositoryException {
174         return DialogFactory.getDialogDialogInstance(request, response, storageNode, configNode);
175     }
176
177     /**
178      * Uses the SaveControl. Override to take influence.
179      * @return close view name
180      */

181     public String JavaDoc save() {
182         Save control = onPreSave();
183         onSave(control);
184         onPostSave(control);
185         return VIEW_CLOSE_WINDOW;
186     }
187
188     protected Save onPreSave() {
189         return new Save(form, request);
190     }
191
192     protected void onSave(Save control) {
193         control.save();
194     }
195
196     protected void onPostSave(Save control) {
197     }
198
199     /**
200      * Defines the node/page containing the data editing in this dialog. The default implementation is using the path
201      * parameter
202      */

203     protected Content getStorageNode() {
204         if (storageNode == null) {
205             try {
206                 Content parentContent = hm.getContent(path);
207                 if (StringUtils.isEmpty(nodeName)) {
208                     if (StringUtils.isEmpty(nodeCollectionName)) {
209                         storageNode = parentContent;
210                     }
211                     else {
212                         storageNode = parentContent.getContent(nodeCollectionName);
213                     }
214                 }
215                 else {
216                     if (StringUtils.isEmpty(nodeCollectionName)) {
217                         storageNode = parentContent.getContent(nodeName);
218
219                     }
220                     else {
221                         storageNode = parentContent.getContent(nodeCollectionName).getContent(nodeName);
222
223                     }
224                 }
225             }
226             catch (RepositoryException re) {
227                 // content does not exist yet
228
}
229         }
230         return storageNode;
231     }
232
233     /**
234      * Returns the node with the dialog definition. Default: null
235      * @return
236      */

237     protected Content getConfigNode() {
238         return null;
239     }
240
241     /*
242      * (non-Javadoc)
243      * @see info.magnolia.cms.servlets.MVCServletHandler#renderHtml(java.lang.String)
244      */

245     public void renderHtml(String JavaDoc view) throws IOException JavaDoc {
246         PrintWriter JavaDoc out = response.getWriter();
247
248         // after saving
249
if (VIEW_CLOSE_WINDOW.equals(view)) {
250             out.println("<html>"); //$NON-NLS-1$
251
out.println(new Sources(request.getContextPath()).getHtmlJs());
252             out.println("<script type=\"text/javascript\">"); //$NON-NLS-1$
253
out.println("mgnlDialogReloadOpener();"); //$NON-NLS-1$
254
out.println("window.close();"); //$NON-NLS-1$
255
out.println("</script></html>"); //$NON-NLS-1$
256
}
257         // show the created dialog
258
else if (view == VIEW_SHOW_DIALOG) {
259             try {
260                 dialog.drawHtml(out);
261             }
262             catch (IOException JavaDoc e) {
263                 log.error(e);
264             }
265         }
266     }
267
268     /**
269      * @return the default repository
270      */

271     public String JavaDoc getRepository() {
272         return ContentRepository.WEBSITE;
273     }
274 }
Popular Tags