KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > taglibs > categories > CategoryTag


1 package org.jahia.taglibs.categories;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import javax.servlet.ServletRequest JavaDoc;
6 import javax.servlet.jsp.JspException JavaDoc;
7 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
8 import javax.servlet.jsp.PageContext JavaDoc;
9
10 import javax.swing.JTree JavaDoc;
11 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
12 import javax.swing.tree.DefaultTreeModel JavaDoc;
13 import javax.swing.tree.MutableTreeNode JavaDoc;
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 JavaDoc;
20
21 /**
22  * <p>Title: The category tag gives access to a category, it's children
23  * objects as well as it's sub-tree.</p>
24  * <p>Description: This tag can be used to display information about a
25  * specific category, or to build navigation trees for categories, or to
26  * display children of a specific category.</p>
27  * <p>Copyright: Copyright (c) 2002</p>
28  * <p>Company: Jahia Ltd</p>
29  * @author Serge Huber
30  * @version 1.0
31  */

32
33 public class CategoryTag extends BodyTagSupport JavaDoc {
34
35     private static org.apache.log4j.Logger logger =
36         org.apache.log4j.Logger.getLogger(CategoryTag.class);
37
38     private static final String JavaDoc TREEID_DATE_POSTFIX = "_gentime";
39
40     private String JavaDoc key = null;
41     private String JavaDoc name = null;
42     private String JavaDoc childCategoriesID = null;
43     private String JavaDoc subTreeID;
44
45     /**
46      * Empty constructor. Does nothing.
47      */

48     public CategoryTag () {
49     }
50
51     /**
52      * Sets the key of the category that this tag will load. Note that if a
53      * name attribute was specified too it will override this setter.
54      * @param key a String corresponding to the key of the category to access
55      * through this tag.
56      */

57     public void setKey (String JavaDoc key) {
58         this.key = key;
59     }
60
61     /**
62      * Sets the name of the pageContext attribute that contains a String containing
63      * the key of the category to use with this tag.
64      * @param name the name of the pageContext attribute that contains a
65      * String containing the key of the category to use with this tag.
66      */

67     public void setName (String JavaDoc name) {
68         this.name = name;
69     }
70
71     /**
72      * Sets the name of the pageContext attribute to use to store the ArrayList
73      * of Category object that represent the child categories of the category
74      * loaded by this tag.
75      * @param childCategoriesID the name of the pageContext attribute
76      */

77     public void setChildCategoriesID (String JavaDoc childCategoriesID) {
78         this.childCategoriesID = childCategoriesID;
79     }
80
81     /**
82      * Sets the name of the pageContext attribute to use to store the JTree
83      * instance that contains the sub-tree of the specified category. Note that
84      * this tag will also first try to load a JTree pageContext attribute by
85      * that same name, so that operations of tree modifications stored in the
86      * session can be preserved. This means that the scope used to store the
87      * JTree in the pageContext is a SESSION_SCOPE.
88      * @param subTreeID the name of the pageContext attribute
89      */

90     public void setSubTreeID (String JavaDoc subTreeID) {
91         this.subTreeID = subTreeID;
92     }
93
94     public int doStartTag ()
95         throws JspException JavaDoc {
96         Category curCategory = null;
97         String JavaDoc categoryKey = null;
98
99         ServletRequest JavaDoc request = pageContext.getRequest();
100         JahiaData jData = (JahiaData) request.getAttribute(
101             "org.jahia.data.JahiaData");
102
103         if (name != null) {
104             categoryKey = (String JavaDoc) 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 JavaDoc(
118                     "Error while trying to access category with key [" +
119                     categoryKey + "]", je);
120             } else {
121                 throw new JspException JavaDoc(
122                     "Error while trying to access root category", je);
123             }
124         }
125
126         JTree JavaDoc tree = null;
127         if (subTreeID != null) {
128             tree = (JTree JavaDoc) pageContext.findAttribute(subTreeID);
129             // here we must test the validity of the tree depending if there
130
// were modifications to the categories after this object was
131
// created.
132
Date JavaDoc genDate = (Date JavaDoc) pageContext.findAttribute(subTreeID + TREEID_DATE_POSTFIX);
133             if (tree != null) {
134                 Date JavaDoc 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 JavaDoc(), 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 JavaDoc(
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     // loops through the next elements
176
public int doAfterBody ()
177         throws JspException JavaDoc {
178         // gets the current container list
179
return SKIP_BODY;
180     }
181
182     public int doEndTag ()
183         throws JspException JavaDoc {
184         // let's reinitialize the tag variables to allow tag object reuse in
185
// pooling.
186
super.doEndTag();
187         key = null;
188         name = null;
189         childCategoriesID = null;
190         return EVAL_PAGE;
191     }
192
193     private JTree JavaDoc buildJTree (Category category)
194         throws JspException JavaDoc {
195         // Root Node
196
DefaultMutableTreeNode JavaDoc top =
197             new DefaultMutableTreeNode JavaDoc(category, true);
198         DefaultTreeModel JavaDoc treeModel = new DefaultTreeModel JavaDoc(top, true);
199         JTree JavaDoc tree = new JTree JavaDoc(treeModel);
200         try {
201             buildCategoryTree(top, category);
202         } catch (JahiaException je) {
203             throw new JspException JavaDoc("Error while building category tree for " +
204                                    category, je);
205         }
206         return tree;
207     }
208
209     private void buildCategoryTree (MutableTreeNode JavaDoc curNode,
210                                     Category currentCategory)
211         throws JahiaException {
212         if ( currentCategory != null ){
213             ArrayList JavaDoc childCategories = currentCategory.getChildCategories();
214             Iterator JavaDoc childIter = childCategories.iterator();
215             while (childIter.hasNext()) {
216                 Category curChildCategory = (Category) childIter.next();
217                 DefaultMutableTreeNode JavaDoc newNode = new DefaultMutableTreeNode JavaDoc(
218                         curChildCategory);
219                 curNode.insert(newNode, 0);
220                 buildCategoryTree(newNode, curChildCategory);
221             }
222         }
223     }
224
225 }
226
Popular Tags