1 package org.jahia.taglibs.categories; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import javax.servlet.ServletRequest ; 6 import javax.servlet.jsp.JspException ; 7 import javax.servlet.jsp.tagext.BodyTagSupport ; 8 import javax.servlet.jsp.PageContext ; 9 10 import javax.swing.JTree ; 11 import javax.swing.tree.DefaultMutableTreeNode ; 12 import javax.swing.tree.DefaultTreeModel ; 13 import javax.swing.tree.MutableTreeNode ; 14 15 import org.jahia.data.JahiaData; 16 import org.jahia.data.beans.CategoryBean; 17 import org.jahia.exceptions.JahiaException; 18 import org.jahia.services.categories.Category; 19 import java.util.Date ; 20 21 32 33 public class CategoryTag extends BodyTagSupport { 34 35 private static org.apache.log4j.Logger logger = 36 org.apache.log4j.Logger.getLogger(CategoryTag.class); 37 38 private static final String TREEID_DATE_POSTFIX = "_gentime"; 39 40 private String key = null; 41 private String name = null; 42 private String childCategoriesID = null; 43 private String subTreeID; 44 45 48 public CategoryTag () { 49 } 50 51 57 public void setKey (String key) { 58 this.key = key; 59 } 60 61 67 public void setName (String name) { 68 this.name = name; 69 } 70 71 77 public void setChildCategoriesID (String childCategoriesID) { 78 this.childCategoriesID = childCategoriesID; 79 } 80 81 90 public void setSubTreeID (String subTreeID) { 91 this.subTreeID = subTreeID; 92 } 93 94 public int doStartTag () 95 throws JspException { 96 Category curCategory = null; 97 String categoryKey = null; 98 99 ServletRequest request = pageContext.getRequest(); 100 JahiaData jData = (JahiaData) request.getAttribute( 101 "org.jahia.data.JahiaData"); 102 103 if (name != null) { 104 categoryKey = (String ) pageContext.getAttribute(name); 105 } else if (key != null) { 106 categoryKey = key; 107 } 108 109 try { 110 if (categoryKey != null) { 111 curCategory = Category.getCategory(key); 112 } else { 113 curCategory = Category.getRootCategory(); 114 } 115 } catch (JahiaException je) { 116 if (categoryKey != null) { 117 throw new JspException ( 118 "Error while trying to access category with key [" + 119 categoryKey + "]", je); 120 } else { 121 throw new JspException ( 122 "Error while trying to access root category", je); 123 } 124 } 125 126 JTree tree = null; 127 if (subTreeID != null) { 128 tree = (JTree ) pageContext.findAttribute(subTreeID); 129 Date genDate = (Date ) pageContext.findAttribute(subTreeID + TREEID_DATE_POSTFIX); 133 if (tree != null) { 134 Date lastCategoryModifDate = Category.getLastModificationDate(); 135 if ((genDate != null) && (lastCategoryModifDate != null)) { 136 long genTime = genDate.getTime(); 137 long lastCategoryModifTime = lastCategoryModifDate.getTime(); 138 if (genTime < lastCategoryModifTime) { 139 pageContext.removeAttribute(subTreeID, 140 PageContext.SESSION_SCOPE); 141 pageContext.removeAttribute(subTreeID + 142 TREEID_DATE_POSTFIX, PageContext.SESSION_SCOPE); 143 tree = null; 144 } 145 } 146 } 147 } 148 149 if (tree == null) { 150 tree = buildJTree(curCategory); 151 } 152 if (subTreeID != null) { 153 pageContext.setAttribute(subTreeID, tree, PageContext.SESSION_SCOPE); 154 pageContext.setAttribute(subTreeID + TREEID_DATE_POSTFIX, new Date (), PageContext.SESSION_SCOPE); 155 } 156 157 if (childCategoriesID != null) { 158 try { 159 pageContext.setAttribute(childCategoriesID, 160 curCategory.getChildCategories()); 161 } catch (JahiaException je) { 162 throw new JspException ( 163 "Error while retrieving child categories for category [" + 164 curCategory + "]", je); 165 } 166 } 167 168 if (getId() != null) { 169 pageContext.setAttribute(getId(), 170 new CategoryBean(curCategory, jData.params())); 171 } 172 return EVAL_BODY_BUFFERED; 173 } 174 175 public int doAfterBody () 177 throws JspException { 178 return SKIP_BODY; 180 } 181 182 public int doEndTag () 183 throws JspException { 184 super.doEndTag(); 187 key = null; 188 name = null; 189 childCategoriesID = null; 190 return EVAL_PAGE; 191 } 192 193 private JTree buildJTree (Category category) 194 throws JspException { 195 DefaultMutableTreeNode top = 197 new DefaultMutableTreeNode (category, true); 198 DefaultTreeModel treeModel = new DefaultTreeModel (top, true); 199 JTree tree = new JTree (treeModel); 200 try { 201 buildCategoryTree(top, category); 202 } catch (JahiaException je) { 203 throw new JspException ("Error while building category tree for " + 204 category, je); 205 } 206 return tree; 207 } 208 209 private void buildCategoryTree (MutableTreeNode curNode, 210 Category currentCategory) 211 throws JahiaException { 212 if ( currentCategory != null ){ 213 ArrayList childCategories = currentCategory.getChildCategories(); 214 Iterator childIter = childCategories.iterator(); 215 while (childIter.hasNext()) { 216 Category curChildCategory = (Category) childIter.next(); 217 DefaultMutableTreeNode newNode = new DefaultMutableTreeNode ( 218 curChildCategory); 219 curNode.insert(newNode, 0); 220 buildCategoryTree(newNode, curChildCategory); 221 } 222 } 223 } 224 225 } 226 | Popular Tags |