KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > QueryTag


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.core.ItemType;
17 import info.magnolia.cms.core.search.Query;
18 import info.magnolia.cms.core.search.QueryResult;
19 import info.magnolia.context.MgnlContext;
20
21 import java.util.Collection JavaDoc;
22
23 import javax.jcr.RepositoryException;
24 import javax.jcr.query.InvalidQueryException;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 import org.apache.commons.lang.exception.NestableRuntimeException;
29
30
31 /**
32  * Tags that executes a query in a Magnolia repository.
33  * @author Fabrizio Giustina
34  * @version $Revision: 6341 $ ($Author: philipp $)
35  */

36 public class QueryTag extends TagSupport JavaDoc {
37
38     /**
39      * Stable serialVersionUID.
40      */

41     private static final long serialVersionUID = 222L;
42
43     /**
44      * The query result will be set in the pageContext using this name.
45      */

46     private String JavaDoc var;
47
48     /**
49      * The query to run.
50      */

51     private String JavaDoc query;
52
53     /**
54      * The repository. Defaults to ContentRepository.WEBSITE
55      */

56     private String JavaDoc repository = ContentRepository.WEBSITE;
57
58     /**
59      * Node type. Defaults to mgnl:content.
60      */

61     private String JavaDoc nodeType = ItemType.CONTENT.getSystemName();
62
63     /**
64      * Query type: SQL or XPATH. Defaults to Query.XPATH.
65      */

66     private String JavaDoc type = Query.XPATH;
67
68     /**
69      * Setter for <code>query</code>.
70      * @param query The query to set.
71      */

72     public void setQuery(String JavaDoc query) {
73         this.query = query;
74     }
75
76     /**
77      * Setter for <code>type</code>.
78      * @param type The type to set.
79      */

80     public void setType(String JavaDoc type) {
81         this.type = type;
82     }
83
84     /**
85      * Setter for <code>var</code>.
86      * @param var The var to set.
87      */

88     public void setVar(String JavaDoc var) {
89         this.var = var;
90     }
91
92     /**
93      * Setter for <code>repository</code>.
94      * @param repository The repository to set.
95      */

96     public void setRepository(String JavaDoc repository) {
97         this.repository = repository;
98     }
99
100     /**
101      * Setter for <code>nodeType</code>.
102      * @param nodeType The nodeType to set.
103      */

104     public void setNodeType(String JavaDoc nodeType) {
105         this.nodeType = nodeType;
106     }
107
108     /**
109      * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
110      */

111     public int doEndTag() throws JspException JavaDoc {
112
113         Query q;
114         try {
115             q = MgnlContext.getQueryManager(repository).createQuery(query, type);
116         }
117         catch (InvalidQueryException e) {
118             throw new NestableRuntimeException(e);
119         }
120         catch (RepositoryException e) {
121             throw new NestableRuntimeException(e);
122         }
123
124         QueryResult queryResult;
125         try {
126             queryResult = q.execute();
127         }
128         catch (RepositoryException e) {
129             throw new NestableRuntimeException(e);
130         }
131         Collection JavaDoc result = queryResult.getContent(nodeType);
132         pageContext.setAttribute(var, result);
133
134         return EVAL_PAGE;
135     }
136
137     /**
138      * @see javax.servlet.jsp.tagext.TagSupport#release()
139      */

140     public void release() {
141         super.release();
142         this.var = null;
143         this.query = null;
144         this.type = Query.XPATH;
145         this.repository = ContentRepository.WEBSITE;
146         this.nodeType = ItemType.CONTENT.getSystemName();
147     }
148 }
149
Popular Tags