View Javadoc

1   package org.apache.torque.task;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Iterator;
20  import java.util.NoSuchElementException;
21  import java.sql.Connection;
22  import java.sql.DriverManager;
23  import java.sql.SQLException;
24  import java.sql.Statement;
25  import org.apache.tools.ant.Project;
26  import org.apache.velocity.context.Context;
27  
28  import com.workingdogs.village.QueryDataSet;
29  import com.workingdogs.village.Record;
30  
31  /***
32   * An extended Texen task used for dumping data from db into XML
33   *
34   * @author <a href="mailto:fedor.karpelevitch@home.com">Fedor Karpelevitch</a>
35   * @author <a href="jvanzyl@zenplex.com">Jason van Zyl</a>
36   * @author <a href="dlr@finemaltcoding.com">Daniel Rall</a>
37   * @version $Id: TorqueDataDumpTask.java,v 1.3.2.2 2004/05/20 04:35:14 seade Exp $
38   */
39  public class TorqueDataDumpTask extends TorqueDataModelTask
40  {
41      /***
42       * Database name.
43       */
44      private String databaseName;
45  
46      /***
47       * Database URL used for JDBC connection.
48       */
49      private String databaseUrl;
50  
51      /***
52       * Database driver used for JDBC connection.
53       */
54      private String databaseDriver;
55  
56      /***
57       * Database user used for JDBC connection.
58       */
59      private String databaseUser;
60  
61      /***
62       * Database password used for JDBC connection.
63       */
64      private String databasePassword;
65  
66      /***
67       * The database connection used to retrieve the data to dump.
68       */
69      private Connection conn;
70  
71      /***
72       * The statement used to acquire the data to dump.
73       */
74      private Statement stmt;
75  
76      /***
77       * Get the database name to dump
78       *
79       * @return  The DatabaseName value
80       */
81      public String getDatabaseName()
82      {
83          return databaseName;
84      }
85  
86      /***
87       * Set the database name
88       *
89       * @param  v The new DatabaseName value
90       */
91      public void setDatabaseName(String v)
92      {
93          databaseName = v;
94      }
95  
96      /***
97       * Get the database url
98       *
99       * @return  The DatabaseUrl value
100      */
101     public String getDatabaseUrl()
102     {
103         return databaseUrl;
104     }
105 
106     /***
107      * Set the database url
108      *
109      * @param  v The new DatabaseUrl value
110      */
111     public void setDatabaseUrl(String v)
112     {
113         databaseUrl = v;
114     }
115 
116     /***
117      * Get the database driver name
118      *
119      * @return  String database driver name
120      */
121     public String getDatabaseDriver()
122     {
123         return databaseDriver;
124     }
125 
126     /***
127      * Set the database driver name
128      *
129      * @param  v The new DatabaseDriver value
130      */
131     public void setDatabaseDriver(String v)
132     {
133         databaseDriver = v;
134     }
135 
136     /***
137      * Get the database user
138      *
139      * @return  String database user
140      */
141     public String getDatabaseUser()
142     {
143         return databaseUser;
144     }
145 
146     /***
147      * Set the database user
148      *
149      * @param  v The new DatabaseUser value
150      */
151     public void setDatabaseUser(String v)
152     {
153         databaseUser = v;
154     }
155 
156     /***
157      * Get the database password
158      *
159      * @return  String database password
160      */
161     public String getDatabasePassword()
162     {
163         return databasePassword;
164     }
165 
166     /***
167      * Set the database password
168      *
169      * @param  v The new DatabasePassword value
170      */
171     public void setDatabasePassword(String v)
172     {
173         databasePassword = v;
174     }
175 
176     /***
177      * Initializes initial context
178      *
179      * @return the context
180      * @throws Exception generic exception
181      */
182     public Context initControlContext() throws Exception
183     {
184         super.initControlContext();
185 
186         context.put("dataset", "all");
187 
188         log("Torque - TorqueDataDump starting");
189         log("Your DB settings are:");
190         log("driver: " + databaseDriver);
191         log("URL: " + databaseUrl);
192         log("user: " + databaseUser);
193         // log("password: " + databasePassword);
194 
195         try
196         {
197             Class.forName(databaseDriver);
198             log("DB driver instantiated sucessfully", Project.MSG_DEBUG);
199 
200             conn = DriverManager.getConnection(
201                     databaseUrl, databaseUser, databasePassword);
202 
203             log("DB connection established", Project.MSG_DEBUG);
204             context.put("tableTool", new TableTool());
205         }
206         catch (SQLException se)
207         {
208             System.err.println("SQLException while connecting to DB:");
209             se.printStackTrace();
210         }
211         catch (ClassNotFoundException cnfe)
212         {
213             System.err.println("cannot load driver:");
214             cnfe.printStackTrace();
215         }
216         context.put("escape", new org.apache.velocity.anakia.Escape());
217         return context;
218     }
219 
220     /***
221      * Closes the db-connection, overriding the <code>cleanup()</code> hook
222      * method in <code>TexenTask</code>.
223      *
224      * @throws Exception Database problem while closing resource.
225      */
226     protected void cleanup() throws Exception
227     {
228         if (stmt != null)
229         {
230             stmt.close();
231         }
232 
233         if (conn != null)
234         {
235             conn.close();
236         }
237     }
238 
239     /***
240      *  A nasty do-it-all tool class. It serves as:
241      *  <ul>
242      *  <li>context tool to fetch a table iterator</li>
243      *  <li>the abovenamed iterator which iterates over the table</li>
244      *  <li>getter for the table fields</li>
245      *  </ul>
246      *
247      */
248     public class TableTool implements Iterator
249     {
250         /*** querydataset */
251         private QueryDataSet qds;
252         /*** is empty */
253         private boolean isEmpty;
254         /*** current index */
255         private int curIndex = -1;
256         /*** current record */
257         private Record curRec = null;
258 
259         /***
260          *  Constructor for the TableTool object
261          */
262         public TableTool()
263         {
264         }
265 
266         /***
267          * Constructor for the TableTool object
268          *
269          * @param qds Description of Parameter
270          * @throws Exception Problem using database record set cursor.
271          */
272         protected TableTool(QueryDataSet qds) throws Exception
273         {
274             this.qds = qds;
275             this.qds.fetchRecords();
276             this.isEmpty = !(qds.size() > 0);
277         }
278 
279         /***
280          * Fetches an <code>Iterator</code> for the data in the named table.
281          *
282          * @param  tableName Description of Parameter
283          * @return <code>Iterator</code> for the fetched data.
284          * @throws Exception Problem creating connection or executing query.
285          */
286         public TableTool fetch(String tableName) throws Exception
287         {
288             log("Fetching data for table " + tableName, Project.MSG_INFO);
289             // Set Statement object in associated TorqueDataDump
290             // instance
291             return new TableTool
292                 (new QueryDataSet(conn, "SELECT * FROM " + tableName));
293         }
294 
295         /***
296          * check if there are more records in the QueryDataSet
297          *
298          * @return true if there are more records
299          */
300         public boolean hasNext()
301         {
302             try
303             {
304                 return ((this.curIndex < this.qds.size() - 1) && (!isEmpty));
305             }
306             catch (Exception se)
307             {
308                 System.err.println("Exception :");
309                 se.printStackTrace();
310             }
311             return false;
312         }
313 
314         /***
315          * load the next record from the QueryDataSet
316          *
317          * @return Description of the Returned Value
318          * @throws NoSuchElementException Description of Exception
319          */
320         public Object next() throws NoSuchElementException
321         {
322             try
323             {
324                 System.err.print(".");
325                 this.curRec = this.qds.getRecord(++curIndex);
326             }
327             catch (Exception se)
328             {
329                 System.err.println("Exception while iterating:");
330                 se.printStackTrace();
331                 throw new NoSuchElementException(se.getMessage());
332             }
333             return this;
334         }
335 
336         /***
337          * Returns the value for the column
338          *
339          * @param  columnName name of the column
340          * @return  value of the column or null if it doesn't exist
341          */
342         public String get(String columnName)
343         {
344             try
345             {
346                 return (this.curRec.getValue(columnName).asString());
347             }
348             catch (Exception se)
349             {
350                 log("Exception fetching value " + columnName + ": "
351                         + se.getMessage(), Project.MSG_ERR);
352             }
353             return null;
354         }
355 
356         /***
357          * unsupported! always throws Exception
358          *
359          * @throws UnsupportedOperationException unsupported
360          */
361         public void remove() throws UnsupportedOperationException
362         {
363             throw new UnsupportedOperationException();
364         }
365     }
366 }