1 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 ; 30 import java.io.PrintWriter ; 31 32 import javax.jcr.RepositoryException; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.apache.commons.lang.StringUtils; 37 import org.apache.log4j.Logger; 38 39 40 45 46 public class DialogMVCHandler extends MVCServletHandlerImpl { 47 48 51 private static Logger log = Logger.getLogger(DialogMVCHandler.class); 52 53 56 protected static final String COMMAND_SAVE = "save"; 58 protected static final String COMMAND_SELECT_PARAGRAPH = "selectParagraph"; 60 protected static final String COMMAND_SHOW_DIALOG = "showDialog"; 62 65 protected static final String VIEW_CLOSE_WINDOW = "close"; 67 protected static final String VIEW_SHOW_DIALOG = "show"; 69 72 protected MultipartForm form; 73 74 77 protected String path = StringUtils.EMPTY; 78 79 82 private String nodeCollectionName = StringUtils.EMPTY; 83 84 87 protected String nodeName = StringUtils.EMPTY; 88 89 protected String richE = StringUtils.EMPTY; 90 91 protected String richEPaste = StringUtils.EMPTY; 92 93 protected String 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 110 public DialogMVCHandler(String name, HttpServletRequest request, HttpServletResponse 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"); nodeCollectionName = params.getParameter("mgnlNodeCollection"); nodeName = params.getParameter("mgnlNode"); richE = params.getParameter("mgnlRichE"); richEPaste = params.getParameter("mgnlRichEPaste"); repository = params.getParameter("mgnlRepository", getRepository()); 127 hm = SessionAccessControl.getHierarchyManager(request, repository); 128 msgs = MessagesManager.getMessages(request); 129 } 130 131 134 public String getCommand() { 135 if (form != null) { 136 return COMMAND_SAVE; 137 } 138 return COMMAND_SHOW_DIALOG; 139 } 140 141 145 public String 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); } 155 156 dialog.setConfig("dialog", getName()); dialog.setConfig("path", path); dialog.setConfig("nodeCollection", nodeCollectionName); dialog.setConfig("node", nodeName); dialog.setConfig("richE", richE); dialog.setConfig("richEPaste", richEPaste); dialog.setConfig("repository", repository); 164 return VIEW_SHOW_DIALOG; 165 } 166 167 173 protected DialogDialog createDialog(Content configNode, Content storageNode) throws RepositoryException { 174 return DialogFactory.getDialogDialogInstance(request, response, storageNode, configNode); 175 } 176 177 181 public String 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 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 } 229 } 230 return storageNode; 231 } 232 233 237 protected Content getConfigNode() { 238 return null; 239 } 240 241 245 public void renderHtml(String view) throws IOException { 246 PrintWriter out = response.getWriter(); 247 248 if (VIEW_CLOSE_WINDOW.equals(view)) { 250 out.println("<html>"); out.println(new Sources(request.getContextPath()).getHtmlJs()); 252 out.println("<script type=\"text/javascript\">"); out.println("mgnlDialogReloadOpener();"); out.println("window.close();"); out.println("</script></html>"); } 257 else if (view == VIEW_SHOW_DIALOG) { 259 try { 260 dialog.drawHtml(out); 261 } 262 catch (IOException e) { 263 log.error(e); 264 } 265 } 266 } 267 268 271 public String getRepository() { 272 return ContentRepository.WEBSITE; 273 } 274 } | Popular Tags |