KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > taglib > QueryData


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/QueryData.java,v 1.17 2004/10/20 10:52:03 hkollmann Exp $
3  * $Revision: 1.17 $
4  * $Date: 2004/10/20 10:52:03 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23 package org.dbforms.taglib;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.dbforms.config.DbEventInterceptorData;
28 import org.dbforms.config.ResultSetVector;
29
30 import java.sql.Connection JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32 import java.sql.SQLException JavaDoc;
33
34 import java.util.List JavaDoc;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37
38
39
40 /**
41  * external data to be nested into radio, checkbox or select - tag! (useful
42  * only in conjunction with radio, checkbox or select - tag)
43  *
44  * <p>
45  * this tag provides data to radio, checkbox or select - tags. it may be used
46  * for cross-references to other tables.
47  * </p>
48  *
49  * <p>
50  * this tag provides similar functionlaity to "TabData", but as it allows to
51  * formulate free querys including all SQL statements your RDBMS supports you
52  * have much more flexibility using this tag.
53  * </p>
54  *
55  * <p>
56  * query building convention: first column is the "key" column for the
57  * radio/check/select elements, all other colums are just "data" columns
58  * visible to the user example: SELECT DISTINCT customer.id, customer.name,
59  * customer.adress, debitors.debit FROM customer INNER JOIN id ON (SELECT id
60  * FROM debitors WHERE debit>100000) ORDER BY debit DESC
61  * </p>
62  * - "id" will be threaten as key-value in select box, "name and address will
63  * be shown in select box
64  *
65  * @author Joachim Peer
66  */

67 public class QueryData extends EmbeddedData
68    implements javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
69    private static Log logCat = LogFactory.getLog(QueryData.class.getName());
70
71    // logging category for this class
72
private String JavaDoc query;
73
74    /**
75     * DOCUMENT ME!
76     *
77     * @param query DOCUMENT ME!
78     */

79    public void setQuery(String JavaDoc query) {
80       this.query = query;
81    }
82
83
84    /**
85     * DOCUMENT ME!
86     *
87     * @return DOCUMENT ME!
88     */

89    public String JavaDoc getQuery() {
90       return query;
91    }
92
93
94    /**
95     * @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
96     */

97    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
98       throw t;
99    }
100
101
102    /**
103     * DOCUMENT ME!
104     */

105    public void doFinally() {
106       query = null;
107       super.doFinally();
108    }
109
110
111    /**
112     * returns Hashtable with data. Its keys represent the "value"-fields for
113     * the DataContainer-Tag, its values represent the visible fields for the
114     * Multitags. (DataContainer are: select, radio, checkbox and a special
115     * flavour of Label).
116     *
117     * @param con DOCUMENT ME!
118     *
119     * @return DOCUMENT ME!
120     */

121    protected List JavaDoc fetchData(Connection JavaDoc con) throws SQLException JavaDoc {
122       logCat.info("about to execute user defined query:" + query);
123
124       ResultSetVector rsv = null;
125       PreparedStatement JavaDoc ps = con.prepareStatement(query);
126
127       try {
128          rsv = new ResultSetVector();
129
130          HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext
131             .getRequest();
132          DbEventInterceptorData data = new DbEventInterceptorData(request,
133                getConfig(), con, null);
134          data.setAttribute(DbEventInterceptorData.PAGECONTEXT,
135                 pageContext);
136          rsv.addResultSet(data, ps.executeQuery());
137       } finally {
138          ps.close(); // #JP Jun 27, 2001
139
}
140
141       return formatEmbeddedResultRows(rsv);
142    }
143 }
144
Popular Tags