KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.taglibs.categories;
2
3 import java.util.ArrayList JavaDoc;
4 import javax.servlet.ServletRequest JavaDoc;
5 import javax.servlet.jsp.JspException JavaDoc;
6 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
7
8 import org.jahia.data.JahiaData;
9 import org.jahia.services.categories.Category;
10 import java.util.Iterator JavaDoc;
11 import org.jahia.data.beans.CategoryBean;
12
13 /**
14  * <p>Title: Tag that allows to look up categories by property names and
15  * values.</p>
16  * <p>Description: It is allowed to use "%" characters to do SQL pattern matching
17  * in the property name and value. The result is stored in the page context
18  * attribute named using the ID parameter and consists of an ArrayList of
19  * CategoryBean objects.</p>
20  * <p>Copyright: Copyright (c) 2002</p>
21  * <p>Company: Jahia Ltd</p>
22  * @author Serge Huber
23  * @version 1.0
24  */

25
26 public class CategoriesByPropertiesTag extends BodyTagSupport JavaDoc {
27
28     private String JavaDoc propertyName = null;
29     private String JavaDoc propertyValue = null;
30     private String JavaDoc propertyNameRef = null;
31     private String JavaDoc propertyValueRef = null;
32
33     /**
34      * @param propertyName The property name to search for. May include "%"
35      * characters for pattern matching
36      */

37     public void setPropertyName(String JavaDoc propertyName) {
38         this.propertyName = propertyName;
39     }
40
41     /**
42      * @param propertyValue The property name to search for. May include "%"
43      * characters for pattern matching
44      */

45     public void setPropertyValue(String JavaDoc propertyValue) {
46         this.propertyValue = propertyValue;
47     }
48
49     /**
50      * @param propertyNameRef the Bean name for a String object containing
51      * the property name to search for
52      */

53     public void setPropertyNameRef(String JavaDoc propertyNameRef) {
54         this.propertyNameRef = propertyNameRef;
55     }
56
57     /**
58      * @param propertyValueRef the Bean name for a String object containing
59      * the property value to search for
60      */

61     public void setPropertyValueRef(String JavaDoc propertyValueRef) {
62         this.propertyValueRef = propertyValueRef;
63     }
64
65     public int doStartTag ()
66         throws JspException JavaDoc {
67
68         ServletRequest JavaDoc request = pageContext.getRequest();
69         JahiaData jData = (JahiaData) request.getAttribute(
70             "org.jahia.data.JahiaData");
71
72         ArrayList JavaDoc foundCategories = new ArrayList JavaDoc();
73         ArrayList JavaDoc foundCategoryBeans = new ArrayList JavaDoc();
74
75         if (propertyNameRef != null) {
76             propertyName = (String JavaDoc) pageContext.findAttribute(propertyNameRef);
77         }
78         if (propertyValueRef != null) {
79             propertyValue = (String JavaDoc) pageContext.findAttribute(propertyValueRef);
80         }
81
82         if ((propertyName == null) || (propertyValue == null)) {
83             throw new JspException JavaDoc(
84                 "Error: either property name or property value is null, aborting");
85         }
86
87         foundCategories = Category.findCategoriesByPropNameAndValue(propertyName, propertyValue);
88
89         Iterator JavaDoc foundCategoriesIter = foundCategories.iterator();
90         while (foundCategoriesIter.hasNext()) {
91             Category curCategory = (Category) foundCategoriesIter.next();
92             CategoryBean curCategoryBean = new CategoryBean(curCategory, jData.params());
93             foundCategoryBeans.add(curCategoryBean);
94         }
95
96         if (getId() != null) {
97             pageContext.setAttribute(getId(),
98                                      foundCategoryBeans);
99         }
100         return EVAL_BODY_BUFFERED;
101     }
102
103     // loops through the next elements
104
public int doAfterBody ()
105         throws JspException JavaDoc {
106         // gets the current container list
107
return SKIP_BODY;
108     }
109
110     public int doEndTag ()
111         throws JspException JavaDoc {
112         // let's reinitialize the tag variables to allow tag object reuse in
113
// pooling.
114
super.doEndTag();
115         propertyName = null;
116         propertyValue = null;
117         propertyNameRef = null;
118         propertyValueRef = null;
119         return EVAL_PAGE;
120     }
121
122 }
123
Popular Tags