View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/datalist/dao/DataSourceFactory.java,v 1.18 2005/11/30 20:31:18 hkollmann Exp $
3    * $Revision: 1.18 $
4    * $Date: 2005/11/30 20:31:18 $
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.event.datalist.dao;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import org.dbforms.config.FieldValue;
28  import org.dbforms.config.FieldValues;
29  import org.dbforms.config.ResultSetVector;
30  import org.dbforms.config.Table;
31  import org.dbforms.interfaces.DbEventInterceptorData;
32  
33  import org.dbforms.util.ReflectionUtil;
34  import org.dbforms.util.Util;
35  
36  import java.sql.Connection;
37  import java.sql.SQLException;
38  
39  
40  
41  /***
42   * Factory class to generate different DataSources. datasource is attribute of
43   * table class and can be changed in dbforms-config. Default class is
44   *
45   * @author hkk
46   */
47  public class DataSourceFactory {
48     // logging category for this class;
49     private static Log logCat = LogFactory.getLog(DataSourceFactory.class
50           .getName());
51     private AbstractDataSource dataHandler;
52  
53     /***
54      * Creates a new DataSourceFactory object.
55      *
56      * @param dbConnectionName name of the used db connection. Can be used to
57      *        get an own db connection, e.g. to hold it during the  session
58      *        (see DataSourceJDBC for example!)
59      * @param con the JDBC Connection object
60      * @param table the input table
61      *
62      * @throws SQLException if any error occurs
63      */
64     public DataSourceFactory(String dbConnectionName, Connection connection,
65        Table table) throws SQLException {
66        String dataAccessClass = table.getDataAccessClass();
67  
68        if (Util.isNull(dataAccessClass)) {
69           dataAccessClass = "org.dbforms.event.datalist.dao.DataSourceJDBC";
70        }
71  
72        try {
73           dataHandler = (AbstractDataSource) ReflectionUtil.newInstance(dataAccessClass);
74           dataHandler.setTable(table);
75           dataHandler.setConnection(connection, dbConnectionName);
76        } catch (Exception e) {
77           logCat.error(e);
78        }
79     }
80  
81     /***
82      * Return a resultSetVector object containing <i>count</i> records starting
83      * from the input position
84      *
85      * @param position the current table position
86      * @param count number of records to fetch
87      *
88      * @return a resultSetVector object containing the current <i>count</i>
89      *         records starting from the input position
90      *
91      * @throws SQLException if any error occurs
92      */
93     public ResultSetVector getCurrent(DbEventInterceptorData interceptorData,
94        String position, int count) throws SQLException {
95        return dataHandler.getCurrent(interceptorData, position, count);
96     }
97  
98  
99     /***
100     * returns the internal DataSource element
101     *
102     * @return the DataSource element
103     */
104    public AbstractDataSource getDataHandler() {
105       return dataHandler;
106    }
107 
108 
109    /***
110     * Return a resultSetVector object containing the first <i>count</i>
111     * records.
112     *
113     * @param count number of records to fetch
114     *
115     * @return a resultSetVector object containing the first <i>count</i>
116     *         records
117     *
118     * @throws SQLException if any error occurs
119     */
120    public ResultSetVector getFirst(DbEventInterceptorData interceptorData,
121       int count) throws SQLException {
122       return dataHandler.getFirst(interceptorData, count);
123    }
124 
125 
126    /***
127     * Return a resultSetVector object containing the last <i>count</i> records.
128     *
129     * @param count number of records to fetch
130     *
131     * @return a resultSetVector object containing the last <i>count</i> records
132     *
133     * @throws SQLException if any error occurs
134     */
135    public ResultSetVector getLast(DbEventInterceptorData interceptorData,
136       int count) throws SQLException {
137       return dataHandler.getLast(interceptorData, count);
138    }
139 
140 
141    /***
142     * Return a resultSetVector object containing the next <i>count</i> records
143     * starting from the input position + <i>count</i> records.
144     *
145     * @param position the current table position
146     * @param count number of records to fetch
147     *
148     * @return a resultSetVector object containing the next <i>count</i> records
149     *         starting from the input position
150     *
151     * @throws SQLException if any error occurs
152     */
153    public ResultSetVector getNext(DbEventInterceptorData interceptorData,
154       String position, int count) throws SQLException {
155       return dataHandler.getNext(interceptorData, position, count);
156    }
157 
158 
159    /***
160     * Return a resultSetVector object containing the previous <i>count</i>
161     * records starting from the input position + <i>count</i> records.
162     *
163     * @param position the current table position
164     * @param count number of records to fetch
165     *
166     * @return a resultSetVector object containing the previous <i>count</i>
167     *         records starting from the input position
168     *
169     * @throws SQLException if any error occurs
170     */
171    public ResultSetVector getPrev(DbEventInterceptorData interceptorData,
172       String position, int count) throws SQLException {
173       return dataHandler.getPrev(interceptorData, position, count);
174    }
175 
176 
177    /***
178     * Sets the select data for this dataSource
179     *
180     * @param filterConstraint FieldValue array used to restrict a set in a
181     *        resultset
182     * @param orderConstraint FieldValue array used to build a cumulation of
183     *        rules for ordering (sorting)
184     * @param sqlFilter sql condition to add to where clause
185     * @param sqlFilterParams DOCUMENT ME!
186     */
187    public void setSelect(FieldValue[] filterConstraint,
188       FieldValue[] orderConstraint, String sqlFilter,
189       FieldValue[] sqlFilterParams) {
190       dataHandler.setSelect(filterConstraint, orderConstraint, sqlFilter,
191          sqlFilterParams);
192    }
193 
194 
195    /***
196     * Sets the select data for this dataSource for free form selects. default
197     * methods just raises an exception
198     *
199     * @param tableList the list of tables involved into the query
200     * @param whereClause free-form whereClause to be appended to query
201     *
202     * @throws SQLException
203     */
204    public void setSelect(String tableList, String whereClause)
205       throws SQLException {
206       dataHandler.setSelect(tableList, whereClause);
207    }
208 
209 
210    /***
211     * Close the underlying dataHandler.
212     */
213    public void close() {
214       dataHandler.close();
215    }
216 
217 
218    /***
219     * Perform a delete operation into the underlying dataSource.
220     *
221     * @param con DOCUMENT ME!
222     * @param keyValuesStr the key value identifying the record to delete
223     *
224     * @throws SQLException if any error occurs
225     */
226    public int doDelete(DbEventInterceptorData interceptorData,
227       String keyValuesStr) throws SQLException {
228       return dataHandler.doDelete(interceptorData, keyValuesStr);
229    }
230 
231 
232    /***
233     * Perform an insert operation into the underlying dataSource.
234     *
235     * @param con DOCUMENT ME!
236     * @param fieldValues the field values to insert
237     *
238     * @throws SQLException if any error occurs
239     */
240    public int doInsert(DbEventInterceptorData interceptorData,
241       FieldValues fieldValues) throws SQLException {
242       return dataHandler.doInsert(interceptorData, fieldValues);
243    }
244 
245 
246    /***
247     * Perform an update operation into the underlying dataSource.
248     *
249     * @param con DOCUMENT ME!
250     * @param fieldValues the field values to update
251     * @param keyValuesStr the key value identifying the record to update
252     *
253     * @throws SQLException if any error occurs
254     */
255    public int doUpdate(DbEventInterceptorData interceptorData,
256       FieldValues fieldValues, String keyValuesStr) throws SQLException {
257       return dataHandler.doUpdate(interceptorData, fieldValues, keyValuesStr);
258    }
259 }