1 13 14 package info.magnolia.module.admininterface; 15 16 import info.magnolia.cms.beans.config.MIMEMapping; 17 import info.magnolia.cms.beans.config.Subscriber; 18 import info.magnolia.cms.core.ItemType; 19 import info.magnolia.cms.gui.control.Tree; 20 import info.magnolia.cms.gui.misc.Sources; 21 import info.magnolia.cms.gui.misc.Spacer; 22 import info.magnolia.cms.servlets.MVCServletHandlerImpl; 23 24 import java.io.IOException ; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.apache.commons.lang.StringUtils; 30 31 32 38 39 public abstract class AdminTreeMVCHandler extends MVCServletHandlerImpl { 40 41 44 protected static final String COMMAND_SHOW_TREE = "show"; 46 protected static final String COMMAND_COPY_NODE = "copy"; 48 protected static final String COMMAND_MOVE_NODE = "move"; 50 protected static final String COMMAND_ACTIVATE = "activate"; 52 protected static final String COMMAND_DEACTIVATE = "deactivate"; 54 protected static final String COMMAND_CREATE_NODE = "createNode"; 56 protected static final String COMMAND_DELETE_NODE = "delete"; 58 protected static final String COMMAND_SAVE_VALUE = "saveValue"; 60 63 64 protected static final String VIEW_TREE = "tree"; 66 protected static final String VIEW_CREATE = "create"; 68 protected static final String VIEW_VALUE = "value"; 70 protected static final String VIEW_NOTHING = "nothing"; 72 protected static final String VIEW_COPY_MOVE = "copymove"; 74 77 private Tree tree; 78 79 private String path; 80 81 private String pathOpen; 82 83 private String pathSelected; 84 85 88 private String displayValue; 89 90 private String newPath; 91 92 95 private boolean browseMode; 96 97 101 protected String getRepository() { 102 return getName(); 103 } 104 105 public AdminTreeMVCHandler(String name, HttpServletRequest request, HttpServletResponse response) { 106 super(name, request, response); 107 108 tree = new Tree(name, getRepository(), request); 109 path = request.getParameter("path"); if (StringUtils.isEmpty(path)) { 111 path = "/"; } 113 114 pathOpen = request.getParameter("pathOpen"); pathSelected = request.getParameter("pathSelected"); 117 this.setBrowseMode(StringUtils.equals(request.getParameter("browseMode"), "true")); 118 } 119 120 124 public String getCommand() { 125 126 if (StringUtils.isNotEmpty(request.getParameter("treeAction"))) { int treeAction = Integer.parseInt(request.getParameter("treeAction")); 130 if (treeAction == Tree.ACTION_COPY) { 131 return COMMAND_COPY_NODE; 132 } 133 if (treeAction == Tree.ACTION_MOVE) { 134 return COMMAND_MOVE_NODE; 135 } 136 if (treeAction == Tree.ACTION_ACTIVATE) { 137 return COMMAND_ACTIVATE; 138 } 139 if (treeAction == Tree.ACTION_DEACTIVATE) { 140 return COMMAND_DEACTIVATE; 141 } 142 143 return request.getParameter("treeAction"); } 145 146 if (request.getParameter("createItemType") != null) { return COMMAND_CREATE_NODE; 149 } 150 151 if (request.getParameter("deleteNode") != null) { return COMMAND_DELETE_NODE; 153 } 154 155 if (request.getParameter("saveName") != null || "true".equals(request.getParameter("isNodeDataValue")) || "true".equals(request.getParameter("isNodeDataType"))) { return COMMAND_SAVE_VALUE; 162 } 163 return COMMAND_SHOW_TREE; 164 } 165 166 169 public String show() { 170 return VIEW_TREE; 171 } 172 173 177 public String createNode() { 178 String createItemType = ItemType.NT_NODEDATA; 179 if (request.getParameter("createItemType") != null) { createItemType = request.getParameter("createItemType"); } 182 183 tree.setPath(path); 184 tree.createNode(createItemType); 185 return VIEW_TREE; 186 } 187 188 191 public String copy() { 192 return copyOrMove(Tree.ACTION_COPY); 193 } 194 195 198 public String move() { 199 return copyOrMove(Tree.ACTION_MOVE); 200 } 201 202 206 private String copyOrMove(int action) { 207 String pathClipboard = request.getParameter("pathClipboard"); int pasteType = Integer.parseInt(request.getParameter("pasteType")); 210 newPath = tree.pasteNode(pathClipboard, pathSelected, pasteType, action); 211 if (pasteType == Tree.PASTETYPE_SUB) { 212 pathOpen = pathSelected; 213 } 214 else { 215 pathOpen = pathSelected.substring(0, pathSelected.lastIndexOf("/")); } 218 219 pathSelected = null; 220 return VIEW_COPY_MOVE; 221 } 222 223 public String delete() { 224 String deleteNode = request.getParameter("deleteNode"); tree.deleteNode(path, deleteNode); 226 return VIEW_TREE; 227 } 228 229 public String activate() { 230 boolean recursive = (request.getParameter("recursive") != null); tree.activateNode(pathSelected, recursive); 232 return VIEW_TREE; 233 } 234 235 public String deactivate() { 236 tree.deActivateNode(pathSelected); 237 return VIEW_TREE; 238 } 239 240 244 public String saveValue() { 245 String saveName = request.getParameter("saveName"); 247 boolean isNodeDataValue = "true".equals(request.getParameter("isNodeDataValue")); 250 boolean isNodeDataType = "true".equals(request.getParameter("isNodeDataType")); 253 String value = StringUtils.defaultString(request.getParameter("saveValue")); displayValue = StringUtils.EMPTY; 255 boolean isMeta = "true".equals(request.getParameter("isMeta")); boolean isLabel = "true".equals(request.getParameter("isLabel")); 260 if (isNodeDataValue || isNodeDataType) { 261 tree.setPath(StringUtils.substringBeforeLast(path, "/")); saveName = StringUtils.substringAfterLast(path, "/"); } 264 else { 265 tree.setPath(path); 267 } 268 269 if (isLabel) { 270 displayValue = rename(value); 271 } 272 else if (isNodeDataType) { 273 int type = Integer.valueOf(value).intValue(); 274 displayValue = tree.saveNodeDataType(saveName, type); 275 } 276 else { 277 displayValue = tree.saveNodeData(saveName, value, isMeta); 278 } 279 280 displayValue = StringUtils.defaultString(request.getParameter("displayValue"), value); 283 if (path.startsWith("/subscribers/")) { Subscriber.reload(); 286 } 287 else if (path.startsWith("/server/MIMEMapping")) { MIMEMapping.reload(); 289 } 290 291 return VIEW_VALUE; 292 } 293 294 299 protected String rename(String value) { 300 return tree.renameNode(value); 301 } 302 303 309 public void renderHtml(String view) throws IOException { 310 StringBuffer html = new StringBuffer (500); 311 312 if (VIEW_TREE.equals(view) || VIEW_CREATE.equals(view) || VIEW_COPY_MOVE.equals(view)) { 313 if (view != VIEW_CREATE) { 315 tree.setPathOpen(pathOpen); 316 tree.setPathSelected(pathSelected); 317 } 318 319 if (view == VIEW_COPY_MOVE) { 321 html.append("<input type=\"hidden\" id=\"mgnlSelectNode\" value=\"" + newPath + "\" />"); } 325 326 renderTree(html); 327 } 328 329 else if (view == VIEW_VALUE) { 331 html.append(displayValue); 332 } 333 response.getWriter().print(html); 334 } 335 336 341 protected abstract void prepareTree(Tree tree, HttpServletRequest request); 342 343 348 protected abstract void prepareContextMenu(Tree tree, HttpServletRequest request); 349 350 354 protected void renderTree(StringBuffer html) { 355 String mode = StringUtils.defaultString(request.getParameter("treeMode")); boolean snippetMode = mode.equals("snippet"); 358 tree.setJavascriptTree("mgnlTreeControl"); tree.setBrowseMode(this.isBrowseMode()); 360 361 if (!snippetMode) { 362 html.append("<html><head>"); html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>"); renderHeaderIncludes(html); 365 html.append("<title>Magnolia</title>"); html.append("</head>"); html.append("<body class=\"mgnlBgDark\" onload=\"" + tree.getJavascriptTree() + ".resizeOnload();\" >"); html.append(Spacer.getHtml(20, 20)); 369 } 370 371 tree.setSnippetMode(snippetMode); 372 tree.setHeight(50); 373 374 tree.setPath(path); 375 376 prepareTree(tree, request); 377 prepareContextMenu(tree, request); 378 379 if (!snippetMode) { 380 html.append("<div id=\"" + tree.getJavascriptTree() + "_DivSuper\" style=\"display:block;\">"); } 382 html.append(tree.getHtml()); 383 if (!snippetMode) { 384 html.append("</div>"); } 386 387 if (!snippetMode) { 388 html.append("</body></html>"); } 390 } 391 392 395 protected void renderHeaderIncludes(StringBuffer html) { 396 html.append(new Sources(request.getContextPath()).getHtmlJs()); 397 html.append(new Sources(request.getContextPath()).getHtmlCss()); 398 } 399 400 protected Tree getTree() { 401 return this.tree; 402 } 403 404 protected void setTree(Tree tree) { 405 this.tree = tree; 406 } 407 408 protected String getPath() { 409 return path; 410 } 411 412 415 public boolean isBrowseMode() { 416 return browseMode; 417 } 418 419 422 public void setBrowseMode(boolean browseMode) { 423 this.browseMode = browseMode; 424 } 425 426 } | Popular Tags |