001: /*
002: * transformica 2
003: * Code generator
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.pavelvlasov.com/pv/content/menu.show@id=products.transformica.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.transformica.jdbc;
024:
025: import java.io.File;
026: import java.io.IOException;
027: import java.sql.SQLException;
028:
029: import org.apache.tools.ant.BuildException;
030:
031: import biz.hammurapi.sql.ConnectionPerThreadDataSource;
032: import biz.hammurapi.sql.SQLProcessor;
033: import biz.hammurapi.sql.metadata.DefaultGenerationPolicy;
034: import biz.hammurapi.sql.metadata.Metadata;
035: import biz.hammurapi.transformica.TransformSession;
036: import biz.hammurapi.transformica.TransformTask;
037: import biz.hammurapi.util.Visitable;
038:
039: /**
040: * Traverses database metadata. Transformations are defined by nested channel elements.
041: * Velocity template engine is used for code generation. Current metadata object is accessible through 'element' context object.
042: * 'session' context object of type biz.hammurapi.transformica.TransformSession gives access to current Ant project and provides
043: * methods to include other templates or channels and to store information.
044: * <section name="Example" suppress-description="yes">
045: <pre>
046: <taskdef name="dbtransform" classname="biz.hammurapi.transformica.jdbc.MetaDataTask"><br/>
047: <tab/><classpath><br/>
048: <tab/><tab/><fileset dir="lib" includes="*.jar"/><br/>
049: <tab/></classpath><br/>
050: </taskdef><br/>
051: <br/>
052: <dbtransform<br/>
053: <tab/>driver="org.hsqldb.jdbcDriver"<br/>
054: <tab/>url="jdbc:hsqldb:hsql://localhost"<br/>
055: <tab/>user="sa"<br/>
056: <tab/>password=""<br/>
057: ><br/>
058: <tab/><channel <br/>
059: <tab/><tab/>outputDir="generated"<br/>
060: <tab/><tab/>template="templates/jdbc.java"<br/>
061: <tab/><tab/>fileNameTemplate='$${element.name}.java' <br/>
062: <tab/>><br/>
063: <tab/><tab/><filetouchdetector fileInfoFile="fileInfo.txt" genRoot="generated"/><br/>
064: <tab/></channel><br/>
065: </jdbctransform><br/>
066: </pre></section>
067: * @ant.element name="jdbcmetadatatransform"
068: * @author Pavel Vlasov
069: */
070: public class MetaDataTask extends TransformTask {
071: private String driver;
072: private String url;
073: private String user;
074: private String password;
075:
076: private File metadataFile;
077:
078: /**
079: * Metadata file
080: * @ant.non-required
081: * @param metadata
082: */
083: public void setMetadata(File metadataFile) {
084: this .metadataFile = metadataFile;
085: }
086:
087: /**
088: * Set it to true to avoid loading index metadata
089: * (e.g. if you don't have sufficient privileges)
090: * @ant.non-required
091: * @param skipIndices
092: */
093: /**
094: * Connection string
095: * @ant.required
096: * @param url The connectionString to set.
097: */
098: public void setURL(String url) {
099: this .url = url;
100: }
101:
102: /**
103: * JDBC Driver class name
104: * @ant.required
105: * @param driver The driver to set.
106: */
107: public void setDriver(String driver) {
108: this .driver = driver;
109: }
110:
111: /**
112: * Password
113: * @ant.non-required
114: * @param password The password to set.
115: */
116: public void setPassword(String password) {
117: this .password = password;
118: }
119:
120: /**
121: * User name
122: * @ant.non-required
123: * @param userName The userName to set.
124: */
125: public void setUser(String userName) {
126: this .user = userName;
127: }
128:
129: protected Visitable getModel(TransformSession session) {
130: try {
131: if (metadataFile == null) {
132: if (driver == null) {
133: throw new BuildException("Driver not set");
134: }
135: if (url == null) {
136: throw new BuildException(
137: "Connection URL is not set");
138: }
139:
140: // TODO - make it better.
141: return new Metadata(newProcessor(), new String[] {
142: "TABLE", "VIEW" },
143: new DefaultGenerationPolicy(), null);
144: } else {
145: return Metadata.load(metadataFile);
146: }
147:
148: } catch (ClassNotFoundException e) {
149: throw new BuildException("Class not found: " + e, e);
150: } catch (SQLException e) {
151: throw new BuildException(e);
152: } catch (IOException e) {
153: throw new BuildException("Could not load metadata file: "
154: + e, e);
155: }
156: }
157:
158: /**
159: * @return
160: * @throws ClassNotFoundException
161: */
162: SQLProcessor newProcessor() throws ClassNotFoundException {
163: return new SQLProcessor(new ConnectionPerThreadDataSource(
164: driver, url, user, password, null), null);
165: }
166:
167: /**
168: * Defines transformation channel.
169: * @ant.required
170: * @return
171: */
172: public TableChannel createTableChannel() {
173: TableChannel ret = new TableChannel(session);
174: channels.add(ret);
175: return ret;
176: }
177:
178: /**
179: * Defines transformation channel.
180: * @ant.required
181: * @return
182: */
183: public SchemaChannel createSchemaChannel() {
184: SchemaChannel ret = new SchemaChannel(session);
185: channels.add(ret);
186: return ret;
187: }
188: }
|