View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/TableDataTag.java,v 1.3 2006/02/06 16:30:45 hkollmann Exp $
3    * $Revision: 1.3 $
4    * $Date: 2006/02/06 16:30:45 $
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 org.dbforms.util.StringUtil;
31  
32  import java.sql.Connection;
33  import java.sql.PreparedStatement;
34  import java.sql.SQLException;
35  
36  import java.util.List;
37  import java.util.Vector;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  
42  
43  /***
44   * external data to be nested into radio, checkbox or select - tag! (useful
45   * only in conjunction with radio, checkbox or select - tag)
46   *
47   * <p>
48   * this tag provides data to radio, checkbox or select - tags. it may be used
49   * for cross-references to other tables.
50   * </p>
51   *
52   * @author Joachim Peer
53   */
54  public class TableDataTag extends AbstractEmbeddedDataTag
55     implements javax.servlet.jsp.tagext.TryCatchFinally {
56     private static Log logCat = LogFactory.getLog(TableDataTag.class.getName());
57  
58     // logging category for this class
59     private String foreignTable;
60     private String orderBy;
61     private String storeField;
62     private String visibleFields;
63  
64     /***
65      * DOCUMENT ME!
66      *
67      * @param foreignTable DOCUMENT ME!
68      */
69     public void setForeignTable(String foreignTable) {
70        this.foreignTable = foreignTable;
71     }
72  
73  
74     /***
75      * DOCUMENT ME!
76      *
77      * @return DOCUMENT ME!
78      */
79     public String getForeignTable() {
80        return foreignTable;
81     }
82  
83  
84     /***
85      * DOCUMENT ME!
86      *
87      * @param orderBy DOCUMENT ME!
88      */
89     public void setOrderBy(String orderBy) {
90        this.orderBy = orderBy;
91        logCat.info("setOrderBy(\"" + orderBy + "\")");
92     }
93  
94  
95     /***
96      * DOCUMENT ME!
97      *
98      * @return DOCUMENT ME!
99      */
100    public String getOrderBy() {
101       return orderBy;
102    }
103 
104 
105    /***
106     * DOCUMENT ME!
107     *
108     * @param storeField DOCUMENT ME!
109     */
110    public void setStoreField(String storeField) {
111       this.storeField = storeField;
112    }
113 
114 
115    /***
116     * DOCUMENT ME!
117     *
118     * @return DOCUMENT ME!
119     */
120    public String getStoreField() {
121       return storeField;
122    }
123 
124 
125    /***
126     * DOCUMENT ME!
127     *
128     * @param visibleFields DOCUMENT ME!
129     */
130    public void setVisibleFields(String visibleFields) {
131       this.visibleFields = visibleFields;
132    }
133 
134 
135    /***
136     * DOCUMENT ME!
137     *
138     * @return DOCUMENT ME!
139     */
140    public String getVisibleFields() {
141       return visibleFields;
142    }
143 
144 
145    /***
146     * DOCUMENT ME!
147     */
148    public void doFinally() {
149       foreignTable     = null;
150       visibleFields    = null;
151       storeField       = null;
152       orderBy          = null;
153       super.doFinally();
154    }
155 
156    /***
157     * returns Hashtable with data. Its keys represent the "value"-fields for
158     * the DataContainer-Tag, its values     represent the visible fields for
159     * the Multitags.     (DataContainer are: select, radio, checkbox and a
160     * special flavour of Label).
161     *
162     * @param con DOCUMENT ME!
163     *
164     * @return DOCUMENT ME!
165     */
166    protected List fetchData(Connection con) throws SQLException {
167       Vector       vf = StringUtil.splitString(visibleFields, ",;~");
168 
169       StringBuffer queryBuf = new StringBuffer();
170 
171       queryBuf.append("SELECT ");
172       queryBuf.append(storeField);
173       queryBuf.append(", ");
174 
175       for (int i = 0; i < vf.size(); i++) {
176          queryBuf.append((String) vf.elementAt(i));
177 
178          if (i < (vf.size() - 1)) {
179             queryBuf.append(", ");
180          }
181       }
182 
183       queryBuf.append(" FROM ");
184       queryBuf.append(foreignTable);
185 
186       if (orderBy != null) {
187          queryBuf.append(" ORDER BY ");
188          queryBuf.append(orderBy);
189       }
190 
191       logCat.info("about to execute:" + queryBuf.toString());
192 
193       PreparedStatement ps  = con.prepareStatement(queryBuf.toString());
194       ResultSetVector   rsv = null;
195 
196       try {
197          rsv = new ResultSetVector();
198 
199          HttpServletRequest     request = (HttpServletRequest) pageContext
200             .getRequest();
201          DbEventInterceptorData data = new DbEventInterceptorData(request,
202                getConfig(), con, null);
203          data.setAttribute(DbEventInterceptorData.PAGECONTEXT,
204                 pageContext);
205          rsv.addResultSet(data, ps.executeQuery());
206       } finally {
207          ps.close(); // #JP Jun 27, 2001
208       }
209 
210       return formatEmbeddedResultRows(rsv);
211    }
212 }