View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/QueryDataTag.java,v 1.2 2006/01/13 13:38:51 hkollmann Exp $
3    * $Revision: 1.2 $
4    * $Date: 2006/01/13 13:38:51 $
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.ResultSetVector;
28  import org.dbforms.interfaces.DbEventInterceptorData;
29  
30  import java.sql.Connection;
31  import java.sql.PreparedStatement;
32  import java.sql.SQLException;
33  
34  import java.util.List;
35  
36  import javax.servlet.http.HttpServletRequest;
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 QueryDataTag extends AbstractEmbeddedDataTag
68     implements javax.servlet.jsp.tagext.TryCatchFinally {
69     private static Log logCat = LogFactory.getLog(QueryDataTag.class.getName());
70  
71     // logging category for this class
72     private String query;
73  
74     /***
75      * DOCUMENT ME!
76      *
77      * @param query DOCUMENT ME!
78      */
79     public void setQuery(String query) {
80        this.query = query;
81     }
82  
83  
84     /***
85      * DOCUMENT ME!
86      *
87      * @return DOCUMENT ME!
88      */
89     public String getQuery() {
90        return query;
91     }
92  
93  
94     /***
95      * DOCUMENT ME!
96      */
97     public void doFinally() {
98        query = null;
99        super.doFinally();
100    }
101 
102 
103    /***
104     * returns Hashtable with data. Its keys represent the "value"-fields for
105     * the DataContainer-Tag, its values represent the visible fields for the
106     * Multitags. (DataContainer are: select, radio, checkbox and a special
107     * flavour of Label).
108     *
109     * @param con DOCUMENT ME!
110     *
111     * @return DOCUMENT ME!
112     */
113    protected List fetchData(Connection con) throws SQLException {
114       logCat.info("about to execute user defined query:" + query);
115 
116       ResultSetVector   rsv = null;
117       PreparedStatement ps = con.prepareStatement(query);
118 
119       try {
120          rsv = new ResultSetVector();
121 
122          HttpServletRequest     request = (HttpServletRequest) pageContext
123             .getRequest();
124          DbEventInterceptorData data = new DbEventInterceptorData(request,
125                getConfig(), con, null);
126          data.setAttribute(DbEventInterceptorData.PAGECONTEXT,
127                 pageContext);
128          rsv.addResultSet(data, ps.executeQuery());
129       } finally {
130          ps.close(); // #JP Jun 27, 2001
131       }
132 
133       return formatEmbeddedResultRows(rsv);
134    }
135 }