1 package com.dotmarketing.viewtools; 2 3 import java.util.List ; 4 5 import javax.servlet.http.HttpServletRequest ; 6 7 import org.apache.velocity.tools.view.context.ViewContext; 8 import org.apache.velocity.tools.view.tools.ViewTool; 9 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.model.HTMLPage; 17 import com.dotmarketing.portlets.links.model.Link; 18 import com.dotmarketing.util.Logger; 19 20 public class SiteMapWebAPI implements ViewTool { 21 22 23 public void init(Object obj) { 24 25 } 26 27 private int columns = 3; 29 private double numberToBreak = 0; 30 private int totalCount = 0; 31 private int percent = 0; 32 33 public String getSiteMap(HttpServletRequest request) { 34 35 StringBuffer stringbuf = new StringBuffer (); 36 37 try { 38 39 int level = 0; 40 41 Host host = HostFactory.getCurrentHost(request); 42 43 java.util.List itemsChildrenList = FolderFactory.getMenuItems(host); 44 double divTotal = ((double) itemsChildrenList.size() / (double) columns); 45 numberToBreak = Math.ceil(divTotal); 46 percent = Math.round(100 / columns); 47 48 stringbuf.append("<br><table border=0 cellpadding=0 cellspacing=0 width=\"95%\" align=\"center\">\n"); 49 stringbuf.append("<tr><td colspan=\"" + columns + "\">"); 50 stringbuf.append("<strong><A HREF='/'>Home</a></strong></td><br></tr>\n"); 51 stringbuf.append("<tr><td valign=top width=\"" + percent + "%\">"); 52 53 stringbuf = getEntries(itemsChildrenList, host, stringbuf, level); 54 55 stringbuf.append("</td></tr></table><BR><BR><BR>"); 56 57 } catch (Exception e) { 58 stringbuf.delete(0, stringbuf.length()); 62 Logger.error(this, e.toString(), e); 63 } 64 return stringbuf.toString(); 65 } 66 67 private StringBuffer getEntries(List itemsChildrenList, Host parentHost, StringBuffer stringbuf, int level) { 68 69 java.util.Iterator itemsChildrenListIter = itemsChildrenList.iterator(); 71 72 while (itemsChildrenListIter.hasNext()) { 73 74 Inode childItem = (Inode) itemsChildrenListIter.next(); 75 76 if (childItem instanceof Folder) { 77 if ((level == 0) && (totalCount != 0) && (totalCount % numberToBreak) == 0) { 78 stringbuf.append("</td>\n<td valign=top width=\"" + percent + "%\">\n"); 79 totalCount = 0; 80 } 81 Folder folderchildItem = (Folder) childItem; 82 if (level == 0) { 83 if (totalCount != 0) { 84 stringbuf.append("</ul>\n"); 85 } 86 totalCount++; 87 stringbuf.append("<strong><a HREF=\"" + folderchildItem.getPath() + "\">" 91 + folderchildItem.getTitle() + "</a></strong><ul>\n"); 92 } else { 93 stringbuf.append("<B><a HREF=\"" + folderchildItem.getPath() + "\">" + folderchildItem.getTitle() 94 + "</a></B><ul>\n"); 95 } 96 java.util.List itemsChildrenListFolder = FolderFactory.getMenuItems(folderchildItem); 97 stringbuf = getEntries(itemsChildrenListFolder, folderchildItem, stringbuf, level + 1); 98 stringbuf.append("</ul>\n"); 99 } else if (childItem instanceof Link) { 100 if (((Link) childItem).isWorking() && !((Link) childItem).isDeleted()) { 101 stringbuf.append("<li><a HREF=\"" + ((Link) childItem).getProtocal() + ((Link) childItem).getUrl() 102 + "\" target=\"" + ((Link) childItem).getTarget() + "\">\n"); 103 stringbuf.append(((Link) childItem).getTitle() + "</a></li>\n"); 104 } 105 } else if (childItem instanceof HTMLPage) { 106 if (((HTMLPage) childItem).isWorking() && !((HTMLPage) childItem).isDeleted()) { 107 stringbuf.append("<li><a HREF=\"" + "/" + ((HTMLPage) childItem).getPageUrl() + "\">\n"); 108 stringbuf.append(((HTMLPage) childItem).getTitle() + "</a></li>\n"); 109 } 110 } else if (childItem instanceof File) { 111 if (((File) childItem).isWorking() && !((File) childItem).isDeleted()) { 112 stringbuf.append("<li><a HREF=\"" + "/" + ((File) childItem).getFileName() + "\">\n"); 113 stringbuf.append(((File) childItem).getTitle() + "</a></li>\n"); 114 } 115 } 116 117 } 118 119 return stringbuf; 120 } 121 122 private StringBuffer getEntries(List itemsChildrenList, Folder parentFolder, StringBuffer stringbuf, int level) { 123 124 java.util.Iterator itemsChildrenListIter = itemsChildrenList.iterator(); 126 127 while (itemsChildrenListIter.hasNext()) { 128 129 Inode childItem = (Inode) itemsChildrenListIter.next(); 130 131 if (childItem instanceof Folder) { 132 if ((level == 0) && (totalCount != 0) && (totalCount % numberToBreak) == 0) { 133 stringbuf.append("</td>\n<td valign=top width=\"" + percent + "%\">\n"); 134 totalCount = 0; 135 } 136 Folder folderchildItem = (Folder) childItem; 137 if (level == 0) { 138 if (totalCount != 0) { 139 stringbuf.append("</ul>\n"); 140 } 141 totalCount++; 142 stringbuf.append("<strong><a HREF=\"" + folderchildItem.getPath() + "\">" 146 + folderchildItem.getTitle() + "</a></strong><ul>\n"); 147 } else { 148 stringbuf.append("<B><a HREF=\"" + folderchildItem.getPath() + "\">" + folderchildItem.getTitle() 149 + "</a></B><ul>\n"); 150 } 151 java.util.List itemsChildrenListFolder = FolderFactory.getMenuItems(folderchildItem); 152 stringbuf = getEntries(itemsChildrenListFolder, folderchildItem, stringbuf, level + 1); 153 stringbuf.append("</ul>\n"); 154 } else if (childItem instanceof Link) { 155 if (((Link) childItem).isWorking() && !((Link) childItem).isDeleted()) { 156 stringbuf.append("<li><a HREF=\"" + ((Link) childItem).getProtocal() + ((Link) childItem).getUrl() 157 + "\" target=\"" + ((Link) childItem).getTarget() + "\">\n"); 158 stringbuf.append(((Link) childItem).getTitle() + "</a></li>\n"); 159 } 160 } else if (childItem instanceof HTMLPage) { 161 if (((HTMLPage) childItem).isWorking() && !((HTMLPage) childItem).isDeleted()) { 162 stringbuf.append("<li><a HREF=\"" + parentFolder.getPath() + ((HTMLPage) childItem).getPageUrl() 163 + "\">\n"); 164 stringbuf.append(((HTMLPage) childItem).getTitle() + "</a></li>\n"); 165 } 166 } else if (childItem instanceof File) { 167 if (((File) childItem).isWorking() && !((File) childItem).isDeleted()) { 168 stringbuf.append("<li><a HREF=\"" + parentFolder.getPath() + ((File) childItem).getFileName() 169 + "\">\n"); 170 stringbuf.append(((File) childItem).getTitle() + "</a></li>\n"); 171 } 172 } 173 174 } 175 176 return stringbuf; 177 178 } 179 } | Popular Tags |