1 package com.dotmarketing.viewtools; 2 3 import java.io.FileOutputStream ; 4 import java.util.ArrayList ; 5 import java.util.HashSet ; 6 import java.util.StringTokenizer ; 7 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.jsp.JspException ; 10 11 import org.apache.velocity.tools.view.tools.ViewTool; 12 13 import com.dotmarketing.beans.Host; 14 import com.dotmarketing.beans.Inode; 15 import com.dotmarketing.cache.FolderCache; 16 import com.dotmarketing.cache.IdentifierCache; 17 import com.dotmarketing.factories.HostFactory; 18 import com.dotmarketing.menubuilders.StaticMenuBuilder; 19 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory; 20 import com.dotmarketing.portlets.contentlet.model.Contentlet; 21 import com.dotmarketing.portlets.files.model.File; 22 import com.dotmarketing.portlets.folders.factories.FolderFactory; 23 import com.dotmarketing.portlets.folders.model.Folder; 24 import com.dotmarketing.portlets.htmlpages.model.HTMLPage; 25 import com.dotmarketing.portlets.links.model.Link; 26 import com.dotmarketing.util.Config; 27 import com.dotmarketing.util.Logger; 28 import com.dotmarketing.util.UtilMethods; 29 30 public class NavigationWebAPI implements ViewTool { 31 private final String MENU_VTL_PATH = "/WEB-INF/velocity/static/menus/"; 32 33 public String crumbTrail(HttpServletRequest request, String imgPath) throws JspException { 34 35 StringBuffer stringbuf = new StringBuffer (); 36 37 HashSet listItems = new HashSet (); 38 39 String path = request.getRequestURI(); 40 41 Logger.debug(NavigationWebAPI.class, "\n\n"); 42 Logger.debug(NavigationWebAPI.class, "CrumbTrailBuilderTag pagePath=" + path); 43 44 int idx1 = path.indexOf("/"); 45 int idx2 = path.indexOf("/", idx1 + 1); 46 47 stringbuf.append("<ul>"); 48 49 stringbuf.append("<li><a HREF=\"/\">Home</a>"); 50 if(UtilMethods.isSet(imgPath)) 51 stringbuf.append("<img SRC=\""+imgPath+"\" alt=\"\" />"); 52 stringbuf.append("</li>"); 53 listItems.add("Home"); 54 55 idx2 = 0; 56 57 String openPath = ""; 58 idx1 = path.indexOf("/", idx2 + 1); 59 int count = 0; 60 61 while (idx1 != -1) { 62 openPath = path.substring(0, idx1 + 1); 63 idx2 = idx1; 64 idx1 = path.indexOf("/", idx2 + 1); 65 Logger.debug(NavigationWebAPI.class, "main loop: openPath=" + openPath); 66 count++; 67 stringbuf = getFolderTrail(stringbuf, openPath, idx1, path, listItems, request, imgPath); 68 } 69 70 stringbuf.append("</ul>"); 71 72 return stringbuf.toString(); 73 } 74 75 private StringBuffer getFolderTrail(StringBuffer stringbuffer, String openPath, int idxOf, String path, HashSet listItems, 76 HttpServletRequest request, String imgPath) { 77 Host host = HostFactory.getCurrentHost(request); 78 Folder folder = FolderCache.getFolderByPath(openPath, host.getInode()); 79 80 if (idxOf != -1) { 81 Logger.debug(NavigationWebAPI.class, "getTrail: idxOf"); 82 if (listItems.add(folder.getTitle()) && folder.isShowOnMenu()) { 83 stringbuffer.append("<li><a HREF=\"" + folder.getPath() + "\">"); 84 stringbuffer.append(folder.getTitle() + "</a>"); 85 if(UtilMethods.isSet(imgPath)) 86 stringbuffer.append("<img SRC=\""+imgPath+"\" alt=\"\" />"); 87 stringbuffer.append("</li>"); 88 } 89 } else { 90 91 int idxTrail = path.lastIndexOf("/"); 92 93 if (idxTrail != (path.length() - 1)) { 94 95 openPath = path.substring(0, idxTrail + 1); 96 97 if (path.indexOf(".") != -1) { 98 99 String pagePath = path.substring(idxTrail + 1, path.length()); 100 Logger.debug(NavigationWebAPI.class, "getTrail: Page Path=" + pagePath); 101 Logger.debug(NavigationWebAPI.class, "getTrail: Path=" + openPath); 102 103 if (!pagePath.equals("index." + Config.getStringProperty("VELOCITY_PAGE_EXTENSION"))) { 104 105 long idInode = IdentifierCache.getPathFromIdCache(openPath + "index." + Config.getStringProperty("VELOCITY_PAGE_EXTENSION"), 106 host).getInode(); 107 if (idInode > 0) { 108 if (listItems.add(folder.getTitle())) { 109 stringbuffer.append("<li><a HREF=\"" + folder.getPath() + "\">"); 110 stringbuffer.append(folder.getTitle() + "</a>"); 111 if(UtilMethods.isSet(imgPath)) 112 stringbuffer.append("<img SRC=\""+imgPath+"\" alt=\"\" />"); 113 stringbuffer.append("</li>"); 114 } 115 } 116 117 Logger.debug(NavigationWebAPI.class, "getTrail: it's not an index page"); 118 stringbuffer = getPageTrail(stringbuffer, openPath, pagePath, listItems, request); 119 } else { 120 Logger.debug(NavigationWebAPI.class, "getTrail: it's index.html"); 121 if (listItems.add(folder.getTitle())) { 122 stringbuffer.append("<li>" + folder.getTitle()); 123 stringbuffer.append("</li>"); 124 } 125 } 126 } 127 } else { 128 Logger.debug(NavigationWebAPI.class, "getTrail: there isn't a page name"); 129 if (listItems.add(folder.getTitle())) { 130 stringbuffer.append("<li>" + folder.getTitle()); 131 stringbuffer.append("</li>"); 132 } 133 } 134 } 135 136 return stringbuffer; 137 } 138 139 @SuppressWarnings ("unchecked") 140 private StringBuffer getPageTrail(StringBuffer stringbuffer, String openPath, String pagePath, HashSet listItems, HttpServletRequest request) { 141 Host host = HostFactory.getCurrentHost(request); 142 143 if (UtilMethods.isSet(request.getParameter("crumbTitle"))) { 144 String title = request.getParameter("crumbTitle"); 145 stringbuffer.append("<li>" + title + "</li>"); 146 } else if (UtilMethods.isSet(request.getParameter("inode"))) { 147 String inode = request.getParameter("inode"); 148 Contentlet cont = ContentletFactory.getContentlet(inode); 149 stringbuffer.append("<li>" + cont.getTitle() + "</li>"); 150 } else { 151 Folder folder = FolderCache.getFolderByPath(openPath, host.getInode()); 152 153 long idInode = IdentifierCache.getPathFromIdCache(folder.getPath() + pagePath, host).getInode(); 154 if (idInode > 0) { 155 stringbuffer.append("<li>$HTMLPAGE_TITLE</li>"); 156 } 157 } 158 return stringbuffer; 159 } 160 161 public String createMenuByDepth(int startFromLevel, int maxDepth, HttpServletRequest request) throws JspException { 162 163 String currentPath = request.getRequestURI(); 164 StringTokenizer st = new StringTokenizer (currentPath, "/"); 165 int i = 1; 166 StringBuffer myPath = new StringBuffer ("/"); 167 boolean rightLevel = false; 168 while (st.hasMoreTokens()) { 169 if (i++ >= startFromLevel) { 170 rightLevel = true; 171 break; 172 } 173 String myToken = st.nextToken(); 174 if (!st.hasMoreTokens()) 175 break; 176 myPath.append(myToken); 177 myPath.append("/"); 178 179 } 180 181 String menuString = (rightLevel ? createMenu(myPath.toString(), maxDepth, request) : ""); 182 return menuString; 183 } 184 185 public String createMenu(String startFromPath, int numberOfLevels, HttpServletRequest request) throws JspException 186 { 187 String currentPath = request.getRequestURI(); 188 Host host = HostFactory.getCurrentHost(request); 189 long hostId = host.getInode(); 190 StringBuffer stringbuf = new StringBuffer (); 191 192 try { 193 194 Logger.debug(StaticMenuBuilder.class, "\n\nStaticMenuBuilder begins"); 195 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder start path=" + startFromPath); 196 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder number of levels=" + numberOfLevels); 197 198 if ((startFromPath == null) || (startFromPath.length() == 0)) { 199 200 Logger.debug(StaticMenuBuilder.class, "pagePath=" + currentPath); 201 202 int idx1 = currentPath.indexOf("/"); 203 int idx2 = currentPath.indexOf("/", idx1 + 1); 204 205 startFromPath = currentPath.substring(idx1, idx2 + 1); 206 207 Logger.debug(StaticMenuBuilder.class, "path=" + startFromPath); 208 } 209 210 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder hostId=" + host.getInode()); 211 212 java.util.List <Inode> itemsList = new ArrayList (); 213 String folderPath = ""; 214 String fileName = ""; 215 boolean fileExists = true; 216 217 java.io.File file = null; 218 String menuId = ""; 219 if ("/".equals(startFromPath)) { 220 String condition = "show_on_menu = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue(); 221 fileName = hostId + "_levels_" + numberOfLevels + "_static.vtl"; 222 menuId = String.valueOf(hostId); 223 file = new java.io.File (Config.CONTEXT.getRealPath(MENU_VTL_PATH + fileName)); 224 if (!file.exists() || file.length() == 0) { 225 itemsList = FolderFactory.getFoldersByParentAndCondition(hostId, condition); 226 folderPath = startFromPath; 227 fileExists = false; 228 } 229 } else { 230 231 Folder folder = FolderCache.getFolderByPath(startFromPath, hostId); 232 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder folder=" + folder.getPath()); 233 234 fileName = folder.getInode() + "_levels_" + numberOfLevels + "_static.vtl"; 235 menuId = String.valueOf(folder.getInode()); 236 file = new java.io.File (Config.CONTEXT.getRealPath(MENU_VTL_PATH + fileName)); 237 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder file=" + MENU_VTL_PATH + fileName); 238 239 if (!file.exists()) { 240 file.createNewFile(); 241 itemsList = FolderFactory.getMenuItems(folder); 242 folderPath = folder.getPath(); 243 fileExists = false; 244 } 245 } 246 String filePath = "static" + java.io.File.separator + "menus" + java.io.File.separator + fileName; 247 248 if (fileExists) { 249 return filePath; 250 } else { 251 252 if (itemsList.size() > 0) { 253 254 stringbuf.append("#if($EDIT_MODE)\n"); 255 stringbuf.append("<form action=\"${directorURL}\" method=\"post\" name=\"form_menu_" + menuId + "\" id=\"form_menu_" + menuId 256 + "\">\n"); 257 stringbuf.append("<input type=\"hidden\" name=\"cmd\" value=\"orderMenu\">\n"); 258 stringbuf.append("<input type=\"hidden\" name=\"path\" value=\"" + startFromPath + "\">\n"); 259 stringbuf.append("<input type=\"hidden\" name=\"hostId\" value=\"" + hostId + "\">\n"); 260 stringbuf.append("<input type=\"hidden\" name=\"pagePath\" value=\"$VTLSERVLET_URI\">\n"); 261 stringbuf.append("<input type=\"hidden\" name=\"referer\" value=\"$VTLSERVLET_URI\">\n"); 262 263 stringbuf.append("<div class=\"menuReorder\"><a HREF=\"javascript:submitMenu('form_menu_" + menuId + "');\"><img border=\"0\" SRC=\"/portal/images/icons/reorder.gif\" width=\"21\" height=\"12\" alt=\"Click to reorder menu\" /></a></div>"); 264 stringbuf.append("</form>"); 265 stringbuf.append("#end \n"); 266 267 stringbuf.append("<ul>\n"); 268 269 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder number of items=" + itemsList.size()); 271 272 for (Inode itemChild : itemsList) { 274 275 if (itemChild instanceof Folder) { 276 277 Folder folderChild = (Folder) itemChild; 278 279 281 stringbuf = buildSubFolderMenu(stringbuf, folderChild, numberOfLevels, 1); 282 283 } else if (itemChild instanceof Link) { 284 if (((Link) itemChild).isWorking() && !((Link) itemChild).isDeleted()) { 285 stringbuf.append("#if ($UtilMethods.inString($VTLSERVLET_URI,\"" + ((Link) itemChild).getProtocal() 286 + ((Link) itemChild).getUrl() + "\"))\n"); 287 stringbuf.append("<li class=\"active\"><a HREF=\"" + ((Link) itemChild).getProtocal() + ((Link) itemChild).getUrl() 288 + "\" target=\"" + ((Link) itemChild).getTarget() + "\">\n"); 289 stringbuf.append(((Link) itemChild).getTitle() + "</a></li>\n"); 290 stringbuf.append("#else\n"); 291 stringbuf.append("<li><a HREF=\"" + ((Link) itemChild).getProtocal() + ((Link) itemChild).getUrl() + "\" target=\"" 292 + ((Link) itemChild).getTarget() + "\">\n"); 293 stringbuf.append(((Link) itemChild).getTitle() + "</a></li>\n"); 294 stringbuf.append("#end \n"); 295 } 296 } else if (itemChild instanceof HTMLPage) { 297 if (((HTMLPage) itemChild).isWorking() && !((HTMLPage) itemChild).isDeleted()) { 298 stringbuf.append("#if ($UtilMethods.inString($VTLSERVLET_URI,\"" + startFromPath 299 + ((HTMLPage) itemChild).getPageUrl() + "\"))\n"); 300 stringbuf.append("<li class=\"active\"><a HREF=\"" + folderPath + ((HTMLPage) itemChild).getPageUrl() + "\">"); 301 stringbuf.append(((HTMLPage) itemChild).getTitle() + "</a></li>\n"); 302 stringbuf.append("#else\n"); 303 stringbuf.append("<li><a HREF=\"" + folderPath + ((HTMLPage) itemChild).getPageUrl() + "\">\n"); 304 stringbuf.append(((HTMLPage) itemChild).getTitle() + "</a></li>\n"); 305 stringbuf.append("#end \n"); 306 } 307 } else if (itemChild instanceof File) { 308 if (((File) itemChild).isWorking() && !((File) itemChild).isDeleted()) { 309 stringbuf.append("#if ($UtilMethods.inString($VTLSERVLET_URI,\"" + startFromPath + ((File) itemChild).getFileName() 310 + "\"))\n"); 311 stringbuf.append("<li><a HREF=\"" + folderPath + ((File) itemChild).getFileName() + "\">"); 312 stringbuf.append(((File) itemChild).getTitle() + "</a></li>\n"); 313 stringbuf.append("#else\n"); 314 stringbuf.append("#end \n"); 315 } 316 } 317 } 318 stringbuf.append("</ul>"); 319 320 } 321 322 323 324 if (stringbuf.toString().getBytes().length > 0) { 325 FileOutputStream fo = new FileOutputStream (file); 326 fo.write(stringbuf.toString().getBytes()); 327 fo.close(); 328 } else { 329 Logger.debug(StaticMenuBuilder.class, "Error creating static horizontal menu!!!!!"); 330 } 331 332 333 Logger.debug(StaticMenuBuilder.class, "End of StaticMenuBuilder" + filePath); 334 335 return filePath; 336 } 337 } catch (Exception e) { 338 stringbuf.delete(0, stringbuf.length()); 342 e.printStackTrace(System.out); 343 } 344 return ""; 345 } 346 347 public void init(Object obj) { 348 java.io.File fileFolder = new java.io.File (Config.CONTEXT.getRealPath(MENU_VTL_PATH)); 349 if (!fileFolder.exists()) { 350 fileFolder.mkdirs(); 351 } 352 } 353 354 private StringBuffer buildSubFolderMenu(StringBuffer stringbuf, Folder thisFolder, int numberOfLevels, int currentLevel) { 355 356 stringbuf.append("#if ($UtilMethods.inString($VTLSERVLET_URI,\"" + thisFolder.getPath() + "\"))\n"); 357 stringbuf.append("\t<li class=\"active\" id=\"" + thisFolder.getName() + "\">\n"); 358 stringbuf.append("#else\n"); 359 stringbuf.append("\t<li id=\"" + thisFolder.getName() + "\">\n"); 360 stringbuf.append("#end\n"); 361 java.util.List <Inode> itemsChildrenList2 = FolderFactory.getMenuItems(thisFolder); 363 364 boolean nextLevelItems = (itemsChildrenList2.size() > 0 && currentLevel < numberOfLevels); 366 367 String folderChildPath = thisFolder.getPath().substring(0, thisFolder.getPath().length() - 1); 368 folderChildPath = folderChildPath.substring(0, folderChildPath.lastIndexOf("/")); 369 370 stringbuf.append("<a HREF=\"" + thisFolder.getPath() + "\">"); 371 stringbuf.append(thisFolder.getTitle()); 372 stringbuf.append("</a>\n"); 373 374 if (currentLevel < numberOfLevels) { 375 376 if (nextLevelItems) { 377 stringbuf.append("#if ($UtilMethods.inString($VTLSERVLET_URI,\"" + thisFolder.getPath() + "\"))\n"); 378 stringbuf.append("<ul>\n"); 379 } 380 381 for (Inode childChild2 : itemsChildrenList2) { 382 if (childChild2 instanceof Folder) { 383 Folder folderChildChild2 = (Folder) childChild2; 384 385 Logger.debug(this, "folderChildChild2= " + folderChildChild2.getTitle() + " currentLevel=" + currentLevel + " numberOfLevels=" 386 + numberOfLevels); 387 if (currentLevel <= numberOfLevels) { 388 stringbuf = buildSubFolderMenu(stringbuf, folderChildChild2, numberOfLevels, currentLevel + 1); 389 } else { 390 391 stringbuf.append("<li><a HREF=\"" + folderChildChild2.getPath() + "index." 392 + Config.getStringProperty("VELOCITY_PAGE_EXTENSION") + "\">"); 393 stringbuf.append(folderChildChild2.getTitle() + "</a></li>\n"); 394 395 } 396 } else if (childChild2 instanceof Link) { 397 if (((Link) childChild2).isWorking() && !((Link) childChild2).isDeleted()) { 398 stringbuf.append("#if ($VTLSERVLET_URI != '" + ((Link) childChild2).getProtocal() + ((Link) childChild2).getUrl() + "')\n"); 399 stringbuf.append("<li><a HREF=\"" + ((Link) childChild2).getProtocal() + ((Link) childChild2).getUrl() + "\" target=\"" 400 + ((Link) childChild2).getTarget() + "\">"); 401 stringbuf.append(((Link) childChild2).getTitle() + "</a></li>\n"); 402 stringbuf.append("#else\n"); 403 stringbuf.append("<li class=\"active\"><a HREF=\"" + ((Link) childChild2).getProtocal() + ((Link) childChild2).getUrl() 404 + "\" target=\"" + ((Link) childChild2).getTarget() + "\">"); 405 stringbuf.append(((Link) childChild2).getTitle() + "</a></li>\n"); 406 stringbuf.append("#end \n"); 407 } 408 } else if (childChild2 instanceof HTMLPage) { 409 if (((HTMLPage) childChild2).isWorking() && !((HTMLPage) childChild2).isDeleted()) { 410 stringbuf.append("#if ($VTLSERVLET_URI != '" + thisFolder.getPath() + ((HTMLPage) childChild2).getPageUrl() + "')\n"); 411 stringbuf.append("<li><a HREF=\"" + thisFolder.getPath() + ((HTMLPage) childChild2).getPageUrl() + "\">"); 412 stringbuf.append(((HTMLPage) childChild2).getTitle() + "</a></li>\n"); 413 stringbuf.append("#else\n"); 414 stringbuf.append("<li class=\"active\"><a HREF=\"" + thisFolder.getPath() + ((HTMLPage) childChild2).getPageUrl() + "\">"); 415 stringbuf.append(((HTMLPage) childChild2).getTitle() + "</a></li>\n"); 416 stringbuf.append("#end \n"); 417 } 418 } else if (childChild2 instanceof File) { 419 if (((File) childChild2).isWorking() && !((File) childChild2).isDeleted()) { 420 stringbuf.append("#if ($VTLSERVLET_URI != '" + thisFolder.getPath() + ((File) childChild2).getFileName() + "')\n"); 421 stringbuf.append("<li><a HREF=\"" + thisFolder.getPath() + ((File) childChild2).getFileName() + "\">"); 422 stringbuf.append(((File) childChild2).getTitle() + "</a></li>\n"); 423 stringbuf.append("#else\n"); 424 stringbuf.append("<li class=\"active\"><a HREF=\"" + thisFolder.getPath() + ((File) childChild2).getFileName() + "\">"); 425 stringbuf.append(((File) childChild2).getTitle() + "</a></li>\n"); 426 stringbuf.append("#end \n"); 427 } 428 } 429 } 430 } 431 if (nextLevelItems) { 432 stringbuf.append("</ul>\n"); 433 stringbuf.append("#end\n"); 434 } 435 stringbuf.append("</li>\n"); 436 return stringbuf; 437 } 438 439 440 443 public String createSiteMapMenu(int startFromLevel, int numberOfLevels, String path, HttpServletRequest request,boolean addHome) throws JspException 444 { 445 String currentPath = path; 446 currentPath += " "; 447 StringTokenizer st = new StringTokenizer (currentPath, "/"); 448 int i = 0; 449 StringBuffer myPath = new StringBuffer ("/"); 450 while (st.hasMoreTokens()) { 451 if (i++ >= startFromLevel) { 452 break; 453 } 454 String myToken = st.nextToken(); 455 if (!st.hasMoreTokens()) 456 break; 457 myPath.append(myToken); 458 myPath.append("/"); 459 460 } 461 462 String startFromPath = myPath.toString().trim(); 463 return crateSiteMapMenu( startFromLevel,numberOfLevels, startFromPath,currentPath,addHome,request); 464 } 465 466 public String createSiteMapMenu(int startFromLevel, int numberOfLevels, HttpServletRequest request) throws JspException 467 468 { 469 String currentPath = request.getRequestURI(); 470 StringTokenizer st = new StringTokenizer (currentPath, "/"); 471 int i = 1; 472 StringBuffer myPath = new StringBuffer ("/"); 473 while (st.hasMoreTokens()) { 474 if (i++ >= startFromLevel) { 475 break; 476 } 477 String myToken = st.nextToken(); 478 if (!st.hasMoreTokens()) 479 break; 480 myPath.append(myToken); 481 myPath.append("/"); 482 483 } 484 String startFromPath = myPath.toString().trim(); 485 boolean addHome = true; 486 return crateSiteMapMenu( startFromLevel, numberOfLevels, startFromPath,currentPath,addHome,request); 487 } 488 489 private String crateSiteMapMenu(int startFromLevel, int numberOfLevels,String startFromPath,String currentPath,boolean addHome,HttpServletRequest request) 490 { 491 492 Host host = HostFactory.getCurrentHost(request); 493 long hostId = host.getInode(); 494 StringBuffer stringbuf = new StringBuffer (); 495 496 try { 497 498 Logger.debug(StaticMenuBuilder.class, "\n\nStaticMenuBuilder begins"); 499 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder start path=" + startFromPath); 500 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder number of levels=" + numberOfLevels); 501 502 if ((startFromPath == null) || (startFromPath.length() == 0)) { 503 504 Logger.debug(StaticMenuBuilder.class, "pagePath=" + currentPath); 505 506 int idx1 = currentPath.indexOf("/"); 507 int idx2 = currentPath.indexOf("/", idx1 + 1); 508 509 startFromPath = currentPath.substring(idx1, idx2 + 1); 510 511 Logger.debug(StaticMenuBuilder.class, "path=" + startFromPath); 512 } 513 514 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder hostId=" + host.getInode()); 515 516 java.util.List <Inode> itemsList = new ArrayList (); 517 String folderPath = ""; 518 String fileName = ""; 519 boolean fileExists = true; 520 521 java.io.File file = null; 522 String menuId = ""; 523 if ("/".equals(startFromPath)) { 524 String condition = "show_on_menu = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue(); 525 fileName = hostId + "_siteMapLevels_" + numberOfLevels + "_static.vtl"; 526 menuId = String.valueOf(hostId); 527 file = new java.io.File (Config.CONTEXT.getRealPath(MENU_VTL_PATH + fileName)); 528 if (!file.exists() || file.length() == 0) { 529 itemsList = FolderFactory.getFoldersByParentAndCondition(hostId, condition); 530 folderPath = startFromPath; 531 fileExists = false; 532 } 533 } else { 534 535 Folder folder = FolderCache.getFolderByPath(startFromPath, hostId); 536 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder folder=" + folder.getPath()); 537 538 fileName = folder.getInode() + "_siteMapLevels_" + numberOfLevels + "_static.vtl"; 539 menuId = String.valueOf(folder.getInode()); 540 file = new java.io.File (Config.CONTEXT.getRealPath(MENU_VTL_PATH + fileName)); 541 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder file=" + MENU_VTL_PATH + fileName); 542 543 if (!file.exists()) { 544 itemsList = FolderFactory.getMenuItems(folder); 545 folderPath = folder.getPath(); 546 fileExists = false; 547 } 548 } 549 String filePath = "static" + java.io.File.separator + "menus" + java.io.File.separator + fileName; 550 551 if (fileExists) { 552 return filePath; 553 } else { 554 555 if (itemsList.size() > 0) { 556 557 stringbuf.append("#if($EDIT_MODE)\n"); 558 stringbuf.append("<form action=\"${directorURL}\" method=\"post\" name=\"form_menu_" + menuId + "\" id=\"form_menu_" + menuId 559 + "\">\n"); 560 stringbuf.append("<input type=\"hidden\" name=\"cmd\" value=\"orderMenu\">\n"); 561 stringbuf.append("<input type=\"hidden\" name=\"path\" value=\"" + startFromPath + "\">\n"); 562 stringbuf.append("<input type=\"hidden\" name=\"hostId\" value=\"" + hostId + "\">\n"); 563 stringbuf.append("<input type=\"hidden\" name=\"pagePath\" value=\"$VTLSERVLET_URI\">\n"); 564 stringbuf.append("<input type=\"hidden\" name=\"referer\" value=\"$VTLSERVLET_URI\">\n"); 565 566 stringbuf 567 .append("<a HREF=\"javascript:submitMenu('form_menu_" 568 + menuId 569 + "');\"><img align=\"absmiddle\" vspace=\"0\" SRC=\"/portal/images/icons/btn_reorder.gif\" border=0 alt=\"Reorder Items\">\n"); 570 stringbuf.append("</a></form>"); 571 stringbuf.append("#end \n"); 572 573 stringbuf.append("<ul>\n"); 574 575 if(addHome) 577 { 578 stringbuf.append("<li id=\"home\"><a HREF=\"/\">Home</a></li>"); 579 } 580 581 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder number of items=" + itemsList.size()); 583 584 for (Inode itemChild : itemsList) { 586 587 if (itemChild instanceof Folder) { 588 589 Folder folderChild = (Folder) itemChild; 590 591 593 stringbuf = buildSubFolderSiteMapMenu(stringbuf, folderChild, numberOfLevels, 1); 594 595 } else if (itemChild instanceof Link) { 596 if (((Link) itemChild).isWorking() && !((Link) itemChild).isDeleted()) { 597 stringbuf.append("<li><a HREF=\"" + ((Link) itemChild).getProtocal() + ((Link) itemChild).getUrl() + "\" target=\"" 598 + ((Link) itemChild).getTarget() + "\">\n"); 599 stringbuf.append(((Link) itemChild).getTitle() + "</a></li>\n"); 600 } 601 } else if (itemChild instanceof HTMLPage) { 602 if (((HTMLPage) itemChild).isWorking() && !((HTMLPage) itemChild).isDeleted()) { 603 stringbuf.append("<li><a HREF=\"" + folderPath + ((HTMLPage) itemChild).getPageUrl() + "\">\n"); 604 stringbuf.append(((HTMLPage) itemChild).getTitle() + "</a></li>\n"); 605 } 606 } else if (itemChild instanceof File) { 607 if (((File) itemChild).isWorking() && !((File) itemChild).isDeleted()) { 608 stringbuf.append("<li><a HREF=\"" + folderPath + ((File) itemChild).getFileName() + "\">"); 609 stringbuf.append(((File) itemChild).getTitle() + "</a></li>\n"); 610 611 } 612 } 613 } 614 stringbuf.append("</ul>"); 615 616 } 617 618 619 620 if (stringbuf.toString().getBytes().length > 0) { 621 FileOutputStream fo = new FileOutputStream (file); 622 fo.write(stringbuf.toString().getBytes()); 623 fo.close(); 624 } else { 625 Logger.debug(StaticMenuBuilder.class, "Error creating static horizontal menu!!!!!"); 626 } 627 628 629 Logger.debug(StaticMenuBuilder.class, "End of StaticMenuBuilder" + filePath); 630 631 return filePath; 632 } 633 634 } catch (Exception e) { 635 stringbuf.delete(0, stringbuf.length()); 639 e.printStackTrace(System.out); 640 } 641 return ""; 642 } 643 644 private StringBuffer buildSubFolderSiteMapMenu(StringBuffer stringbuf, Folder thisFolder, int numberOfLevels, int currentLevel) { 645 646 647 stringbuf.append("\t<li id=\"" + thisFolder.getName() + "\">\n"); 648 java.util.List <Inode> itemsChildrenList2 = FolderFactory.getMenuItems(thisFolder); 650 651 boolean nextLevelItems = (itemsChildrenList2.size() > 0 && currentLevel < numberOfLevels); 653 654 String folderChildPath = thisFolder.getPath().substring(0, thisFolder.getPath().length() - 1); 655 folderChildPath = folderChildPath.substring(0, folderChildPath.lastIndexOf("/")); 656 657 stringbuf.append("<a HREF=\"" + thisFolder.getPath() + "\">"); 658 stringbuf.append(thisFolder.getTitle()); 659 stringbuf.append("</a>\n"); 660 661 if (currentLevel < numberOfLevels) { 662 663 if (nextLevelItems) { 664 stringbuf.append("<ul>\n"); 665 } 666 667 for (Inode childChild2 : itemsChildrenList2) { 668 if (childChild2 instanceof Folder) { 669 Folder folderChildChild2 = (Folder) childChild2; 670 671 Logger.debug(this, "folderChildChild2= " + folderChildChild2.getTitle() + " currentLevel=" + currentLevel + " numberOfLevels=" 672 + numberOfLevels); 673 if (currentLevel <= numberOfLevels) { 674 stringbuf = buildSubFolderSiteMapMenu(stringbuf, folderChildChild2, numberOfLevels, currentLevel + 1); 675 } else { 676 677 stringbuf.append("<li><a HREF=\"" + folderChildChild2.getPath() + "index." 678 + Config.getStringProperty("VELOCITY_PAGE_EXTENSION") + "\">"); 679 stringbuf.append(folderChildChild2.getTitle() + "</a></li>\n"); 680 681 } 682 } else if (childChild2 instanceof Link) { 683 if (((Link) childChild2).isWorking() && !((Link) childChild2).isDeleted()) { 684 stringbuf.append("<li><a HREF=\"" + ((Link) childChild2).getProtocal() + ((Link) childChild2).getUrl() + "\" target=\"" 685 + ((Link) childChild2).getTarget() + "\">"); 686 stringbuf.append(((Link) childChild2).getTitle() + "</a></li>\n"); 687 688 } 689 } else if (childChild2 instanceof HTMLPage) { 690 if (((HTMLPage) childChild2).isWorking() && !((HTMLPage) childChild2).isDeleted()) { 691 stringbuf.append("<li><a HREF=\"" + thisFolder.getPath() + ((HTMLPage) childChild2).getPageUrl() + "\">"); 692 stringbuf.append(((HTMLPage) childChild2).getTitle() + "</a></li>\n"); 693 694 } 695 } else if (childChild2 instanceof File) { 696 if (((File) childChild2).isWorking() && !((File) childChild2).isDeleted()) { 697 stringbuf.append("<li><a HREF=\"" + thisFolder.getPath() + ((File) childChild2).getFileName() + "\">"); 698 stringbuf.append(((File) childChild2).getTitle() + "</a></li>\n"); 699 700 } 701 } 702 } 703 } 704 if (nextLevelItems) { 705 stringbuf.append("</ul>\n"); 706 707 } 708 stringbuf.append("</li>\n"); 709 return stringbuf; 710 } 711 712 public boolean menuItemsByDepth(int startFromLevel, int maxDepth, HttpServletRequest request) throws JspException 713 { 714 String currentPath = request.getRequestURI(); 715 StringTokenizer st = new StringTokenizer (currentPath, "/"); 716 int i = 1; 717 StringBuffer myPath = new StringBuffer ("/"); 718 while (st.hasMoreTokens()) { 719 if (i++ >= startFromLevel) { 720 break; 721 } 722 String myToken = st.nextToken(); 723 if (!st.hasMoreTokens()) 724 break; 725 myPath.append(myToken); 726 myPath.append("/"); 727 } 728 boolean returnValue = menuItems(myPath.toString(), maxDepth, request); 729 return returnValue; 730 } 731 732 public boolean menuItems(String startFromPath, int numberOfLevels, HttpServletRequest request) throws JspException 733 { 734 boolean fileExists = false; 735 try 736 { 737 createMenu(startFromPath,numberOfLevels,request); 739 String currentPath = request.getRequestURI(); 741 Host host = HostFactory.getCurrentHost(request); 742 long hostId = host.getInode(); 743 744 if ((startFromPath == null) || (startFromPath.length() == 0)) { 745 746 Logger.debug(StaticMenuBuilder.class, "pagePath=" + currentPath); 747 748 int idx1 = currentPath.indexOf("/"); 749 int idx2 = currentPath.indexOf("/", idx1 + 1); 750 751 startFromPath = currentPath.substring(idx1, idx2 + 1); 752 753 Logger.debug(StaticMenuBuilder.class, "path=" + startFromPath); 754 } 755 756 String fileName = ""; 757 java.io.File file = null; 758 if ("/".equals(startFromPath)) 759 { 760 fileName = hostId + "_levels_" + numberOfLevels + "_static.vtl"; 761 file = new java.io.File (Config.CONTEXT.getRealPath(MENU_VTL_PATH + fileName)); 762 if (file.exists() && file.length() > 0) 763 { 764 fileExists = true; 765 } 766 } 767 else 768 { 769 Folder folder = FolderCache.getFolderByPath(startFromPath, hostId); 770 fileName = folder.getInode() + "_levels_" + numberOfLevels + "_static.vtl"; 771 file = new java.io.File (Config.CONTEXT.getRealPath(MENU_VTL_PATH + fileName)); 772 if (file.exists() && file.length() > 0) 773 { 774 fileExists = true; 775 } 776 } 777 } 778 catch(Exception ex) 779 { 780 Logger.debug(this,ex.toString()); 781 } 782 finally 783 { 784 return fileExists; 785 } 786 } 787 } 788 | Popular Tags |