1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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();
131 }
132
133 return formatEmbeddedResultRows(rsv);
134 }
135 }