1 package com.dotmarketing.menubuilders; 2 3 import java.io.FileOutputStream ; 4 import java.util.ArrayList ; 5 import javax.servlet.http.HttpServletRequest ; 6 import javax.servlet.http.HttpServletResponse ; 7 import javax.servlet.jsp.JspException ; 8 import org.apache.velocity.tools.view.context.ViewContext; 9 import org.apache.velocity.tools.view.tools.ViewTool; 10 import com.dotmarketing.beans.Host; 11 import com.dotmarketing.beans.Inode; 12 import com.dotmarketing.factories.HostFactory; 13 import com.dotmarketing.portlets.files.model.File; 14 import com.dotmarketing.portlets.folders.factories.FolderFactory; 15 import com.dotmarketing.portlets.folders.model.Folder; 16 import com.dotmarketing.portlets.htmlpages.factories.HTMLPageFactory; 17 import com.dotmarketing.portlets.htmlpages.model.HTMLPage; 18 import com.dotmarketing.portlets.links.model.Link; 19 import com.dotmarketing.util.Config; 20 import com.dotmarketing.util.Logger; 21 22 23 public class StaticMenuBuilder implements ViewTool { 24 25 26 protected HttpServletRequest request; 27 protected HttpServletResponse response; 28 29 public String createMenu(String path, String pagePath, String divName, int numberOfLevels) throws JspException  35 { 36 Host host = HostFactory.getDefaultHost(); 37 return createMenu(path, pagePath, host.getInode(),divName,"",true,"","",numberOfLevels); 38 } 39 40 public String createMenu(String path, String pagePath, Host host, String divName, int numberOfLevels) throws JspException  41 { 42 return createMenu(path, pagePath, host.getInode(),divName,"",true,"","",numberOfLevels); 43 } 44 45 public String createMenu(String path, String pagePath, Host host, String divName, String ulClassName, int numberOfLevels) throws JspException  46 { 47 return createMenu(path, pagePath, host.getInode(),divName,ulClassName,true,"","",numberOfLevels); 48 } 49 50 public String createMenu(String path, String pagePath, Host host, String divName, String imagePrefix, String imageSuffix, int numberOfLevels) throws JspException  51 { 52 return createMenu(path, pagePath, host.getInode(),divName, "", true,imagePrefix, imageSuffix,numberOfLevels); 53 } 54 55 public String createMenu(String path, String pagePath, Host host, String divName, String ulClassName, String imagePrefix, String imageSuffix, int numberOfLevels) throws JspException  56 { 57 return createMenu(path, pagePath, host.getInode(),divName, ulClassName, true, imagePrefix, imageSuffix,numberOfLevels); 58 } 59 60 public String createMenu(String path, String pagePath, String divName, boolean linkFolders, int numberOfLevels) throws JspException  62 { 63 Host host = HostFactory.getDefaultHost(); 64 return createMenu(path, pagePath, host.getInode(),divName,"",linkFolders,"","",numberOfLevels); 65 } 66 67 public String createMenu(String path, String pagePath, Host host, String divName, boolean linkFolders, int numberOfLevels) throws JspException  68 { 69 return createMenu(path, pagePath, host.getInode(),divName,"",linkFolders,"","",numberOfLevels); 70 } 71 72 public String createMenu(String path, String pagePath, Host host, String divName, String ulClassName, boolean linkFolders, int numberOfLevels) throws JspException  73 { 74 return createMenu(path, pagePath, host.getInode(),divName,ulClassName,linkFolders,"","",numberOfLevels); 75 } 76 77 public String createMenu(String path, String pagePath, Host host, String divName, boolean linkFolders, String imagePrefix, String imageSuffix, int numberOfLevels) throws JspException  78 { 79 return createMenu(path, pagePath, host.getInode(),divName, "", linkFolders,imagePrefix, imageSuffix,numberOfLevels); 80 } 81 82 public String createMenu(String path, String pagePath, Host host, String divName, String ulClassName, boolean linkFolders, String imagePrefix, String imageSuffix, int numberOfLevels) throws JspException  83 { 84 return createMenu(path, pagePath, host.getInode(),divName, ulClassName, linkFolders, imagePrefix, imageSuffix,numberOfLevels); 85 } 86 87 88 private String createMenu(String path, String pagePath, long hostId, String divName, String ulClassName, boolean linkFolders, String imagePrefix, String imageSuffix, int numberOfLevels) throws JspException  90 { 97 if (numberOfLevels == 0) { 98 numberOfLevels = Integer.parseInt(Config.getStringProperty("number_levels")); 99 } 100 StringBuffer stringbuf = new StringBuffer (); 101 String menuVtlPath="/WEB-INF/velocity/static/menus/"; 102 try { 103 104 Logger.debug(StaticMenuBuilder.class, "\n\n\n\nStaticMenuBuilder begins"); 105 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder number of levels=" + numberOfLevels); 106 107 if ((path==null) || (path.length()==0)) { 108 109 Logger.debug(StaticMenuBuilder.class, "pagePath=" + pagePath); 110 111 int idx1 = pagePath.indexOf("/"); 112 int idx2 = pagePath.indexOf("/",idx1+1); 113 114 path = pagePath.substring(idx1,idx2+1); 115 116 Logger.debug(StaticMenuBuilder.class, "path=" + path); 117 } 118 119 120 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder path=" + path); 121 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder hostId=" + hostId); 122 123 java.util.List itemsList = new ArrayList (); 124 String folderPath = ""; 125 String fileName = ""; 126 boolean fileExists = true; 127 128 java.io.File fileFolder = new java.io.File (Config.CONTEXT.getRealPath(menuVtlPath)); 129 if(!fileFolder.exists()){ 130 fileFolder.mkdirs(); 131 } 132 java.io.File file = null; 133 String menuId = ""; 134 if (path.equals("/")) { 135 String condition = "show_on_menu = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue(); 136 fileName = hostId + "_static.vtl"; 137 menuId = String.valueOf(hostId); 138 file = new java.io.File (Config.CONTEXT.getRealPath(menuVtlPath) + java.io.File.separator + fileName); 139 if (!file.exists()) { 140 itemsList = FolderFactory.getFoldersByParentAndCondition(hostId,condition); 141 folderPath = path; 142 fileExists = false; 143 } 144 } 145 else { 146 Folder folder = FolderFactory.getFolderByPath(path,hostId); 147 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder folder=" + folder.getPath()); 148 fileName = folder.getInode() + "_static.vtl"; 149 menuId = String.valueOf(folder.getInode()); 150 file = new java.io.File (Config.CONTEXT.getRealPath(menuVtlPath) + java.io.File.separator + fileName); 151 if (!file.exists()) { 152 itemsList = FolderFactory.getMenuItems(folder); 153 folderPath = folder.getPath(); 154 fileExists = false; 155 } 156 } 157 String filePath = "static" + java.io.File.separator + "menus" + java.io.File.separator + fileName; 158 159 if (fileExists) { 160 return filePath; 161 } 162 else { 163 164 if (itemsList.size()>0) { 165 if (divName!=null && divName.length()>0) { 166 String beforeMenu = "<div id=\"" + divName + "\">"; 167 stringbuf.append(beforeMenu); 168 } 169 170 if (divName!=null && divName.length()>0) { 171 stringbuf.append("#if($EDIT_MODE)\n"); 172 stringbuf.append("<form action=\"${directorURL}\" method=\"post\" name=\"form_menu_" + menuId + "\" id=\"form_menu_" + menuId + "\">\n"); 173 stringbuf.append("<input type=\"hidden\" name=\"cmd\" value=\"orderMenu\">\n"); 174 stringbuf.append("<input type=\"hidden\" name=\"path\" value=\""+path+"\">\n"); 175 stringbuf.append("<input type=\"hidden\" name=\"hostId\" value=\"" + hostId + "\">\n"); 176 stringbuf.append("<input type=\"hidden\" name=\"pagePath\" value=\"$VTLSERVLET_URI\">\n"); 177 stringbuf.append("<input type=\"hidden\" name=\"referer\" value=\"$VTLSERVLET_URI\">\n"); 178 179 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>"); 180 stringbuf.append("</form>\n"); 181 stringbuf.append("#end \n"); 182 } 183 184 if (ulClassName!=null && ulClassName.length() >0) { 185 stringbuf.append("<ul class=\"" + ulClassName + "\">"); 186 } 187 else { 188 stringbuf.append("<ul>"); 189 } 190 191 192 java.util.Iterator itemListIterator = itemsList.iterator(); 194 Logger.debug(StaticMenuBuilder.class, "StaticMenuBuilder number of items=" + itemsList.size()); 195 196 while (itemListIterator.hasNext()) { 198 199 Inode itemChild = (Inode) itemListIterator.next(); 200 201 if (itemChild instanceof Folder) { 202 203 Folder folderChild = (Folder) itemChild; 204 205 stringbuf = getMenuItems(stringbuf, folderChild, linkFolders, numberOfLevels, 1, imagePrefix, imageSuffix); 207 208 } 209 else if (itemChild instanceof Link) { 210 if (((Link)itemChild).isWorking() && !((Link)itemChild).isDeleted()) { 211 stringbuf.append("#if ($UtilMethods.inString($VTLSERVLET_URI,\"" + ((Link)itemChild).getProtocal() + ((Link)itemChild).getUrl() + "\"))\n"); 212 stringbuf.append("<li><a HREF=\"" + ((Link)itemChild).getProtocal() + ((Link)itemChild).getUrl() + "\" target=\"" + ((Link)itemChild).getTarget() + "\">\n"); 213 stringbuf.append(((Link)itemChild).getTitle() + "</a></li>\n"); 214 stringbuf.append("#else\n"); 215 stringbuf.append("<li class=\"active\"><a HREF=\"" + ((Link)itemChild).getProtocal() + ((Link)itemChild).getUrl() + "\" target=\"" + ((Link)itemChild).getTarget() + "\">\n"); 216 stringbuf.append(((Link)itemChild).getTitle() + "</a></li>\n"); 217 stringbuf.append("#end \n"); 218 } 219 } 220 else if (itemChild instanceof HTMLPage) { 221 if (((HTMLPage)itemChild).isWorking() && !((HTMLPage)itemChild).isDeleted()) { 222 stringbuf.append("#if ($UtilMethods.inString($VTLSERVLET_URI,\"" + path + ((HTMLPage)itemChild).getPageUrl() + "\"))\n"); 223 stringbuf.append("<li><a HREF=\"" + folderPath + ((HTMLPage)itemChild).getPageUrl() + "\">\n"); 224 stringbuf.append(((HTMLPage)itemChild).getTitle() + "</a></li>\n"); 225 stringbuf.append("#else\n"); 226 stringbuf.append("<li class=\"active\"><a HREF=\"" + folderPath + ((HTMLPage)itemChild).getPageUrl() + "\">\n"); 227 stringbuf.append(((HTMLPage)itemChild).getTitle() + "</a></li>\n"); 228 stringbuf.append("#end \n"); 229 } 230 } 231 else if (itemChild instanceof File) { 232 if (((File)itemChild).isWorking() && !((File)itemChild).isDeleted()) { 233 stringbuf.append("#if ($UtilMethods.inString($VTLSERVLET_URI,\"" + path + ((File)itemChild).getFileName() + "\"))\n"); 234 stringbuf.append("<li><a HREF=\"" + folderPath + ((File)itemChild).getFileName() + "\">\n"); 235 stringbuf.append(((File)itemChild).getTitle() + "</a></li>\n"); 236 stringbuf.append("#else\n"); 237 stringbuf.append("#end \n"); 238 } 239 } 240 } 241 stringbuf.append("</ul>"); 242 243 244 if (divName!=null && divName.length()>0) { 245 stringbuf.append("</div>"); 246 } 247 } 248 249 FileOutputStream fo = new FileOutputStream (file); 250 251 if (stringbuf.toString().getBytes().length>0) { 252 fo.write(stringbuf.toString().getBytes()); 253 } 254 else { 255 Logger.debug(StaticMenuBuilder.class, "Error creating static horizontal menu!!!!!"); 256 } 257 258 fo.close(); 259 Logger.debug(StaticMenuBuilder.class, "End of StaticMenuBuilder" + filePath); 260 261 return filePath; 262 } 263 } catch(Exception e) { 264 stringbuf.delete(0, stringbuf.length()); 267 e.printStackTrace(System.out); 268 } 269 return ""; 270 } 271 272 278 public void init(Object obj) { 279 ViewContext context = (ViewContext) obj; 280 this.request = context.getRequest(); 281 this.response = context.getResponse(); 282 } 283 284 private StringBuffer getMenuItems(StringBuffer stringbuf, Folder folderChildChild, boolean linkFolders, int numberOfLevels, int currentLevel, String imagePrefix, String imageSuffix) { 285 286 Host host = HostFactory.getCurrentHost(request); 287 288 java.util.List itemsChildrenList2 = FolderFactory.getMenuItems(folderChildChild); 290 java.util.Iterator itemsChildrenListIter2 = itemsChildrenList2.iterator(); 291 boolean nextLevelItems = false; 292 293 294 if (itemsChildrenListIter2.hasNext()) { 295 nextLevelItems = true; 296 } 297 298 String folderChildPath = folderChildChild.getPath().substring(0, folderChildChild.getPath().length() - 1); 299 folderChildPath = folderChildPath.substring(0, folderChildPath.lastIndexOf("/")); 300 301 stringbuf.append("#if ($VTLSERVLET_URI != '" + folderChildPath + "' && $VTLSERVLET_URI != '" + folderChildChild.getPath() + "index." + Config.getStringProperty("VELOCITY_PAGE_EXTENSION") + "')\n"); 302 stringbuf.append("<li id=\"" + folderChildChild.getName()+ "\">"); 303 304 HTMLPage page = HTMLPageFactory.getLiveHTMLPageByPath(folderChildChild.getPath() + "index." + Config.getStringProperty("VELOCITY_PAGE_EXTENSION"), host); 305 if (page.getInode() == 0) { 306 if (linkFolders) stringbuf.append("<a HREF=\""+folderChildChild.getPath() + "\">"); 307 } else { 308 if (linkFolders) stringbuf.append("<a HREF=\""+folderChildChild.getPath() + "index." + Config.getStringProperty("VELOCITY_PAGE_EXTENSION")+"\">"); 309 } 310 311 if ((imagePrefix!=null && imagePrefix.length()>0) || 313 (imageSuffix!=null && imageSuffix.length() >0)) { 314 stringbuf.append("<img SRC=\"" + imagePrefix + folderChildChild.getName() + imageSuffix + "\" alt=\"" + folderChildChild.getTitle() + "\" />"); 315 } 316 else { 317 stringbuf.append(folderChildChild.getTitle()); 318 } 319 if (linkFolders) { 320 stringbuf.append("</a>\n"); 321 } 322 stringbuf.append("#else\n"); 323 stringbuf.append("<li class=\"active\" id=\"" + folderChildChild.getName()+ "\"><a HREF=\"#\">" + folderChildChild.getTitle() + "</a></li>"); 324 stringbuf.append("#end \n"); 325 326 if (nextLevelItems) { 327 stringbuf.append("#if ($UtilMethods.inString($VTLSERVLET_URI,\"" + folderChildChild.getPath() + "\"))\n"); 328 stringbuf.append("<ul>\n"); 329 } 330 331 while (itemsChildrenListIter2.hasNext()) { 332 333 Inode childChild2 = (Inode) itemsChildrenListIter2.next(); 334 335 if (childChild2 instanceof Folder) { 336 Folder folderChildChild2 = (Folder) childChild2; 337 338 Logger.debug(this, "folderChildChild2= " + folderChildChild2.getTitle() + " currentLevel="+ currentLevel + " numberOfLevels=" + numberOfLevels); 339 if (currentLevel <= numberOfLevels) { 340 stringbuf = getMenuItems(stringbuf, folderChildChild2, linkFolders, numberOfLevels, currentLevel + 1, imagePrefix, imageSuffix); 341 } 342 else { 343 if (linkFolders) { 344 stringbuf.append("<li><a HREF=\"" + folderChildChild2.getPath() + "index." + Config.getStringProperty("VELOCITY_PAGE_EXTENSION") + "\">\n"); 345 stringbuf.append(folderChildChild2.getTitle() + "</a></li>\n"); 346 } 347 else { 348 stringbuf.append("<li>" + folderChildChild2.getTitle() + "</li>\n"); 349 } 350 } 351 } 352 else if (childChild2 instanceof Link) { 353 if (((Link)childChild2).isWorking() && !((Link)childChild2).isDeleted()) { 354 stringbuf.append("#if ($VTLSERVLET_URI != '" + ((Link)childChild2).getProtocal() + ((Link)childChild2).getUrl() + "')\n"); 355 stringbuf.append("<li><a HREF=\"" + ((Link)childChild2).getProtocal() + ((Link)childChild2).getUrl() + "\" target=\"" + ((Link)childChild2).getTarget() + "\">\n"); 356 stringbuf.append(((Link)childChild2).getTitle() + "</a></li>\n"); 357 stringbuf.append("#else\n"); 358 stringbuf.append("<li class=\"active\"><a HREF=\"" + ((Link)childChild2).getProtocal() + ((Link)childChild2).getUrl() + "\" target=\"" + ((Link)childChild2).getTarget() + "\">\n"); 359 stringbuf.append(((Link)childChild2).getTitle() + "</a></li>\n"); 360 stringbuf.append("#end \n"); 361 } 362 } 363 else if (childChild2 instanceof HTMLPage) { 364 if (((HTMLPage)childChild2).isWorking() && !((HTMLPage)childChild2).isDeleted()) { 365 stringbuf.append("#if ($VTLSERVLET_URI != '" + folderChildChild.getPath() + ((HTMLPage)childChild2).getPageUrl() + "')\n"); 366 stringbuf.append("<li><a HREF=\"" + folderChildChild.getPath() + ((HTMLPage)childChild2).getPageUrl() + "\">\n"); 367 stringbuf.append(((HTMLPage)childChild2).getTitle() + "</a></li>\n"); 368 stringbuf.append("#else\n"); 369 stringbuf.append("<li class=\"active\"><a HREF=\"" + folderChildChild.getPath() + ((HTMLPage)childChild2).getPageUrl() + "\">\n"); 370 stringbuf.append(((HTMLPage)childChild2).getTitle() + "</a></li>\n"); 371 stringbuf.append("#end \n"); 372 } 373 } 374 else if (childChild2 instanceof File) { 375 if (((File)childChild2).isWorking() && !((File)childChild2).isDeleted()) { 376 stringbuf.append("#if ($VTLSERVLET_URI != '" + folderChildChild.getPath() + ((File)childChild2).getFileName() + "')\n"); 377 stringbuf.append("<li><a HREF=\"" + folderChildChild.getPath() + ((File)childChild2).getFileName() + "\">\n"); 378 stringbuf.append(((File)childChild2).getTitle() + "</a></li>\n"); 379 stringbuf.append("#else\n"); 380 stringbuf.append("<li class=\"active\"><a HREF=\"" + folderChildChild.getPath() + ((File)childChild2).getFileName() + "\">\n"); 381 stringbuf.append(((File)childChild2).getTitle() + "</a></li>\n"); 382 stringbuf.append("#end \n"); 383 } 384 } 385 } 386 if (nextLevelItems) { 387 stringbuf.append("</ul></li>\n"); 388 stringbuf.append("#end\n"); 389 } 390 else { 391 stringbuf.append("</li>\n"); 392 } 393 394 return stringbuf; 395 } 396 } 397
| Popular Tags
|