KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > dbtags > resultset > ResultSetTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.taglibs.dbtags.resultset;
17
18 import java.io.IOException JavaDoc;
19 import java.sql.ResultSet JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.sql.Statement JavaDoc;
22
23 import javax.servlet.jsp.JspTagException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
26
27 import org.apache.taglibs.dbtags.statement.StatementTag;
28
29
30 /**
31  * <p>JSP tag resultSet, executes the query and loops through the results
32  * for the enclosing statement or preparedstatement tag. The body of
33  * this tag is executed once per row in the resultset. The optional
34  * "loop" attribute, which default to true, specifies whether to execute
35  * the tag body once per row "true", or to simply assign the ResultSet
36  * to the page attribute specified by "id". The optional "name" and "scope"
37  * attributes can be used to retrieve a resultset (or rowset) from context.</p>
38  *
39  * <p>JSP Tag Lib Descriptor
40  * <pre>
41  * &lt;name>resultSet&lt;/name>
42  * &lt;tagclass>org.apache.taglibs.dbtags.resultset.ResultSetTag&lt;/tagclass>
43  * &lt;teiclass>org.apache.taglibs.dbtags.connection.ResultSetTEI&lt;/teiclass>
44  * &lt;bodycontent>JSP&lt;/bodycontent>
45  * &lt;info>JSP tag resulset, executes the query and loops through the results
46  * for the enclosing statement or preparedstatement tag. The body of
47  * this tag is executed once per row in the resultset. The optional
48  * "loop" attribute, which default to true, specifies whether to execute
49  * the tag body once per row "true", or to simply assign the ResultSet
50  * to the page attribute specified by "id". The optional "name" and "scope"
51  * attributes can be used to retrieve a resultset (or rowset) from context.<&lt;/info>
52  * &lt;attribute>
53  * &lt;name>id&lt;/name>
54  * &lt;required>true&lt;/required>
55  * &lt;rtexprvalue>false&lt;/rtexprvalue>
56  * &lt;/attribute>
57  * &lt;attribute>
58  * &lt;name>loop&lt;/name>
59  * &lt;required>false&lt;/required>
60  * &lt;rtexprvalue>false&lt;/rtexprvalue>
61  * &lt;/attribute>
62  * &lt;attribute>
63  * &lt;name>name&lt;/name>
64  * &lt;required>false&lt;/required>
65  * &lt;rtexprvalue>true&lt;/rtexprvalue>
66  * &lt;/attribute>
67  * &lt;attribute>
68  * &lt;name>scope&lt;/name>
69  * &lt;required>false&lt;/required>
70  * &lt;rtexprvalue>false&lt;/rtexprvalue>
71  * &lt;/attribute>
72  * </pre>
73  *
74  * @author Morgan Delagrange
75  * @author Ted Husted
76  * @author Craig McClanahan
77  * @see org.apache.taglibs.dbtags.statement.StatementImplTag
78  * @see org.apache.taglibs.dbtags.statement.QueryTag
79  * @see org.apache.taglibs.dbtags.preparedstatement.PreparedStatementImplTag
80  */

81 public class ResultSetTag extends BodyTagSupport JavaDoc{
82
83   private Statement JavaDoc _statement = null;
84   private ResultSet JavaDoc _rset = null;
85   private boolean _shouldLoop = true;
86   private String JavaDoc _name = null;
87   private String JavaDoc _scope = null;
88   private int _rowCount = 0;
89
90   /**
91    * Locate and return the specified bean, from an optionally specified
92    * scope, in the specified page context. If no such bean is found,
93    * return <code>null</code> instead. If an exception is thrown, it will
94    * have already been saved via a call to <code>saveException()</code>.
95    *
96    * @param pageContext Page context to be searched
97    * @param name Name of the bean to be retrieved
98    * @param scope Scope to be searched (page, request, session, application)
99    * or <code>null</code> to use <code>findAttribute()</code> instead
100    *
101    * @exception JspException if an invalid scope name
102    * is requested
103    */

104   protected Object JavaDoc lookup(PageContext JavaDoc pageContext,
105       String JavaDoc name, String JavaDoc scope) throws JspTagException JavaDoc {
106
107       Object JavaDoc bean = null;
108       if (scope == null)
109           bean = pageContext.findAttribute(name);
110       else if (scope.equalsIgnoreCase("page"))
111           bean = pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
112       else if (scope.equalsIgnoreCase("request"))
113           bean = pageContext.getAttribute(name, PageContext.REQUEST_SCOPE);
114       else if (scope.equalsIgnoreCase("session"))
115           bean = pageContext.getAttribute(name, PageContext.SESSION_SCOPE);
116       else if (scope.equalsIgnoreCase("application"))
117           bean =
118               pageContext.getAttribute(name, PageContext.APPLICATION_SCOPE);
119       else {
120           JspTagException JavaDoc e = new JspTagException JavaDoc("Invalid scope " + scope);
121           throw e;
122       }
123       return (bean);
124
125    }
126
127   /**
128    * Name of the bean that contains the rowset to process.
129    */

130
131
132   public void setName(String JavaDoc name) {
133        _name = name;
134    }
135
136   public void setScope(String JavaDoc scope) {
137        _scope = scope;
138    }
139
140   public void setLoop(boolean shouldLoop) {
141     _shouldLoop = shouldLoop;
142   }
143
144   public ResultSet JavaDoc getResultSet() {
145     return _rset;
146   }
147
148   public int doStartTag() throws JspTagException JavaDoc {
149
150     // reset body's content, otherwise it could result in an error if container is pooling
151
// tag handlers (see bug 26863 for more details)
152
super.bodyContent = null;
153     
154       try {
155             // if name property given, retrieve resultset from context
156
// may also be a related class, like RowSet
157
if (_name!=null) {
158           _rset = (ResultSet JavaDoc) lookup(pageContext, _name, _scope);
159         }
160         else {
161             StatementTag _stmtTag =
162                 (StatementTag) findAncestorWithClass(this,
163                     Class.forName("org.apache.taglibs.dbtags.statement.StatementTag"));
164             _rset = _stmtTag.executeQuery();
165         }
166
167         pageContext.setAttribute(getId(), _rset);
168
169         if (_shouldLoop == false) {
170             return EVAL_BODY_TAG;
171         }
172
173         if (_rset.next() == false) {
174           setTotalRowCount(_rowCount);
175           return SKIP_BODY;
176         }
177
178       ++_rowCount;
179       setTotalRowCount(_rowCount);
180
181     } catch (ClassNotFoundException JavaDoc e) {
182       throw new JspTagException JavaDoc(e.toString());
183     } catch (SQLException JavaDoc e) {
184       throw new JspTagException JavaDoc(e.toString());
185     }
186
187     return EVAL_BODY_TAG;
188   }
189
190   public int doEndTag() throws JspTagException JavaDoc{
191     pageContext.removeAttribute(getId());
192     try {
193       if (getBodyContent() != null && getPreviousOut() != null) {
194         getPreviousOut().write(getBodyContent().getString());
195       }
196     } catch (IOException JavaDoc e) {
197       throw new JspTagException JavaDoc(e.toString());
198     } finally {
199       try {
200         _rset.close();
201       }
202       catch (SQLException JavaDoc e) {
203         // it's not fatal if the result set cannot be closed
204
e.printStackTrace();
205       }
206     }
207
208     // we have to call this guy manually now
209
// with the spec clarification
210
release();
211
212     return EVAL_PAGE;
213   }
214
215   public int doAfterBody() throws JspTagException JavaDoc{
216
217     if (_shouldLoop == false) {
218       return EVAL_PAGE;
219     }
220
221     try {
222       if (_rset.next() == true) {
223         ++_rowCount;
224         setTotalRowCount(_rowCount);
225         return EVAL_BODY_TAG;
226       }
227     } catch (SQLException JavaDoc e) {
228       throw new JspTagException JavaDoc(e.toString());
229     }
230
231     setTotalRowCount(_rowCount);
232     return EVAL_PAGE;
233   }
234
235   public void release() {
236     _statement = null;
237     _rset = null;
238     _shouldLoop = true;
239     _name = null;
240     _scope = null;
241     _rowCount = 0;
242   }
243
244   protected void setTotalRowCount(int rowCount) {
245     pageContext.setAttribute("org.apache.taglibs.dbtags.resultset.rowcount",
246                               new Integer JavaDoc(rowCount));
247   }
248
249 }
250
Popular Tags